blob: 4a6cfb497103a3de116b6efd7c3aff613612b7b7 [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"
Frank van der Linden012a2112020-06-23 22:39:03 +000069#include "nfs42.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070070
Trond Myklebustc6d01c62013-08-09 11:51:26 -040071#include "nfs4trace.h"
72
Linus Torvalds1da177e2005-04-16 15:20:36 -070073#define NFSDBG_FACILITY NFSDBG_PROC
74
Trond Myklebust30846df2018-04-07 13:44:28 -040075#define NFS4_BITMASK_SZ 3
76
Trond Myklebust2066fe82006-09-15 08:30:46 -040077#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070078#define NFS4_POLL_RETRY_MAX (15*HZ)
79
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020080/* file attributes which can be mapped to nfs attributes */
81#define NFS4_VALID_ATTRS (ATTR_MODE \
82 | ATTR_UID \
83 | ATTR_GID \
84 | ATTR_SIZE \
85 | ATTR_ATIME \
86 | ATTR_MTIME \
87 | ATTR_CTIME \
88 | ATTR_ATIME_SET \
89 | ATTR_MTIME_SET)
90
Trond Myklebustcdd4e682006-01-03 09:55:12 +010091struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080092static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070093static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050094static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040095static 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
Frank van der Lindenb78ef842020-06-23 22:38:55 +0000260 | FATTR4_WORD2_XATTR_SUPPORT
Linus Torvalds1da177e2005-04-16 15:20:36 -0700261};
262
David Quigleya09df2c2013-05-22 12:50:41 -0400263const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500264 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400265 | FATTR4_WORD0_SIZE
266 | FATTR4_WORD0_FSID
267 | FATTR4_WORD0_FILEID
268 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500269 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400270 | FATTR4_WORD1_OWNER_GROUP
271 | FATTR4_WORD1_RAWDEV
272 | FATTR4_WORD1_SPACE_USED
273 | FATTR4_WORD1_TIME_ACCESS
274 | FATTR4_WORD1_TIME_METADATA
275 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400276 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400277};
278
Trond Myklebust30846df2018-04-07 13:44:28 -0400279static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
280 struct inode *inode)
281{
282 unsigned long cache_validity;
283
284 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
285 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
286 return;
287
288 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
289 if (!(cache_validity & NFS_INO_REVAL_FORCED))
290 cache_validity &= ~(NFS_INO_INVALID_CHANGE
291 | NFS_INO_INVALID_SIZE);
292
293 if (!(cache_validity & NFS_INO_INVALID_SIZE))
294 dst[0] &= ~FATTR4_WORD0_SIZE;
295
296 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
297 dst[0] &= ~FATTR4_WORD0_CHANGE;
298}
299
300static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
301 const __u32 *src, struct inode *inode)
302{
303 nfs4_bitmap_copy_adjust(dst, src, inode);
304}
305
Al Virobc4785c2006-10-19 23:28:51 -0700306static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700307 struct nfs4_readdir_arg *readdir)
308{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400309 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700310 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311
Linus Torvalds1da177e2005-04-16 15:20:36 -0700312 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000313 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
315 return;
316 }
317
318 readdir->cookie = 0;
319 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
320 if (cookie == 2)
321 return;
322
323 /*
324 * NFSv4 servers do not return entries for '.' and '..'
325 * Therefore, we fake these entries here. We let '.'
326 * have cookie 0 and '..' have cookie 1. Note that
327 * when talking to the server, we always send cookie 0
328 * instead of 1 or 2.
329 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800330 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331
332 if (cookie == 0) {
333 *p++ = xdr_one; /* next */
334 *p++ = xdr_zero; /* cookie, first word */
335 *p++ = xdr_one; /* cookie, second word */
336 *p++ = xdr_one; /* entry len */
337 memcpy(p, ".\0\0\0", 4); /* entry */
338 p++;
339 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400340 *p++ = htonl(attrs); /* bitmap */
341 *p++ = htonl(12); /* attribute buffer length */
342 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000343 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 }
345
346 *p++ = xdr_one; /* next */
347 *p++ = xdr_zero; /* cookie, first word */
348 *p++ = xdr_two; /* cookie, second word */
349 *p++ = xdr_two; /* entry len */
350 memcpy(p, "..\0\0", 4); /* entry */
351 p++;
352 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400353 *p++ = htonl(attrs); /* bitmap */
354 *p++ = htonl(12); /* attribute buffer length */
355 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000356 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357
358 readdir->pgbase = (char *)p - (char *)start;
359 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800360 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361}
362
Trond Myklebust26d36302016-09-22 13:39:05 -0400363static void nfs4_test_and_free_stateid(struct nfs_server *server,
364 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100365 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400366{
367 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
368
369 ops->test_and_free_expired(server, stateid, cred);
370}
371
372static void __nfs4_free_revoked_stateid(struct nfs_server *server,
373 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100374 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400375{
376 stateid->type = NFS4_REVOKED_STATEID_TYPE;
377 nfs4_test_and_free_stateid(server, stateid, cred);
378}
379
380static void nfs4_free_revoked_stateid(struct nfs_server *server,
381 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100382 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400383{
384 nfs4_stateid tmp;
385
386 nfs4_stateid_copy(&tmp, stateid);
387 __nfs4_free_revoked_stateid(server, &tmp, cred);
388}
389
NeilBrown8478eaa2014-09-18 16:09:27 +1000390static long nfs4_update_delay(long *timeout)
391{
392 long ret;
393 if (!timeout)
394 return NFS4_POLL_RETRY_MAX;
395 if (*timeout <= 0)
396 *timeout = NFS4_POLL_RETRY_MIN;
397 if (*timeout > NFS4_POLL_RETRY_MAX)
398 *timeout = NFS4_POLL_RETRY_MAX;
399 ret = *timeout;
400 *timeout <<= 1;
401 return ret;
402}
403
Trond Myklebust0688e642019-04-07 13:59:09 -0400404static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500405{
Trond Myklebust65de8722008-12-23 15:21:44 -0500406 might_sleep();
407
NeilBrown8478eaa2014-09-18 16:09:27 +1000408 freezable_schedule_timeout_killable_unsafe(
409 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400410 if (!__fatal_signal_pending(current))
411 return 0;
412 return -EINTR;
413}
414
415static int nfs4_delay_interruptible(long *timeout)
416{
417 might_sleep();
418
He Zhe59679d92020-07-06 17:52:24 +0800419 freezable_schedule_timeout_interruptible_unsafe(nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400420 if (!signal_pending(current))
421 return 0;
422 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
423}
424
425static int nfs4_delay(long *timeout, bool interruptible)
426{
427 if (interruptible)
428 return nfs4_delay_interruptible(timeout);
429 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500430}
431
Trond Myklebust50c80002019-07-11 19:02:18 -0400432static const nfs4_stateid *
433nfs4_recoverable_stateid(const nfs4_stateid *stateid)
434{
435 if (!stateid)
436 return NULL;
437 switch (stateid->type) {
438 case NFS4_OPEN_STATEID_TYPE:
439 case NFS4_LOCK_STATEID_TYPE:
440 case NFS4_DELEGATION_STATEID_TYPE:
441 return stateid;
442 default:
443 break;
444 }
445 return NULL;
446}
447
Trond Myklebust65de8722008-12-23 15:21:44 -0500448/* This is the error handling routine for processes that are allowed
449 * to sleep.
450 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400451static int nfs4_do_handle_exception(struct nfs_server *server,
452 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500453{
454 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500455 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400456 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500457 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500458 int ret = errorcode;
459
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400460 exception->delay = 0;
461 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500462 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400463
Trond Myklebust50c80002019-07-11 19:02:18 -0400464 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400465 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400466 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400467
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 switch(errorcode) {
469 case 0:
470 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400471 case -NFS4ERR_BADHANDLE:
472 case -ESTALE:
473 if (inode != NULL && S_ISREG(inode->i_mode))
474 pnfs_destroy_layout(NFS_I(inode));
475 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400476 case -NFS4ERR_DELEG_REVOKED:
477 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400478 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400479 case -NFS4ERR_BAD_STATEID:
Olga Kornievskaiafefa1a82019-06-14 14:22:12 -0400480 case -NFS4ERR_PARTNER_NO_AUTH:
Trond Myklebust272289a2016-09-22 13:39:15 -0400481 if (inode != NULL && stateid != NULL) {
482 nfs_inode_find_state_and_recover(inode,
483 stateid);
484 goto wait_on_recovery;
485 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500486 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400487 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400488 if (inode) {
489 int err;
490
491 err = nfs_async_inode_return_delegation(inode,
492 stateid);
493 if (err == 0)
494 goto wait_on_recovery;
495 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
496 exception->retry = 1;
497 break;
498 }
499 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500500 if (state == NULL)
501 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400502 ret = nfs4_schedule_stateid_recovery(server, state);
503 if (ret < 0)
504 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500505 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500506 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500507 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500508 nfs4_schedule_lease_recovery(clp);
509 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400510 case -NFS4ERR_MOVED:
511 ret = nfs4_schedule_migration_recovery(server);
512 if (ret < 0)
513 break;
514 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400515 case -NFS4ERR_LEASE_MOVED:
516 nfs4_schedule_lease_moved_recovery(clp);
517 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500518#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400519 case -NFS4ERR_BADSESSION:
520 case -NFS4ERR_BADSLOT:
521 case -NFS4ERR_BAD_HIGH_SLOT:
522 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
523 case -NFS4ERR_DEADSESSION:
524 case -NFS4ERR_SEQ_FALSE_RETRY:
525 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust5c441542019-11-13 08:34:00 +0100526 /* Handled in nfs41_sequence_process() */
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
Anna Schumaker913fadc2020-07-08 10:33:40 -0400779static void nfs4_probe_sequence(struct nfs_client *client, const struct cred *cred,
780 struct nfs4_slot *slot)
781{
782 struct rpc_task *task = _nfs41_proc_sequence(client, cred, slot, true);
783 if (!IS_ERR(task))
784 rpc_put_task_async(task);
785}
786
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400787static int nfs41_sequence_process(struct rpc_task *task,
788 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400789{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500790 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500791 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400792 struct nfs_client *clp;
Trond Myklebust5c441542019-11-13 08:34:00 +0100793 int status;
Trond Myklebust85563072012-12-11 10:31:12 -0500794 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400795
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500796 if (slot == NULL)
797 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400798 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500799 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400800 goto out;
801
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500802 session = slot->table->session;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400803 clp = session->clp;
Trond Myklebust933602e2012-11-16 12:12:38 -0500804
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400805 trace_nfs4_sequence_done(session, res);
Trond Myklebust5c441542019-11-13 08:34:00 +0100806
807 status = res->sr_status;
808 if (task->tk_status == -NFS4ERR_DEADSESSION)
809 status = -NFS4ERR_DEADSESSION;
810
Andy Adamson691daf32009-12-04 15:55:39 -0500811 /* Check the SEQUENCE operation status */
Trond Myklebust5c441542019-11-13 08:34:00 +0100812 switch (status) {
Trond Myklebust14516c32010-07-31 14:29:06 -0400813 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400814 /* Mark this sequence number as having been acked */
815 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400816 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400817 slot->seq_done = 1;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500818 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500819 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400820 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
821 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500822 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400823 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500824 case 1:
825 /*
826 * sr_status remains 1 if an RPC level error occurred.
827 * The server may or may not have processed the sequence
828 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500829 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400830 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
831 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500832 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400833 case -NFS4ERR_DELAY:
834 /* The server detected a resend of the RPC call and
835 * returned NFS4ERR_DELAY as per Section 2.10.6.2
836 * of RFC5661.
837 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500838 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400839 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500840 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500841 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400842 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400843 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400844 case -NFS4ERR_RETRY_UNCACHED_REP:
845 case -NFS4ERR_SEQ_FALSE_RETRY:
846 /*
847 * The server thinks we tried to replay a request.
848 * Retry the call after bumping the sequence ID.
849 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400850 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400851 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500852 case -NFS4ERR_BADSLOT:
853 /*
854 * The slot id we used was probably retired. Try again
855 * using a different slot id.
856 */
Trond Myklebust99589102018-06-09 12:50:50 -0400857 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400858 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500859 goto retry_nowait;
860 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400861 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500862 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400863 * Were one or more calls using this slot interrupted?
864 * If the server never received the request, then our
Anna Schumaker913fadc2020-07-08 10:33:40 -0400865 * transmitted slot sequence number may be too high. However,
866 * if the server did receive the request then it might
867 * accidentally give us a reply with a mismatched operation.
868 * We can sort this out by sending a lone sequence operation
869 * to the server on the same slot.
Trond Myklebustac20d162012-12-15 15:36:07 -0500870 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400871 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
872 slot->seq_nr--;
Anna Schumaker913fadc2020-07-08 10:33:40 -0400873 if (task->tk_msg.rpc_proc != &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE]) {
874 nfs4_probe_sequence(clp, task->tk_msg.rpc_cred, slot);
875 res->sr_slot = NULL;
876 }
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500877 goto retry_nowait;
878 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400879 /*
880 * RFC5661:
881 * A retry might be sent while the original request is
882 * still in progress on the replier. The replier SHOULD
883 * deal with the issue by returning NFS4ERR_DELAY as the
884 * reply to SEQUENCE or CB_SEQUENCE operation, but
885 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
886 *
887 * Restart the search after a delay.
888 */
889 slot->seq_nr = slot->seq_nr_highest_sent;
890 goto out_retry;
Trond Myklebust5c441542019-11-13 08:34:00 +0100891 case -NFS4ERR_BADSESSION:
892 case -NFS4ERR_DEADSESSION:
893 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
894 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400895 default:
896 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400897 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400898 }
899out:
900 /* The session may be reset by one of the error handlers. */
901 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500902out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500903 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400904session_recover:
Trond Myklebust5c441542019-11-13 08:34:00 +0100905 nfs4_schedule_session_recovery(session, status);
906 dprintk("%s ERROR: %d Reset session\n", __func__, status);
907 nfs41_sequence_free_slot(res);
908 goto out;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400909retry_new_seq:
910 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500911retry_nowait:
912 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400913 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500914 task->tk_status = 0;
915 ret = 0;
916 }
917 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400918out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400919 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400920 goto out;
921 rpc_delay(task, NFS4_POLL_RETRY_MAX);
922 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400923}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400924
925int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
926{
927 if (!nfs41_sequence_process(task, res))
928 return 0;
929 if (res->sr_slot != NULL)
930 nfs41_sequence_free_slot(res);
931 return 1;
932
933}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500934EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400935
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400936static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
937{
938 if (res->sr_slot == NULL)
939 return 1;
940 if (res->sr_slot->table->session != NULL)
941 return nfs41_sequence_process(task, res);
942 return nfs40_sequence_done(task, res);
943}
944
945static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
946{
947 if (res->sr_slot != NULL) {
948 if (res->sr_slot->table->session != NULL)
949 nfs41_sequence_free_slot(res);
950 else
951 nfs40_sequence_free_slot(res);
952 }
953}
954
Peng Tao2c4b1312014-06-11 05:24:16 +0800955int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400956{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500957 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400958 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400959 if (!res->sr_slot->table->session)
960 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400961 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400962}
Peng Tao2c4b1312014-06-11 05:24:16 +0800963EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400964
Andy Adamsonce5039c2009-04-01 09:22:13 -0400965static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
966{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400967 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400968
Trond Myklebust035168ab2010-06-16 09:52:26 -0400969 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
970
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500971 nfs4_setup_sequence(data->seq_server->nfs_client,
972 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400973}
974
Andy Adamson69ab40c2009-04-01 09:22:19 -0400975static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
976{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400977 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400978
Trond Myklebust14516c32010-07-31 14:29:06 -0400979 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400980}
981
Trond Myklebust17280172012-03-11 13:11:00 -0400982static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400983 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400984 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400985};
986
Chuck Lever3bd23842013-08-09 12:49:19 -0400987#else /* !CONFIG_NFS_V4_1 */
988
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400989static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
990{
991 return nfs40_sequence_done(task, res);
992}
993
994static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
995{
996 if (res->sr_slot != NULL)
997 nfs40_sequence_free_slot(res);
998}
999
Peng Tao2c4b1312014-06-11 05:24:16 +08001000int nfs4_sequence_done(struct rpc_task *task,
1001 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -04001002{
Chuck Lever3bd23842013-08-09 12:49:19 -04001003 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -04001004}
Peng Tao2c4b1312014-06-11 05:24:16 +08001005EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -04001006
1007#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -04001008
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001009static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
1010{
1011 res->sr_timestamp = jiffies;
1012 res->sr_status_flags = 0;
1013 res->sr_status = 1;
1014}
1015
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001016static
1017void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
1018 struct nfs4_sequence_res *res,
1019 struct nfs4_slot *slot)
1020{
1021 if (!slot)
1022 return;
1023 slot->privileged = args->sa_privileged ? 1 : 0;
1024 args->sa_slot = slot;
1025
1026 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001027}
1028
1029int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001030 struct nfs4_sequence_args *args,
1031 struct nfs4_sequence_res *res,
1032 struct rpc_task *task)
1033{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001034 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001035 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001036 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001037
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001038 /* slot already allocated? */
1039 if (res->sr_slot != NULL)
1040 goto out_start;
1041
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001042 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001043 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001044
Trond Myklebust3453d572018-06-20 17:53:34 -04001045 spin_lock(&tbl->slot_tbl_lock);
1046 /* The state manager will wait until the slot table is empty */
1047 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1048 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001049
Trond Myklebust3453d572018-06-20 17:53:34 -04001050 slot = nfs4_alloc_slot(tbl);
1051 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001052 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001053 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001054 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001055 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001056 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001057
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001058 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001059
Anna Schumakerad05cc02017-01-11 13:37:06 -05001060 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001061out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001062 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001063 rpc_call_start(task);
1064 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001065out_sleep_timeout:
1066 /* Try again in 1/4 second */
1067 if (args->sa_privileged)
1068 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1069 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1070 else
1071 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1072 NULL, jiffies + (HZ >> 2));
1073 spin_unlock(&tbl->slot_tbl_lock);
1074 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001075out_sleep:
1076 if (args->sa_privileged)
1077 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001078 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001079 else
1080 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1081 spin_unlock(&tbl->slot_tbl_lock);
1082 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001083}
1084EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1085
Chuck Lever9915ea72013-08-09 12:48:27 -04001086static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1087{
1088 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001089 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001090 data->seq_args, data->seq_res, task);
1091}
1092
1093static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1094{
1095 struct nfs4_call_sync_data *data = calldata;
1096 nfs4_sequence_done(task, data->seq_res);
1097}
1098
1099static const struct rpc_call_ops nfs40_call_sync_ops = {
1100 .rpc_call_prepare = nfs40_call_sync_prepare,
1101 .rpc_call_done = nfs40_call_sync_done,
1102};
1103
Anna Schumaker48c05852019-08-14 15:27:00 -04001104static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1105{
1106 int ret;
1107 struct rpc_task *task;
1108
1109 task = rpc_run_task(task_setup);
1110 if (IS_ERR(task))
1111 return PTR_ERR(task);
1112
1113 ret = task->tk_status;
1114 rpc_put_task(task);
1115 return ret;
1116}
1117
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001118static int nfs4_do_call_sync(struct rpc_clnt *clnt,
1119 struct nfs_server *server,
1120 struct rpc_message *msg,
1121 struct nfs4_sequence_args *args,
1122 struct nfs4_sequence_res *res,
1123 unsigned short task_flags)
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001124{
Chuck Lever9915ea72013-08-09 12:48:27 -04001125 struct nfs_client *clp = server->nfs_client;
1126 struct nfs4_call_sync_data data = {
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001127 .seq_server = server,
Trond Myklebustad389da2007-06-05 12:30:00 -04001128 .seq_args = args,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001129 .seq_res = res,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001130 };
1131 struct rpc_task_setup task_setup = {
Trond Myklebustad389da2007-06-05 12:30:00 -04001132 .rpc_client = clnt,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001133 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001134 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001135 .callback_data = &data,
1136 .flags = task_flags,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001137 };
1138
Anna Schumaker48c05852019-08-14 15:27:00 -04001139 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001140}
1141
Trond Myklebustc74dfe92020-01-06 15:39:37 -05001142static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
1143 struct nfs_server *server,
1144 struct rpc_message *msg,
1145 struct nfs4_sequence_args *args,
1146 struct nfs4_sequence_res *res)
1147{
1148 return nfs4_do_call_sync(clnt, server, msg, args, res, 0);
1149}
1150
1151
Bryan Schumaker7c513052011-03-24 17:12:24 +00001152int nfs4_call_sync(struct rpc_clnt *clnt,
1153 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001154 struct rpc_message *msg,
1155 struct nfs4_sequence_args *args,
1156 struct nfs4_sequence_res *res,
1157 int cache_reply)
1158{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001159 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001160 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001161}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001162
Trond Myklebust3c591172018-07-31 15:54:10 -04001163static void
1164nfs4_inc_nlink_locked(struct inode *inode)
1165{
1166 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1167 inc_nlink(inode);
1168}
1169
1170static void
1171nfs4_dec_nlink_locked(struct inode *inode)
1172{
1173 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1174 drop_nlink(inode);
1175}
1176
1177static void
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001178nfs4_update_changeattr_locked(struct inode *inode,
1179 struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001180 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001181{
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001182 struct nfs_inode *nfsi = NFS_I(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183
Trond Myklebust16e14372018-03-20 16:53:31 -04001184 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1185 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001186 | cache_validity;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001187
1188 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(inode)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001189 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1190 nfsi->attrtimeo_timestamp = jiffies;
1191 } else {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001192 if (S_ISDIR(inode->i_mode)) {
1193 nfsi->cache_validity |= NFS_INO_INVALID_DATA;
1194 nfs_force_lookup_revalidate(inode);
1195 } else {
1196 if (!NFS_PROTO(inode)->have_delegation(inode,
1197 FMODE_READ))
1198 nfsi->cache_validity |= NFS_INO_REVAL_PAGECACHE;
1199 }
1200
1201 if (cinfo->before != inode_peek_iversion_raw(inode))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001202 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
Frank van der Linden0f44da52020-06-23 22:39:00 +00001203 NFS_INO_INVALID_ACL |
1204 NFS_INO_INVALID_XATTR;
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001205 }
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001206 inode_set_iversion_raw(inode, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001207 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001208 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001209 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001210
1211 if (nfsi->cache_validity & NFS_INO_INVALID_DATA)
1212 nfs_fscache_invalidate(inode);
Trond Myklebust3c591172018-07-31 15:54:10 -04001213}
1214
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001215void
1216nfs4_update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001217 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001218{
1219 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00001220 nfs4_update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221 spin_unlock(&dir->i_lock);
1222}
1223
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001224struct nfs4_open_createattrs {
1225 struct nfs4_label *label;
1226 struct iattr *sattr;
1227 const __u32 verf[2];
1228};
1229
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001230static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1231 int err, struct nfs4_exception *exception)
1232{
1233 if (err != -EINVAL)
1234 return false;
1235 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1236 return false;
1237 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1238 exception->retry = 1;
1239 return true;
1240}
1241
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001242static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1243{
1244 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1245}
1246
1247static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1248{
1249 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1250
1251 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1252}
1253
Trond Myklebust6ae37332015-01-30 14:21:14 -05001254static u32
1255nfs4_map_atomic_open_share(struct nfs_server *server,
1256 fmode_t fmode, int openflags)
1257{
1258 u32 res = 0;
1259
1260 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1261 case FMODE_READ:
1262 res = NFS4_SHARE_ACCESS_READ;
1263 break;
1264 case FMODE_WRITE:
1265 res = NFS4_SHARE_ACCESS_WRITE;
1266 break;
1267 case FMODE_READ|FMODE_WRITE:
1268 res = NFS4_SHARE_ACCESS_BOTH;
1269 }
1270 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1271 goto out;
1272 /* Want no delegation if we're using O_DIRECT */
1273 if (openflags & O_DIRECT)
1274 res |= NFS4_SHARE_WANT_NO_DELEG;
1275out:
1276 return res;
1277}
1278
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001279static enum open_claim_type4
1280nfs4_map_atomic_open_claim(struct nfs_server *server,
1281 enum open_claim_type4 claim)
1282{
1283 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1284 return claim;
1285 switch (claim) {
1286 default:
1287 return claim;
1288 case NFS4_OPEN_CLAIM_FH:
1289 return NFS4_OPEN_CLAIM_NULL;
1290 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1291 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1292 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1293 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1294 }
1295}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001296
1297static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001298{
1299 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001300 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 p->o_res.seqid = p->o_arg.seqid;
1302 p->c_res.seqid = p->c_arg.seqid;
1303 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001304 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001306 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001307}
1308
Al Viro82a2c1b2011-06-22 18:30:55 -04001309static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001310 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001311 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001312 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001313 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001314{
Al Viro82a2c1b2011-06-22 18:30:55 -04001315 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001316 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001317 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001318 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001319 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001320 struct nfs4_opendata *p;
1321
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001322 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001323 if (p == NULL)
1324 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001325
1326 p->f_label = nfs4_label_alloc(server, gfp_mask);
1327 if (IS_ERR(p->f_label))
1328 goto err_free_p;
1329
Kinglong Meea49c2692015-07-27 15:31:38 +08001330 p->a_label = nfs4_label_alloc(server, gfp_mask);
1331 if (IS_ERR(p->a_label))
1332 goto err_free_f;
1333
Trond Myklebust63f5f792015-01-23 19:19:25 -05001334 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1335 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001336 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001337 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001338 nfs_sb_active(dentry->d_sb);
1339 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001340 p->dir = parent;
1341 p->owner = sp;
1342 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001343 p->o_arg.open_flags = flags;
1344 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001345 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001346 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1347 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001348 if (flags & O_CREAT) {
1349 p->o_arg.umask = current_umask();
1350 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1351 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1352 p->o_arg.u.attrs = &p->attrs;
1353 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1354
1355 memcpy(p->o_arg.u.verifier.data, c->verf,
1356 sizeof(p->o_arg.u.verifier.data));
1357 }
1358 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001359 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1360 * will return permission denied for all bits until close */
1361 if (!(flags & O_EXCL)) {
1362 /* ask server to check for all possible rights as results
1363 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001364 switch (p->o_arg.claim) {
1365 default:
1366 break;
1367 case NFS4_OPEN_CLAIM_NULL:
1368 case NFS4_OPEN_CLAIM_FH:
1369 p->o_arg.access = NFS4_ACCESS_READ |
1370 NFS4_ACCESS_MODIFY |
1371 NFS4_ACCESS_EXTEND |
1372 NFS4_ACCESS_EXECUTE;
Frank van der Linden72832a22020-06-23 22:38:58 +00001373#ifdef CONFIG_NFS_V4_2
1374 if (server->caps & NFS_CAP_XATTR)
1375 p->o_arg.access |= NFS4_ACCESS_XAREAD |
1376 NFS4_ACCESS_XAWRITE |
1377 NFS4_ACCESS_XALIST;
1378#endif
Trond Myklebust536585c2016-11-10 15:40:34 -05001379 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001380 }
David Howells7539bba2006-08-22 20:06:09 -04001381 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001382 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1383 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001384 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001385 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001386 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001387 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001388 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001389 case NFS4_OPEN_CLAIM_NULL:
1390 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1391 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1392 p->o_arg.fh = NFS_FH(dir);
1393 break;
1394 case NFS4_OPEN_CLAIM_PREVIOUS:
1395 case NFS4_OPEN_CLAIM_FH:
1396 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1397 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001398 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001399 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001400 p->c_arg.fh = &p->o_res.fh;
1401 p->c_arg.stateid = &p->o_res.stateid;
1402 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001403 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001404 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001405 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001406
1407err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001408 nfs4_label_free(p->a_label);
1409err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001410 nfs4_label_free(p->f_label);
1411err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001412 kfree(p);
1413err:
1414 dput(parent);
1415 return NULL;
1416}
1417
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001418static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001419{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001420 struct nfs4_opendata *p = container_of(kref,
1421 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001422 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001423
Fred Isaman30ae2412016-10-18 13:39:51 -04001424 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001425 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001426 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001427 if (p->state != NULL)
1428 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001429 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001430
Kinglong Meea49c2692015-07-27 15:31:38 +08001431 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001432 nfs4_label_free(p->f_label);
1433
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001434 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001435 dput(p->dentry);
1436 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001437 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001438 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001439 kfree(p);
1440}
1441
1442static void nfs4_opendata_put(struct nfs4_opendata *p)
1443{
1444 if (p != NULL)
1445 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001446}
1447
Trond Myklebust24311f82015-09-20 10:50:17 -04001448static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1449 fmode_t fmode)
1450{
1451 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1452 case FMODE_READ|FMODE_WRITE:
1453 return state->n_rdwr != 0;
1454 case FMODE_WRITE:
1455 return state->n_wronly != 0;
1456 case FMODE_READ:
1457 return state->n_rdonly != 0;
1458 }
1459 WARN_ON_ONCE(1);
1460 return false;
1461}
1462
Trond Myklebustbe189f72018-09-27 17:12:33 -04001463static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1464 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001465{
1466 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001467
Trond Myklebust536e43d2012-01-17 22:04:26 -05001468 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001469 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001470 switch (claim) {
1471 case NFS4_OPEN_CLAIM_NULL:
1472 case NFS4_OPEN_CLAIM_FH:
1473 goto out;
1474 default:
1475 break;
1476 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001477 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001478 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001479 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1480 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001481 break;
1482 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001483 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1484 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001485 break;
1486 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001487 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1488 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001489 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001490out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001491 return ret;
1492}
1493
Trond Myklebust2a606182015-08-19 22:30:00 -05001494static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1495 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001496{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001497 if (delegation == NULL)
1498 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001499 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001500 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001501 switch (claim) {
1502 case NFS4_OPEN_CLAIM_NULL:
1503 case NFS4_OPEN_CLAIM_FH:
1504 break;
1505 case NFS4_OPEN_CLAIM_PREVIOUS:
1506 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1507 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001508 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001509 default:
1510 return 0;
1511 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001512 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001513 return 1;
1514}
1515
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001516static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001517{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001518 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001519 case FMODE_WRITE:
1520 state->n_wronly++;
1521 break;
1522 case FMODE_READ:
1523 state->n_rdonly++;
1524 break;
1525 case FMODE_READ|FMODE_WRITE:
1526 state->n_rdwr++;
1527 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001528 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001529}
1530
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001531#ifdef CONFIG_NFS_V4_1
1532static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1533{
1534 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1535 return true;
1536 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1537 return true;
1538 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1539 return true;
1540 return false;
1541}
1542#endif /* CONFIG_NFS_V4_1 */
1543
Trond Myklebustc9399f22017-11-06 15:28:01 -05001544static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1545{
1546 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1547 wake_up_all(&state->waitq);
1548}
1549
1550static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1551 const nfs4_stateid *stateid)
1552{
1553 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1554 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1555
1556 if (stateid_seqid == state_seqid + 1U ||
1557 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1558 nfs_state_log_update_open_stateid(state);
1559 else
1560 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1561}
1562
Trond Myklebust4f14c192014-02-12 19:15:06 -05001563static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001564{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001565 struct nfs_client *clp = state->owner->so_server->nfs_client;
1566 bool need_recover = false;
1567
1568 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1569 need_recover = true;
1570 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1571 need_recover = true;
1572 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1573 need_recover = true;
1574 if (need_recover)
1575 nfs4_state_mark_reclaim_nograce(clp, state);
1576}
1577
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578/*
1579 * Check for whether or not the caller may update the open stateid
1580 * to the value passed in by stateid.
1581 *
1582 * Note: This function relies heavily on the server implementing
1583 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1584 * correctly.
1585 * i.e. The stateid seqids have to be initialised to 1, and
1586 * are then incremented on every state transition.
1587 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001588static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001589 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001590{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001591 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1592 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1593 if (stateid->seqid == cpu_to_be32(1))
1594 nfs_state_log_update_open_stateid(state);
1595 else
1596 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001597 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001598 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001599
1600 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1601 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001602 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001603 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001604 return false;
1605}
1606
Trond Myklebustf95549c2015-01-23 18:06:09 -05001607static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1608{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001609 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1610 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001611 if (state->n_wronly)
1612 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1613 if (state->n_rdonly)
1614 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1615 if (state->n_rdwr)
1616 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001617 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001618}
1619
Trond Myklebust226056c2014-02-11 10:41:07 -05001620static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1621 nfs4_stateid *stateid, fmode_t fmode)
1622{
1623 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1624 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1625 case FMODE_WRITE:
1626 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1627 break;
1628 case FMODE_READ:
1629 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1630 break;
1631 case 0:
1632 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1633 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1634 clear_bit(NFS_OPEN_STATE, &state->flags);
1635 }
1636 if (stateid == NULL)
1637 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001638 /* Handle OPEN+OPEN_DOWNGRADE races */
1639 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1640 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001641 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001642 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001643 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001644 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001645 nfs4_stateid_copy(&state->stateid, stateid);
1646 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001647 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001648out:
1649 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001650}
1651
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001652static void nfs_clear_open_stateid(struct nfs4_state *state,
1653 nfs4_stateid *arg_stateid,
1654 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001655{
1656 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001657 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1658 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1659 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001660 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001661 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1662 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001663}
1664
Trond Myklebust1393d962016-09-22 13:39:13 -04001665static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001666 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001667 __must_hold(&state->owner->so_lock)
1668 __must_hold(&state->seqlock)
1669 __must_hold(RCU)
1670
Trond Myklebust003707c2007-07-05 18:07:55 -04001671{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001672 DEFINE_WAIT(wait);
1673 int status = 0;
1674 for (;;) {
1675
1676 if (!nfs_need_update_open_stateid(state, stateid))
1677 return;
1678 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001679 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001680 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001681 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001682 /* Rely on seqids for serialisation with NFSv4.0 */
1683 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1684 break;
1685
1686 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1687 /*
1688 * Ensure we process the state changes in the same order
1689 * in which the server processed them by delaying the
1690 * update of the stateid until we are in sequence.
1691 */
1692 write_sequnlock(&state->seqlock);
1693 spin_unlock(&state->owner->so_lock);
1694 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001695 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001696 if (!signal_pending(current)) {
1697 if (schedule_timeout(5*HZ) == 0)
1698 status = -EAGAIN;
1699 else
1700 status = 0;
1701 } else
1702 status = -EINTR;
1703 finish_wait(&state->waitq, &wait);
1704 rcu_read_lock();
1705 spin_lock(&state->owner->so_lock);
1706 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001707 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001708
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001709 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1710 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001711 nfs4_stateid_copy(freeme, &state->open_stateid);
1712 nfs_test_and_clear_all_open_stateid(state);
1713 }
1714
Trond Myklebuste999e802014-02-10 18:20:47 -05001715 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1716 nfs4_stateid_copy(&state->stateid, stateid);
1717 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001718 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001719 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001720}
1721
Trond Myklebustc9399f22017-11-06 15:28:01 -05001722static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001723 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001724 fmode_t fmode,
1725 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001727 /*
1728 * Protect the call to nfs4_state_set_mode_locked and
1729 * serialise the stateid update
1730 */
1731 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001732 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1733 switch (fmode) {
1734 case FMODE_READ:
1735 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1736 break;
1737 case FMODE_WRITE:
1738 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1739 break;
1740 case FMODE_READ|FMODE_WRITE:
1741 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001742 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001743 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001744 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001745}
1746
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001747static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1748{
1749 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1750 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1751 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1752 clear_bit(NFS_OPEN_STATE, &state->flags);
1753}
1754
Trond Myklebustc9399f22017-11-06 15:28:01 -05001755static void nfs_state_set_delegation(struct nfs4_state *state,
1756 const nfs4_stateid *deleg_stateid,
1757 fmode_t fmode)
1758{
1759 /*
1760 * Protect the call to nfs4_state_set_mode_locked and
1761 * serialise the stateid update
1762 */
1763 write_seqlock(&state->seqlock);
1764 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1765 set_bit(NFS_DELEGATED_STATE, &state->flags);
1766 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001767}
1768
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001769static void nfs_state_clear_delegation(struct nfs4_state *state)
1770{
1771 write_seqlock(&state->seqlock);
1772 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1773 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1774 write_sequnlock(&state->seqlock);
1775}
1776
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04001777int update_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001778 const nfs4_stateid *open_stateid,
1779 const nfs4_stateid *delegation,
1780 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001781{
Trond Myklebust1393d962016-09-22 13:39:13 -04001782 struct nfs_server *server = NFS_SERVER(state->inode);
1783 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001784 struct nfs_inode *nfsi = NFS_I(state->inode);
1785 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001786 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001787 int ret = 0;
1788
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001789 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001790
1791 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001792 spin_lock(&state->owner->so_lock);
1793 if (open_stateid != NULL) {
1794 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1795 ret = 1;
1796 }
1797
Trond Myklebust333ac782019-10-22 12:12:17 -04001798 deleg_cur = nfs4_get_valid_delegation(state->inode);
Trond Myklebust34310432008-12-23 15:21:38 -05001799 if (deleg_cur == NULL)
1800 goto no_delegation;
1801
1802 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001803 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001804 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001805 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001806 goto no_delegation_unlock;
1807
1808 if (delegation == NULL)
1809 delegation = &deleg_cur->stateid;
Trond Myklebust333ac782019-10-22 12:12:17 -04001810 else if (!nfs4_stateid_match_other(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001811 goto no_delegation_unlock;
1812
Trond Myklebustb7391f42008-12-23 15:21:52 -05001813 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001814 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001815 ret = 1;
1816no_delegation_unlock:
1817 spin_unlock(&deleg_cur->lock);
1818no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001819 if (ret)
1820 update_open_stateflags(state, fmode);
1821 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001822 rcu_read_unlock();
1823
Trond Myklebust4f14c192014-02-12 19:15:06 -05001824 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001825 nfs4_schedule_state_manager(clp);
1826 if (freeme.type != 0)
1827 nfs4_test_and_free_stateid(server, &freeme,
1828 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001829
1830 return ret;
1831}
1832
Trond Myklebust39071e62015-01-24 15:07:56 -05001833static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1834 const nfs4_stateid *stateid)
1835{
1836 struct nfs4_state *state = lsp->ls_state;
1837 bool ret = false;
1838
1839 spin_lock(&state->state_lock);
1840 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1841 goto out_noupdate;
1842 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1843 goto out_noupdate;
1844 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1845 ret = true;
1846out_noupdate:
1847 spin_unlock(&state->state_lock);
1848 return ret;
1849}
Trond Myklebust34310432008-12-23 15:21:38 -05001850
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001851static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001852{
1853 struct nfs_delegation *delegation;
1854
Trond Myklebustf5086242018-03-20 16:43:13 -04001855 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856 rcu_read_lock();
Trond Myklebust40e6aa12019-10-27 13:38:45 -04001857 delegation = nfs4_get_valid_delegation(inode);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001858 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001859 rcu_read_unlock();
1860 return;
1861 }
1862 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001863 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001864}
1865
Trond Myklebust6ee41262007-07-08 14:11:36 -04001866static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001867{
1868 struct nfs4_state *state = opendata->state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001869 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001870 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001871 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001872 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001873 nfs4_stateid stateid;
1874 int ret = -EAGAIN;
1875
Trond Myklebustaac00a82007-07-05 19:02:21 -04001876 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001877 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001878 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001879 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001880 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001881 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001882 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001883 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001884 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04001885 delegation = nfs4_get_valid_delegation(state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05001886 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001887 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001888 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001889 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001890 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001891 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001892 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001893 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001894 if (!opendata->is_recover) {
1895 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1896 if (ret != 0)
1897 goto out;
1898 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001899 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001900
1901 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001902 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001903 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001904 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001905out:
1906 return ERR_PTR(ret);
1907out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001908 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001909 return state;
1910}
1911
Andy Adamsone23008e2012-10-02 21:07:32 -04001912static void
1913nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1914{
1915 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1916 struct nfs_delegation *delegation;
1917 int delegation_flags = 0;
1918
1919 rcu_read_lock();
1920 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1921 if (delegation)
1922 delegation_flags = delegation->flags;
1923 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001924 switch (data->o_arg.claim) {
1925 default:
1926 break;
1927 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1928 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001929 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1930 "returning a delegation for "
1931 "OPEN(CLAIM_DELEGATE_CUR)\n",
1932 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001933 return;
1934 }
1935 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001936 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001937 data->owner->so_cred,
1938 data->o_res.delegation_type,
1939 &data->o_res.delegation,
1940 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001941 else
1942 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001943 data->owner->so_cred,
1944 data->o_res.delegation_type,
1945 &data->o_res.delegation,
1946 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001947
1948 if (data->o_res.do_recall)
1949 nfs_async_inode_return_delegation(state->inode,
1950 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001951}
1952
1953/*
1954 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1955 * and update the nfs4_state.
1956 */
1957static struct nfs4_state *
1958_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1959{
1960 struct inode *inode = data->state->inode;
1961 struct nfs4_state *state = data->state;
1962 int ret;
1963
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001964 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001965 if (data->rpc_status)
1966 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001967 /* cached opens have already been processed */
1968 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001969 }
1970
Andy Adamsone23008e2012-10-02 21:07:32 -04001971 ret = nfs_refresh_inode(inode, &data->f_attr);
1972 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001973 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001974
1975 if (data->o_res.delegation_type != 0)
1976 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001977update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001978 if (!update_open_stateid(state, &data->o_res.stateid,
1979 NULL, data->o_arg.fmode))
1980 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001981 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001982
1983 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001984}
1985
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001986static struct inode *
1987nfs4_opendata_get_inode(struct nfs4_opendata *data)
1988{
1989 struct inode *inode;
1990
1991 switch (data->o_arg.claim) {
1992 case NFS4_OPEN_CLAIM_NULL:
1993 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1994 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1995 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1996 return ERR_PTR(-EAGAIN);
1997 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1998 &data->f_attr, data->f_label);
1999 break;
2000 default:
2001 inode = d_inode(data->dentry);
2002 ihold(inode);
2003 nfs_refresh_inode(inode, &data->f_attr);
2004 }
2005 return inode;
2006}
2007
Andy Adamsone23008e2012-10-02 21:07:32 -04002008static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04002009nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
2010{
2011 struct nfs4_state *state;
2012 struct inode *inode;
2013
2014 inode = nfs4_opendata_get_inode(data);
2015 if (IS_ERR(inode))
2016 return ERR_CAST(inode);
2017 if (data->state != NULL && data->state->inode == inode) {
2018 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002019 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04002020 } else
2021 state = nfs4_get_open_state(inode, data->owner);
2022 iput(inode);
2023 if (state == NULL)
2024 state = ERR_PTR(-ENOMEM);
2025 return state;
2026}
2027
Andy Adamsone23008e2012-10-02 21:07:32 -04002028static struct nfs4_state *
2029_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002030{
Trond Myklebust75e8c482017-08-08 10:38:07 -04002031 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002032
Trond Myklebustaac00a82007-07-05 19:02:21 -04002033 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04002034 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002035 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002036 goto out;
2037 }
2038
Trond Myklebust75e8c482017-08-08 10:38:07 -04002039 state = nfs4_opendata_find_nfs4_state(data);
2040 if (IS_ERR(state))
2041 goto out;
2042
Andy Adamsone23008e2012-10-02 21:07:32 -04002043 if (data->o_res.delegation_type != 0)
2044 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01002045 if (!update_open_stateid(state, &data->o_res.stateid,
2046 NULL, data->o_arg.fmode)) {
2047 nfs4_put_open_state(state);
2048 state = ERR_PTR(-EAGAIN);
2049 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002050out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08002051 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002052 return state;
2053}
2054
Andy Adamsone23008e2012-10-02 21:07:32 -04002055static struct nfs4_state *
2056nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2057{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002058 struct nfs4_state *ret;
2059
Andy Adamsone23008e2012-10-02 21:07:32 -04002060 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002061 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2062 else
2063 ret = _nfs4_opendata_to_nfs4_state(data);
2064 nfs4_sequence_free_slot(&data->o_res.seq_res);
2065 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002066}
2067
Trond Myklebust0de43972018-09-02 15:57:01 -04002068static struct nfs_open_context *
2069nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002070{
2071 struct nfs_inode *nfsi = NFS_I(state->inode);
2072 struct nfs_open_context *ctx;
2073
Trond Myklebust0de43972018-09-02 15:57:01 -04002074 rcu_read_lock();
2075 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002076 if (ctx->state != state)
2077 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002078 if ((ctx->mode & mode) != mode)
2079 continue;
2080 if (!get_nfs_open_context(ctx))
2081 continue;
2082 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002083 return ctx;
2084 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002085 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002086 return ERR_PTR(-ENOENT);
2087}
2088
Trond Myklebust0de43972018-09-02 15:57:01 -04002089static struct nfs_open_context *
2090nfs4_state_find_open_context(struct nfs4_state *state)
2091{
2092 struct nfs_open_context *ctx;
2093
2094 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2095 if (!IS_ERR(ctx))
2096 return ctx;
2097 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2098 if (!IS_ERR(ctx))
2099 return ctx;
2100 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2101}
2102
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002103static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2104 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002105{
2106 struct nfs4_opendata *opendata;
2107
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002108 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002109 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002110 if (opendata == NULL)
2111 return ERR_PTR(-ENOMEM);
2112 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002113 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002114 return opendata;
2115}
2116
Trond Myklebust24311f82015-09-20 10:50:17 -04002117static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2118 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002119{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002120 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002121 int ret;
2122
Trond Myklebust24311f82015-09-20 10:50:17 -04002123 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002124 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002125 opendata->o_arg.open_flags = 0;
2126 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002127 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2128 NFS_SB(opendata->dentry->d_sb),
2129 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002130 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2131 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2132 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002133 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002134 if (ret != 0)
2135 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002136 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002137 if (IS_ERR(newstate))
2138 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002139 if (newstate != opendata->state)
2140 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002141 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002142 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002143}
2144
2145static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2146{
Trond Myklebust864472e2006-01-03 09:55:15 +01002147 int ret;
2148
2149 /* memory barrier prior to reading state->n_* */
2150 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002151 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2152 if (ret != 0)
2153 return ret;
2154 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2155 if (ret != 0)
2156 return ret;
2157 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2158 if (ret != 0)
2159 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002160 /*
2161 * We may have performed cached opens for all three recoveries.
2162 * Check if we need to update the current stateid.
2163 */
2164 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002165 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002166 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002167 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002168 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002169 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002170 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002171 return 0;
2172}
2173
Linus Torvalds1da177e2005-04-16 15:20:36 -07002174/*
2175 * OPEN_RECLAIM:
2176 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002177 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002178static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002180 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002181 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002182 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002183 int status;
2184
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002185 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2186 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002187 if (IS_ERR(opendata))
2188 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002189 rcu_read_lock();
2190 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002191 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002192 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002193 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002194 opendata->o_arg.u.delegation_type = delegation_type;
2195 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002196 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002197 return status;
2198}
2199
Trond Myklebust539cd032007-06-05 11:46:42 -04002200static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201{
2202 struct nfs_server *server = NFS_SERVER(state->inode);
2203 struct nfs4_exception exception = { };
2204 int err;
2205 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002206 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002207 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002208 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2209 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002210 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002211 break;
2212 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002213 } while (exception.retry);
2214 return err;
2215}
2216
Trond Myklebust864472e2006-01-03 09:55:15 +01002217static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2218{
2219 struct nfs_open_context *ctx;
2220 int ret;
2221
2222 ctx = nfs4_state_find_open_context(state);
2223 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002224 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002225 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2226 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002227 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002228 put_nfs_open_context(ctx);
2229 return ret;
2230}
2231
NeilBrowndce26302017-12-13 09:57:09 +11002232static 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 -07002233{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002234 switch (err) {
2235 default:
2236 printk(KERN_ERR "NFS: %s: unhandled error "
2237 "%d.\n", __func__, err);
2238 case 0:
2239 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002240 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002241 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002242 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002243 break;
2244 case -NFS4ERR_BADSESSION:
2245 case -NFS4ERR_BADSLOT:
2246 case -NFS4ERR_BAD_HIGH_SLOT:
2247 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2248 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002249 return -EAGAIN;
2250 case -NFS4ERR_STALE_CLIENTID:
2251 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002252 /* Don't recall a delegation if it was lost */
2253 nfs4_schedule_lease_recovery(server->nfs_client);
2254 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002255 case -NFS4ERR_MOVED:
2256 nfs4_schedule_migration_recovery(server);
2257 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002258 case -NFS4ERR_LEASE_MOVED:
2259 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2260 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002261 case -NFS4ERR_DELEG_REVOKED:
2262 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002263 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002264 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002265 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002266 nfs_inode_find_state_and_recover(state->inode,
2267 stateid);
2268 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002269 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002270 case -NFS4ERR_DELAY:
2271 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002272 ssleep(1);
2273 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002274 case -ENOMEM:
2275 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002276 if (fl) {
2277 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2278 if (lsp)
2279 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2280 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002281 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002282 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002283 return err;
2284}
2285
Trond Myklebust24311f82015-09-20 10:50:17 -04002286int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002287 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002288{
2289 struct nfs_server *server = NFS_SERVER(state->inode);
2290 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002291 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002292
2293 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2294 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2295 if (IS_ERR(opendata))
2296 return PTR_ERR(opendata);
2297 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002298 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002299 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2300 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002301 goto out;
2302 }
2303 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002304 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2305 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002306 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002307 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002308 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2309 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2310 if (err)
2311 goto out;
2312 }
2313 nfs_state_clear_delegation(state);
2314out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002315 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002316 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002317}
2318
Chuck Leverbe05c862013-08-09 12:49:47 -04002319static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2320{
2321 struct nfs4_opendata *data = calldata;
2322
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002323 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2324 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002325}
2326
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002327static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2328{
2329 struct nfs4_opendata *data = calldata;
2330
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002331 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002332
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002333 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002334 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002335 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002336 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002337 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002338 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002339 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002340}
2341
2342static void nfs4_open_confirm_release(void *calldata)
2343{
2344 struct nfs4_opendata *data = calldata;
2345 struct nfs4_state *state = NULL;
2346
2347 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002348 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002349 goto out_free;
2350 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002351 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002352 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002353 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002354 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002355 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002356out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002357 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002358}
2359
2360static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002361 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002362 .rpc_call_done = nfs4_open_confirm_done,
2363 .rpc_release = nfs4_open_confirm_release,
2364};
2365
2366/*
2367 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2368 */
2369static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2370{
David Howells2b0143b2015-03-17 22:25:59 +00002371 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002372 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002373 struct rpc_message msg = {
2374 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2375 .rpc_argp = &data->c_arg,
2376 .rpc_resp = &data->c_res,
2377 .rpc_cred = data->owner->so_cred,
2378 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002379 struct rpc_task_setup task_setup_data = {
2380 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002381 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002382 .callback_ops = &nfs4_open_confirm_ops,
2383 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002384 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002385 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002386 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002387 int status;
2388
Anna Schumakerfba83f32018-05-04 16:22:50 -04002389 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2390 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002391 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002392 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002393 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002394 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002395 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002396 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002397 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002398 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002399 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002400 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002401 smp_wmb();
2402 } else
2403 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002404 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 return status;
2406}
2407
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002408static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002409{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002410 struct nfs4_opendata *data = calldata;
2411 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002412 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002413 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002414
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002415 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002416 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002417 /*
2418 * Check if we still need to send an OPEN call, or if we can use
2419 * a delegation instead.
2420 */
2421 if (data->state != NULL) {
2422 struct nfs_delegation *delegation;
2423
Trond Myklebustbe189f72018-09-27 17:12:33 -04002424 if (can_open_cached(data->state, data->o_arg.fmode,
2425 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002426 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002427 rcu_read_lock();
Trond Myklebustbe3df3d2019-10-31 18:40:32 -04002428 delegation = nfs4_get_valid_delegation(data->state->inode);
Trond Myklebust2a606182015-08-19 22:30:00 -05002429 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002430 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002431 rcu_read_unlock();
2432 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002433 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002434 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002435 switch (claim) {
2436 default:
2437 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002438 case NFS4_OPEN_CLAIM_PREVIOUS:
2439 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2440 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002441 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002442 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002443 case NFS4_OPEN_CLAIM_FH:
2444 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002445 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002446 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002447 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002448 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002449 &data->o_res.seq_res,
2450 task) != 0)
2451 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002452
2453 /* Set the create mode (note dependency on the session type) */
2454 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2455 if (data->o_arg.open_flags & O_EXCL) {
2456 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2457 if (nfs4_has_persistent_session(clp))
2458 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2459 else if (clp->cl_mvops->minor_version > 0)
2460 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2461 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002462 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002463unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002464 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002465 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002466out_no_action:
2467 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002468out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002469 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002470}
2471
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472static void nfs4_open_done(struct rpc_task *task, void *calldata)
2473{
2474 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002475
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002476 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002477
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002478 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002479 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002480
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002481 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002482 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2483 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002484 case S_IFREG:
2485 break;
2486 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002487 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002488 break;
2489 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002490 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002491 break;
2492 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002493 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002494 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002495 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002496 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002497 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2498 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002499 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002500 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002501}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002502
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002503static void nfs4_open_release(void *calldata)
2504{
2505 struct nfs4_opendata *data = calldata;
2506 struct nfs4_state *state = NULL;
2507
2508 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002509 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002510 goto out_free;
2511 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002512 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002513 goto out_free;
2514 /* In case we need an open_confirm, no cleanup! */
2515 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2516 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002517 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002518 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002519 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002520out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002521 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002522}
2523
2524static const struct rpc_call_ops nfs4_open_ops = {
2525 .rpc_call_prepare = nfs4_open_prepare,
2526 .rpc_call_done = nfs4_open_done,
2527 .rpc_release = nfs4_open_release,
2528};
2529
Fred Isaman3b65a302016-09-19 10:06:49 -04002530static int nfs4_run_open_task(struct nfs4_opendata *data,
2531 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002532{
David Howells2b0143b2015-03-17 22:25:59 +00002533 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002534 struct nfs_server *server = NFS_SERVER(dir);
2535 struct nfs_openargs *o_arg = &data->o_arg;
2536 struct nfs_openres *o_res = &data->o_res;
2537 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002538 struct rpc_message msg = {
2539 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2540 .rpc_argp = o_arg,
2541 .rpc_resp = o_res,
2542 .rpc_cred = data->owner->so_cred,
2543 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002544 struct rpc_task_setup task_setup_data = {
2545 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002546 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002547 .callback_ops = &nfs4_open_ops,
2548 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002549 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05002550 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002551 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002552 int status;
2553
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002554 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002555 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002556 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002557 data->cancelled = false;
2558 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002559 if (!ctx) {
2560 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002561 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002562 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002563 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002564 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002565 pnfs_lgopen_prepare(data, ctx);
2566 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002567 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002568 if (IS_ERR(task))
2569 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002570 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002571 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002572 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002573 smp_wmb();
2574 } else
2575 status = data->rpc_status;
2576 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002577
2578 return status;
2579}
2580
2581static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2582{
David Howells2b0143b2015-03-17 22:25:59 +00002583 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002584 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002585 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002586
Fred Isaman3b65a302016-09-19 10:06:49 -04002587 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002588 if (status != 0 || !data->rpc_done)
2589 return status;
2590
Trond Myklebust6926afd12012-01-07 13:22:46 -05002591 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2592
Anna Schumakerd7e98252017-01-11 16:13:29 -05002593 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002594 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002595
2596 return status;
2597}
2598
Trond Myklebustf3792d62014-07-10 08:54:32 -04002599/*
2600 * Additional permission checks in order to distinguish between an
2601 * open for read, and an open for execute. This works around the
2602 * fact that NFSv4 OPEN treats read and execute permissions as being
2603 * the same.
2604 * Note that in the non-execute case, we want to turn off permission
2605 * checking if we just created a new file (POSIX open() semantics).
2606 */
NeilBrowna52458b2018-12-03 11:30:31 +11002607static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002608 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002609 struct nfs4_state *state, fmode_t fmode,
2610 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002611{
2612 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002613 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002614
2615 /* access call failed or for some reason the server doesn't
2616 * support any access modes -- defer access call until later */
2617 if (opendata->o_res.access_supported == 0)
2618 return 0;
2619
2620 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002621 /*
2622 * Use openflags to check for exec, because fmode won't
2623 * always have FMODE_EXEC set when file open for exec.
2624 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002625 if (openflags & __FMODE_EXEC) {
2626 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002627 if (S_ISDIR(state->inode->i_mode))
2628 mask = NFS4_ACCESS_LOOKUP;
2629 else
2630 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002631 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002632 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002633
2634 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002635 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2636 nfs_access_add_cache(state->inode, &cache);
2637
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002638 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2639 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002640 return 0;
2641
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002642 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002643}
2644
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002645/*
2646 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2647 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002648static int _nfs4_proc_open(struct nfs4_opendata *data,
2649 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002650{
David Howells2b0143b2015-03-17 22:25:59 +00002651 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002652 struct nfs_server *server = NFS_SERVER(dir);
2653 struct nfs_openargs *o_arg = &data->o_arg;
2654 struct nfs_openres *o_res = &data->o_res;
2655 int status;
2656
Fred Isaman3b65a302016-09-19 10:06:49 -04002657 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002658 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002659 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002660 if (status != 0) {
2661 if (status == -NFS4ERR_BADNAME &&
2662 !(o_arg->open_flags & O_CREAT))
2663 return -ENOENT;
2664 return status;
2665 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002666
Trond Myklebust6926afd12012-01-07 13:22:46 -05002667 nfs_fattr_map_and_free_names(server, &data->f_attr);
2668
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002669 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002670 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002671 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002672 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002673 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002674 if (data->file_created ||
2675 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00002676 nfs4_update_changeattr(dir, &o_res->cinfo,
2677 o_res->f_attr->time_start,
2678 NFS_INO_INVALID_DATA);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002679 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002680 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2681 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002682 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002683 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002685 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002687 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2688 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002689 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2690 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002691 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002692 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693}
2694
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695/*
2696 * OPEN_EXPIRED:
2697 * reclaim state on the server after a network partition.
2698 * Assumes caller holds the appropriate lock
2699 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002700static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002702 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002703 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002704
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002705 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002706 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002707 if (IS_ERR(opendata))
2708 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002709 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002710 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002711 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002712 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002713 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714}
2715
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002716static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002717{
Trond Myklebust539cd032007-06-05 11:46:42 -04002718 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002719 struct nfs4_exception exception = { };
2720 int err;
2721
2722 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002723 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002724 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002725 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2726 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002727 switch (err) {
2728 default:
2729 goto out;
2730 case -NFS4ERR_GRACE:
2731 case -NFS4ERR_DELAY:
2732 nfs4_handle_exception(server, err, &exception);
2733 err = 0;
2734 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002735 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002736out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002737 return err;
2738}
2739
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2741{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002743 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744
Trond Myklebust864472e2006-01-03 09:55:15 +01002745 ctx = nfs4_state_find_open_context(state);
2746 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002747 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002748 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002749 put_nfs_open_context(ctx);
2750 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002751}
2752
Trond Myklebust41020b62016-09-22 13:38:58 -04002753static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2754 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002755{
Trond Myklebust41020b62016-09-22 13:38:58 -04002756 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002757 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002758}
2759
2760static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2761{
2762 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002763 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002764}
2765
2766static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2767{
2768 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2769 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002770 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002771 return nfs4_open_expired(sp, state);
2772}
2773
Trond Myklebust45870d62016-09-22 13:38:59 -04002774static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2775 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002776 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002777{
2778 return -NFS4ERR_BAD_STATEID;
2779}
2780
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002781#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002782static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2783 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002784 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002785{
2786 int status;
2787
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002788 switch (stateid->type) {
2789 default:
2790 break;
2791 case NFS4_INVALID_STATEID_TYPE:
2792 case NFS4_SPECIAL_STATEID_TYPE:
2793 return -NFS4ERR_BAD_STATEID;
2794 case NFS4_REVOKED_STATEID_TYPE:
2795 goto out_free;
2796 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002797
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002798 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002799 switch (status) {
2800 case -NFS4ERR_EXPIRED:
2801 case -NFS4ERR_ADMIN_REVOKED:
2802 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002803 break;
2804 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002805 return status;
2806 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002807out_free:
2808 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002809 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002810 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002811}
2812
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002813static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002814{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002815 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002816 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002817 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002818 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002819 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002820
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002821 /* Get the delegation credential for use by test/free_stateid */
2822 rcu_read_lock();
2823 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002824 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002825 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002826 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002827 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002828 }
2829
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002830 spin_lock(&delegation->lock);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002831 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002832
Trond Myklebust994b15b2018-09-05 14:07:14 -04002833 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2834 &delegation->flags)) {
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002835 spin_unlock(&delegation->lock);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002836 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002837 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002838 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002839
NeilBrowna52458b2018-12-03 11:30:31 +11002840 if (delegation->cred)
2841 cred = get_cred(delegation->cred);
Trond Myklebustfc51b1c2020-04-02 15:27:09 -04002842 spin_unlock(&delegation->lock);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002843 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002844 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002845 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002846 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002847 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002848 else
2849 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002850
Trond Myklebust8c39a392019-07-19 13:48:44 -04002851 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002852 return ret;
2853}
2854
2855static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2856{
2857 nfs4_stateid tmp;
2858
2859 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2860 nfs4_copy_delegation_stateid(state->inode, state->state,
2861 &tmp, NULL) &&
2862 nfs4_stateid_match_other(&state->stateid, &tmp))
2863 nfs_state_set_delegation(state, &tmp, state->state);
2864 else
2865 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002866}
2867
2868/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002869 * nfs41_check_expired_locks - possibly free a lock stateid
2870 *
2871 * @state: NFSv4 state for an inode
2872 *
2873 * Returns NFS_OK if recovery for this stateid is now finished.
2874 * Otherwise a negative NFS4ERR value is returned.
2875 */
2876static int nfs41_check_expired_locks(struct nfs4_state *state)
2877{
2878 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002879 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002880 struct nfs_server *server = NFS_SERVER(state->inode);
2881
2882 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2883 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002884
2885 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002886 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2887 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002888 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002889
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002890 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002891 spin_unlock(&state->state_lock);
2892
2893 nfs4_put_lock_state(prev);
2894 prev = lsp;
2895
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002896 status = nfs41_test_and_free_expired_stateid(server,
2897 &lsp->ls_stateid,
2898 cred);
2899 trace_nfs4_test_lock_stateid(state, lsp, status);
2900 if (status == -NFS4ERR_EXPIRED ||
2901 status == -NFS4ERR_BAD_STATEID) {
2902 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002903 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002904 if (!recover_lost_locks)
2905 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2906 } else if (status != NFS_OK) {
2907 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002908 nfs4_put_lock_state(prev);
2909 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002910 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002911 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002912 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002913 }
2914 spin_unlock(&state->state_lock);
2915 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002916out:
2917 return ret;
2918}
2919
2920/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002921 * nfs41_check_open_stateid - possibly free an open stateid
2922 *
2923 * @state: NFSv4 state for an inode
2924 *
2925 * Returns NFS_OK if recovery for this stateid is now finished.
2926 * Otherwise a negative NFS4ERR value is returned.
2927 */
2928static int nfs41_check_open_stateid(struct nfs4_state *state)
2929{
2930 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002931 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002932 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002933 int status;
2934
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002935 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002936 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002937 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002938 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002939 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002940 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002941 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002942 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002943 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002944 if (nfs_open_stateid_recover_openmode(state))
2945 return -NFS4ERR_OPENMODE;
2946 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002947}
2948
2949static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2950{
Chuck Levereb64cf92012-07-11 16:30:05 -04002951 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002952
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002953 status = nfs41_check_delegation_stateid(state);
2954 if (status != NFS_OK)
2955 return status;
2956 nfs41_delegation_recover_stateid(state);
2957
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002958 status = nfs41_check_expired_locks(state);
2959 if (status != NFS_OK)
2960 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002961 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002962 if (status != NFS_OK)
2963 status = nfs4_open_expired(sp, state);
2964 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002965}
2966#endif
2967
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002969 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2970 * fields corresponding to attributes that were used to store the verifier.
2971 * Make sure we clobber those fields in the later setattr call
2972 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002973static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002974 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002975{
Trond Myklebust609339c2018-03-28 16:18:17 -04002976 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2977 __u32 attrset[3];
2978 unsigned ret;
2979 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002980
Trond Myklebust609339c2018-03-28 16:18:17 -04002981 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2982 attrset[i] = opendata->o_res.attrset[i];
2983 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2984 attrset[i] &= ~bitmask[i];
2985 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002986
Trond Myklebust609339c2018-03-28 16:18:17 -04002987 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2988 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002989
Trond Myklebust609339c2018-03-28 16:18:17 -04002990 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2991 if (sattr->ia_valid & ATTR_ATIME_SET)
2992 ret |= ATTR_ATIME_SET;
2993 else
2994 ret |= ATTR_ATIME;
2995 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002996
Trond Myklebust609339c2018-03-28 16:18:17 -04002997 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2998 if (sattr->ia_valid & ATTR_MTIME_SET)
2999 ret |= ATTR_MTIME_SET;
3000 else
3001 ret |= ATTR_MTIME;
3002 }
3003
3004 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08003005 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04003006 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003007}
3008
Trond Myklebustc21443c2013-02-07 14:26:21 -05003009static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003010 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003011{
3012 struct nfs4_state_owner *sp = opendata->owner;
3013 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04003014 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003015 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003016 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003017 struct inode *dir = d_inode(opendata->dir);
3018 unsigned long dir_verifier;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003019 unsigned int seq;
3020 int ret;
3021
3022 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003023 dir_verifier = nfs_save_change_attribute(dir);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003024
Fred Isaman3b65a302016-09-19 10:06:49 -04003025 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03003026 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05003027 goto out;
3028
Trond Myklebustae55e592018-05-22 11:17:16 -04003029 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003030 ret = PTR_ERR(state);
3031 if (IS_ERR(state))
3032 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05003033 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05003034 if (server->caps & NFS_CAP_POSIX_LOCK)
3035 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04003036 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
3037 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003038
Trond Myklebust275bb302013-05-29 13:11:28 -04003039 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00003040 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05003041 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003042 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003043 alias = d_exact_alias(dentry, state->inode);
3044 if (!alias)
3045 alias = d_splice_alias(igrab(state->inode), dentry);
3046 /* d_splice_alias() can't fail here - it's a non-directory */
3047 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04003048 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05003049 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04003050 }
Trond Myklebustcf5b4052020-02-05 09:01:53 -05003051 }
3052
3053 switch(opendata->o_arg.claim) {
3054 default:
3055 break;
3056 case NFS4_OPEN_CLAIM_NULL:
3057 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
3058 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
3059 if (!opendata->rpc_done)
3060 break;
3061 if (opendata->o_res.delegation_type != 0)
3062 dir_verifier = nfs_save_change_attribute(dir);
3063 nfs_set_verifier(dentry, dir_verifier);
Trond Myklebust275bb302013-05-29 13:11:28 -04003064 }
3065
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04003066 /* Parse layoutget results before we check for access */
3067 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
3068
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003069 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
3070 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003071 if (ret != 0)
3072 goto out;
3073
David Howells2b0143b2015-03-17 22:25:59 +00003074 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003075 nfs_inode_attach_open_context(ctx);
3076 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3077 nfs4_schedule_stateid_recovery(server, state);
3078 }
Fred Isaman2409a972016-10-06 12:11:21 -04003079
Trond Myklebustc21443c2013-02-07 14:26:21 -05003080out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003081 if (!opendata->cancelled)
3082 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003083 return ret;
3084}
3085
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003086/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003087 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 */
Andy Adamson82be4172012-05-23 05:02:35 -04003089static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003090 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003091 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003092 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003093 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003094{
3095 struct nfs4_state_owner *sp;
3096 struct nfs4_state *state = NULL;
3097 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003098 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003099 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003100 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003101 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003102 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003103 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003104 struct iattr *sattr = c->sattr;
3105 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003106 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003107 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108
3109 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003111 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3112 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003113 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3114 goto out_err;
3115 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003116 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003117 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003118 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003119 if (d_really_is_positive(dentry))
3120 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003121 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003122 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003123 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003124 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3125 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003126 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003127 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003128
David Quigley14c43f72013-05-22 12:50:43 -04003129 if (label) {
3130 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3131 if (IS_ERR(olabel)) {
3132 status = PTR_ERR(olabel);
3133 goto err_opendata_put;
3134 }
3135 }
3136
Trond Myklebuste911b812014-03-26 13:24:37 -07003137 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3138 if (!opendata->f_attr.mdsthreshold) {
3139 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3140 if (!opendata->f_attr.mdsthreshold)
3141 goto err_free_label;
3142 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003143 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003144 }
David Howells2b0143b2015-03-17 22:25:59 +00003145 if (d_really_is_positive(dentry))
3146 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003147
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003148 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003149 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003150 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003151 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003152
NeilBrownefcbc042015-07-30 13:00:56 +10003153 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003154 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003155 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003156 /*
3157 * send create attributes which was not set by open
3158 * with an extra setattr.
3159 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003160 if (attrs || label) {
3161 unsigned ia_old = sattr->ia_valid;
3162
3163 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003164 nfs_fattr_init(opendata->o_res.f_attr);
3165 status = nfs4_do_setattr(state->inode, cred,
3166 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003167 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003168 if (status == 0) {
3169 nfs_setattr_update_inode(state->inode, sattr,
3170 opendata->o_res.f_attr);
3171 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3172 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003173 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003174 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003175 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003176 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003177 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003178
Trond Myklebuste911b812014-03-26 13:24:37 -07003179 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003180 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003181 opendata->f_attr.mdsthreshold = NULL;
3182 }
Andy Adamson82be4172012-05-23 05:02:35 -04003183
David Quigley14c43f72013-05-22 12:50:43 -04003184 nfs4_label_free(olabel);
3185
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003186 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003187 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003188 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003189err_free_label:
3190 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003191err_opendata_put:
3192 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003193err_put_state_owner:
3194 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003195out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003196 return status;
3197}
3198
3199
Andy Adamson82be4172012-05-23 05:02:35 -04003200static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003201 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003202 int flags,
3203 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003204 struct nfs4_label *label,
3205 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003206{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003207 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003208 struct nfs4_exception exception = {
3209 .interruptible = true,
3210 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003212 struct nfs4_open_createattrs c = {
3213 .label = label,
3214 .sattr = sattr,
3215 .verf = {
3216 [0] = (__u32)jiffies,
3217 [1] = (__u32)current->pid,
3218 },
3219 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 int status;
3221
3222 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003223 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003224 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003225 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 if (status == 0)
3227 break;
3228 /* NOTE: BAD_SEQID means the server and client disagree about the
3229 * book-keeping w.r.t. state-changing operations
3230 * (OPEN/CLOSE/LOCK/LOCKU...)
3231 * It is actually a sign of a bug on the client or on the server.
3232 *
3233 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003234 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 * have unhashed the old state_owner for us, and that we can
3236 * therefore safely retry using a new one. We should still warn
3237 * the user though...
3238 */
3239 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003240 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003241 " returned a bad sequence-id error!\n",
3242 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 exception.retry = 1;
3244 continue;
3245 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003246 /*
3247 * BAD_STATEID on OPEN means that the server cancelled our
3248 * state before it received the OPEN_CONFIRM.
3249 * Recover by retrying the request as per the discussion
3250 * on Page 181 of RFC3530.
3251 */
3252 if (status == -NFS4ERR_BAD_STATEID) {
3253 exception.retry = 1;
3254 continue;
3255 }
Robert Milkowski924491f2020-01-28 08:37:47 +00003256 if (status == -NFS4ERR_EXPIRED) {
3257 nfs4_schedule_lease_recovery(server->nfs_client);
3258 exception.retry = 1;
3259 continue;
3260 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003261 if (status == -EAGAIN) {
3262 /* We must have found a delegation */
3263 exception.retry = 1;
3264 continue;
3265 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003266 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3267 continue;
3268 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003269 status, &exception));
3270 } while (exception.retry);
3271 return res;
3272}
3273
Trond Myklebust8487c472016-06-26 08:44:35 -04003274static int _nfs4_do_setattr(struct inode *inode,
3275 struct nfs_setattrargs *arg,
3276 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003277 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003278 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003279{
3280 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003281 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003282 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3283 .rpc_argp = arg,
3284 .rpc_resp = res,
3285 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003286 };
NeilBrowna52458b2018-12-03 11:30:31 +11003287 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003288 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003289 bool truncate;
3290 int status;
3291
3292 nfs_fattr_init(res->fattr);
3293
3294 /* Servers should only apply open mode checks for file size changes */
3295 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003296 if (!truncate)
3297 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003298
Trond Myklebust991eedb2018-04-09 11:15:30 -04003299 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003300 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003301 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003302 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003303 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003304 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003305 l_ctx = nfs_get_lock_context(ctx);
3306 if (IS_ERR(l_ctx))
3307 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003308 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3309 &arg->stateid, &delegation_cred);
3310 nfs_put_lock_context(l_ctx);
3311 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003312 return -EBADF;
Olga Kornievskaiad826e5b2019-12-18 16:50:42 -05003313 else if (status == -EAGAIN)
3314 goto zero_stateid;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003315 } else {
3316zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003317 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003318 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003319 if (delegation_cred)
3320 msg.rpc_cred = delegation_cred;
3321
3322 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3323
NeilBrowna52458b2018-12-03 11:30:31 +11003324 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003325 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003326 renew_lease(server, timestamp);
3327 trace_nfs4_setattr(inode, &arg->stateid, status);
3328 return status;
3329}
3330
NeilBrowna52458b2018-12-03 11:30:31 +11003331static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003332 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003333 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003334 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003335{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003336 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003337 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003338 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003339 struct nfs_setattrargs arg = {
3340 .fh = NFS_FH(inode),
3341 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003343 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003344 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003345 };
3346 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003347 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003348 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003350 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003351 struct nfs4_exception exception = {
3352 .state = state,
3353 .inode = inode,
3354 .stateid = &arg.stateid,
3355 };
3356 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003359 nfs4_bitmap_copy_adjust_setattr(bitmask,
3360 nfs4_bitmask(server, olabel),
3361 inode);
3362
NeilBrown29b59f92016-10-13 15:26:47 +11003363 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003364 switch (err) {
3365 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003366 if (!(sattr->ia_valid & ATTR_SIZE)) {
3367 pr_warn_once("NFSv4: server %s is incorrectly "
3368 "applying open mode checks to "
3369 "a SETATTR that is not "
3370 "changing file size.\n",
3371 server->nfs_client->cl_hostname);
3372 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003373 if (state && !(state->state & FMODE_WRITE)) {
3374 err = -EBADF;
3375 if (sattr->ia_valid & ATTR_OPEN)
3376 err = -EACCES;
3377 goto out;
3378 }
3379 }
3380 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003382out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003383 return err;
3384}
3385
Peng Tao500d7012015-09-22 11:35:22 +08003386static bool
3387nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3388{
3389 if (inode == NULL || !nfs_have_layout(inode))
3390 return false;
3391
3392 return pnfs_wait_on_layoutreturn(inode, task);
3393}
3394
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003395/*
3396 * Update the seqid of an open stateid
3397 */
3398static void nfs4_sync_open_stateid(nfs4_stateid *dst,
3399 struct nfs4_state *state)
3400{
3401 __be32 seqid_open;
3402 u32 dst_seqid;
3403 int seq;
3404
3405 for (;;) {
3406 if (!nfs4_valid_open_stateid(state))
3407 break;
3408 seq = read_seqbegin(&state->seqlock);
3409 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3410 nfs4_stateid_copy(dst, &state->open_stateid);
3411 if (read_seqretry(&state->seqlock, seq))
3412 continue;
3413 break;
3414 }
3415 seqid_open = state->open_stateid.seqid;
3416 if (read_seqretry(&state->seqlock, seq))
3417 continue;
3418
3419 dst_seqid = be32_to_cpu(dst->seqid);
3420 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) < 0)
3421 dst->seqid = seqid_open;
3422 break;
3423 }
3424}
3425
3426/*
3427 * Update the seqid of an open stateid after receiving
3428 * NFS4ERR_OLD_STATEID
3429 */
3430static bool nfs4_refresh_open_old_stateid(nfs4_stateid *dst,
3431 struct nfs4_state *state)
3432{
3433 __be32 seqid_open;
3434 u32 dst_seqid;
3435 bool ret;
3436 int seq;
3437
3438 for (;;) {
3439 ret = false;
3440 if (!nfs4_valid_open_stateid(state))
3441 break;
3442 seq = read_seqbegin(&state->seqlock);
3443 if (!nfs4_state_match_open_stateid_other(state, dst)) {
3444 if (read_seqretry(&state->seqlock, seq))
3445 continue;
3446 break;
3447 }
3448 seqid_open = state->open_stateid.seqid;
3449 if (read_seqretry(&state->seqlock, seq))
3450 continue;
3451
3452 dst_seqid = be32_to_cpu(dst->seqid);
3453 if ((s32)(dst_seqid - be32_to_cpu(seqid_open)) >= 0)
3454 dst->seqid = cpu_to_be32(dst_seqid + 1);
3455 else
3456 dst->seqid = seqid_open;
3457 ret = true;
3458 break;
3459 }
3460
3461 return ret;
3462}
3463
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464struct nfs4_closedata {
3465 struct inode *inode;
3466 struct nfs4_state *state;
3467 struct nfs_closeargs arg;
3468 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003469 struct {
3470 struct nfs4_layoutreturn_args arg;
3471 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003472 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003473 u32 roc_barrier;
3474 bool roc;
3475 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003476 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003477 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478};
3479
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003480static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003481{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003482 struct nfs4_closedata *calldata = data;
3483 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003484 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003485
Trond Myklebustcf805162016-11-15 14:56:07 -05003486 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003487 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3488 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003489 nfs4_put_open_state(calldata->state);
3490 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003491 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003492 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003493 kfree(calldata);
3494}
3495
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003496static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003498 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003501 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003502 struct nfs4_exception exception = {
3503 .state = state,
3504 .inode = calldata->inode,
3505 .stateid = &calldata->arg.stateid,
3506 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507
Chuck Levera3ca5652012-03-01 17:00:40 -05003508 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003509 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3510 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003511 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003512
3513 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04003514 if (pnfs_roc_done(task, calldata->inode,
3515 &calldata->arg.lr_args,
3516 &calldata->res.lr_res,
3517 &calldata->res.lr_ret) == -EAGAIN)
3518 goto out_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003519
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003520 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 * the state_owner. we keep this around to process errors
3522 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003523 switch (task->tk_status) {
3524 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003525 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003526 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003527 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003528 case -NFS4ERR_ACCESS:
3529 if (calldata->arg.bitmask != NULL) {
3530 calldata->arg.bitmask = NULL;
3531 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003532 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003533
3534 }
3535 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003536 case -NFS4ERR_OLD_STATEID:
3537 /* Did we race with OPEN? */
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003538 if (nfs4_refresh_open_old_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003539 state))
3540 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003541 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003542 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003543 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003544 case -NFS4ERR_EXPIRED:
3545 nfs4_free_revoked_stateid(server,
3546 &calldata->arg.stateid,
3547 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003548 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003549 case -NFS4ERR_BAD_STATEID:
Trond Myklebuste217e822019-09-20 07:23:46 -04003550 if (calldata->arg.fmode == 0)
3551 break;
3552 /* Fallthrough */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003554 task->tk_status = nfs4_async_handle_exception(task,
3555 server, task->tk_status, &exception);
3556 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003557 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003559 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3560 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003561out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003562 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003563 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003564 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003565 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003566 return;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003567out_restart:
3568 task->tk_status = 0;
3569 rpc_restart_call_prepare(task);
3570 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003571}
3572
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003573static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003574{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003575 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003576 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003577 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003578 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003579 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003580 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003581
Chuck Levera3ca5652012-03-01 17:00:40 -05003582 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003583 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003584 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003585
Trond Myklebust88069f72009-12-08 08:33:16 -05003586 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003587 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003588 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3589 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3590 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003591 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003592 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003593 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003594 if (state->n_rdonly == 0)
3595 call_close |= is_rdonly;
3596 else if (is_rdonly)
3597 calldata->arg.fmode |= FMODE_READ;
3598 if (state->n_wronly == 0)
3599 call_close |= is_wronly;
3600 else if (is_wronly)
3601 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003602 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3603 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003604 } else if (is_rdwr)
3605 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3606
Trond Myklebust0e0cb352019-09-20 07:23:47 -04003607 nfs4_sync_open_stateid(&calldata->arg.stateid, state);
3608 if (!nfs4_valid_open_stateid(state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003609 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003610 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003611
3612 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003613 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003614 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003615 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003616
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003617 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003618 nfs_release_seqid(calldata->arg.seqid);
3619 goto out_wait;
3620 }
3621
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003622 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3623 if (lo && !pnfs_layout_is_valid(lo)) {
3624 calldata->arg.lr_args = NULL;
3625 calldata->res.lr_res = NULL;
3626 }
3627
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003628 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003629 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003630
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003631 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003632 /* Close-to-open cache consistency revalidation */
3633 if (!nfs4_have_delegation(inode, FMODE_READ))
3634 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3635 else
3636 calldata->arg.bitmask = NULL;
3637 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003638
Trond Myklebust6ae37332015-01-30 14:21:14 -05003639 calldata->arg.share_access =
3640 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3641 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003642
Trond Myklebustd8d84982016-12-19 12:14:44 -05003643 if (calldata->res.fattr == NULL)
3644 calldata->arg.bitmask = NULL;
3645 else if (calldata->arg.bitmask == NULL)
3646 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003647 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003648 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003649 &calldata->arg.seq_args,
3650 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003651 task) != 0)
3652 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003653 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003654 return;
3655out_no_action:
3656 task->tk_action = NULL;
3657out_wait:
3658 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003659}
3660
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003661static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003662 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003663 .rpc_call_done = nfs4_close_done,
3664 .rpc_release = nfs4_free_closedata,
3665};
3666
Linus Torvalds1da177e2005-04-16 15:20:36 -07003667/*
3668 * It is possible for data to be read/written from a mem-mapped file
3669 * after the sys_close call (which hits the vfs layer as a flush).
3670 * This means that we can't safely call nfsv4 close on a file until
3671 * the inode is cleared. This in turn means that we are not good
3672 * NFSv4 citizens - we do not indicate to the server to update the file's
3673 * share state even when we are done with one of the three share
3674 * stateid's in the inode.
3675 *
3676 * NOTE: Caller must be holding the sp->so_owner semaphore!
3677 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003678int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003680 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003681 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003682 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003683 struct nfs4_state_owner *sp = state->owner;
3684 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003685 struct rpc_message msg = {
3686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3687 .rpc_cred = state->owner->so_cred,
3688 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003689 struct rpc_task_setup task_setup_data = {
3690 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003691 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003692 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003693 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05003694 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003695 };
Trond Myklebust95121352005-10-18 14:20:12 -07003696 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003698 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3699 &task_setup_data.rpc_client, &msg);
3700
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003701 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003703 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003704 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003705 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003706 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003707 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003708 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3709 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003710 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003711 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3712 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003713 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003714 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003715 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003716 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003717 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003718 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003719 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003720 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003721 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003722 calldata->lr.roc = pnfs_roc(state->inode,
3723 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3724 if (calldata->lr.roc) {
3725 calldata->arg.lr_args = &calldata->lr.arg;
3726 calldata->res.lr_res = &calldata->lr.res;
3727 }
Al Viro643168c2011-06-22 18:20:23 -04003728 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003729
Trond Myklebust1174dd12010-12-21 10:52:24 -05003730 msg.rpc_argp = &calldata->arg;
3731 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003732 task_setup_data.callback_data = calldata;
3733 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003734 if (IS_ERR(task))
3735 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003736 status = 0;
3737 if (wait)
3738 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003739 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003740 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003741out_free_calldata:
3742 kfree(calldata);
3743out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003744 nfs4_put_open_state(state);
3745 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003746 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003747}
3748
Trond Myklebust2b484292010-09-17 10:56:51 -04003749static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003750nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3751 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003752{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003754 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3755
3756 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003757
Trond Myklebust565277f2007-10-15 18:17:53 -04003758 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003759 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003760
3761 nfs4_label_release_security(label);
3762
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003763 if (IS_ERR(state))
3764 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003765 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003766}
3767
Trond Myklebust1185a552009-12-03 15:54:02 -05003768static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003769{
3770 if (ctx->state == NULL)
3771 return;
3772 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003773 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003774 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003775 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003776}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003777
Trond Myklebustb944dba2013-11-04 15:20:20 -05003778#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3779#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Frank van der Lindenb78ef842020-06-23 22:38:55 +00003780#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_XATTR_SUPPORT - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003781
Linus Torvalds1da177e2005-04-16 15:20:36 -07003782static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3783{
Kinglong Mee8c612822015-08-26 21:12:58 +08003784 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003785 struct nfs4_server_caps_arg args = {
3786 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003787 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003788 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003789 struct nfs4_server_caps_res res = {};
3790 struct rpc_message msg = {
3791 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003792 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003793 .rpc_resp = &res,
3794 };
3795 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003796 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003797
Kinglong Mee8c612822015-08-26 21:12:58 +08003798 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3799 FATTR4_WORD0_FH_EXPIRE_TYPE |
3800 FATTR4_WORD0_LINK_SUPPORT |
3801 FATTR4_WORD0_SYMLINK_SUPPORT |
3802 FATTR4_WORD0_ACLSUPPORT;
3803 if (minorversion)
3804 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3805
Bryan Schumaker7c513052011-03-24 17:12:24 +00003806 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003807 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003808 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003809 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003810 case 0:
3811 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3812 res.attr_bitmask[2] = 0;
3813 break;
3814 case 1:
3815 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3816 break;
3817 case 2:
3818 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3819 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003820 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003821 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3822 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3823 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3824 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003825 NFS_CAP_CTIME|NFS_CAP_MTIME|
3826 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003827 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3828 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003829 server->caps |= NFS_CAP_ACLS;
3830 if (res.has_links != 0)
3831 server->caps |= NFS_CAP_HARDLINKS;
3832 if (res.has_symlinks != 0)
3833 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003834 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3835 server->caps |= NFS_CAP_FILEID;
3836 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3837 server->caps |= NFS_CAP_MODE;
3838 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3839 server->caps |= NFS_CAP_NLINK;
3840 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3841 server->caps |= NFS_CAP_OWNER;
3842 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3843 server->caps |= NFS_CAP_OWNER_GROUP;
3844 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3845 server->caps |= NFS_CAP_ATIME;
3846 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3847 server->caps |= NFS_CAP_CTIME;
3848 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3849 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003850#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3851 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3852 server->caps |= NFS_CAP_SECURITY_LABEL;
3853#endif
3854 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3855 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003856 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003857
Trond Myklebusta65318b2009-03-11 14:10:28 -04003858 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3859 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3860 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003861 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003862
3863 /* Avoid a regression due to buggy server */
3864 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3865 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003866 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3867 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003868
Linus Torvalds1da177e2005-04-16 15:20:36 -07003869 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003870 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003872
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873 return status;
3874}
3875
Trond Myklebust55a97592006-06-09 09:34:19 -04003876int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003877{
Trond Myklebust0688e642019-04-07 13:59:09 -04003878 struct nfs4_exception exception = {
3879 .interruptible = true,
3880 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003881 int err;
3882 do {
3883 err = nfs4_handle_exception(server,
3884 _nfs4_server_capabilities(server, fhandle),
3885 &exception);
3886 } while (exception.retry);
3887 return err;
3888}
3889
3890static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3891 struct nfs_fsinfo *info)
3892{
David Quigleyaa9c2662013-05-22 12:50:44 -04003893 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003894 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003895 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003896 };
3897 struct nfs4_lookup_res res = {
3898 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003899 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003900 .fh = fhandle,
3901 };
3902 struct rpc_message msg = {
3903 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3904 .rpc_argp = &args,
3905 .rpc_resp = &res,
3906 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003907
David Quigleyaa9c2662013-05-22 12:50:44 -04003908 bitmask[0] = nfs4_fattr_bitmap[0];
3909 bitmask[1] = nfs4_fattr_bitmap[1];
3910 /*
3911 * Process the label in the upcoming getfattr
3912 */
3913 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3914
Trond Myklebust0e574af2005-10-27 22:12:38 -04003915 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003916 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003917}
3918
3919static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3920 struct nfs_fsinfo *info)
3921{
Trond Myklebust0688e642019-04-07 13:59:09 -04003922 struct nfs4_exception exception = {
3923 .interruptible = true,
3924 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003925 int err;
3926 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003927 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003928 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003929 switch (err) {
3930 case 0:
3931 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003932 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003933 default:
3934 err = nfs4_handle_exception(server, err, &exception);
3935 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003937out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 return err;
3939}
3940
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003941static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3942 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3943{
Trond Myklebustc2190662013-08-26 19:23:04 -04003944 struct rpc_auth_create_args auth_args = {
3945 .pseudoflavor = flavor,
3946 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003947 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003948
Trond Myklebustc2190662013-08-26 19:23:04 -04003949 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003950 if (IS_ERR(auth))
3951 return -EACCES;
3952 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003953}
3954
Chuck Lever9a744ba2013-03-16 15:56:02 -04003955/*
3956 * Retry pseudoroot lookup with various security flavors. We do this when:
3957 *
3958 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3959 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3960 *
3961 * Returns zero on success, or a negative NFS4ERR value, or a
3962 * negative errno value.
3963 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003964static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003965 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003966{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003967 /* Per 3530bis 15.33.5 */
3968 static const rpc_authflavor_t flav_array[] = {
3969 RPC_AUTH_GSS_KRB5P,
3970 RPC_AUTH_GSS_KRB5I,
3971 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003972 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003973 RPC_AUTH_NULL,
3974 };
3975 int status = -EPERM;
3976 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003977
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003978 if (server->auth_info.flavor_len > 0) {
3979 /* try each flavor specified by user */
3980 for (i = 0; i < server->auth_info.flavor_len; i++) {
3981 status = nfs4_lookup_root_sec(server, fhandle, info,
3982 server->auth_info.flavors[i]);
3983 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3984 continue;
3985 break;
3986 }
3987 } else {
3988 /* no flavors specified by user, try default list */
3989 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3990 status = nfs4_lookup_root_sec(server, fhandle, info,
3991 flav_array[i]);
3992 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3993 continue;
3994 break;
3995 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003996 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003997
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003998 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003999 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04004000 * to access the mount. It could also mean that we tried to mount
4001 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
4002 * existing mount programs don't handle -EACCES very well so it should
4003 * be mapped to -EPERM instead.
4004 */
4005 if (status == -EACCES)
4006 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004007 return status;
4008}
4009
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004010/**
4011 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
4012 * @server: initialized nfs_server handle
4013 * @fhandle: we fill in the pseudo-fs root file handle
4014 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004015 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004016 *
4017 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004018 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04004019int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004020 struct nfs_fsinfo *info,
4021 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04004022{
Andre Przywarac7757072015-04-23 17:17:40 +01004023 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004024
Andre Przywarac7757072015-04-23 17:17:40 +01004025 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04004026 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01004027
4028 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04004029 status = server->nfs_client->cl_mvops->find_root_sec(server,
4030 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004031
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 if (status == 0)
4033 status = nfs4_server_capabilities(server, fhandle);
4034 if (status == 0)
4035 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04004036
Trond Myklebustc12e87f2006-03-13 21:20:47 -08004037 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004038}
4039
Bryan Schumakerbae36242012-05-10 15:07:31 -04004040static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
4041 struct nfs_fsinfo *info)
4042{
4043 int error;
4044 struct nfs_fattr *fattr = info->fattr;
Scott Mayhew779df6a2020-03-03 17:58:37 -05004045 struct nfs4_label *label = fattr->label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004046
4047 error = nfs4_server_capabilities(server, mntfh);
4048 if (error < 0) {
4049 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
4050 return error;
4051 }
4052
Trond Myklebusta841b542018-04-07 13:50:59 -04004053 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04004054 if (error < 0) {
4055 dprintk("nfs4_get_root: getattr error = %d\n", -error);
Scott Mayhew779df6a2020-03-03 17:58:37 -05004056 goto out;
Bryan Schumakerbae36242012-05-10 15:07:31 -04004057 }
4058
4059 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
4060 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
4061 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
4062
Scott Mayhew779df6a2020-03-03 17:58:37 -05004063out:
Bryan Schumakerbae36242012-05-10 15:07:31 -04004064 return error;
4065}
4066
Manoj Naik6b97fd32006-06-09 09:34:29 -04004067/*
4068 * Get locations and (maybe) other attributes of a referral.
4069 * Note that we'll actually follow the referral later when
4070 * we detect fsid mismatch in inode revalidation
4071 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004072static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
4073 const struct qstr *name, struct nfs_fattr *fattr,
4074 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04004075{
4076 int status = -ENOMEM;
4077 struct page *page = NULL;
4078 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004079
4080 page = alloc_page(GFP_KERNEL);
4081 if (page == NULL)
4082 goto out;
4083 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
4084 if (locations == NULL)
4085 goto out;
4086
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004087 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004088 if (status != 0)
4089 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04004090
4091 /*
4092 * If the fsid didn't change, this is a migration event, not a
4093 * referral. Cause us to drop into the exception handler, which
4094 * will kick off migration recovery.
4095 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004096 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04004097 dprintk("%s: server did not return a different fsid for"
4098 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04004099 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04004100 goto out;
4101 }
Andy Adamson533eb462011-06-13 18:25:56 -04004102 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
4103 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004104
Andy Adamson533eb462011-06-13 18:25:56 -04004105 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04004106 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04004107 memset(fhandle, 0, sizeof(struct nfs_fh));
4108out:
4109 if (page)
4110 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04004111 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04004112 return status;
4113}
4114
David Quigley1775fd32013-05-22 12:50:42 -04004115static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004116 struct nfs_fattr *fattr, struct nfs4_label *label,
4117 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004118{
Trond Myklebust771734f2018-04-07 13:54:23 -04004119 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004120 struct nfs4_getattr_arg args = {
4121 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004122 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004123 };
4124 struct nfs4_getattr_res res = {
4125 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004126 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004127 .server = server,
4128 };
4129 struct rpc_message msg = {
4130 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4131 .rpc_argp = &args,
4132 .rpc_resp = &res,
4133 };
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004134 unsigned short task_flags = 0;
4135
4136 /* Is this is an attribute revalidation, subject to softreval? */
4137 if (inode && (server->flags & NFS_MOUNT_SOFTREVAL))
4138 task_flags |= RPC_TASK_TIMEOUT;
David Quigleyaa9c2662013-05-22 12:50:44 -04004139
Trond Myklebust771734f2018-04-07 13:54:23 -04004140 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004141
Trond Myklebust0e574af2005-10-27 22:12:38 -04004142 nfs_fattr_init(fattr);
Trond Myklebustc74dfe92020-01-06 15:39:37 -05004143 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4144 return nfs4_do_call_sync(server->client, server, &msg,
4145 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146}
4147
Olga Kornievskaiaec4b0922019-10-08 16:33:53 -04004148int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004149 struct nfs_fattr *fattr, struct nfs4_label *label,
4150 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151{
Trond Myklebust0688e642019-04-07 13:59:09 -04004152 struct nfs4_exception exception = {
4153 .interruptible = true,
4154 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 int err;
4156 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004157 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004158 trace_nfs4_getattr(server, fhandle, fattr, err);
4159 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004160 &exception);
4161 } while (exception.retry);
4162 return err;
4163}
4164
4165/*
4166 * The file is not closed if it is opened due to the a request to change
4167 * the size of the file. The open call will not be needed once the
4168 * VFS layer lookup-intents are implemented.
4169 *
4170 * Close is called when the inode is destroyed.
4171 * If we haven't opened the file for O_WRONLY, we
4172 * need to in the size_change case to obtain a stateid.
4173 *
4174 * Got race?
4175 * Because OPEN is always done by name in nfsv4, it is
4176 * possible that we opened a different file by the same
4177 * name. We can recognize this race condition, but we
4178 * can't do anything about it besides returning an error.
4179 *
4180 * This will be fixed with VFS changes (lookup-intent).
4181 */
4182static int
4183nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4184 struct iattr *sattr)
4185{
David Howells2b0143b2015-03-17 22:25:59 +00004186 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004187 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004188 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004189 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 int status;
4191
Peng Tao88ac8152014-09-12 11:04:10 +08004192 if (pnfs_ld_layoutret_on_setattr(inode) &&
4193 sattr->ia_valid & ATTR_SIZE &&
4194 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004195 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004196
Trond Myklebust0e574af2005-10-27 22:12:38 -04004197 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198
Andy Adamson26699402012-05-30 16:12:24 -04004199 /* Deal with open(O_TRUNC) */
4200 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004201 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004202
4203 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004204 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004205 return 0;
4206
Trond Myklebustd5308382005-11-04 15:33:38 -05004207 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004208 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004209
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004210 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004211 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004212 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004213 }
4214
David Quigley14c43f72013-05-22 12:50:43 -04004215 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4216 if (IS_ERR(label))
4217 return PTR_ERR(label);
4218
Trond Myklebust199366f2018-03-20 16:43:18 -04004219 /* Return any delegations if we're going to change ACLs */
4220 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004221 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004222
NeilBrown29b59f92016-10-13 15:26:47 +11004223 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004224 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004225 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004226 nfs_setsecurity(inode, fattr, label);
4227 }
David Quigley14c43f72013-05-22 12:50:43 -04004228 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 return status;
4230}
4231
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004232static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004233 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004234 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004235{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004236 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004237 int status;
4238 struct nfs4_lookup_arg args = {
4239 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004240 .dir_fh = NFS_FH(dir),
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004241 .name = &dentry->d_name,
David Howells2b3de442006-08-22 20:06:09 -04004242 };
4243 struct nfs4_lookup_res res = {
4244 .server = server,
4245 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004246 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004247 .fh = fhandle,
4248 };
4249 struct rpc_message msg = {
4250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4251 .rpc_argp = &args,
4252 .rpc_resp = &res,
4253 };
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004254 unsigned short task_flags = 0;
4255
4256 /* Is this is an attribute revalidation, subject to softreval? */
4257 if (nfs_lookup_is_soft_revalidate(dentry))
4258 task_flags |= RPC_TASK_TIMEOUT;
David Howells2b3de442006-08-22 20:06:09 -04004259
David Quigleyaa9c2662013-05-22 12:50:44 -04004260 args.bitmask = nfs4_bitmask(server, label);
4261
David Howells2b3de442006-08-22 20:06:09 -04004262 nfs_fattr_init(fattr);
4263
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004264 dprintk("NFS call lookup %pd2\n", dentry);
4265 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
4266 status = nfs4_do_call_sync(clnt, server, &msg,
4267 &args.seq_args, &res.seq_res, task_flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 dprintk("NFS reply lookup: %d\n", status);
4269 return status;
4270}
4271
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004272static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004273{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004274 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004275 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004276 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4277 fattr->nlink = 2;
4278}
4279
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004280static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004281 struct dentry *dentry, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004282 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004283{
Trond Myklebust0688e642019-04-07 13:59:09 -04004284 struct nfs4_exception exception = {
4285 .interruptible = true,
4286 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004287 struct rpc_clnt *client = *clnt;
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004288 const struct qstr *name = &dentry->d_name;
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004289 int err;
4290 do {
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004291 err = _nfs4_proc_lookup(client, dir, dentry, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004292 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004293 switch (err) {
4294 case -NFS4ERR_BADNAME:
4295 err = -ENOENT;
4296 goto out;
4297 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004298 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004299 if (err == -NFS4ERR_MOVED)
4300 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004301 goto out;
4302 case -NFS4ERR_WRONGSEC:
4303 err = -EPERM;
4304 if (client != *clnt)
4305 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004306 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004307 if (IS_ERR(client))
4308 return PTR_ERR(client);
4309
4310 exception.retry = 1;
4311 break;
4312 default:
4313 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4314 }
4315 } while (exception.retry);
4316
4317out:
4318 if (err == 0)
4319 *clnt = client;
4320 else if (client != *clnt)
4321 rpc_shutdown_client(client);
4322
4323 return err;
4324}
4325
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004326static int nfs4_proc_lookup(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004327 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4328 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004330 int status;
4331 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004332
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004333 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004334 if (client != NFS_CLIENT(dir)) {
4335 rpc_shutdown_client(client);
4336 nfs_fixup_secinfo_attributes(fattr);
4337 }
4338 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339}
4340
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004341struct rpc_clnt *
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004342nfs4_proc_lookup_mountpoint(struct inode *dir, struct dentry *dentry,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004343 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4344{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004345 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004346 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004347
Trond Myklebustf7b37b82020-01-14 12:06:34 -05004348 status = nfs4_proc_lookup_common(&client, dir, dentry, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004349 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004350 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004351 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004352}
4353
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004354static int _nfs4_proc_lookupp(struct inode *inode,
4355 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4356 struct nfs4_label *label)
4357{
4358 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4359 struct nfs_server *server = NFS_SERVER(inode);
4360 int status;
4361 struct nfs4_lookupp_arg args = {
4362 .bitmask = server->attr_bitmask,
4363 .fh = NFS_FH(inode),
4364 };
4365 struct nfs4_lookupp_res res = {
4366 .server = server,
4367 .fattr = fattr,
4368 .label = label,
4369 .fh = fhandle,
4370 };
4371 struct rpc_message msg = {
4372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4373 .rpc_argp = &args,
4374 .rpc_resp = &res,
4375 };
4376
4377 args.bitmask = nfs4_bitmask(server, label);
4378
4379 nfs_fattr_init(fattr);
4380
4381 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4382 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4383 &res.seq_res, 0);
4384 dprintk("NFS reply lookupp: %d\n", status);
4385 return status;
4386}
4387
4388static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4389 struct nfs_fattr *fattr, struct nfs4_label *label)
4390{
Trond Myklebust0688e642019-04-07 13:59:09 -04004391 struct nfs4_exception exception = {
4392 .interruptible = true,
4393 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004394 int err;
4395 do {
4396 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4397 trace_nfs4_lookupp(inode, err);
4398 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4399 &exception);
4400 } while (exception.retry);
4401 return err;
4402}
4403
Linus Torvalds1da177e2005-04-16 15:20:36 -07004404static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4405{
Trond Myklebust76b32992007-08-10 17:45:11 -04004406 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004407 struct nfs4_accessargs args = {
4408 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004409 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004410 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004411 struct nfs4_accessres res = {
4412 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004413 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414 struct rpc_message msg = {
4415 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4416 .rpc_argp = &args,
4417 .rpc_resp = &res,
4418 .rpc_cred = entry->cred,
4419 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004420 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421
Trond Myklebust7c672652018-06-04 15:00:53 -04004422 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004423 res.fattr = nfs_alloc_fattr();
4424 if (res.fattr == NULL)
4425 return -ENOMEM;
4426 args.bitmask = server->cache_consistency_bitmask;
4427 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004428 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004429 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004430 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004431 if (res.fattr)
4432 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004433 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004434 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return status;
4436}
4437
4438static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4439{
Trond Myklebust0688e642019-04-07 13:59:09 -04004440 struct nfs4_exception exception = {
4441 .interruptible = true,
4442 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 int err;
4444 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004445 err = _nfs4_proc_access(inode, entry);
4446 trace_nfs4_access(inode, err);
4447 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448 &exception);
4449 } while (exception.retry);
4450 return err;
4451}
4452
4453/*
4454 * TODO: For the time being, we don't try to get any attributes
4455 * along with any of the zero-copy operations READ, READDIR,
4456 * READLINK, WRITE.
4457 *
4458 * In the case of the first three, we want to put the GETATTR
4459 * after the read-type operation -- this is because it is hard
4460 * to predict the length of a GETATTR response in v4, and thus
4461 * align the READ data correctly. This means that the GETATTR
4462 * may end up partially falling into the page cache, and we should
4463 * shift it into the 'tail' of the xdr_buf before processing.
4464 * To do this efficiently, we need to know the total length
4465 * of data received, which doesn't seem to be available outside
4466 * of the RPC layer.
4467 *
4468 * In the case of WRITE, we also want to put the GETATTR after
4469 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004470 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 *
4472 * Both of these changes to the XDR layer would in fact be quite
4473 * minor, but I decided to leave them for a subsequent patch.
4474 */
4475static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4476 unsigned int pgbase, unsigned int pglen)
4477{
4478 struct nfs4_readlink args = {
4479 .fh = NFS_FH(inode),
4480 .pgbase = pgbase,
4481 .pglen = pglen,
4482 .pages = &page,
4483 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004484 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 struct rpc_message msg = {
4486 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4487 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004488 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 };
4490
Bryan Schumaker7c513052011-03-24 17:12:24 +00004491 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 -07004492}
4493
4494static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4495 unsigned int pgbase, unsigned int pglen)
4496{
Trond Myklebust0688e642019-04-07 13:59:09 -04004497 struct nfs4_exception exception = {
4498 .interruptible = true,
4499 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 int err;
4501 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004502 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4503 trace_nfs4_readlink(inode, err);
4504 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004505 &exception);
4506 } while (exception.retry);
4507 return err;
4508}
4509
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004511 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513static int
4514nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004515 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004517 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004518 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004519 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004521 int status = 0;
4522
NeilBrown532d4de2016-10-13 15:26:47 +11004523 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004524 if (IS_ERR(ctx))
4525 return PTR_ERR(ctx);
4526
David Quigleyaa9c2662013-05-22 12:50:44 -04004527 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4528
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004529 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4530 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004531 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 if (IS_ERR(state)) {
4533 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004534 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004537 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004538 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 return status;
4540}
4541
Trond Myklebust3c591172018-07-31 15:54:10 -04004542static int
4543_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544{
Trond Myklebust16e42952005-10-27 22:12:44 -04004545 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004546 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004548 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004550 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004551 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004552 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004554 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4555 .rpc_argp = &args,
4556 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004558 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004559 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004560
Bryan Schumaker7c513052011-03-24 17:12:24 +00004561 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004562 if (status == 0) {
4563 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004564 nfs4_update_changeattr_locked(dir, &res.cinfo, timestamp,
4565 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004566 /* Removing a directory decrements nlink in the parent */
4567 if (ftype == NF4DIR && dir->i_nlink > 2)
4568 nfs4_dec_nlink_locked(dir);
4569 spin_unlock(&dir->i_lock);
4570 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004571 return status;
4572}
4573
Trond Myklebust912678d2018-03-20 16:43:15 -04004574static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4575{
Trond Myklebust0688e642019-04-07 13:59:09 -04004576 struct nfs4_exception exception = {
4577 .interruptible = true,
4578 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004579 struct inode *inode = d_inode(dentry);
4580 int err;
4581
Trond Myklebustc01d3642018-03-20 16:43:20 -04004582 if (inode) {
4583 if (inode->i_nlink == 1)
4584 nfs4_inode_return_delegation(inode);
4585 else
4586 nfs4_inode_make_writeable(inode);
4587 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004588 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004589 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004590 trace_nfs4_remove(dir, &dentry->d_name, err);
4591 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4592 &exception);
4593 } while (exception.retry);
4594 return err;
4595}
4596
4597static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
Trond Myklebust0688e642019-04-07 13:59:09 -04004599 struct nfs4_exception exception = {
4600 .interruptible = true,
4601 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004602 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004603
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004605 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004606 trace_nfs4_remove(dir, name, err);
4607 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 &exception);
4609 } while (exception.retry);
4610 return err;
4611}
4612
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004613static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4614 struct dentry *dentry,
4615 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004617 struct nfs_removeargs *args = msg->rpc_argp;
4618 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619
Trond Myklebust977fcc22018-03-20 16:43:17 -04004620 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004622 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004623
4624 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004625
4626 if (inode)
4627 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628}
4629
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004630static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4631{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004632 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004633 &data->args.seq_args,
4634 &data->res.seq_res,
4635 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636}
4637
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004638static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004640 struct nfs_unlinkdata *data = task->tk_calldata;
4641 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004642
Trond Myklebust14516c32010-07-31 14:29:06 -04004643 if (!nfs4_sequence_done(task, &res->seq_res))
4644 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004645 if (nfs4_async_handle_error(task, res->server, NULL,
4646 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004647 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004648 if (task->tk_status == 0)
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004649 nfs4_update_changeattr(dir, &res->cinfo,
4650 res->dir_attr->time_start,
4651 NFS_INO_INVALID_DATA);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004652 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653}
4654
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004655static void nfs4_proc_rename_setup(struct rpc_message *msg,
4656 struct dentry *old_dentry,
4657 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004658{
Jeff Laytond3d41522010-09-17 17:31:57 -04004659 struct nfs_renameargs *arg = msg->rpc_argp;
4660 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004661 struct inode *old_inode = d_inode(old_dentry);
4662 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004663
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004664 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004665 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004666 if (new_inode)
4667 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004668 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004669 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004670 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004671}
4672
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004673static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4674{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004675 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004676 &data->args.seq_args,
4677 &data->res.seq_res,
4678 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004679}
4680
4681static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4682 struct inode *new_dir)
4683{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004684 struct nfs_renamedata *data = task->tk_calldata;
4685 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004686
4687 if (!nfs4_sequence_done(task, &res->seq_res))
4688 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004689 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004690 return 0;
4691
Trond Myklebustc733c492017-01-11 12:32:26 -05004692 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004693 if (new_dir != old_dir) {
4694 /* Note: If we moved a directory, nlink will change */
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004695 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004696 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004697 NFS_INO_INVALID_OTHER |
4698 NFS_INO_INVALID_DATA);
4699 nfs4_update_changeattr(new_dir, &res->new_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004700 res->new_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004701 NFS_INO_INVALID_OTHER |
4702 NFS_INO_INVALID_DATA);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004703 } else
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004704 nfs4_update_changeattr(old_dir, &res->old_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004705 res->old_fattr->time_start,
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004706 NFS_INO_INVALID_DATA);
Trond Myklebustc733c492017-01-11 12:32:26 -05004707 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004708 return 1;
4709}
4710
Al Virobeffb8f2016-07-20 16:34:42 -04004711static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004713 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004714 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004715 struct nfs4_link_arg arg = {
4716 .fh = NFS_FH(inode),
4717 .dir_fh = NFS_FH(dir),
4718 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004719 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004721 struct nfs4_link_res res = {
4722 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004723 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004724 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 struct rpc_message msg = {
4726 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4727 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004728 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004730 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731
Trond Myklebust136f2622010-04-16 16:22:49 -04004732 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004733 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004734 goto out;
4735
David Quigley14c43f72013-05-22 12:50:43 -04004736 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4737 if (IS_ERR(res.label)) {
4738 status = PTR_ERR(res.label);
4739 goto out;
4740 }
4741
Trond Myklebustc01d3642018-03-20 16:43:20 -04004742 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004743 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004744
Bryan Schumaker7c513052011-03-24 17:12:24 +00004745 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004746 if (!status) {
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004747 nfs4_update_changeattr(dir, &res.cinfo, res.fattr->time_start,
4748 NFS_INO_INVALID_DATA);
David Quigleyaa9c2662013-05-22 12:50:44 -04004749 status = nfs_post_op_update_inode(inode, res.fattr);
4750 if (!status)
4751 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004752 }
David Quigley14c43f72013-05-22 12:50:43 -04004753
4754
4755 nfs4_label_free(res.label);
4756
Trond Myklebust136f2622010-04-16 16:22:49 -04004757out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004758 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759 return status;
4760}
4761
Al Virobeffb8f2016-07-20 16:34:42 -04004762static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763{
Trond Myklebust0688e642019-04-07 13:59:09 -04004764 struct nfs4_exception exception = {
4765 .interruptible = true,
4766 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 int err;
4768 do {
4769 err = nfs4_handle_exception(NFS_SERVER(inode),
4770 _nfs4_proc_link(inode, dir, name),
4771 &exception);
4772 } while (exception.retry);
4773 return err;
4774}
4775
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004776struct nfs4_createdata {
4777 struct rpc_message msg;
4778 struct nfs4_create_arg arg;
4779 struct nfs4_create_res res;
4780 struct nfs_fh fh;
4781 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004782 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004783};
4784
4785static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004786 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004787{
4788 struct nfs4_createdata *data;
4789
4790 data = kzalloc(sizeof(*data), GFP_KERNEL);
4791 if (data != NULL) {
4792 struct nfs_server *server = NFS_SERVER(dir);
4793
David Quigley14c43f72013-05-22 12:50:43 -04004794 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4795 if (IS_ERR(data->label))
4796 goto out_free;
4797
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004798 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4799 data->msg.rpc_argp = &data->arg;
4800 data->msg.rpc_resp = &data->res;
4801 data->arg.dir_fh = NFS_FH(dir);
4802 data->arg.server = server;
4803 data->arg.name = name;
4804 data->arg.attrs = sattr;
4805 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004806 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004807 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004808 data->res.server = server;
4809 data->res.fh = &data->fh;
4810 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004811 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004812 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004813 }
4814 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004815out_free:
4816 kfree(data);
4817 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004818}
4819
4820static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4821{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004822 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004823 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004824 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004825 spin_lock(&dir->i_lock);
Frank van der Linden1b523ca2020-06-23 22:38:59 +00004826 nfs4_update_changeattr_locked(dir, &data->res.dir_cinfo,
4827 data->res.fattr->time_start,
4828 NFS_INO_INVALID_DATA);
Trond Myklebust3c591172018-07-31 15:54:10 -04004829 /* Creating a directory bumps nlink in the parent */
4830 if (data->arg.ftype == NF4DIR)
4831 nfs4_inc_nlink_locked(dir);
4832 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004833 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004834 }
4835 return status;
4836}
4837
4838static void nfs4_free_createdata(struct nfs4_createdata *data)
4839{
David Quigley14c43f72013-05-22 12:50:43 -04004840 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004841 kfree(data);
4842}
4843
Chuck Lever4f390c12006-08-22 20:06:22 -04004844static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004845 struct page *page, unsigned int len, struct iattr *sattr,
4846 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004848 struct nfs4_createdata *data;
4849 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850
Chuck Lever94a6d752006-08-22 20:06:23 -04004851 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004852 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004853
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004854 status = -ENOMEM;
4855 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4856 if (data == NULL)
4857 goto out;
4858
4859 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4860 data->arg.u.symlink.pages = &page;
4861 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004862 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004864 status = nfs4_do_create(dir, dentry, data);
4865
4866 nfs4_free_createdata(data);
4867out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004868 return status;
4869}
4870
Chuck Lever4f390c12006-08-22 20:06:22 -04004871static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004872 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873{
Trond Myklebust0688e642019-04-07 13:59:09 -04004874 struct nfs4_exception exception = {
4875 .interruptible = true,
4876 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004877 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004878 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004879
4880 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4881
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004883 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4884 trace_nfs4_symlink(dir, &dentry->d_name, err);
4885 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886 &exception);
4887 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004888
4889 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890 return err;
4891}
4892
4893static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004894 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004895{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004896 struct nfs4_createdata *data;
4897 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004899 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4900 if (data == NULL)
4901 goto out;
4902
David Quigley1775fd32013-05-22 12:50:42 -04004903 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004904 status = nfs4_do_create(dir, dentry, data);
4905
4906 nfs4_free_createdata(data);
4907out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908 return status;
4909}
4910
4911static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4912 struct iattr *sattr)
4913{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004914 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004915 struct nfs4_exception exception = {
4916 .interruptible = true,
4917 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004918 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004920
David Quigleyaa9c2662013-05-22 12:50:44 -04004921 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4922
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004923 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4924 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004925 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004926 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4927 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4928 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 &exception);
4930 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004931 nfs4_label_release_security(label);
4932
Linus Torvalds1da177e2005-04-16 15:20:36 -07004933 return err;
4934}
4935
NeilBrown684f39b2018-12-03 11:30:30 +11004936static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004937 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938{
David Howells2b0143b2015-03-17 22:25:59 +00004939 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004940 struct nfs4_readdir_arg args = {
4941 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004942 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943 .pgbase = 0,
4944 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004945 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004946 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 };
4948 struct nfs4_readdir_res res;
4949 struct rpc_message msg = {
4950 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4951 .rpc_argp = &args,
4952 .rpc_resp = &res,
4953 .rpc_cred = cred,
4954 };
4955 int status;
4956
Al Viro6de14722013-09-16 10:53:17 -04004957 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4958 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004959 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004960 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004962 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 -05004963 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004964 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004965 status += args.pgbase;
4966 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004967
4968 nfs_invalidate_atime(dir);
4969
Harvey Harrison3110ff82008-05-02 13:42:44 -07004970 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004971 return status;
4972}
4973
NeilBrown684f39b2018-12-03 11:30:30 +11004974static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004975 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
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 {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004982 err = _nfs4_proc_readdir(dentry, cred, cookie,
4983 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004984 trace_nfs4_readdir(d_inode(dentry), err);
4985 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986 &exception);
4987 } while (exception.retry);
4988 return err;
4989}
4990
4991static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004992 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004994 struct nfs4_createdata *data;
4995 int mode = sattr->ia_mode;
4996 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004998 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4999 if (data == NULL)
5000 goto out;
5001
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005003 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005005 data->arg.ftype = NF4BLK;
5006 data->arg.u.device.specdata1 = MAJOR(rdev);
5007 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 }
5009 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005010 data->arg.ftype = NF4CHR;
5011 data->arg.u.device.specdata1 = MAJOR(rdev);
5012 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005013 } else if (!S_ISSOCK(mode)) {
5014 status = -EINVAL;
5015 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005016 }
David Quigley1775fd32013-05-22 12:50:42 -04005017
David Quigleyaa9c2662013-05-22 12:50:44 -04005018 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005019 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04005020out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04005021 nfs4_free_createdata(data);
5022out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005023 return status;
5024}
5025
5026static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
5027 struct iattr *sattr, dev_t rdev)
5028{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005029 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04005030 struct nfs4_exception exception = {
5031 .interruptible = true,
5032 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005033 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00005035
David Quigleyaa9c2662013-05-22 12:50:44 -04005036 label = nfs4_label_init_security(dir, dentry, sattr, &l);
5037
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05005038 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
5039 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04005041 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
5042 trace_nfs4_mknod(dir, &dentry->d_name, err);
5043 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044 &exception);
5045 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04005046
5047 nfs4_label_release_security(label);
5048
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049 return err;
5050}
5051
5052static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
5053 struct nfs_fsstat *fsstat)
5054{
5055 struct nfs4_statfs_arg args = {
5056 .fh = fhandle,
5057 .bitmask = server->attr_bitmask,
5058 };
Benny Halevy24ad1482009-04-01 09:21:56 -04005059 struct nfs4_statfs_res res = {
5060 .fsstat = fsstat,
5061 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 struct rpc_message msg = {
5063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
5064 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04005065 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005066 };
5067
Trond Myklebust0e574af2005-10-27 22:12:38 -04005068 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005069 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005070}
5071
5072static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
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 do {
5079 err = nfs4_handle_exception(server,
5080 _nfs4_proc_statfs(server, fhandle, fsstat),
5081 &exception);
5082 } while (exception.retry);
5083 return err;
5084}
5085
5086static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
5087 struct nfs_fsinfo *fsinfo)
5088{
5089 struct nfs4_fsinfo_arg args = {
5090 .fh = fhandle,
5091 .bitmask = server->attr_bitmask,
5092 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04005093 struct nfs4_fsinfo_res res = {
5094 .fsinfo = fsinfo,
5095 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 struct rpc_message msg = {
5097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
5098 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04005099 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005100 };
5101
Bryan Schumaker7c513052011-03-24 17:12:24 +00005102 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
5105static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5106{
Trond Myklebust0688e642019-04-07 13:59:09 -04005107 struct nfs4_exception exception = {
5108 .interruptible = true,
5109 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110 int err;
5111
5112 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04005113 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04005114 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005115 if (err == 0) {
Robert Milkowski7dc29932020-01-30 09:43:25 +00005116 nfs4_set_lease_period(server->nfs_client, fsinfo->lease_time * HZ);
Chuck Lever83ca7f52013-03-16 15:55:53 -04005117 break;
5118 }
5119 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 } while (exception.retry);
5121 return err;
5122}
5123
5124static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
5125{
Bryan Schumakere38eb652012-06-20 15:53:40 -04005126 int error;
5127
Trond Myklebust0e574af2005-10-27 22:12:38 -04005128 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04005129 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005130 if (error == 0) {
5131 /* block layout checks this! */
5132 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04005133 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08005134 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04005135
5136 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005137}
5138
5139static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5140 struct nfs_pathconf *pathconf)
5141{
5142 struct nfs4_pathconf_arg args = {
5143 .fh = fhandle,
5144 .bitmask = server->attr_bitmask,
5145 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005146 struct nfs4_pathconf_res res = {
5147 .pathconf = pathconf,
5148 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005149 struct rpc_message msg = {
5150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5151 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005152 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005153 };
5154
5155 /* None of the pathconf attributes are mandatory to implement */
5156 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5157 memset(pathconf, 0, sizeof(*pathconf));
5158 return 0;
5159 }
5160
Trond Myklebust0e574af2005-10-27 22:12:38 -04005161 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005162 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005163}
5164
5165static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5166 struct nfs_pathconf *pathconf)
5167{
Trond Myklebust0688e642019-04-07 13:59:09 -04005168 struct nfs4_exception exception = {
5169 .interruptible = true,
5170 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005171 int err;
5172
5173 do {
5174 err = nfs4_handle_exception(server,
5175 _nfs4_proc_pathconf(server, fhandle, pathconf),
5176 &exception);
5177 } while (exception.retry);
5178 return err;
5179}
5180
Trond Myklebust5521abf2013-03-16 20:54:34 -04005181int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005182 const struct nfs_open_context *ctx,
5183 const struct nfs_lock_context *l_ctx,
5184 fmode_t fmode)
5185{
NeilBrown17393472016-10-13 15:26:47 +11005186 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005187}
5188EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5189
Trond Myklebust5521abf2013-03-16 20:54:34 -04005190static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5191 const struct nfs_open_context *ctx,
5192 const struct nfs_lock_context *l_ctx,
5193 fmode_t fmode)
5194{
Ben Dooksd49dd112019-10-16 17:28:21 +01005195 nfs4_stateid _current_stateid;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005196
Trond Myklebuste1253be2014-03-05 08:44:23 -05005197 /* If the current stateid represents a lost lock, then exit */
Ben Dooksd49dd112019-10-16 17:28:21 +01005198 if (nfs4_set_rw_stateid(&_current_stateid, ctx, l_ctx, fmode) == -EIO)
Trond Myklebuste1253be2014-03-05 08:44:23 -05005199 return true;
Ben Dooksd49dd112019-10-16 17:28:21 +01005200 return nfs4_stateid_match(stateid, &_current_stateid);
Trond Myklebust5521abf2013-03-16 20:54:34 -04005201}
5202
5203static bool nfs4_error_stateid_expired(int err)
5204{
5205 switch (err) {
5206 case -NFS4ERR_DELEG_REVOKED:
5207 case -NFS4ERR_ADMIN_REVOKED:
5208 case -NFS4ERR_BAD_STATEID:
5209 case -NFS4ERR_STALE_STATEID:
5210 case -NFS4ERR_OLD_STATEID:
5211 case -NFS4ERR_OPENMODE:
5212 case -NFS4ERR_EXPIRED:
5213 return true;
5214 }
5215 return false;
5216}
5217
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005218static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005219{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005220 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005222 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005223 if (task->tk_status < 0) {
5224 struct nfs4_exception exception = {
5225 .inode = hdr->inode,
5226 .state = hdr->args.context->state,
5227 .stateid = &hdr->args.stateid,
5228 };
5229 task->tk_status = nfs4_async_handle_exception(task,
5230 server, task->tk_status, &exception);
5231 if (exception.retry) {
5232 rpc_restart_call_prepare(task);
5233 return -EAGAIN;
5234 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005235 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005236
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005238 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005239 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005240}
5241
Trond Myklebust5521abf2013-03-16 20:54:34 -04005242static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005243 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005244{
5245
5246 if (!nfs4_error_stateid_expired(task->tk_status) ||
5247 nfs4_stateid_is_current(&args->stateid,
5248 args->context,
5249 args->lock_context,
5250 FMODE_READ))
5251 return false;
5252 rpc_restart_call_prepare(task);
5253 return true;
5254}
5255
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005256static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005257{
5258
5259 dprintk("--> %s\n", __func__);
5260
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005261 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005262 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005263 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005264 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005265 if (task->tk_status > 0)
5266 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005267 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5268 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005269}
5270
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005271static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5272 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005274 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005275 if (!hdr->pgio_done_cb)
5276 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005277 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005278 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005279}
5280
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005281static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5282 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005283{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005284 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005285 &hdr->args.seq_args,
5286 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005287 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005288 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005289 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5290 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005291 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005292 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005293 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005294 return -EIO;
5295 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005296}
5297
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005298static int nfs4_write_done_cb(struct rpc_task *task,
5299 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005300{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005301 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005302
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005303 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005304 if (task->tk_status < 0) {
5305 struct nfs4_exception exception = {
5306 .inode = hdr->inode,
5307 .state = hdr->args.context->state,
5308 .stateid = &hdr->args.stateid,
5309 };
5310 task->tk_status = nfs4_async_handle_exception(task,
5311 NFS_SERVER(inode), task->tk_status,
5312 &exception);
5313 if (exception.retry) {
5314 rpc_restart_call_prepare(task);
5315 return -EAGAIN;
5316 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005317 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005318 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005319 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005320 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005321 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005322 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005323}
5324
Trond Myklebust5521abf2013-03-16 20:54:34 -04005325static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005326 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005327{
5328
5329 if (!nfs4_error_stateid_expired(task->tk_status) ||
5330 nfs4_stateid_is_current(&args->stateid,
5331 args->context,
5332 args->lock_context,
5333 FMODE_WRITE))
5334 return false;
5335 rpc_restart_call_prepare(task);
5336 return true;
5337}
5338
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005339static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005340{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005341 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005342 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005343 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005344 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005345 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5346 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005347}
5348
Trond Myklebust5a37f852012-04-28 14:55:16 -04005349static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005350bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005351{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005352 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005353 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005354 return false;
5355 /* Otherwise, request attributes if and only if we don't hold
5356 * a delegation
5357 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005358 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005359}
Fred Isamana69aef12011-03-03 15:13:47 +00005360
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005361static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005362 struct rpc_message *msg,
5363 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005364{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005365 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005366
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005367 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5368 hdr->args.bitmask = NULL;
5369 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005370 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005371 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005372
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005373 if (!hdr->pgio_done_cb)
5374 hdr->pgio_done_cb = nfs4_write_done_cb;
5375 hdr->res.server = server;
5376 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005377
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005378 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Olga Kornievskaiacd1b6592020-02-12 17:32:12 -05005379 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005380 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005381}
5382
Fred Isaman0b7c0152012-04-20 14:47:39 -04005383static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5384{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005385 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005386 &data->args.seq_args,
5387 &data->res.seq_res,
5388 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005389}
5390
Fred Isaman0b7c0152012-04-20 14:47:39 -04005391static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005393 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005394
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005395 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005396 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5397 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005398 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005399 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005401 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005402}
5403
Fred Isaman0b7c0152012-04-20 14:47:39 -04005404static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005405{
5406 if (!nfs4_sequence_done(task, &data->res.seq_res))
5407 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005408 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005409}
5410
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005411static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5412 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005413{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005414 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005415
Fred Isaman0b7c0152012-04-20 14:47:39 -04005416 if (data->commit_done_cb == NULL)
5417 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005418 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005419 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005420 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005421 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005422}
5423
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005424static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5425 struct nfs_commitres *res)
5426{
5427 struct inode *dst_inode = file_inode(dst);
5428 struct nfs_server *server = NFS_SERVER(dst_inode);
5429 struct rpc_message msg = {
5430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5431 .rpc_argp = args,
5432 .rpc_resp = res,
5433 };
5434
5435 args->fh = NFS_FH(dst_inode);
5436 return nfs4_call_sync(server->client, server, &msg,
5437 &args->seq_args, &res->seq_res, 1);
5438}
5439
5440int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5441{
5442 struct nfs_commitargs args = {
5443 .offset = offset,
5444 .count = count,
5445 };
5446 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5447 struct nfs4_exception exception = { };
5448 int status;
5449
5450 do {
5451 status = _nfs4_proc_commit(dst, &args, res);
5452 status = nfs4_handle_exception(dst_server, status, &exception);
5453 } while (exception.retry);
5454
5455 return status;
5456}
5457
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005458struct nfs4_renewdata {
5459 struct nfs_client *client;
5460 unsigned long timestamp;
5461};
5462
Linus Torvalds1da177e2005-04-16 15:20:36 -07005463/*
5464 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5465 * standalone procedure for queueing an asynchronous RENEW.
5466 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005467static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005468{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005469 struct nfs4_renewdata *data = calldata;
5470 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005471
Elena Reshetova212bf412017-10-20 12:53:38 +03005472 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005473 nfs4_schedule_state_renewal(clp);
5474 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005475 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005476}
5477
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005478static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005479{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005480 struct nfs4_renewdata *data = calldata;
5481 struct nfs_client *clp = data->client;
5482 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005483
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005484 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005485 switch (task->tk_status) {
5486 case 0:
5487 break;
5488 case -NFS4ERR_LEASE_MOVED:
5489 nfs4_schedule_lease_moved_recovery(clp);
5490 break;
5491 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005492 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005493 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5494 return;
5495 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005496 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005497 return;
5498 }
5499 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005500 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005501 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005502}
5503
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005504static const struct rpc_call_ops nfs4_renew_ops = {
5505 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005506 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005507};
5508
NeilBrowna52458b2018-12-03 11:30:31 +11005509static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005510{
5511 struct rpc_message msg = {
5512 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5513 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005514 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005515 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005516 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005517
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005518 if (renew_flags == 0)
5519 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005520 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005521 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005522 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005523 if (data == NULL) {
5524 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005525 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005526 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005527 data->client = clp;
5528 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005529 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005530 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005531}
5532
NeilBrowna52458b2018-12-03 11:30:31 +11005533static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005534{
5535 struct rpc_message msg = {
5536 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5537 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005538 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005539 };
5540 unsigned long now = jiffies;
5541 int status;
5542
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005543 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005544 if (status < 0)
5545 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005546 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005547 return 0;
5548}
5549
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005550static inline int nfs4_server_supports_acls(struct nfs_server *server)
5551{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005552 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005553}
5554
Trond Myklebust21f498c2012-08-24 10:59:25 -04005555/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5556 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005557 * the stack.
5558 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005559#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005560
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005561int nfs4_buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005562 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005563{
5564 struct page *newpage, **spages;
5565 int rc = 0;
5566 size_t len;
5567 spages = pages;
5568
5569 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005570 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005571 newpage = alloc_page(GFP_KERNEL);
5572
5573 if (newpage == NULL)
5574 goto unwind;
5575 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005576 buf += len;
5577 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005578 *pages++ = newpage;
5579 rc++;
5580 } while (buflen != 0);
5581
5582 return rc;
5583
5584unwind:
5585 for(; rc > 0; rc--)
5586 __free_page(spages[rc-1]);
5587 return -ENOMEM;
5588}
5589
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005590struct nfs4_cached_acl {
5591 int cached;
5592 size_t len;
Gustavo A. R. Silva5601cda2020-03-09 13:24:42 -05005593 char data[];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005594};
5595
5596static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005597{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005598 struct nfs_inode *nfsi = NFS_I(inode);
5599
5600 spin_lock(&inode->i_lock);
5601 kfree(nfsi->nfs4_acl);
5602 nfsi->nfs4_acl = acl;
5603 spin_unlock(&inode->i_lock);
5604}
5605
5606static void nfs4_zap_acl_attr(struct inode *inode)
5607{
5608 nfs4_set_cached_acl(inode, NULL);
5609}
5610
5611static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5612{
5613 struct nfs_inode *nfsi = NFS_I(inode);
5614 struct nfs4_cached_acl *acl;
5615 int ret = -ENOENT;
5616
5617 spin_lock(&inode->i_lock);
5618 acl = nfsi->nfs4_acl;
5619 if (acl == NULL)
5620 goto out;
5621 if (buf == NULL) /* user is just asking for length */
5622 goto out_len;
5623 if (acl->cached == 0)
5624 goto out;
5625 ret = -ERANGE; /* see getxattr(2) man page */
5626 if (acl->len > buflen)
5627 goto out;
5628 memcpy(buf, acl->data, acl->len);
5629out_len:
5630 ret = acl->len;
5631out:
5632 spin_unlock(&inode->i_lock);
5633 return ret;
5634}
5635
Sachin Prabhu5794d212012-04-17 14:36:40 +01005636static 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 +00005637{
5638 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005639 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005640
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005641 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005642 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005643 if (acl == NULL)
5644 goto out;
5645 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005646 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005647 } else {
5648 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5649 if (acl == NULL)
5650 goto out;
5651 acl->cached = 0;
5652 }
5653 acl->len = acl_len;
5654out:
5655 nfs4_set_cached_acl(inode, acl);
5656}
5657
Andy Adamsonbf118a32011-12-07 11:55:27 -05005658/*
5659 * The getxattr API returns the required buffer length when called with a
5660 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5661 * the required buf. On a NULL buf, we send a page of data to the server
5662 * guessing that the ACL request can be serviced by a page. If so, we cache
5663 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5664 * the cache. If not so, we throw away the page, and cache the required
5665 * length. The next getxattr call will then produce another round trip to
5666 * the server, this time with the input buf of the required size.
5667 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005668static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005669{
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005670 struct page **pages;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005671 struct nfs_getaclargs args = {
5672 .fh = NFS_FH(inode),
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005673 .acl_len = buflen,
5674 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005675 struct nfs_getaclres res = {
5676 .acl_len = buflen,
5677 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005678 struct rpc_message msg = {
5679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5680 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005681 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005682 };
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005683 unsigned int npages;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005684 int ret = -ENOMEM, i;
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005685 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005686
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005687 if (buflen == 0)
5688 buflen = server->rsize;
5689
5690 npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
5691 pages = kmalloc_array(npages, sizeof(struct page *), GFP_NOFS);
5692 if (!pages)
5693 return -ENOMEM;
5694
5695 args.acl_pages = pages;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005696
Andy Adamsonbf118a32011-12-07 11:55:27 -05005697 for (i = 0; i < npages; i++) {
5698 pages[i] = alloc_page(GFP_KERNEL);
5699 if (!pages[i])
5700 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005701 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005702
5703 /* for decoding across pages */
5704 res.acl_scratch = alloc_page(GFP_KERNEL);
5705 if (!res.acl_scratch)
5706 goto out_free;
5707
Andy Adamsonbf118a32011-12-07 11:55:27 -05005708 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005709
Peng Taode040be2012-01-10 22:42:47 +08005710 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005711 __func__, buf, buflen, npages, args.acl_len);
5712 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5713 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005714 if (ret)
5715 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005716
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005717 /* Handle the case where the passed-in buffer is too short */
5718 if (res.acl_flags & NFS4_ACL_TRUNC) {
5719 /* Did the user only issue a request for the acl length? */
5720 if (buf == NULL)
5721 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005722 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005723 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005724 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005725 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005726 if (buf) {
5727 if (res.acl_len > buflen) {
5728 ret = -ERANGE;
5729 goto out_free;
5730 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005731 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005732 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005733out_ok:
5734 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005735out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005736 for (i = 0; i < npages; i++)
5737 if (pages[i])
5738 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005739 if (res.acl_scratch)
5740 __free_page(res.acl_scratch);
Olga Kornievskaia62a15732020-01-02 17:09:54 -05005741 kfree(pages);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005742 return ret;
5743}
5744
Trond Myklebust16b42892006-08-24 12:27:15 -04005745static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5746{
Trond Myklebust0688e642019-04-07 13:59:09 -04005747 struct nfs4_exception exception = {
5748 .interruptible = true,
5749 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005750 ssize_t ret;
5751 do {
5752 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005753 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005754 if (ret >= 0)
5755 break;
5756 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5757 } while (exception.retry);
5758 return ret;
5759}
5760
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005761static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5762{
5763 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005764 int ret;
5765
5766 if (!nfs4_server_supports_acls(server))
5767 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005768 ret = nfs_revalidate_inode(server, inode);
5769 if (ret < 0)
5770 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005771 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5772 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005773 ret = nfs4_read_cached_acl(inode, buf, buflen);
5774 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005775 /* -ENOENT is returned if there is no ACL or if there is an ACL
5776 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005777 return ret;
5778 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005779}
5780
Trond Myklebust16b42892006-08-24 12:27:15 -04005781static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005782{
5783 struct nfs_server *server = NFS_SERVER(inode);
5784 struct page *pages[NFS4ACL_MAXPAGES];
5785 struct nfs_setaclargs arg = {
5786 .fh = NFS_FH(inode),
5787 .acl_pages = pages,
5788 .acl_len = buflen,
5789 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005790 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005791 struct rpc_message msg = {
5792 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5793 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005794 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005795 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005796 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005797 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005798
5799 if (!nfs4_server_supports_acls(server))
5800 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005801 if (npages > ARRAY_SIZE(pages))
5802 return -ERANGE;
Frank van der Lindenccde1e92020-06-23 22:39:01 +00005803 i = nfs4_buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005804 if (i < 0)
5805 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005806 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005807 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005808
5809 /*
5810 * Free each page after tx, so the only ref left is
5811 * held by the network stack
5812 */
5813 for (; i > 0; i--)
5814 put_page(pages[i-1]);
5815
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005816 /*
5817 * Acl update can result in inode attribute update.
5818 * so mark the attribute cache invalid.
5819 */
5820 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005821 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005822 | NFS_INO_INVALID_CTIME
5823 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005824 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005825 nfs_access_zap_cache(inode);
5826 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005827 return ret;
5828}
5829
Trond Myklebust16b42892006-08-24 12:27:15 -04005830static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5831{
5832 struct nfs4_exception exception = { };
5833 int err;
5834 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005835 err = __nfs4_proc_set_acl(inode, buf, buflen);
5836 trace_nfs4_set_acl(inode, err);
5837 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005838 &exception);
5839 } while (exception.retry);
5840 return err;
5841}
5842
David Quigleyaa9c2662013-05-22 12:50:44 -04005843#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5844static int _nfs4_get_security_label(struct inode *inode, void *buf,
5845 size_t buflen)
5846{
5847 struct nfs_server *server = NFS_SERVER(inode);
5848 struct nfs_fattr fattr;
5849 struct nfs4_label label = {0, 0, buflen, buf};
5850
5851 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005852 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005853 .fh = NFS_FH(inode),
5854 .bitmask = bitmask,
5855 };
5856 struct nfs4_getattr_res res = {
5857 .fattr = &fattr,
5858 .label = &label,
5859 .server = server,
5860 };
5861 struct rpc_message msg = {
5862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005863 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005864 .rpc_resp = &res,
5865 };
5866 int ret;
5867
5868 nfs_fattr_init(&fattr);
5869
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005870 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005871 if (ret)
5872 return ret;
5873 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5874 return -ENOENT;
David Quigleyaa9c2662013-05-22 12:50:44 -04005875 return 0;
5876}
5877
5878static int nfs4_get_security_label(struct inode *inode, void *buf,
5879 size_t buflen)
5880{
Trond Myklebust0688e642019-04-07 13:59:09 -04005881 struct nfs4_exception exception = {
5882 .interruptible = true,
5883 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005884 int err;
5885
5886 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5887 return -EOPNOTSUPP;
5888
5889 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005890 err = _nfs4_get_security_label(inode, buf, buflen);
5891 trace_nfs4_get_security_label(inode, err);
5892 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005893 &exception);
5894 } while (exception.retry);
5895 return err;
5896}
5897
5898static int _nfs4_do_set_security_label(struct inode *inode,
5899 struct nfs4_label *ilabel,
5900 struct nfs_fattr *fattr,
5901 struct nfs4_label *olabel)
5902{
5903
5904 struct iattr sattr = {0};
5905 struct nfs_server *server = NFS_SERVER(inode);
5906 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005907 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005908 .fh = NFS_FH(inode),
5909 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005910 .server = server,
5911 .bitmask = bitmask,
5912 .label = ilabel,
5913 };
5914 struct nfs_setattrres res = {
5915 .fattr = fattr,
5916 .label = olabel,
5917 .server = server,
5918 };
5919 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005920 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5921 .rpc_argp = &arg,
5922 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005923 };
5924 int status;
5925
Jeff Layton12207f62013-11-01 10:49:32 -04005926 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005927
Jeff Layton12207f62013-11-01 10:49:32 -04005928 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005929 if (status)
5930 dprintk("%s failed: %d\n", __func__, status);
5931
5932 return status;
5933}
5934
5935static int nfs4_do_set_security_label(struct inode *inode,
5936 struct nfs4_label *ilabel,
5937 struct nfs_fattr *fattr,
5938 struct nfs4_label *olabel)
5939{
5940 struct nfs4_exception exception = { };
5941 int err;
5942
5943 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005944 err = _nfs4_do_set_security_label(inode, ilabel,
5945 fattr, olabel);
5946 trace_nfs4_set_security_label(inode, err);
5947 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005948 &exception);
5949 } while (exception.retry);
5950 return err;
5951}
5952
5953static int
Al Viro59301222016-05-27 10:19:30 -04005954nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005955{
5956 struct nfs4_label ilabel, *olabel = NULL;
5957 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005958 int status;
5959
5960 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5961 return -EOPNOTSUPP;
5962
5963 nfs_fattr_init(&fattr);
5964
5965 ilabel.pi = 0;
5966 ilabel.lfs = 0;
5967 ilabel.label = (char *)buf;
5968 ilabel.len = buflen;
5969
David Quigleyaa9c2662013-05-22 12:50:44 -04005970 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5971 if (IS_ERR(olabel)) {
5972 status = -PTR_ERR(olabel);
5973 goto out;
5974 }
5975
5976 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5977 if (status == 0)
5978 nfs_setsecurity(inode, &fattr, olabel);
5979
5980 nfs4_label_free(olabel);
5981out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005982 return status;
5983}
5984#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5985
5986
Chuck Leverf0920752012-05-21 22:45:41 -04005987static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5988 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005989{
5990 __be32 verf[2];
5991
Chuck Lever2c820d92012-05-21 22:45:33 -04005992 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5993 /* An impossible timestamp guarantees this value
5994 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005995 verf[0] = cpu_to_be32(U32_MAX);
5996 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005997 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005998 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005999 u64 ns = ktime_to_ns(nn->boot_time);
6000
6001 verf[0] = cpu_to_be32(ns >> 32);
6002 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04006003 }
Chuck Levercd937102012-03-02 17:14:31 -05006004 memcpy(bootverf->data, verf, sizeof(bootverf->data));
6005}
6006
Jeff Laytona3192682015-06-09 19:43:59 -04006007static int
6008nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006009{
Jeff Laytona3192682015-06-09 19:43:59 -04006010 size_t len;
6011 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04006012
Trond Myklebustceb3a162015-01-03 15:16:04 -05006013 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04006014 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08006015
Jeff Laytona3192682015-06-09 19:43:59 -04006016 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006017 len = 14 +
6018 strlen(clp->cl_rpcclient->cl_nodename) +
6019 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04006020 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04006021 1;
6022 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006023 if (nfs4_client_id_uniquifier[0] != '\0')
6024 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04006025 if (len > NFS4_OPAQUE_LIMIT + 1)
6026 return -EINVAL;
6027
6028 /*
6029 * Since this string is allocated at mount time, and held until the
6030 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6031 * about a memory-reclaim deadlock.
6032 */
6033 str = kmalloc(len, GFP_KERNEL);
6034 if (!str)
6035 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006036
Chuck Levere984a552012-09-14 17:24:21 -04006037 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04006038 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04006039 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006040 clp->cl_rpcclient->cl_nodename,
6041 nfs4_client_id_uniquifier,
6042 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006043 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04006044 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04006045 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04006046 clp->cl_rpcclient->cl_nodename,
6047 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04006048 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04006049 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04006050
Jeff Laytona3192682015-06-09 19:43:59 -04006051 clp->cl_owner_id = str;
6052 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006053}
6054
Jeff Layton873e3852015-06-09 19:44:00 -04006055static int
6056nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04006057{
Jeff Layton873e3852015-06-09 19:44:00 -04006058 size_t len;
6059 char *str;
6060
6061 len = 10 + 10 + 1 + 10 + 1 +
6062 strlen(nfs4_client_id_uniquifier) + 1 +
6063 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6064
6065 if (len > NFS4_OPAQUE_LIMIT + 1)
6066 return -EINVAL;
6067
6068 /*
6069 * Since this string is allocated at mount time, and held until the
6070 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6071 * about a memory-reclaim deadlock.
6072 */
6073 str = kmalloc(len, GFP_KERNEL);
6074 if (!str)
6075 return -ENOMEM;
6076
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006077 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04006078 clp->rpc_ops->version, clp->cl_minorversion,
6079 nfs4_client_id_uniquifier,
6080 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006081 clp->cl_owner_id = str;
6082 return 0;
6083}
6084
6085static int
6086nfs4_init_uniform_client_string(struct nfs_client *clp)
6087{
Jeff Layton873e3852015-06-09 19:44:00 -04006088 size_t len;
6089 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05006090
6091 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04006092 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04006093
6094 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04006095 return nfs4_init_uniquifier_client_string(clp);
6096
6097 len = 10 + 10 + 1 + 10 + 1 +
6098 strlen(clp->cl_rpcclient->cl_nodename) + 1;
6099
6100 if (len > NFS4_OPAQUE_LIMIT + 1)
6101 return -EINVAL;
6102
6103 /*
6104 * Since this string is allocated at mount time, and held until the
6105 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
6106 * about a memory-reclaim deadlock.
6107 */
6108 str = kmalloc(len, GFP_KERNEL);
6109 if (!str)
6110 return -ENOMEM;
6111
Trond Myklebustf2dd4362015-10-08 11:33:17 -04006112 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04006113 clp->rpc_ops->version, clp->cl_minorversion,
6114 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04006115 clp->cl_owner_id = str;
6116 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04006117}
6118
Chuck Lever706cb8d2014-03-12 12:51:47 -04006119/*
6120 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
6121 * services. Advertise one based on the address family of the
6122 * clientaddr.
6123 */
6124static unsigned int
6125nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
6126{
6127 if (strchr(clp->cl_ipaddr, ':') != NULL)
6128 return scnprintf(buf, len, "tcp6");
6129 else
6130 return scnprintf(buf, len, "tcp");
6131}
6132
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006133static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
6134{
6135 struct nfs4_setclientid *sc = calldata;
6136
6137 if (task->tk_status == 0)
6138 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
6139}
6140
6141static const struct rpc_call_ops nfs4_setclientid_ops = {
6142 .rpc_call_done = nfs4_setclientid_done,
6143};
6144
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006145/**
6146 * nfs4_proc_setclientid - Negotiate client ID
6147 * @clp: state data structure
6148 * @program: RPC program for NFSv4 callback service
6149 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006150 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006151 * @res: where to place the result
6152 *
6153 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6154 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006155int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006156 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006157 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006158{
6159 nfs4_verifier sc_verifier;
6160 struct nfs4_setclientid setclientid = {
6161 .sc_verifier = &sc_verifier,
6162 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006163 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006164 };
6165 struct rpc_message msg = {
6166 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6167 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006168 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006169 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006170 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006171 struct rpc_task_setup task_setup_data = {
6172 .rpc_client = clp->cl_rpcclient,
6173 .rpc_message = &msg,
6174 .callback_ops = &nfs4_setclientid_ops,
6175 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006176 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006177 };
Robert Milkowski7dc29932020-01-30 09:43:25 +00006178 unsigned long now = jiffies;
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006179 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006180
Chuck Leverde734832012-07-11 16:30:50 -04006181 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006182 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006183
6184 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6185 status = nfs4_init_uniform_client_string(clp);
6186 else
Jeff Laytona3192682015-06-09 19:43:59 -04006187 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006188
6189 if (status)
6190 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006191
Chuck Leverde734832012-07-11 16:30:50 -04006192 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006193 setclientid.sc_netid_len =
6194 nfs4_init_callback_netid(clp,
6195 setclientid.sc_netid,
6196 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006197 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006198 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006199 clp->cl_ipaddr, port >> 8, port & 255);
6200
Jeff Layton3a6bb732015-06-09 19:43:57 -04006201 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006202 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006203 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006204
6205 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006206 if (setclientid.sc_cred) {
Chuck Lever1047ec82019-10-04 09:58:54 -04006207 kfree(clp->cl_acceptor);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006208 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6209 put_rpccred(setclientid.sc_cred);
6210 }
Robert Milkowski7dc29932020-01-30 09:43:25 +00006211
6212 if (status == 0)
6213 do_renew_lease(clp, now);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006214out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006215 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006216 dprintk("NFS reply setclientid: %d\n", status);
6217 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218}
6219
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006220/**
6221 * nfs4_proc_setclientid_confirm - Confirm client ID
6222 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006223 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006224 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006225 *
6226 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6227 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006228int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006229 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006230 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006231{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006232 struct rpc_message msg = {
6233 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006234 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006235 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237 int status;
6238
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006239 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6240 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6241 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006242 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6243 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006244 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006245 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006246 return status;
6247}
6248
Trond Myklebustfe650402006-01-03 09:55:18 +01006249struct nfs4_delegreturndata {
6250 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006251 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006252 struct nfs_fh fh;
6253 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006254 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006255 struct {
6256 struct nfs4_layoutreturn_args arg;
6257 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006258 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006259 u32 roc_barrier;
6260 bool roc;
6261 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006262 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006263 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006264 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006265};
6266
Trond Myklebustfe650402006-01-03 09:55:18 +01006267static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6268{
6269 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006270 struct nfs4_exception exception = {
6271 .inode = data->inode,
6272 .stateid = &data->stateid,
6273 };
Andy Adamson938e1012009-04-01 09:22:28 -04006274
Trond Myklebust14516c32010-07-31 14:29:06 -04006275 if (!nfs4_sequence_done(task, &data->res.seq_res))
6276 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006277
Trond Myklebustca8acf82013-08-13 10:36:56 -04006278 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006279
6280 /* Handle Layoutreturn errors */
Trond Myklebust287a9c52019-09-20 07:23:41 -04006281 if (pnfs_roc_done(task, data->inode,
6282 &data->args.lr_args,
6283 &data->res.lr_res,
6284 &data->res.lr_ret) == -EAGAIN)
6285 goto out_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006286
Ricardo Labiaga79708862009-12-07 09:23:21 -05006287 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006288 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006289 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006290 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006291 case -NFS4ERR_ADMIN_REVOKED:
6292 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006293 case -NFS4ERR_EXPIRED:
6294 nfs4_free_revoked_stateid(data->res.server,
6295 data->args.stateid,
6296 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006297 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006298 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006299 case -NFS4ERR_STALE_STATEID:
Trond Myklebust244fcd22019-12-20 10:43:37 -05006300 case -ETIMEDOUT:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006301 task->tk_status = 0;
6302 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006303 case -NFS4ERR_OLD_STATEID:
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006304 if (!nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6305 nfs4_stateid_seqid_inc(&data->stateid);
Trond Myklebust70d136b2019-10-26 22:37:40 -04006306 if (data->args.bitmask) {
6307 data->args.bitmask = NULL;
6308 data->res.fattr = NULL;
6309 }
Trond Myklebust246afc0a2019-10-24 18:00:35 -04006310 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006311 case -NFS4ERR_ACCESS:
6312 if (data->args.bitmask) {
6313 data->args.bitmask = NULL;
6314 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006315 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006316 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006317 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006318 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006319 task->tk_status = nfs4_async_handle_exception(task,
6320 data->res.server, task->tk_status,
6321 &exception);
6322 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006323 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006324 }
Trond Myklebustd51f91d2019-10-21 14:22:14 -04006325 nfs_delegation_mark_returned(data->inode, data->args.stateid);
Ricardo Labiaga79708862009-12-07 09:23:21 -05006326 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006327 return;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006328out_restart:
6329 task->tk_status = 0;
6330 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006331}
6332
6333static void nfs4_delegreturn_release(void *calldata)
6334{
Peng Tao039b7562014-07-03 13:05:02 +08006335 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006336 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006337
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006338 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006339 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006340 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6341 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006342 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006343 nfs_iput_and_deactive(inode);
6344 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006345 kfree(calldata);
6346}
6347
Andy Adamson938e1012009-04-01 09:22:28 -04006348static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6349{
6350 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006351 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006352
6353 d_data = (struct nfs4_delegreturndata *)data;
6354
Trond Myklebust5326de92019-11-13 09:39:36 +01006355 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task)) {
6356 nfs4_sequence_done(task, &d_data->res.seq_res);
Peng Tao500d7012015-09-22 11:35:22 +08006357 return;
Trond Myklebust5326de92019-11-13 09:39:36 +01006358 }
Peng Tao500d7012015-09-22 11:35:22 +08006359
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006360 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6361 if (lo && !pnfs_layout_is_valid(lo)) {
6362 d_data->args.lr_args = NULL;
6363 d_data->res.lr_res = NULL;
6364 }
6365
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006366 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006367 &d_data->args.seq_args,
6368 &d_data->res.seq_res,
6369 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006370}
Andy Adamson938e1012009-04-01 09:22:28 -04006371
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006372static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006373 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006374 .rpc_call_done = nfs4_delegreturn_done,
6375 .rpc_release = nfs4_delegreturn_release,
6376};
6377
NeilBrowna52458b2018-12-03 11:30:31 +11006378static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006379{
6380 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006381 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006382 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006383 struct rpc_message msg = {
6384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6385 .rpc_cred = cred,
6386 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006387 struct rpc_task_setup task_setup_data = {
6388 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006389 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006390 .callback_ops = &nfs4_delegreturn_ops,
Trond Myklebustf304a802020-05-11 10:42:04 -04006391 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006392 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006393 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006394
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006395 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006396 if (data == NULL)
6397 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006398 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006399
6400 nfs4_state_protect(server->nfs_client,
6401 NFS_SP4_MACH_CRED_CLEANUP,
6402 &task_setup_data.rpc_client, &msg);
6403
Trond Myklebustfe650402006-01-03 09:55:18 +01006404 data->args.fhandle = &data->fh;
6405 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006406 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006407 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006408 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006409 data->res.fattr = &data->fattr;
6410 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006411 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006412 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006413 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006414 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006415 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006416 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006417 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006418 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006419 if (data->lr.roc) {
6420 data->args.lr_args = &data->lr.arg;
6421 data->res.lr_res = &data->lr.res;
6422 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006423 } else if (data->lr.roc) {
6424 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6425 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006426 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006427
Trond Myklebustc970aa82007-07-14 15:39:59 -04006428 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006429 msg.rpc_argp = &data->args;
6430 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006431 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006432 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006433 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006434 if (!issync)
6435 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006436 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006437 if (status != 0)
6438 goto out;
6439 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006440out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006441 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006442 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006443}
6444
NeilBrowna52458b2018-12-03 11:30:31 +11006445int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006446{
6447 struct nfs_server *server = NFS_SERVER(inode);
6448 struct nfs4_exception exception = { };
6449 int err;
6450 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006451 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006452 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006453 switch (err) {
6454 case -NFS4ERR_STALE_STATEID:
6455 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006456 case 0:
6457 return 0;
6458 }
6459 err = nfs4_handle_exception(server, err, &exception);
6460 } while (exception.retry);
6461 return err;
6462}
6463
Linus Torvalds1da177e2005-04-16 15:20:36 -07006464static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6465{
6466 struct inode *inode = state->inode;
6467 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006468 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006469 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006471 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006473 struct nfs_lockt_res res = {
6474 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006475 };
6476 struct rpc_message msg = {
6477 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006478 .rpc_argp = &arg,
6479 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006480 .rpc_cred = state->owner->so_cred,
6481 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006482 struct nfs4_lock_state *lsp;
6483 int status;
6484
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006485 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006486 status = nfs4_set_lock_state(state, request);
6487 if (status != 0)
6488 goto out;
6489 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006490 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006491 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006492 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006493 switch (status) {
6494 case 0:
6495 request->fl_type = F_UNLCK;
6496 break;
6497 case -NFS4ERR_DENIED:
6498 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006499 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006500 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006501 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006502out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006503 return status;
6504}
6505
6506static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6507{
Trond Myklebust0688e642019-04-07 13:59:09 -04006508 struct nfs4_exception exception = {
6509 .interruptible = true,
6510 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006511 int err;
6512
6513 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006514 err = _nfs4_proc_getlk(state, cmd, request);
6515 trace_nfs4_get_lock(request, state, cmd, err);
6516 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006517 &exception);
6518 } while (exception.retry);
6519 return err;
6520}
6521
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006522/*
6523 * Update the seqid of a lock stateid after receiving
6524 * NFS4ERR_OLD_STATEID
6525 */
6526static bool nfs4_refresh_lock_old_stateid(nfs4_stateid *dst,
6527 struct nfs4_lock_state *lsp)
6528{
6529 struct nfs4_state *state = lsp->ls_state;
6530 bool ret = false;
6531
6532 spin_lock(&state->state_lock);
6533 if (!nfs4_stateid_match_other(dst, &lsp->ls_stateid))
6534 goto out;
6535 if (!nfs4_stateid_is_newer(&lsp->ls_stateid, dst))
6536 nfs4_stateid_seqid_inc(dst);
6537 else
6538 dst->seqid = lsp->ls_stateid.seqid;
6539 ret = true;
6540out:
6541 spin_unlock(&state->state_lock);
6542 return ret;
6543}
6544
6545static bool nfs4_sync_lock_stateid(nfs4_stateid *dst,
6546 struct nfs4_lock_state *lsp)
6547{
6548 struct nfs4_state *state = lsp->ls_state;
6549 bool ret;
6550
6551 spin_lock(&state->state_lock);
6552 ret = !nfs4_stateid_match_other(dst, &lsp->ls_stateid);
6553 nfs4_stateid_copy(dst, &lsp->ls_stateid);
6554 spin_unlock(&state->state_lock);
6555 return ret;
6556}
6557
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006558struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006559 struct nfs_locku_args arg;
6560 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006561 struct nfs4_lock_state *lsp;
6562 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006563 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006564 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006565 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006566 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006567};
6568
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006569static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6570 struct nfs_open_context *ctx,
6571 struct nfs4_lock_state *lsp,
6572 struct nfs_seqid *seqid)
6573{
6574 struct nfs4_unlockdata *p;
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006575 struct nfs4_state *state = lsp->ls_state;
6576 struct inode *inode = state->inode;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006577
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006578 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006579 if (p == NULL)
6580 return NULL;
6581 p->arg.fh = NFS_FH(inode);
6582 p->arg.fl = &p->fl;
6583 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006584 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006585 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006586 /* Ensure we don't close file until we're done freeing locks! */
6587 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006588 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006589 locks_init_lock(&p->fl);
6590 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006591 p->server = NFS_SERVER(inode);
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006592 spin_lock(&state->state_lock);
6593 nfs4_stateid_copy(&p->arg.stateid, &lsp->ls_stateid);
6594 spin_unlock(&state->state_lock);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006595 return p;
6596}
6597
Trond Myklebust06f814a2006-01-03 09:55:07 +01006598static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006599{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006600 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006601 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006602 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006603 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006604 put_nfs_open_context(calldata->ctx);
6605 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006606}
6607
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006608static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006609{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006610 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006611 struct nfs4_exception exception = {
6612 .inode = calldata->lsp->ls_state->inode,
6613 .stateid = &calldata->arg.stateid,
6614 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006615
Trond Myklebust14516c32010-07-31 14:29:06 -04006616 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6617 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006618 switch (task->tk_status) {
6619 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006620 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006621 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006622 if (nfs4_update_lock_stateid(calldata->lsp,
6623 &calldata->res.stateid))
6624 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006625 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006626 case -NFS4ERR_ADMIN_REVOKED:
6627 case -NFS4ERR_EXPIRED:
6628 nfs4_free_revoked_stateid(calldata->server,
6629 &calldata->arg.stateid,
6630 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006631 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006632 case -NFS4ERR_BAD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006633 case -NFS4ERR_STALE_STATEID:
Trond Myklebust32c6e7e2019-09-20 07:23:48 -04006634 if (nfs4_sync_lock_stateid(&calldata->arg.stateid,
6635 calldata->lsp))
6636 rpc_restart_call_prepare(task);
6637 break;
6638 case -NFS4ERR_OLD_STATEID:
6639 if (nfs4_refresh_lock_old_stateid(&calldata->arg.stateid,
6640 calldata->lsp))
Trond Myklebust425c1d42015-01-24 14:57:53 -05006641 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006642 break;
6643 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006644 task->tk_status = nfs4_async_handle_exception(task,
6645 calldata->server, task->tk_status,
6646 &exception);
6647 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006648 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006649 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006650 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006651}
6652
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006653static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006654{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006655 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006656
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006657 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6658 nfs_async_iocounter_wait(task, calldata->l_ctx))
6659 return;
6660
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006661 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006662 goto out_wait;
Trond Myklebust795a88c2012-09-10 13:26:49 -04006663 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006664 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006665 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006666 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006667 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006668 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006669 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006670 &calldata->res.seq_res,
6671 task) != 0)
6672 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006673 return;
6674out_no_action:
6675 task->tk_action = NULL;
6676out_wait:
6677 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006678}
6679
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006680static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006681 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006682 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006683 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006684};
6685
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006686static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6687 struct nfs_open_context *ctx,
6688 struct nfs4_lock_state *lsp,
6689 struct nfs_seqid *seqid)
6690{
6691 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006692 struct rpc_message msg = {
6693 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6694 .rpc_cred = ctx->cred,
6695 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006696 struct rpc_task_setup task_setup_data = {
6697 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006698 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006699 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006700 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006701 .flags = RPC_TASK_ASYNC,
6702 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006703
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006704 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6705 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6706
Frank Filz137d6ac2007-07-09 15:32:29 -07006707 /* Ensure this is an unlock - when canceling a lock, the
6708 * canceled lock is passed in, and it won't be an unlock.
6709 */
6710 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006711 if (fl->fl_flags & FL_CLOSE)
6712 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006713
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006714 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6715 if (data == NULL) {
6716 nfs_free_seqid(seqid);
6717 return ERR_PTR(-ENOMEM);
6718 }
6719
Anna Schumakerfba83f32018-05-04 16:22:50 -04006720 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006721 msg.rpc_argp = &data->arg;
6722 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006723 task_setup_data.callback_data = data;
6724 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006725}
6726
Linus Torvalds1da177e2005-04-16 15:20:36 -07006727static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6728{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006729 struct inode *inode = state->inode;
6730 struct nfs4_state_owner *sp = state->owner;
6731 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006732 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006733 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006734 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006735 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006736 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006737 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006738
Trond Myklebust9b073572006-06-29 16:38:34 -04006739 status = nfs4_set_lock_state(state, request);
6740 /* Unlock _before_ we do the RPC call */
6741 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006742 /* Exclude nfs_delegation_claim_locks() */
6743 mutex_lock(&sp->so_delegreturn_mutex);
6744 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006745 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006746 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006747 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006748 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006749 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006750 }
6751 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006752 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006753 if (status != 0)
6754 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006755 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006756 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006757 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6758 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006759 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6760 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006761 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006762 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006763 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006764 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006765 status = PTR_ERR(task);
6766 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006767 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006768 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006769 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006770out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006771 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006772 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006773 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774}
6775
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006776struct nfs4_lockdata {
6777 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006778 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006779 struct nfs4_lock_state *lsp;
6780 struct nfs_open_context *ctx;
6781 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006782 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006783 int rpc_status;
6784 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006785 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006786};
6787
6788static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006789 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6790 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006791{
6792 struct nfs4_lockdata *p;
6793 struct inode *inode = lsp->ls_state->inode;
6794 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006795 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006796
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006797 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006798 if (p == NULL)
6799 return NULL;
6800
6801 p->arg.fh = NFS_FH(inode);
6802 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006803 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006804 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006805 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006806 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6807 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006808 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006809 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006810 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006811 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006812 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006813 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006814 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006815 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006816 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006817 locks_init_lock(&p->fl);
6818 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006819 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006820out_free_seqid:
6821 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006822out_free:
6823 kfree(p);
6824 return NULL;
6825}
6826
6827static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6828{
6829 struct nfs4_lockdata *data = calldata;
6830 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006831
Harvey Harrison3110ff82008-05-02 13:42:44 -07006832 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006833 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006834 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006835 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006836 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006837 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006838 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006839 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006840 nfs4_stateid_copy(&data->arg.open_stateid,
6841 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006842 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006843 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006844 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006845 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006846 nfs4_stateid_copy(&data->arg.lock_stateid,
6847 &data->lsp->ls_stateid);
6848 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006849 if (!nfs4_valid_open_stateid(state)) {
6850 data->rpc_status = -EBADF;
6851 task->tk_action = NULL;
6852 goto out_release_open_seqid;
6853 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006854 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006855 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006856 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006857 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006858 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006859 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006860out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006861 nfs_release_seqid(data->arg.open_seqid);
6862out_release_lock_seqid:
6863 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006864out_wait:
6865 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006866 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006867}
6868
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006869static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6870{
6871 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006872 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006873
Harvey Harrison3110ff82008-05-02 13:42:44 -07006874 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006875
Trond Myklebust14516c32010-07-31 14:29:06 -04006876 if (!nfs4_sequence_done(task, &data->res.seq_res))
6877 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006878
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006879 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006880 switch (task->tk_status) {
6881 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006882 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006883 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006884 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006885 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006886 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006887 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006888 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006889 if (data->arg.new_lock_owner != 0) {
6890 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6891 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6892 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006893 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6894 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006895 break;
6896 case -NFS4ERR_BAD_STATEID:
6897 case -NFS4ERR_OLD_STATEID:
6898 case -NFS4ERR_STALE_STATEID:
6899 case -NFS4ERR_EXPIRED:
6900 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006901 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006902 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006903 goto out_restart;
6904 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006905 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006906 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006907 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006908out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006909 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006910 return;
6911out_restart:
6912 if (!data->cancelled)
6913 rpc_restart_call_prepare(task);
6914 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006915}
6916
6917static void nfs4_lock_release(void *calldata)
6918{
6919 struct nfs4_lockdata *data = calldata;
6920
Harvey Harrison3110ff82008-05-02 13:42:44 -07006921 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006922 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006923 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006924 struct rpc_task *task;
6925 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6926 data->arg.lock_seqid);
6927 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006928 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006929 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006930 } else
6931 nfs_free_seqid(data->arg.lock_seqid);
6932 nfs4_put_lock_state(data->lsp);
6933 put_nfs_open_context(data->ctx);
6934 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006935 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006936}
6937
6938static const struct rpc_call_ops nfs4_lock_ops = {
6939 .rpc_call_prepare = nfs4_lock_prepare,
6940 .rpc_call_done = nfs4_lock_done,
6941 .rpc_release = nfs4_lock_release,
6942};
6943
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006944static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6945{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006946 switch (error) {
6947 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006948 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006949 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006950 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006951 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006952 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006953 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006954 break;
6955 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006956 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006957 nfs4_schedule_lease_recovery(server->nfs_client);
zhengbin8b98a532019-12-19 18:34:47 +08006958 }
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006959}
6960
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006961static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006962{
6963 struct nfs4_lockdata *data;
6964 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006965 struct rpc_message msg = {
6966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6967 .rpc_cred = state->owner->so_cred,
6968 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006969 struct rpc_task_setup task_setup_data = {
6970 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006971 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006972 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006973 .workqueue = nfsiod_workqueue,
Trond Myklebust61296502020-02-07 19:38:12 -05006974 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006975 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006976 int ret;
6977
Harvey Harrison3110ff82008-05-02 13:42:44 -07006978 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006979 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006980 fl->fl_u.nfs4_fl.owner,
6981 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006982 if (data == NULL)
6983 return -ENOMEM;
6984 if (IS_SETLKW(cmd))
6985 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006986 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6987 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006988 msg.rpc_argp = &data->arg;
6989 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006990 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006991 if (recovery_type > NFS_LOCK_NEW) {
6992 if (recovery_type == NFS_LOCK_RECLAIM)
6993 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006994 } else
6995 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006996 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006997 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006998 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006999 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007000 if (ret == 0) {
7001 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05007002 if (ret)
7003 nfs4_handle_setlk_error(data->server, data->lsp,
7004 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007005 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04007006 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05007007 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007008 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05007009 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01007010 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007011}
7012
7013static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
7014{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007015 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007016 struct nfs4_exception exception = {
7017 .inode = state->inode,
7018 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007019 int err;
7020
7021 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007022 /* Cache the lock if possible... */
7023 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7024 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007025 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04007026 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00007027 break;
7028 nfs4_handle_exception(server, err, &exception);
7029 } while (exception.retry);
7030 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031}
7032
7033static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
7034{
Trond Myklebust202b50d2005-06-22 17:16:29 +00007035 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04007036 struct nfs4_exception exception = {
7037 .inode = state->inode,
7038 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00007039 int err;
7040
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05007041 err = nfs4_set_lock_state(state, request);
7042 if (err != 0)
7043 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04007044 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10007045 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
7046 return 0;
7047 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007048 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04007049 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
7050 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007051 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007052 switch (err) {
7053 default:
7054 goto out;
7055 case -NFS4ERR_GRACE:
7056 case -NFS4ERR_DELAY:
7057 nfs4_handle_exception(server, err, &exception);
7058 err = 0;
7059 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00007060 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05007061out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00007062 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007063}
7064
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007065#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007066static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
7067{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007068 struct nfs4_lock_state *lsp;
7069 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05007070
Trond Myklebustc5896fc2016-09-22 13:39:03 -04007071 status = nfs4_set_lock_state(state, request);
7072 if (status != 0)
7073 return status;
7074 lsp = request->fl_u.nfs4_fl.owner;
7075 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
7076 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
7077 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05007078 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04007079}
7080#endif
7081
Linus Torvalds1da177e2005-04-16 15:20:36 -07007082static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7083{
Trond Myklebust19e03c52008-12-23 15:21:44 -05007084 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04007085 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007086 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007087 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007088
Trond Myklebust01c3b862006-06-29 16:38:39 -04007089 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04007090 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007091 if (status < 0)
7092 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04007093 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05007094 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007095 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04007096 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04007097 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05007098 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04007099 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007100 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007101 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05007102 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04007103 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05007104 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04007105 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08007106 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04007107out:
7108 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007109 return status;
7110}
7111
7112static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7113{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007114 struct nfs4_exception exception = {
7115 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04007116 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04007117 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05007118 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07007119 int err;
7120
7121 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07007122 err = _nfs4_proc_setlk(state, cmd, request);
7123 if (err == -NFS4ERR_DENIED)
7124 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007125 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07007126 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007127 } while (exception.retry);
7128 return err;
7129}
7130
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007131#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
7132#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
7133
7134static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04007135nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
7136 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007137{
7138 int status = -ERESTARTSYS;
7139 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
7140
7141 while(!signalled()) {
7142 status = nfs4_proc_setlk(state, cmd, request);
7143 if ((status != -EAGAIN) || IS_SETLK(cmd))
7144 break;
7145 freezable_schedule_timeout_interruptible(timeout);
7146 timeout *= 2;
7147 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
7148 status = -ERESTARTSYS;
7149 }
7150 return status;
7151}
7152
Jeff Laytona1d617d82016-09-17 18:17:39 -04007153#ifdef CONFIG_NFS_V4_1
7154struct nfs4_lock_waiter {
7155 struct task_struct *task;
7156 struct inode *inode;
7157 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007158};
7159
7160static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02007161nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04007162{
7163 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007164 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007165
Jeff Layton57174592018-03-18 08:37:03 -04007166 /* NULL key means to wake up everyone */
7167 if (key) {
7168 struct cb_notify_lock_args *cbnl = key;
7169 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
7170 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007171
Jeff Layton57174592018-03-18 08:37:03 -04007172 /* Only wake if the callback was for the same owner. */
7173 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
7174 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007175
Jeff Layton57174592018-03-18 08:37:03 -04007176 /* Make sure it's for the right inode */
7177 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7178 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007179 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007180
7181 /* override "private" so we can use default_wake_function */
7182 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007183 ret = woken_wake_function(wait, mode, flags, key);
7184 if (ret)
7185 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007186 wait->private = waiter;
7187 return ret;
7188}
7189
7190static int
7191nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7192{
7193 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007194 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7195 struct nfs_server *server = NFS_SERVER(state->inode);
7196 struct nfs_client *clp = server->nfs_client;
7197 wait_queue_head_t *q = &clp->cl_lock_waitq;
7198 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7199 .id = lsp->ls_seqid.owner_id,
7200 .s_dev = server->s_dev };
7201 struct nfs4_lock_waiter waiter = { .task = current,
7202 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007203 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007204 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007205
7206 /* Don't bother with waitqueue if we don't expect a callback */
7207 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7208 return nfs4_retry_setlk_simple(state, cmd, request);
7209
7210 init_wait(&wait);
7211 wait.private = &waiter;
7212 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007213
7214 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007215 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007216 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007217 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7218 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007219 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007220 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007221
7222 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007223 freezer_do_not_count();
7224 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7225 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007226 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007227 }
7228
Jeff Laytona1d617d82016-09-17 18:17:39 -04007229 return status;
7230}
7231#else /* !CONFIG_NFS_V4_1 */
7232static inline int
7233nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7234{
7235 return nfs4_retry_setlk_simple(state, cmd, request);
7236}
7237#endif
7238
Linus Torvalds1da177e2005-04-16 15:20:36 -07007239static int
7240nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7241{
7242 struct nfs_open_context *ctx;
7243 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007244 int status;
7245
7246 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007247 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007248 state = ctx->state;
7249
Trond Myklebustd9531262009-07-21 19:22:38 -04007250 if (IS_GETLK(cmd)) {
7251 if (state != NULL)
7252 return nfs4_proc_getlk(state, F_GETLK, request);
7253 return 0;
7254 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007255
7256 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7257 return -EINVAL;
7258
Trond Myklebustd9531262009-07-21 19:22:38 -04007259 if (request->fl_type == F_UNLCK) {
7260 if (state != NULL)
7261 return nfs4_proc_unlck(state, cmd, request);
7262 return 0;
7263 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007264
Trond Myklebustd9531262009-07-21 19:22:38 -04007265 if (state == NULL)
7266 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007267
7268 if ((request->fl_flags & FL_POSIX) &&
7269 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7270 return -ENOLCK;
7271
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007272 /*
7273 * Don't rely on the VFS having checked the file open mode,
7274 * since it won't do this for flock() locks.
7275 */
7276 switch (request->fl_type) {
7277 case F_RDLCK:
7278 if (!(filp->f_mode & FMODE_READ))
7279 return -EBADF;
7280 break;
7281 case F_WRLCK:
7282 if (!(filp->f_mode & FMODE_WRITE))
7283 return -EBADF;
7284 }
7285
Jeff Layton1ea67db2016-09-17 18:17:37 -04007286 status = nfs4_set_lock_state(state, request);
7287 if (status != 0)
7288 return status;
7289
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007290 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007291}
7292
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007293int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007294{
7295 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007296 int err;
7297
7298 err = nfs4_set_lock_state(state, fl);
7299 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007300 return err;
Olga Kornievskaia3d7a9522020-08-20 18:52:43 -04007301 do {
7302 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
7303 if (err != -NFS4ERR_DELAY)
7304 break;
7305 ssleep(1);
7306 } while (err == -NFS4ERR_DELAY);
NeilBrowndce26302017-12-13 09:57:09 +11007307 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007308}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007309
Trond Myklebustcf470c32012-03-07 13:49:12 -05007310struct nfs_release_lockowner_data {
7311 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007312 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007313 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007314 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007315 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007316};
7317
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007318static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7319{
7320 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007321 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007322 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7323 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007324 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007325 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007326}
7327
7328static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7329{
7330 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007331 struct nfs_server *server = data->server;
7332
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007333 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007334
7335 switch (task->tk_status) {
7336 case 0:
7337 renew_lease(server, data->timestamp);
7338 break;
7339 case -NFS4ERR_STALE_CLIENTID:
7340 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007341 nfs4_schedule_lease_recovery(server->nfs_client);
7342 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007343 case -NFS4ERR_LEASE_MOVED:
7344 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007345 if (nfs4_async_handle_error(task, server,
7346 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007347 rpc_restart_call_prepare(task);
7348 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007349}
7350
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007351static void nfs4_release_lockowner_release(void *calldata)
7352{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007353 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007354 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007355 kfree(calldata);
7356}
7357
Trond Myklebust17280172012-03-11 13:11:00 -04007358static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007359 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7360 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007361 .rpc_release = nfs4_release_lockowner_release,
7362};
7363
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007364static void
7365nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007366{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007367 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007368 struct rpc_message msg = {
7369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7370 };
7371
7372 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007373 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007374
Trond Myklebustcf470c32012-03-07 13:49:12 -05007375 data = kmalloc(sizeof(*data), GFP_NOFS);
7376 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007377 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007378 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007379 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007380 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7381 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7382 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007383
Trond Myklebustcf470c32012-03-07 13:49:12 -05007384 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007385 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007386 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007387 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007388}
7389
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007390#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7391
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007392static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007393 struct dentry *unused, struct inode *inode,
7394 const char *key, const void *buf,
7395 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007396{
Al Viro59301222016-05-27 10:19:30 -04007397 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007398}
7399
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007400static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007401 struct dentry *unused, struct inode *inode,
7402 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007403{
Al Virob2968212016-04-10 20:48:24 -04007404 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007405}
7406
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007407static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007408{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007409 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007410}
7411
David Quigleyc9bccef2013-05-22 12:50:45 -04007412#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007413
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007414static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007415 struct dentry *unused, struct inode *inode,
7416 const char *key, const void *buf,
7417 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007418{
7419 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007420 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007421
7422 return -EOPNOTSUPP;
7423}
7424
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007425static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007426 struct dentry *unused, struct inode *inode,
7427 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007428{
7429 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007430 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007431 return -EOPNOTSUPP;
7432}
7433
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007434static ssize_t
7435nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007436{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007437 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007438
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007439 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7440 len = security_inode_listsecurity(inode, list, list_len);
7441 if (list_len && len > list_len)
7442 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007443 }
7444 return len;
7445}
7446
7447static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7448 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007449 .get = nfs4_xattr_get_nfs4_label,
7450 .set = nfs4_xattr_set_nfs4_label,
7451};
David Quigleyc9bccef2013-05-22 12:50:45 -04007452
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007453#else
7454
7455static ssize_t
7456nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7457{
7458 return 0;
7459}
7460
7461#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007462
Frank van der Linden012a2112020-06-23 22:39:03 +00007463#ifdef CONFIG_NFS_V4_2
7464static int nfs4_xattr_set_nfs4_user(const struct xattr_handler *handler,
7465 struct dentry *unused, struct inode *inode,
7466 const char *key, const void *buf,
7467 size_t buflen, int flags)
7468{
7469 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007470 int ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007471
7472 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7473 return -EOPNOTSUPP;
7474
7475 /*
7476 * There is no mapping from the MAY_* flags to the NFS_ACCESS_XA*
7477 * flags right now. Handling of xattr operations use the normal
7478 * file read/write permissions.
7479 *
7480 * Just in case the server has other ideas (which RFC 8276 allows),
7481 * do a cached access check for the XA* flags to possibly avoid
7482 * doing an RPC and getting EACCES back.
7483 */
7484 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7485 if (!(cache.mask & NFS_ACCESS_XAWRITE))
7486 return -EACCES;
7487 }
7488
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007489 if (buf == NULL) {
7490 ret = nfs42_proc_removexattr(inode, key);
7491 if (!ret)
7492 nfs4_xattr_cache_remove(inode, key);
7493 } else {
7494 ret = nfs42_proc_setxattr(inode, key, buf, buflen, flags);
7495 if (!ret)
7496 nfs4_xattr_cache_add(inode, key, buf, NULL, buflen);
7497 }
7498
7499 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007500}
7501
7502static int nfs4_xattr_get_nfs4_user(const struct xattr_handler *handler,
7503 struct dentry *unused, struct inode *inode,
7504 const char *key, void *buf, size_t buflen)
7505{
7506 struct nfs_access_entry cache;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007507 ssize_t ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007508
7509 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7510 return -EOPNOTSUPP;
7511
7512 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7513 if (!(cache.mask & NFS_ACCESS_XAREAD))
7514 return -EACCES;
7515 }
7516
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007517 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7518 if (ret)
7519 return ret;
7520
7521 ret = nfs4_xattr_cache_get(inode, key, buf, buflen);
7522 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7523 return ret;
7524
7525 ret = nfs42_proc_getxattr(inode, key, buf, buflen);
7526
7527 return ret;
Frank van der Linden012a2112020-06-23 22:39:03 +00007528}
7529
7530static ssize_t
7531nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7532{
7533 u64 cookie;
7534 bool eof;
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007535 ssize_t ret, size;
Frank van der Linden012a2112020-06-23 22:39:03 +00007536 char *buf;
7537 size_t buflen;
7538 struct nfs_access_entry cache;
7539
7540 if (!nfs_server_capable(inode, NFS_CAP_XATTR))
7541 return 0;
7542
7543 if (!nfs_access_get_cached(inode, current_cred(), &cache, true)) {
7544 if (!(cache.mask & NFS_ACCESS_XALIST))
7545 return 0;
7546 }
7547
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007548 ret = nfs_revalidate_inode(NFS_SERVER(inode), inode);
7549 if (ret)
7550 return ret;
7551
7552 ret = nfs4_xattr_cache_list(inode, list, list_len);
7553 if (ret >= 0 || (ret < 0 && ret != -ENOENT))
7554 return ret;
7555
Frank van der Linden012a2112020-06-23 22:39:03 +00007556 cookie = 0;
7557 eof = false;
7558 buflen = list_len ? list_len : XATTR_LIST_MAX;
7559 buf = list_len ? list : NULL;
7560 size = 0;
7561
7562 while (!eof) {
7563 ret = nfs42_proc_listxattrs(inode, buf, buflen,
7564 &cookie, &eof);
7565 if (ret < 0)
7566 return ret;
7567
7568 if (list_len) {
7569 buf += ret;
7570 buflen -= ret;
7571 }
7572 size += ret;
7573 }
7574
Frank van der Linden95ad37f2020-06-23 22:39:04 +00007575 if (list_len)
7576 nfs4_xattr_cache_set_list(inode, list, size);
7577
Frank van der Linden012a2112020-06-23 22:39:03 +00007578 return size;
7579}
7580
7581#else
7582
7583static ssize_t
7584nfs4_listxattr_nfs4_user(struct inode *inode, char *list, size_t list_len)
7585{
7586 return 0;
7587}
7588#endif /* CONFIG_NFS_V4_2 */
7589
Andy Adamson533eb462011-06-13 18:25:56 -04007590/*
7591 * nfs_fhget will use either the mounted_on_fileid or the fileid
7592 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007593static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7594{
Andy Adamson533eb462011-06-13 18:25:56 -04007595 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7596 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7597 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007598 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007599 return;
7600
7601 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007602 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007603 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7604 fattr->nlink = 2;
7605}
7606
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007607static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7608 const struct qstr *name,
7609 struct nfs4_fs_locations *fs_locations,
7610 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007611{
7612 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007613 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007614 struct nfs4_fs_locations_arg args = {
7615 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007616 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007617 .page = page,
7618 .bitmask = bitmask,
7619 };
Benny Halevy22958462009-04-01 09:22:02 -04007620 struct nfs4_fs_locations_res res = {
7621 .fs_locations = fs_locations,
7622 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007623 struct rpc_message msg = {
7624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7625 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007626 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007627 };
7628 int status;
7629
Harvey Harrison3110ff82008-05-02 13:42:44 -07007630 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007631
Chuck Leverc05cefc2017-11-05 15:45:22 -05007632 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7633 bitmask[1] = nfs4_fattr_bitmap[1];
7634
Andy Adamson533eb462011-06-13 18:25:56 -04007635 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7636 * is not supported */
7637 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007638 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007639 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007640 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007641
Trond Myklebustc228fd32007-01-13 02:28:11 -05007642 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007643 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007644 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007645 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007646 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007647 return status;
7648}
7649
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007650int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7651 const struct qstr *name,
7652 struct nfs4_fs_locations *fs_locations,
7653 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007654{
Trond Myklebust0688e642019-04-07 13:59:09 -04007655 struct nfs4_exception exception = {
7656 .interruptible = true,
7657 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007658 int err;
7659 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007660 err = _nfs4_proc_fs_locations(client, dir, name,
7661 fs_locations, page);
7662 trace_nfs4_get_fs_locations(dir, name, err);
7663 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007664 &exception);
7665 } while (exception.retry);
7666 return err;
7667}
7668
Chuck Leverb03d7352013-10-17 14:12:50 -04007669/*
7670 * This operation also signals the server that this client is
7671 * performing migration recovery. The server can stop returning
7672 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7673 * appended to this compound to identify the client ID which is
7674 * performing recovery.
7675 */
7676static int _nfs40_proc_get_locations(struct inode *inode,
7677 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007678 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007679{
7680 struct nfs_server *server = NFS_SERVER(inode);
7681 struct rpc_clnt *clnt = server->client;
7682 u32 bitmask[2] = {
7683 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7684 };
7685 struct nfs4_fs_locations_arg args = {
7686 .clientid = server->nfs_client->cl_clientid,
7687 .fh = NFS_FH(inode),
7688 .page = page,
7689 .bitmask = bitmask,
7690 .migration = 1, /* skip LOOKUP */
7691 .renew = 1, /* append RENEW */
7692 };
7693 struct nfs4_fs_locations_res res = {
7694 .fs_locations = locations,
7695 .migration = 1,
7696 .renew = 1,
7697 };
7698 struct rpc_message msg = {
7699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7700 .rpc_argp = &args,
7701 .rpc_resp = &res,
7702 .rpc_cred = cred,
7703 };
7704 unsigned long now = jiffies;
7705 int status;
7706
7707 nfs_fattr_init(&locations->fattr);
7708 locations->server = server;
7709 locations->nlocations = 0;
7710
Anna Schumakerfba83f32018-05-04 16:22:50 -04007711 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007712 status = nfs4_call_sync_sequence(clnt, server, &msg,
7713 &args.seq_args, &res.seq_res);
7714 if (status)
7715 return status;
7716
7717 renew_lease(server, now);
7718 return 0;
7719}
7720
7721#ifdef CONFIG_NFS_V4_1
7722
7723/*
7724 * This operation also signals the server that this client is
7725 * performing migration recovery. The server can stop asserting
7726 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7727 * performing this operation is identified in the SEQUENCE
7728 * operation in this compound.
7729 *
7730 * When the client supports GETATTR(fs_locations_info), it can
7731 * be plumbed in here.
7732 */
7733static int _nfs41_proc_get_locations(struct inode *inode,
7734 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007735 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007736{
7737 struct nfs_server *server = NFS_SERVER(inode);
7738 struct rpc_clnt *clnt = server->client;
7739 u32 bitmask[2] = {
7740 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7741 };
7742 struct nfs4_fs_locations_arg args = {
7743 .fh = NFS_FH(inode),
7744 .page = page,
7745 .bitmask = bitmask,
7746 .migration = 1, /* skip LOOKUP */
7747 };
7748 struct nfs4_fs_locations_res res = {
7749 .fs_locations = locations,
7750 .migration = 1,
7751 };
7752 struct rpc_message msg = {
7753 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7754 .rpc_argp = &args,
7755 .rpc_resp = &res,
7756 .rpc_cred = cred,
7757 };
7758 int status;
7759
7760 nfs_fattr_init(&locations->fattr);
7761 locations->server = server;
7762 locations->nlocations = 0;
7763
Anna Schumakerfba83f32018-05-04 16:22:50 -04007764 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007765 status = nfs4_call_sync_sequence(clnt, server, &msg,
7766 &args.seq_args, &res.seq_res);
7767 if (status == NFS4_OK &&
7768 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7769 status = -NFS4ERR_LEASE_MOVED;
7770 return status;
7771}
7772
7773#endif /* CONFIG_NFS_V4_1 */
7774
7775/**
7776 * nfs4_proc_get_locations - discover locations for a migrated FSID
7777 * @inode: inode on FSID that is migrating
7778 * @locations: result of query
7779 * @page: buffer
7780 * @cred: credential to use for this operation
7781 *
7782 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7783 * operation failed, or a negative errno if a local error occurred.
7784 *
7785 * On success, "locations" is filled in, but if the server has
7786 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7787 * asserted.
7788 *
7789 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7790 * from this client that require migration recovery.
7791 */
7792int nfs4_proc_get_locations(struct inode *inode,
7793 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007794 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007795{
7796 struct nfs_server *server = NFS_SERVER(inode);
7797 struct nfs_client *clp = server->nfs_client;
7798 const struct nfs4_mig_recovery_ops *ops =
7799 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007800 struct nfs4_exception exception = {
7801 .interruptible = true,
7802 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007803 int status;
7804
7805 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7806 (unsigned long long)server->fsid.major,
7807 (unsigned long long)server->fsid.minor,
7808 clp->cl_hostname);
7809 nfs_display_fhandle(NFS_FH(inode), __func__);
7810
7811 do {
7812 status = ops->get_locations(inode, locations, page, cred);
7813 if (status != -NFS4ERR_DELAY)
7814 break;
7815 nfs4_handle_exception(server, status, &exception);
7816 } while (exception.retry);
7817 return status;
7818}
7819
Chuck Lever44c99932013-10-17 14:13:30 -04007820/*
7821 * This operation also signals the server that this client is
7822 * performing "lease moved" recovery. The server can stop
7823 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7824 * is appended to this compound to identify the client ID which is
7825 * performing recovery.
7826 */
NeilBrowna52458b2018-12-03 11:30:31 +11007827static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007828{
7829 struct nfs_server *server = NFS_SERVER(inode);
7830 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7831 struct rpc_clnt *clnt = server->client;
7832 struct nfs4_fsid_present_arg args = {
7833 .fh = NFS_FH(inode),
7834 .clientid = clp->cl_clientid,
7835 .renew = 1, /* append RENEW */
7836 };
7837 struct nfs4_fsid_present_res res = {
7838 .renew = 1,
7839 };
7840 struct rpc_message msg = {
7841 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7842 .rpc_argp = &args,
7843 .rpc_resp = &res,
7844 .rpc_cred = cred,
7845 };
7846 unsigned long now = jiffies;
7847 int status;
7848
7849 res.fh = nfs_alloc_fhandle();
7850 if (res.fh == NULL)
7851 return -ENOMEM;
7852
Anna Schumakerfba83f32018-05-04 16:22:50 -04007853 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007854 status = nfs4_call_sync_sequence(clnt, server, &msg,
7855 &args.seq_args, &res.seq_res);
7856 nfs_free_fhandle(res.fh);
7857 if (status)
7858 return status;
7859
7860 do_renew_lease(clp, now);
7861 return 0;
7862}
7863
7864#ifdef CONFIG_NFS_V4_1
7865
7866/*
7867 * This operation also signals the server that this client is
7868 * performing "lease moved" recovery. The server can stop asserting
7869 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7870 * this operation is identified in the SEQUENCE operation in this
7871 * compound.
7872 */
NeilBrowna52458b2018-12-03 11:30:31 +11007873static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007874{
7875 struct nfs_server *server = NFS_SERVER(inode);
7876 struct rpc_clnt *clnt = server->client;
7877 struct nfs4_fsid_present_arg args = {
7878 .fh = NFS_FH(inode),
7879 };
7880 struct nfs4_fsid_present_res res = {
7881 };
7882 struct rpc_message msg = {
7883 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7884 .rpc_argp = &args,
7885 .rpc_resp = &res,
7886 .rpc_cred = cred,
7887 };
7888 int status;
7889
7890 res.fh = nfs_alloc_fhandle();
7891 if (res.fh == NULL)
7892 return -ENOMEM;
7893
Anna Schumakerfba83f32018-05-04 16:22:50 -04007894 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007895 status = nfs4_call_sync_sequence(clnt, server, &msg,
7896 &args.seq_args, &res.seq_res);
7897 nfs_free_fhandle(res.fh);
7898 if (status == NFS4_OK &&
7899 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7900 status = -NFS4ERR_LEASE_MOVED;
7901 return status;
7902}
7903
7904#endif /* CONFIG_NFS_V4_1 */
7905
7906/**
7907 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7908 * @inode: inode on FSID to check
7909 * @cred: credential to use for this operation
7910 *
7911 * Server indicates whether the FSID is present, moved, or not
7912 * recognized. This operation is necessary to clear a LEASE_MOVED
7913 * condition for this client ID.
7914 *
7915 * Returns NFS4_OK if the FSID is present on this server,
7916 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7917 * NFS4ERR code if some error occurred on the server, or a
7918 * negative errno if a local failure occurred.
7919 */
NeilBrowna52458b2018-12-03 11:30:31 +11007920int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007921{
7922 struct nfs_server *server = NFS_SERVER(inode);
7923 struct nfs_client *clp = server->nfs_client;
7924 const struct nfs4_mig_recovery_ops *ops =
7925 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007926 struct nfs4_exception exception = {
7927 .interruptible = true,
7928 };
Chuck Lever44c99932013-10-17 14:13:30 -04007929 int status;
7930
7931 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7932 (unsigned long long)server->fsid.major,
7933 (unsigned long long)server->fsid.minor,
7934 clp->cl_hostname);
7935 nfs_display_fhandle(NFS_FH(inode), __func__);
7936
7937 do {
7938 status = ops->fsid_present(inode, cred);
7939 if (status != -NFS4ERR_DELAY)
7940 break;
7941 nfs4_handle_exception(server, status, &exception);
7942 } while (exception.retry);
7943 return status;
7944}
7945
Trond Myklebust302fad72019-02-18 13:32:38 -05007946/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007947 * If 'use_integrity' is true and the state managment nfs_client
7948 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7949 * and the machine credential as per RFC3530bis and RFC5661 Security
7950 * Considerations sections. Otherwise, just use the user cred with the
7951 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007952 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007953static 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 +00007954{
7955 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007956 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7957 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007958 struct nfs4_secinfo_arg args = {
7959 .dir_fh = NFS_FH(dir),
7960 .name = name,
7961 };
7962 struct nfs4_secinfo_res res = {
7963 .flavors = flavors,
7964 };
7965 struct rpc_message msg = {
7966 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7967 .rpc_argp = &args,
7968 .rpc_resp = &res,
7969 };
Anna Schumaker50493362019-08-14 15:30:16 -04007970 struct nfs4_call_sync_data data = {
7971 .seq_server = NFS_SERVER(dir),
7972 .seq_args = &args.seq_args,
7973 .seq_res = &res.seq_res,
7974 };
7975 struct rpc_task_setup task_setup = {
7976 .rpc_client = clnt,
7977 .rpc_message = &msg,
7978 .callback_ops = clp->cl_mvops->call_sync_ops,
7979 .callback_data = &data,
7980 .flags = RPC_TASK_NO_ROUND_ROBIN,
7981 };
NeilBrowna52458b2018-12-03 11:30:31 +11007982 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007983
7984 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007985 clnt = clp->cl_rpcclient;
7986 task_setup.rpc_client = clnt;
7987
7988 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007989 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007990 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007991
7992 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007993
Anna Schumaker50493362019-08-14 15:30:16 -04007994 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7995 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7996 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007997
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007998 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007999
NeilBrowna52458b2018-12-03 11:30:31 +11008000 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008001 return status;
8002}
8003
Bryan Schumaker72de53e2012-04-27 13:27:40 -04008004int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
8005 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008006{
Trond Myklebust0688e642019-04-07 13:59:09 -04008007 struct nfs4_exception exception = {
8008 .interruptible = true,
8009 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008010 int err;
8011 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04008012 err = -NFS4ERR_WRONGSEC;
8013
8014 /* try to use integrity protection with machine cred */
8015 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
8016 err = _nfs4_proc_secinfo(dir, name, flavors, true);
8017
8018 /*
8019 * if unable to use integrity protection, or SECINFO with
8020 * integrity protection returns NFS4ERR_WRONGSEC (which is
8021 * disallowed by spec, but exists in deployed servers) use
8022 * the current filesystem's rpc_client and the user cred.
8023 */
8024 if (err == -NFS4ERR_WRONGSEC)
8025 err = _nfs4_proc_secinfo(dir, name, flavors, false);
8026
Trond Myklebust078ea3d2013-08-12 16:45:55 -04008027 trace_nfs4_secinfo(dir, name, err);
8028 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00008029 &exception);
8030 } while (exception.retry);
8031 return err;
8032}
8033
Andy Adamson557134a2009-04-01 09:21:53 -04008034#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04008035/*
Andy Adamson357f54d2010-12-14 10:11:57 -05008036 * Check the exchange flags returned by the server for invalid flags, having
8037 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
8038 * DS flags set.
8039 */
8040static int nfs4_check_cl_exchange_flags(u32 flags)
8041{
8042 if (flags & ~EXCHGID4_FLAG_MASK_R)
8043 goto out_inval;
8044 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
8045 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
8046 goto out_inval;
8047 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
8048 goto out_inval;
8049 return NFS_OK;
8050out_inval:
8051 return -NFS4ERR_INVAL;
8052}
8053
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008054static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04008055nfs41_same_server_scope(struct nfs41_server_scope *a,
8056 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008057{
Anna Schumaker49ad0142017-01-11 16:51:59 -05008058 if (a->server_scope_sz != b->server_scope_sz)
8059 return false;
8060 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008061}
8062
Andy Adamson02a95de2016-02-05 16:08:37 -05008063static void
8064nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
8065{
Trond Myklebust5c441542019-11-13 08:34:00 +01008066 struct nfs41_bind_conn_to_session_args *args = task->tk_msg.rpc_argp;
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008067 struct nfs41_bind_conn_to_session_res *res = task->tk_msg.rpc_resp;
Trond Myklebust5c441542019-11-13 08:34:00 +01008068 struct nfs_client *clp = args->client;
8069
8070 switch (task->tk_status) {
8071 case -NFS4ERR_BADSESSION:
8072 case -NFS4ERR_DEADSESSION:
8073 nfs4_schedule_session_recovery(clp->cl_session,
8074 task->tk_status);
8075 }
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008076 if (args->dir == NFS4_CDFC4_FORE_OR_BOTH &&
8077 res->dir != NFS4_CDFS4_BOTH) {
8078 rpc_task_close_connection(task);
8079 if (args->retries++ < MAX_BIND_CONN_TO_SESSION_RETRIES)
8080 rpc_restart_call(task);
8081 }
Andy Adamson02a95de2016-02-05 16:08:37 -05008082}
8083
8084static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
Olga Kornievskaia1c709b72020-04-26 11:30:00 -04008085 .rpc_call_done = nfs4_bind_one_conn_to_session_done,
Andy Adamson02a95de2016-02-05 16:08:37 -05008086};
8087
Andy Adamson357f54d2010-12-14 10:11:57 -05008088/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008089 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008090 *
8091 * The 4.1 client currently uses the same TCP connection for the
8092 * fore and backchannel.
8093 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008094static
8095int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
8096 struct rpc_xprt *xprt,
8097 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008098 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008099{
8100 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08008101 struct nfs41_bind_conn_to_session_args args = {
8102 .client = clp,
8103 .dir = NFS4_CDFC4_FORE_OR_BOTH,
Olga Kornievskaiadff58532020-04-24 17:45:50 -04008104 .retries = 0,
Trond Myklebust71a097c2015-02-18 09:27:18 -08008105 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008106 struct nfs41_bind_conn_to_session_res res;
8107 struct rpc_message msg = {
8108 .rpc_proc =
8109 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08008110 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008111 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04008112 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008113 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008114 struct rpc_task_setup task_setup_data = {
8115 .rpc_client = clnt,
8116 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05008117 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008118 .rpc_message = &msg,
8119 .flags = RPC_TASK_TIMEOUT,
8120 };
8121 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008122
Trond Myklebust71a097c2015-02-18 09:27:18 -08008123 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
8124 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
8125 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008126
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008127 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
8128 if (xprt != rcu_access_pointer(clnt->cl_xprt))
8129 args.dir = NFS4_CDFC4_FORE;
8130
8131 task = rpc_run_task(&task_setup_data);
8132 if (!IS_ERR(task)) {
8133 status = task->tk_status;
8134 rpc_put_task(task);
8135 } else
8136 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008137 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008138 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08008139 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008140 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
8141 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008142 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008143 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008144 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008145 dprintk("NFS: %s: Unexpected direction from server\n",
8146 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008147 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008148 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08008149 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008150 dprintk("NFS: %s: Server returned RDMA mode = true\n",
8151 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008152 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008153 }
8154 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04008155
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008156 return status;
8157}
8158
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008159struct rpc_bind_conn_calldata {
8160 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11008161 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008162};
8163
8164static int
8165nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
8166 struct rpc_xprt *xprt,
8167 void *calldata)
8168{
8169 struct rpc_bind_conn_calldata *p = calldata;
8170
8171 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
8172}
8173
NeilBrowna52458b2018-12-03 11:30:31 +11008174int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05008175{
8176 struct rpc_bind_conn_calldata data = {
8177 .clp = clp,
8178 .cred = cred,
8179 };
8180 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
8181 nfs4_proc_bind_conn_to_session_callback, &data);
8182}
8183
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04008184/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008185 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
8186 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04008187 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008188static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
8189 .how = SP4_MACH_CRED,
8190 .enforce.u.words = {
8191 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8192 1 << (OP_EXCHANGE_ID - 32) |
8193 1 << (OP_CREATE_SESSION - 32) |
8194 1 << (OP_DESTROY_SESSION - 32) |
8195 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008196 },
8197 .allow.u.words = {
8198 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008199 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008200 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008201 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008202 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008203 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008204 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05008205 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008206 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04008207 1 << (OP_FREE_STATEID - 32) |
8208 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008209 }
8210};
8211
8212/*
8213 * Select the state protection mode for client `clp' given the server results
8214 * from exchange_id in `sp'.
8215 *
8216 * Returns 0 on success, negative errno otherwise.
8217 */
8218static int nfs4_sp4_select_mode(struct nfs_client *clp,
8219 struct nfs41_state_protection *sp)
8220{
8221 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
8222 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
8223 1 << (OP_EXCHANGE_ID - 32) |
8224 1 << (OP_CREATE_SESSION - 32) |
8225 1 << (OP_DESTROY_SESSION - 32) |
8226 1 << (OP_DESTROY_CLIENTID - 32)
8227 };
Trond Myklebust937e3132017-08-01 07:32:50 -04008228 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008229 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04008230 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008231
8232 if (sp->how == SP4_MACH_CRED) {
8233 /* Print state protect result */
8234 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
8235 for (i = 0; i <= LAST_NFS4_OP; i++) {
8236 if (test_bit(i, sp->enforce.u.longs))
8237 dfprintk(MOUNT, " enforce op %d\n", i);
8238 if (test_bit(i, sp->allow.u.longs))
8239 dfprintk(MOUNT, " allow op %d\n", i);
8240 }
8241
8242 /* make sure nothing is on enforce list that isn't supported */
8243 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
8244 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
8245 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008246 ret = -EINVAL;
8247 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008248 }
8249 }
8250
8251 /*
8252 * Minimal mode - state operations are allowed to use machine
8253 * credential. Note this already happens by default, so the
8254 * client doesn't have to do anything more than the negotiation.
8255 *
8256 * NOTE: we don't care if EXCHANGE_ID is in the list -
8257 * we're already using the machine cred for exchange_id
8258 * and will never use a different cred.
8259 */
8260 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
8261 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
8262 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
8263 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
8264 dfprintk(MOUNT, "sp4_mach_cred:\n");
8265 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008266 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008267 } else {
8268 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008269 ret = -EINVAL;
8270 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008271 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008272
8273 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05008274 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
8275 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008276 test_bit(OP_LOCKU, sp->allow.u.longs)) {
8277 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008278 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04008279 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008280
Andrew Elble99ade3c2015-12-02 09:39:51 -05008281 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
8282 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008283 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05008284 }
8285
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008286 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
8287 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
8288 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008289 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04008290 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008291
8292 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
8293 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
8294 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008295 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04008296 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008297
8298 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
8299 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008300 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008301 }
8302
8303 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
8304 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04008305 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04008306 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008307 }
Trond Myklebust937e3132017-08-01 07:32:50 -04008308out:
8309 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00008310 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008311}
8312
Andy Adamson8d89bd72016-09-09 09:22:18 -04008313struct nfs41_exchange_id_data {
8314 struct nfs41_exchange_id_res res;
8315 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008316};
8317
Andy Adamson8d89bd72016-09-09 09:22:18 -04008318static void nfs4_exchange_id_release(void *data)
8319{
8320 struct nfs41_exchange_id_data *cdata =
8321 (struct nfs41_exchange_id_data *)data;
8322
Olga Kornievskaia63513232017-03-13 10:36:19 -04008323 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008324 kfree(cdata->res.impl_id);
8325 kfree(cdata->res.server_scope);
8326 kfree(cdata->res.server_owner);
8327 kfree(cdata);
8328}
8329
8330static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008331 .rpc_release = nfs4_exchange_id_release,
8332};
8333
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008334/*
8335 * _nfs4_proc_exchange_id()
8336 *
8337 * Wrapper for EXCHANGE_ID operation.
8338 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008339static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008340nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008341 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008342{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008343 struct rpc_message msg = {
8344 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008345 .rpc_cred = cred,
8346 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008347 struct rpc_task_setup task_setup_data = {
8348 .rpc_client = clp->cl_rpcclient,
8349 .callback_ops = &nfs4_exchange_id_call_ops,
8350 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008351 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008352 };
8353 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008354 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008355
Elena Reshetova212bf412017-10-20 12:53:38 +03008356 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008357 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008358
Trond Myklebust9c760d12017-07-31 18:38:50 -04008359 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008360 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008361 if (!calldata)
8362 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008363
Trond Myklebustfd405592017-08-01 16:02:47 -04008364 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008365
8366 status = nfs4_init_uniform_client_string(clp);
8367 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008368 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008369
Andy Adamson8d89bd72016-09-09 09:22:18 -04008370 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8371 GFP_NOFS);
8372 status = -ENOMEM;
8373 if (unlikely(calldata->res.server_owner == NULL))
8374 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008375
Andy Adamson8d89bd72016-09-09 09:22:18 -04008376 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008377 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008378 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008379 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008380
Andy Adamson8d89bd72016-09-09 09:22:18 -04008381 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8382 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008383 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008384
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008385 switch (sp4_how) {
8386 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008387 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008388 break;
8389
8390 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008391 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008392 break;
8393
8394 default:
8395 /* unsupported! */
8396 WARN_ON_ONCE(1);
8397 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008398 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008399 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008400 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008401 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008402 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008403 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8404 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008405 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008406 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008407 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8408 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008409#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008410 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008411#endif
8412 msg.rpc_argp = &calldata->args;
8413 msg.rpc_resp = &calldata->res;
8414 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008415
Trond Myklebust9c760d12017-07-31 18:38:50 -04008416 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008417
8418out_impl_id:
8419 kfree(calldata->res.impl_id);
8420out_server_scope:
8421 kfree(calldata->res.server_scope);
8422out_server_owner:
8423 kfree(calldata->res.server_owner);
8424out_calldata:
8425 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008426out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008427 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008428 return ERR_PTR(status);
8429}
8430
8431/*
8432 * _nfs4_proc_exchange_id()
8433 *
8434 * Wrapper for EXCHANGE_ID operation.
8435 */
NeilBrowna52458b2018-12-03 11:30:31 +11008436static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008437 u32 sp4_how)
8438{
8439 struct rpc_task *task;
8440 struct nfs41_exchange_id_args *argp;
8441 struct nfs41_exchange_id_res *resp;
Robert Milkowski7dc29932020-01-30 09:43:25 +00008442 unsigned long now = jiffies;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008443 int status;
8444
8445 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8446 if (IS_ERR(task))
8447 return PTR_ERR(task);
8448
8449 argp = task->tk_msg.rpc_argp;
8450 resp = task->tk_msg.rpc_resp;
8451 status = task->tk_status;
8452 if (status != 0)
8453 goto out;
8454
8455 status = nfs4_check_cl_exchange_flags(resp->flags);
8456 if (status != 0)
8457 goto out;
8458
8459 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8460 if (status != 0)
8461 goto out;
8462
Robert Milkowski7dc29932020-01-30 09:43:25 +00008463 do_renew_lease(clp, now);
8464
Trond Myklebust9c760d12017-07-31 18:38:50 -04008465 clp->cl_clientid = resp->clientid;
8466 clp->cl_exchange_flags = resp->flags;
8467 clp->cl_seqid = resp->seqid;
8468 /* Client ID is not confirmed */
8469 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8470 clear_bit(NFS4_SESSION_ESTABLISHED,
8471 &clp->cl_session->session_state);
8472
8473 if (clp->cl_serverscope != NULL &&
8474 !nfs41_same_server_scope(clp->cl_serverscope,
8475 resp->server_scope)) {
8476 dprintk("%s: server_scope mismatch detected\n",
8477 __func__);
8478 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8479 }
8480
8481 swap(clp->cl_serverowner, resp->server_owner);
8482 swap(clp->cl_serverscope, resp->server_scope);
8483 swap(clp->cl_implid, resp->impl_id);
8484
8485 /* Save the EXCHANGE_ID verifier session trunk tests */
8486 memcpy(clp->cl_confirm.data, argp->verifier.data,
8487 sizeof(clp->cl_confirm.data));
8488out:
8489 trace_nfs4_exchange_id(clp, status);
8490 rpc_put_task(task);
8491 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008492}
8493
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008494/*
8495 * nfs4_proc_exchange_id()
8496 *
8497 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8498 *
8499 * Since the clientid has expired, all compounds using sessions
8500 * associated with the stale clientid will be returning
8501 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8502 * be in some phase of session reset.
8503 *
8504 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8505 */
NeilBrowna52458b2018-12-03 11:30:31 +11008506int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008507{
8508 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8509 int status;
8510
8511 /* try SP4_MACH_CRED if krb5i/p */
8512 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8513 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008514 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008515 if (!status)
8516 return 0;
8517 }
8518
8519 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008520 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008521}
8522
Andy Adamson04fa2c62016-09-09 09:22:29 -04008523/**
8524 * nfs4_test_session_trunk
8525 *
8526 * This is an add_xprt_test() test function called from
8527 * rpc_clnt_setup_test_and_add_xprt.
8528 *
8529 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8530 * and is dereferrenced in nfs4_exchange_id_release
8531 *
8532 * Upon success, add the new transport to the rpc_clnt
8533 *
8534 * @clnt: struct rpc_clnt to get new transport
8535 * @xprt: the rpc_xprt to test
8536 * @data: call data for _nfs4_proc_exchange_id.
8537 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308538void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008539 void *data)
8540{
8541 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008542 struct rpc_task *task;
8543 int status;
8544
Andy Adamson04fa2c62016-09-09 09:22:29 -04008545 u32 sp4_how;
8546
8547 dprintk("--> %s try %s\n", __func__,
8548 xprt->address_strings[RPC_DISPLAY_ADDR]);
8549
8550 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8551
8552 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008553 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8554 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308555 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008556
8557 status = task->tk_status;
8558 if (status == 0)
8559 status = nfs4_detect_session_trunking(adata->clp,
8560 task->tk_msg.rpc_resp, xprt);
8561
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308562 if (status == 0)
8563 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8564
Trond Myklebust9c760d12017-07-31 18:38:50 -04008565 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008566}
8567EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8568
Trond Myklebust66245532012-05-25 17:18:09 -04008569static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008570 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008571{
8572 struct rpc_message msg = {
8573 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8574 .rpc_argp = clp,
8575 .rpc_cred = cred,
8576 };
8577 int status;
8578
NeilBrown5a0c2572019-05-30 10:41:28 +10008579 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8580 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008581 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008582 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008583 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008584 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8585 return status;
8586}
8587
8588static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008589 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008590{
8591 unsigned int loop;
8592 int ret;
8593
8594 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8595 ret = _nfs4_proc_destroy_clientid(clp, cred);
8596 switch (ret) {
8597 case -NFS4ERR_DELAY:
8598 case -NFS4ERR_CLIENTID_BUSY:
8599 ssleep(1);
8600 break;
8601 default:
8602 return ret;
8603 }
8604 }
8605 return 0;
8606}
8607
8608int nfs4_destroy_clientid(struct nfs_client *clp)
8609{
NeilBrowna52458b2018-12-03 11:30:31 +11008610 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008611 int ret = 0;
8612
8613 if (clp->cl_mvops->minor_version < 1)
8614 goto out;
8615 if (clp->cl_exchange_flags == 0)
8616 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008617 if (clp->cl_preserve_clid)
8618 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008619 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008620 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008621 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008622 switch (ret) {
8623 case 0:
8624 case -NFS4ERR_STALE_CLIENTID:
8625 clp->cl_exchange_flags = 0;
8626 }
8627out:
8628 return ret;
8629}
8630
Donald Buczek0efb01b2019-07-07 21:26:08 +02008631#endif /* CONFIG_NFS_V4_1 */
8632
Andy Adamson2050f0c2009-04-01 09:22:30 -04008633struct nfs4_get_lease_time_data {
8634 struct nfs4_get_lease_time_args *args;
8635 struct nfs4_get_lease_time_res *res;
8636 struct nfs_client *clp;
8637};
8638
8639static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8640 void *calldata)
8641{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008642 struct nfs4_get_lease_time_data *data =
8643 (struct nfs4_get_lease_time_data *)calldata;
8644
8645 dprintk("--> %s\n", __func__);
8646 /* just setup sequence, do not trigger session recovery
8647 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008648 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008649 &data->args->la_seq_args,
8650 &data->res->lr_seq_res,
8651 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008652 dprintk("<-- %s\n", __func__);
8653}
8654
8655/*
8656 * Called from nfs4_state_manager thread for session setup, so don't recover
8657 * from sequence operation or clientid errors.
8658 */
8659static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8660{
8661 struct nfs4_get_lease_time_data *data =
8662 (struct nfs4_get_lease_time_data *)calldata;
8663
8664 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008665 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008666 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008667 switch (task->tk_status) {
8668 case -NFS4ERR_DELAY:
8669 case -NFS4ERR_GRACE:
8670 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8671 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8672 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008673 /* fall through */
8674 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008675 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008676 return;
8677 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008678 dprintk("<-- %s\n", __func__);
8679}
8680
Trond Myklebust17280172012-03-11 13:11:00 -04008681static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008682 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8683 .rpc_call_done = nfs4_get_lease_time_done,
8684};
8685
8686int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8687{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008688 struct nfs4_get_lease_time_args args;
8689 struct nfs4_get_lease_time_res res = {
8690 .lr_fsinfo = fsinfo,
8691 };
8692 struct nfs4_get_lease_time_data data = {
8693 .args = &args,
8694 .res = &res,
8695 .clp = clp,
8696 };
8697 struct rpc_message msg = {
8698 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8699 .rpc_argp = &args,
8700 .rpc_resp = &res,
8701 };
8702 struct rpc_task_setup task_setup = {
8703 .rpc_client = clp->cl_rpcclient,
8704 .rpc_message = &msg,
8705 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008706 .callback_data = &data,
8707 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008708 };
Andy Adamson2050f0c2009-04-01 09:22:30 -04008709
Anna Schumakerfba83f32018-05-04 16:22:50 -04008710 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Anna Schumakerf836b272019-08-19 10:18:28 -04008711 return nfs4_call_sync_custom(&task_setup);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008712}
8713
Donald Buczek0efb01b2019-07-07 21:26:08 +02008714#ifdef CONFIG_NFS_V4_1
8715
Andy Adamsonfc931582009-04-01 09:22:31 -04008716/*
8717 * Initialize the values to be used by the client in CREATE_SESSION
8718 * If nfs4_init_session set the fore channel request and response sizes,
8719 * use them.
8720 *
8721 * Set the back channel max_resp_sz_cached to zero to force the client to
8722 * always set csa_cachethis to FALSE because the current implementation
8723 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8724 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008725static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8726 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008727{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008728 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008729 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008730 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008731
Andy Adamson18aad3d2013-06-26 12:21:49 -04008732 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8733 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8734
Andy Adamsonfc931582009-04-01 09:22:31 -04008735 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008736 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8737 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008738 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008739 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008740
8741 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008742 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008743 __func__,
8744 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008745 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008746
8747 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008748 args->bc_attrs.max_rqst_sz = max_bc_payload;
8749 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008750 args->bc_attrs.max_resp_sz_cached = 0;
8751 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008752 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008753 if (args->bc_attrs.max_reqs > max_bc_slots)
8754 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008755
8756 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8757 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8758 __func__,
8759 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8760 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8761 args->bc_attrs.max_reqs);
8762}
8763
Trond Myklebust79969dd2015-02-18 11:30:18 -08008764static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8765 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008766{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008767 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008768 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008769
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008770 if (rcvd->max_resp_sz > sent->max_resp_sz)
8771 return -EINVAL;
8772 /*
8773 * Our requested max_ops is the minimum we need; we're not
8774 * prepared to break up compounds into smaller pieces than that.
8775 * So, no point even trying to continue if the server won't
8776 * cooperate:
8777 */
8778 if (rcvd->max_ops < sent->max_ops)
8779 return -EINVAL;
8780 if (rcvd->max_reqs == 0)
8781 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008782 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8783 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008784 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008785}
8786
Trond Myklebust79969dd2015-02-18 11:30:18 -08008787static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8788 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008789{
8790 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008791 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008792
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008793 if (!(res->flags & SESSION4_BACK_CHAN))
8794 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008795 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8796 return -EINVAL;
8797 if (rcvd->max_resp_sz < sent->max_resp_sz)
8798 return -EINVAL;
8799 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8800 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008801 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008802 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008803 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008804 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008805out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008806 return 0;
8807}
Andy Adamson8d353012009-04-01 09:22:32 -04008808
Andy Adamson8d353012009-04-01 09:22:32 -04008809static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008810 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008811{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008812 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008813
Trond Myklebust79969dd2015-02-18 11:30:18 -08008814 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008815 if (ret)
8816 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008817 return nfs4_verify_back_channel_attrs(args, res);
8818}
8819
8820static void nfs4_update_session(struct nfs4_session *session,
8821 struct nfs41_create_session_res *res)
8822{
8823 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008824 /* Mark client id and session as being confirmed */
8825 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8826 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008827 session->flags = res->flags;
8828 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008829 if (res->flags & SESSION4_BACK_CHAN)
8830 memcpy(&session->bc_attrs, &res->bc_attrs,
8831 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008832}
8833
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008834static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008835 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008836{
8837 struct nfs4_session *session = clp->cl_session;
8838 struct nfs41_create_session_args args = {
8839 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008840 .clientid = clp->cl_clientid,
8841 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008842 .cb_program = NFS4_CALLBACK,
8843 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008844 struct nfs41_create_session_res res;
8845
Andy Adamsonfc931582009-04-01 09:22:31 -04008846 struct rpc_message msg = {
8847 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8848 .rpc_argp = &args,
8849 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008850 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008851 };
8852 int status;
8853
Chuck Lever6b26cc82016-05-02 14:40:40 -04008854 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008855 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008856
NeilBrown5a0c2572019-05-30 10:41:28 +10008857 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8858 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008859 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008860
Trond Myklebustb519d402016-09-11 14:50:01 -04008861 switch (status) {
8862 case -NFS4ERR_STALE_CLIENTID:
8863 case -NFS4ERR_DELAY:
8864 case -ETIMEDOUT:
8865 case -EACCES:
8866 case -EAGAIN:
8867 goto out;
zhengbin8b98a532019-12-19 18:34:47 +08008868 }
Trond Myklebustb519d402016-09-11 14:50:01 -04008869
8870 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008871 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008872 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008873 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008874 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008875 if (status)
8876 goto out;
8877 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008878 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008879out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008880 return status;
8881}
8882
8883/*
8884 * Issues a CREATE_SESSION operation to the server.
8885 * It is the responsibility of the caller to verify the session is
8886 * expired before calling this routine.
8887 */
NeilBrowna52458b2018-12-03 11:30:31 +11008888int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008889{
8890 int status;
8891 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008892 struct nfs4_session *session = clp->cl_session;
8893
8894 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8895
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008896 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008897 if (status)
8898 goto out;
8899
Andy Adamsonaacd5532011-11-09 13:58:21 -05008900 /* Init or reset the session slot tables */
8901 status = nfs4_setup_session_slot_tables(session);
8902 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008903 if (status)
8904 goto out;
8905
8906 ptr = (unsigned *)&session->sess_id.data[0];
8907 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8908 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008909out:
8910 dprintk("<-- %s\n", __func__);
8911 return status;
8912}
8913
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008914/*
8915 * Issue the over-the-wire RPC DESTROY_SESSION.
8916 * The caller must serialize access to this routine.
8917 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008918int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008919 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008920{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008921 struct rpc_message msg = {
8922 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8923 .rpc_argp = session,
8924 .rpc_cred = cred,
8925 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008926 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008927
8928 dprintk("--> nfs4_proc_destroy_session\n");
8929
8930 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008931 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8932 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008933
NeilBrown5a0c2572019-05-30 10:41:28 +10008934 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8935 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008936 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008937
8938 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008939 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008940 "Session has been destroyed regardless...\n", status);
8941
8942 dprintk("<-- nfs4_proc_destroy_session\n");
8943 return status;
8944}
8945
Trond Myklebust7b38c362012-05-23 13:23:31 -04008946/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008947 * Renew the cl_session lease.
8948 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008949struct nfs4_sequence_data {
8950 struct nfs_client *clp;
8951 struct nfs4_sequence_args args;
8952 struct nfs4_sequence_res res;
8953};
8954
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008955static void nfs41_sequence_release(void *data)
8956{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008957 struct nfs4_sequence_data *calldata = data;
8958 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008959
Elena Reshetova212bf412017-10-20 12:53:38 +03008960 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008961 nfs4_schedule_state_renewal(clp);
8962 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008963 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008964}
8965
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008966static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8967{
8968 switch(task->tk_status) {
8969 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008970 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8971 return -EAGAIN;
8972 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008973 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008974 }
8975 return 0;
8976}
8977
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008978static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008979{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008980 struct nfs4_sequence_data *calldata = data;
8981 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008982
Trond Myklebust14516c32010-07-31 14:29:06 -04008983 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8984 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008985
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008986 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008987 if (task->tk_status < 0) {
8988 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008989 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008990 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008991
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008992 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8993 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008994 return;
8995 }
8996 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008997 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008998out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008999 dprintk("<-- %s\n", __func__);
9000}
9001
9002static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
9003{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009004 struct nfs4_sequence_data *calldata = data;
9005 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009006 struct nfs4_sequence_args *args;
9007 struct nfs4_sequence_res *res;
9008
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009009 args = task->tk_msg.rpc_argp;
9010 res = task->tk_msg.rpc_resp;
9011
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009012 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009013}
9014
9015static const struct rpc_call_ops nfs41_sequence_ops = {
9016 .rpc_call_done = nfs41_sequence_call_done,
9017 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08009018 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009019};
9020
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009021static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009022 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009023 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009024 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009025{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009026 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009027 struct rpc_message msg = {
9028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
9029 .rpc_cred = cred,
9030 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009031 struct rpc_task_setup task_setup_data = {
9032 .rpc_client = clp->cl_rpcclient,
9033 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009034 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04009035 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009036 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009037 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009038
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009039 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03009040 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009041 goto out_err;
9042
9043 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04009044 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009045 if (calldata == NULL)
9046 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009047 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009048 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04009049 msg.rpc_argp = &calldata->args;
9050 msg.rpc_resp = &calldata->res;
9051 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009052 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009053
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009054 ret = rpc_run_task(&task_setup_data);
9055 if (IS_ERR(ret))
9056 goto out_err;
9057 return ret;
9058out_put_clp:
9059 nfs_put_client(clp);
9060out_err:
9061 nfs41_release_slot(slot);
9062 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009063}
9064
NeilBrowna52458b2018-12-03 11:30:31 +11009065static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009066{
9067 struct rpc_task *task;
9068 int ret = 0;
9069
Trond Myklebust2f60ea62011-08-24 15:07:37 -04009070 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04009071 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009072 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009073 if (IS_ERR(task))
9074 ret = PTR_ERR(task);
9075 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08009076 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009077 dprintk("<-- %s status=%d\n", __func__, ret);
9078 return ret;
9079}
9080
NeilBrowna52458b2018-12-03 11:30:31 +11009081static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009082{
9083 struct rpc_task *task;
9084 int ret;
9085
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04009086 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009087 if (IS_ERR(task)) {
9088 ret = PTR_ERR(task);
9089 goto out;
9090 }
9091 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04009092 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04009093 ret = task->tk_status;
9094 rpc_put_task(task);
9095out:
9096 dprintk("<-- %s status=%d\n", __func__, ret);
9097 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04009098}
9099
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009100struct nfs4_reclaim_complete_data {
9101 struct nfs_client *clp;
9102 struct nfs41_reclaim_complete_args arg;
9103 struct nfs41_reclaim_complete_res res;
9104};
9105
9106static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
9107{
9108 struct nfs4_reclaim_complete_data *calldata = data;
9109
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009110 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009111 &calldata->arg.seq_args,
9112 &calldata->res.seq_res,
9113 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009114}
9115
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009116static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
9117{
9118 switch(task->tk_status) {
9119 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04009120 wake_up_all(&clp->cl_lock_waitq);
9121 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009122 case -NFS4ERR_COMPLETE_ALREADY:
9123 case -NFS4ERR_WRONG_CRED: /* What to do here? */
9124 break;
9125 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009126 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04009127 /* fall through */
9128 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009129 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009130 case -NFS4ERR_BADSESSION:
9131 case -NFS4ERR_DEADSESSION:
9132 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
Trond Myklebust0048fdd2017-05-04 13:44:04 -04009133 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009134 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05009135 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009136 }
9137 return 0;
9138}
9139
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009140static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
9141{
9142 struct nfs4_reclaim_complete_data *calldata = data;
9143 struct nfs_client *clp = calldata->clp;
9144 struct nfs4_sequence_res *res = &calldata->res.seq_res;
9145
9146 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04009147 if (!nfs41_sequence_done(task, res))
9148 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009149
Trond Myklebustc6d01c62013-08-09 11:51:26 -04009150 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04009151 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
9152 rpc_restart_call_prepare(task);
9153 return;
9154 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009155 dprintk("<-- %s\n", __func__);
9156}
9157
9158static void nfs4_free_reclaim_complete_data(void *data)
9159{
9160 struct nfs4_reclaim_complete_data *calldata = data;
9161
9162 kfree(calldata);
9163}
9164
9165static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
9166 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
9167 .rpc_call_done = nfs4_reclaim_complete_done,
9168 .rpc_release = nfs4_free_reclaim_complete_data,
9169};
9170
9171/*
9172 * Issue a global reclaim complete.
9173 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04009174static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11009175 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009176{
9177 struct nfs4_reclaim_complete_data *calldata;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009178 struct rpc_message msg = {
9179 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04009180 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009181 };
9182 struct rpc_task_setup task_setup_data = {
9183 .rpc_client = clp->cl_rpcclient,
9184 .rpc_message = &msg,
9185 .callback_ops = &nfs4_reclaim_complete_call_ops,
Anna Schumaker4c952e32019-08-14 15:46:48 -04009186 .flags = RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009187 };
9188 int status = -ENOMEM;
9189
9190 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04009191 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009192 if (calldata == NULL)
9193 goto out;
9194 calldata->clp = clp;
9195 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009196
Anna Schumakerfba83f32018-05-04 16:22:50 -04009197 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009198 msg.rpc_argp = &calldata->arg;
9199 msg.rpc_resp = &calldata->res;
9200 task_setup_data.callback_data = calldata;
Anna Schumaker4c952e32019-08-14 15:46:48 -04009201 status = nfs4_call_sync_custom(&task_setup_data);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009202out:
9203 dprintk("<-- %s status=%d\n", __func__, status);
9204 return status;
9205}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009206
9207static void
9208nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
9209{
9210 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00009211 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009212
9213 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009214 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04009215 &lgp->res.seq_res, task);
9216 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009217}
9218
9219static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
9220{
9221 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04009222
9223 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009224 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04009225 dprintk("<-- %s\n", __func__);
9226}
9227
9228static int
9229nfs4_layoutget_handle_exception(struct rpc_task *task,
9230 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
9231{
Trond Myklebustee314c22012-10-01 17:25:48 -07009232 struct inode *inode = lgp->args.inode;
9233 struct nfs_server *server = NFS_SERVER(inode);
9234 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009235 int nfs4err = task->tk_status;
9236 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009237 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009238
Boaz Harroshed7e5422014-01-22 20:34:54 +02009239 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009240
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009241 nfs4_sequence_free_slot(&lgp->res.seq_res);
9242
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009243 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009244 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07009245 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009246
9247 /*
9248 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
9249 * on the file. set tk_status to -ENODATA to tell upper layer to
9250 * retry go inband.
9251 */
9252 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04009253 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08009254 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009255 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07009256 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
9257 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
9258 */
9259 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009260 status = -EOVERFLOW;
9261 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07009262 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02009263 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07009264 * (or clients) writing to the same RAID stripe except when
9265 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04009266 *
9267 * Treat it like we would RECALLCONFLICT -- we retry for a little
9268 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02009269 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009270 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04009271 if (lgp->args.minlength == 0) {
9272 status = -EOVERFLOW;
9273 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02009274 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009275 status = -EBUSY;
9276 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04009277 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04009278 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009279 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04009280 case -NFS4ERR_DELEG_REVOKED:
9281 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07009282 case -NFS4ERR_EXPIRED:
9283 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04009284 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07009285 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009286 lo = NFS_I(inode)->layout;
9287 /* If the open stateid was bad, then recover it. */
9288 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04009289 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07009290 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04009291 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04009292 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04009293 break;
9294 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009295
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009296 /*
9297 * Mark the bad layout state as invalid, then retry
9298 */
Trond Myklebust668f4552016-07-24 17:08:59 -04009299 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009300 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04009301 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04009302 pnfs_free_lseg_list(&head);
9303 status = -EAGAIN;
9304 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009305 }
Jeff Layton183d9e72016-05-17 12:28:47 -04009306
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04009307 err = nfs4_handle_exception(server, nfs4err, exception);
9308 if (!status) {
9309 if (exception->retry)
9310 status = -EAGAIN;
9311 else
9312 status = err;
9313 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009314out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009315 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009316 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009317}
9318
Fred Isamandacb4522016-09-19 17:47:09 -04009319size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009320{
9321 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9322 return nfs_page_array_len(0, max_resp_sz);
9323}
9324
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009325static void nfs4_layoutget_release(void *calldata)
9326{
9327 struct nfs4_layoutget *lgp = calldata;
9328
9329 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009330 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009331 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009332 dprintk("<-- %s\n", __func__);
9333}
9334
9335static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9336 .rpc_call_prepare = nfs4_layoutget_prepare,
9337 .rpc_call_done = nfs4_layoutget_done,
9338 .rpc_release = nfs4_layoutget_release,
9339};
9340
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009341struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009342nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009343{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009344 struct inode *inode = lgp->args.inode;
9345 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009346 struct rpc_task *task;
9347 struct rpc_message msg = {
9348 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9349 .rpc_argp = &lgp->args,
9350 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009351 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009352 };
9353 struct rpc_task_setup task_setup_data = {
9354 .rpc_client = server->client,
9355 .rpc_message = &msg,
9356 .callback_ops = &nfs4_layoutget_call_ops,
9357 .callback_data = lgp,
Trond Myklebust63ec2b62020-02-07 19:40:14 -05009358 .flags = RPC_TASK_ASYNC | RPC_TASK_CRED_NOREF,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009359 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009360 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009361 struct nfs4_exception exception = {
9362 .inode = inode,
9363 .timeout = *timeout,
9364 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009365 int status = 0;
9366
9367 dprintk("--> %s\n", __func__);
9368
Peng Tao4bd5a982014-11-17 11:05:17 +08009369 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9370 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9371
Anna Schumakerfba83f32018-05-04 16:22:50 -04009372 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009373
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009374 task = rpc_run_task(&task_setup_data);
Xiyu Yang6e476662020-04-25 21:04:40 +08009375
Anna Schumaker820bf852017-01-11 15:01:43 -05009376 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009377 if (status != 0)
9378 goto out;
9379
Trond Myklebust18c07782019-02-13 07:55:31 -05009380 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009381 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9382 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009383 } else if (lgp->res.layoutp->len == 0) {
9384 status = -EAGAIN;
9385 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009386 } else
9387 lseg = pnfs_layout_process(lgp);
9388out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009389 trace_nfs4_layoutget(lgp->args.ctx,
9390 &lgp->args.range,
9391 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009392 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009393 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009394
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009395 rpc_put_task(task);
9396 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009397 if (status)
9398 return ERR_PTR(status);
9399 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009400}
9401
Benny Halevycbe82602011-05-22 19:52:37 +03009402static void
9403nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9404{
9405 struct nfs4_layoutreturn *lrp = calldata;
9406
9407 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009408 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009409 &lrp->args.seq_args,
9410 &lrp->res.seq_res,
9411 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009412 if (!pnfs_layout_is_valid(lrp->args.layout))
9413 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009414}
9415
9416static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9417{
9418 struct nfs4_layoutreturn *lrp = calldata;
9419 struct nfs_server *server;
9420
9421 dprintk("--> %s\n", __func__);
9422
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009423 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009424 return;
9425
Trond Myklebust6109bcf2019-09-20 07:23:43 -04009426 /*
9427 * Was there an RPC level error? Assume the call succeeded,
9428 * and that we need to release the layout
9429 */
9430 if (task->tk_rpc_status != 0 && RPC_WAS_SENT(task)) {
9431 lrp->res.lrs_present = 0;
9432 return;
9433 }
9434
Benny Halevycbe82602011-05-22 19:52:37 +03009435 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009436 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009437 case -NFS4ERR_OLD_STATEID:
Trond Myklebust30cb3ee2019-09-20 07:23:45 -04009438 if (nfs4_layout_refresh_old_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009439 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009440 lrp->args.inode))
9441 goto out_restart;
9442 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009443 default:
9444 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009445 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009446 case 0:
9447 break;
9448 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009449 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009450 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009451 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009452 }
Benny Halevycbe82602011-05-22 19:52:37 +03009453 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009454 return;
9455out_restart:
9456 task->tk_status = 0;
9457 nfs4_sequence_free_slot(&lrp->res.seq_res);
9458 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009459}
9460
9461static void nfs4_layoutreturn_release(void *calldata)
9462{
9463 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009464 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009465
9466 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009467 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009468 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009469 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009470 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9471 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009472 pnfs_put_layout_hdr(lrp->args.layout);
9473 nfs_iput_and_deactive(lrp->inode);
Trond Myklebust44ea8df2020-04-02 15:37:02 -04009474 put_cred(lrp->cred);
Benny Halevycbe82602011-05-22 19:52:37 +03009475 kfree(calldata);
9476 dprintk("<-- %s\n", __func__);
9477}
9478
9479static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9480 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9481 .rpc_call_done = nfs4_layoutreturn_done,
9482 .rpc_release = nfs4_layoutreturn_release,
9483};
9484
Peng Tao6c166052014-11-17 09:30:40 +08009485int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009486{
9487 struct rpc_task *task;
9488 struct rpc_message msg = {
9489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9490 .rpc_argp = &lrp->args,
9491 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009492 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009493 };
9494 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009495 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009496 .rpc_message = &msg,
9497 .callback_ops = &nfs4_layoutreturn_call_ops,
9498 .callback_data = lrp,
9499 };
Peng Tao6c166052014-11-17 09:30:40 +08009500 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009501
Andrew Elble99ade3c2015-12-02 09:39:51 -05009502 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9503 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9504 &task_setup_data.rpc_client, &msg);
9505
Benny Halevycbe82602011-05-22 19:52:37 +03009506 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009507 if (!sync) {
9508 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9509 if (!lrp->inode) {
9510 nfs4_layoutreturn_release(lrp);
9511 return -EAGAIN;
9512 }
9513 task_setup_data.flags |= RPC_TASK_ASYNC;
9514 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009515 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009516 task = rpc_run_task(&task_setup_data);
9517 if (IS_ERR(task))
9518 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009519 if (sync)
9520 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009521 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009522 dprintk("<-- %s status=%d\n", __func__, status);
9523 rpc_put_task(task);
9524 return status;
9525}
9526
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009527static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009528_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9529 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009530 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009531{
9532 struct nfs4_getdeviceinfo_args args = {
9533 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009534 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9535 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009536 };
9537 struct nfs4_getdeviceinfo_res res = {
9538 .pdev = pdev,
9539 };
9540 struct rpc_message msg = {
9541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9542 .rpc_argp = &args,
9543 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009544 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009545 };
9546 int status;
9547
9548 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009549 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009550 if (res.notification & ~args.notify_types)
9551 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009552 if (res.notification != args.notify_types)
9553 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009554
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009555 dprintk("<-- %s status=%d\n", __func__, status);
9556
9557 return status;
9558}
9559
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009560int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9561 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009562 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009563{
9564 struct nfs4_exception exception = { };
9565 int err;
9566
9567 do {
9568 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009569 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009570 &exception);
9571 } while (exception.retry);
9572 return err;
9573}
9574EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9575
Andy Adamson863a3c62011-03-23 13:27:54 +00009576static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9577{
9578 struct nfs4_layoutcommit_data *data = calldata;
9579 struct nfs_server *server = NFS_SERVER(data->args.inode);
9580
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009581 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009582 &data->args.seq_args,
9583 &data->res.seq_res,
9584 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009585}
9586
9587static void
9588nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9589{
9590 struct nfs4_layoutcommit_data *data = calldata;
9591 struct nfs_server *server = NFS_SERVER(data->args.inode);
9592
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009593 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009594 return;
9595
9596 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009597 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9598 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9599 case -NFS4ERR_BADLAYOUT: /* no layout */
9600 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009601 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009602 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009603 break;
9604 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009605 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009606 rpc_restart_call_prepare(task);
9607 return;
9608 }
9609 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009610}
9611
9612static void nfs4_layoutcommit_release(void *calldata)
9613{
9614 struct nfs4_layoutcommit_data *data = calldata;
9615
Andy Adamsondb29c082011-07-30 20:52:38 -04009616 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009617 nfs_post_op_update_inode_force_wcc(data->args.inode,
9618 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009619 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009620 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009621 kfree(data);
9622}
9623
9624static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9625 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9626 .rpc_call_done = nfs4_layoutcommit_done,
9627 .rpc_release = nfs4_layoutcommit_release,
9628};
9629
9630int
Andy Adamsonef311532011-03-12 02:58:10 -05009631nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009632{
9633 struct rpc_message msg = {
9634 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9635 .rpc_argp = &data->args,
9636 .rpc_resp = &data->res,
9637 .rpc_cred = data->cred,
9638 };
9639 struct rpc_task_setup task_setup_data = {
9640 .task = &data->task,
9641 .rpc_client = NFS_CLIENT(data->args.inode),
9642 .rpc_message = &msg,
9643 .callback_ops = &nfs4_layoutcommit_ops,
9644 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009645 };
9646 struct rpc_task *task;
9647 int status = 0;
9648
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009649 dprintk("NFS: initiating layoutcommit call. sync %d "
9650 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009651 data->args.lastbytewritten,
9652 data->args.inode->i_ino);
9653
Trond Myklebust472e2592015-02-05 16:50:30 -05009654 if (!sync) {
9655 data->inode = nfs_igrab_and_active(data->args.inode);
9656 if (data->inode == NULL) {
9657 nfs4_layoutcommit_release(data);
9658 return -EAGAIN;
9659 }
9660 task_setup_data.flags = RPC_TASK_ASYNC;
9661 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009662 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009663 task = rpc_run_task(&task_setup_data);
9664 if (IS_ERR(task))
9665 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009666 if (sync)
9667 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009668 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009669 dprintk("%s: status %d\n", __func__, status);
9670 rpc_put_task(task);
9671 return status;
9672}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009673
Trond Myklebust302fad72019-02-18 13:32:38 -05009674/*
Andy Adamson97431202013-08-08 10:57:56 -04009675 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9676 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9677 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009678static int
9679_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009680 struct nfs_fsinfo *info,
9681 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009682{
9683 struct nfs41_secinfo_no_name_args args = {
9684 .style = SECINFO_STYLE_CURRENT_FH,
9685 };
9686 struct nfs4_secinfo_res res = {
9687 .flavors = flavors,
9688 };
9689 struct rpc_message msg = {
9690 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9691 .rpc_argp = &args,
9692 .rpc_resp = &res,
9693 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009694 struct rpc_clnt *clnt = server->client;
Anna Schumakercc15e242019-08-14 16:22:31 -04009695 struct nfs4_call_sync_data data = {
9696 .seq_server = server,
9697 .seq_args = &args.seq_args,
9698 .seq_res = &res.seq_res,
9699 };
9700 struct rpc_task_setup task_setup = {
9701 .rpc_client = server->client,
9702 .rpc_message = &msg,
9703 .callback_ops = server->nfs_client->cl_mvops->call_sync_ops,
9704 .callback_data = &data,
9705 .flags = RPC_TASK_NO_ROUND_ROBIN,
9706 };
NeilBrowna52458b2018-12-03 11:30:31 +11009707 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009708 int status;
9709
9710 if (use_integrity) {
9711 clnt = server->nfs_client->cl_rpcclient;
Anna Schumakercc15e242019-08-14 16:22:31 -04009712 task_setup.rpc_client = clnt;
9713
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009714 cred = nfs4_get_clid_cred(server->nfs_client);
9715 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009716 }
9717
9718 dprintk("--> %s\n", __func__);
Anna Schumakercc15e242019-08-14 16:22:31 -04009719 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
9720 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009721 dprintk("<-- %s status=%d\n", __func__, status);
9722
NeilBrowna52458b2018-12-03 11:30:31 +11009723 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009724
9725 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009726}
9727
9728static int
9729nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9730 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9731{
Trond Myklebust0688e642019-04-07 13:59:09 -04009732 struct nfs4_exception exception = {
9733 .interruptible = true,
9734 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009735 int err;
9736 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009737 /* first try using integrity protection */
9738 err = -NFS4ERR_WRONGSEC;
9739
9740 /* try to use integrity protection with machine cred */
9741 if (_nfs4_is_integrity_protected(server->nfs_client))
9742 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9743 flavors, true);
9744
9745 /*
9746 * if unable to use integrity protection, or SECINFO with
9747 * integrity protection returns NFS4ERR_WRONGSEC (which is
9748 * disallowed by spec, but exists in deployed servers) use
9749 * the current filesystem's rpc_client and the user cred.
9750 */
9751 if (err == -NFS4ERR_WRONGSEC)
9752 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9753 flavors, false);
9754
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009755 switch (err) {
9756 case 0:
9757 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009758 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009759 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009760 default:
9761 err = nfs4_handle_exception(server, err, &exception);
9762 }
9763 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009764out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009765 return err;
9766}
9767
9768static int
9769nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9770 struct nfs_fsinfo *info)
9771{
9772 int err;
9773 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009774 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009775 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009776 struct nfs4_secinfo4 *secinfo;
9777 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009778
9779 page = alloc_page(GFP_KERNEL);
9780 if (!page) {
9781 err = -ENOMEM;
9782 goto out;
9783 }
9784
9785 flavors = page_address(page);
9786 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9787
9788 /*
9789 * Fall back on "guess and check" method if
9790 * the server doesn't support SECINFO_NO_NAME
9791 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009792 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009793 err = nfs4_find_root_sec(server, fhandle, info);
9794 goto out_freepage;
9795 }
9796 if (err)
9797 goto out_freepage;
9798
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009799 for (i = 0; i < flavors->num_flavors; i++) {
9800 secinfo = &flavors->flavors[i];
9801
9802 switch (secinfo->flavor) {
9803 case RPC_AUTH_NULL:
9804 case RPC_AUTH_UNIX:
9805 case RPC_AUTH_GSS:
9806 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9807 &secinfo->flavor_info);
9808 break;
9809 default:
9810 flavor = RPC_AUTH_MAXFLAVOR;
9811 break;
9812 }
9813
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009814 if (!nfs_auth_info_match(&server->auth_info, flavor))
9815 flavor = RPC_AUTH_MAXFLAVOR;
9816
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009817 if (flavor != RPC_AUTH_MAXFLAVOR) {
9818 err = nfs4_lookup_root_sec(server, fhandle,
9819 info, flavor);
9820 if (!err)
9821 break;
9822 }
9823 }
9824
9825 if (flavor == RPC_AUTH_MAXFLAVOR)
9826 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009827
9828out_freepage:
9829 put_page(page);
9830 if (err == -EACCES)
9831 return -EPERM;
9832out:
9833 return err;
9834}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009835
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009836static int _nfs41_test_stateid(struct nfs_server *server,
9837 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009838 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009839{
9840 int status;
9841 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009842 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009843 };
9844 struct nfs41_test_stateid_res res;
9845 struct rpc_message msg = {
9846 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9847 .rpc_argp = &args,
9848 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009849 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009850 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009851 struct rpc_clnt *rpc_client = server->client;
9852
9853 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9854 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009855
Chuck Lever38527b12012-07-11 16:30:23 -04009856 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009857 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009858 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009859 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009860 if (status != NFS_OK) {
9861 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009862 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009863 }
9864 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009865 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009866}
9867
Trond Myklebust43912bb2016-09-22 13:38:56 -04009868static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9869 int err, struct nfs4_exception *exception)
9870{
9871 exception->retry = 0;
9872 switch(err) {
9873 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009874 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009875 nfs4_handle_exception(server, err, exception);
9876 break;
9877 case -NFS4ERR_BADSESSION:
9878 case -NFS4ERR_BADSLOT:
9879 case -NFS4ERR_BAD_HIGH_SLOT:
9880 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9881 case -NFS4ERR_DEADSESSION:
9882 nfs4_do_handle_exception(server, err, exception);
9883 }
9884}
9885
Chuck Lever38527b12012-07-11 16:30:23 -04009886/**
9887 * nfs41_test_stateid - perform a TEST_STATEID operation
9888 *
9889 * @server: server / transport on which to perform the operation
9890 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009891 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009892 *
9893 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9894 * Otherwise a negative NFS4ERR value is returned if the operation
9895 * failed or the state ID is not currently valid.
9896 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009897static int nfs41_test_stateid(struct nfs_server *server,
9898 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009899 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009900{
Trond Myklebust0688e642019-04-07 13:59:09 -04009901 struct nfs4_exception exception = {
9902 .interruptible = true,
9903 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009904 int err;
9905 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009906 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009907 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009908 } while (exception.retry);
9909 return err;
9910}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009911
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009912struct nfs_free_stateid_data {
9913 struct nfs_server *server;
9914 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009915 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009916};
9917
9918static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9919{
9920 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009921 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009922 &data->args.seq_args,
9923 &data->res.seq_res,
9924 task);
9925}
9926
9927static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9928{
9929 struct nfs_free_stateid_data *data = calldata;
9930
9931 nfs41_sequence_done(task, &data->res.seq_res);
9932
9933 switch (task->tk_status) {
9934 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009935 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009936 rpc_restart_call_prepare(task);
9937 }
9938}
9939
9940static void nfs41_free_stateid_release(void *calldata)
9941{
9942 kfree(calldata);
9943}
9944
Trond Myklebust17f26b12013-08-21 15:48:42 -04009945static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009946 .rpc_call_prepare = nfs41_free_stateid_prepare,
9947 .rpc_call_done = nfs41_free_stateid_done,
9948 .rpc_release = nfs41_free_stateid_release,
9949};
9950
Anna Schumaker2f261022018-05-15 13:03:39 -04009951/**
9952 * nfs41_free_stateid - perform a FREE_STATEID operation
9953 *
9954 * @server: server / transport on which to perform the operation
9955 * @stateid: state ID to release
9956 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009957 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009958 *
9959 * Note: this function is always asynchronous.
9960 */
9961static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009962 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009963 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009964 bool privileged)
9965{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009966 struct rpc_message msg = {
9967 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009968 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009969 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009970 struct rpc_task_setup task_setup = {
9971 .rpc_client = server->client,
9972 .rpc_message = &msg,
9973 .callback_ops = &nfs41_free_stateid_ops,
9974 .flags = RPC_TASK_ASYNC,
9975 };
9976 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009977 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009978
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009979 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9980 &task_setup.rpc_client, &msg);
9981
Chuck Lever38527b12012-07-11 16:30:23 -04009982 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009983 data = kmalloc(sizeof(*data), GFP_NOFS);
9984 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009985 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009986 data->server = server;
9987 nfs4_stateid_copy(&data->args.stateid, stateid);
9988
9989 task_setup.callback_data = data;
9990
9991 msg.rpc_argp = &data->args;
9992 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009993 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009994 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009995 if (IS_ERR(task))
9996 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009997 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009998 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009999}
Trond Myklebust36281ca2012-03-04 18:13:56 -050010000
Jeff Laytonf1cdae82014-05-01 06:28:47 -040010001static void
10002nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010003{
NeilBrowna52458b2018-12-03 11:30:31 +110010004 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010005
Trond Myklebustf0b0bf82016-09-22 13:39:04 -040010006 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010007 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010008}
10009
Trond Myklebust36281ca2012-03-04 18:13:56 -050010010static bool nfs41_match_stateid(const nfs4_stateid *s1,
10011 const nfs4_stateid *s2)
10012{
Trond Myklebust93b717f2016-05-16 17:42:43 -040010013 if (s1->type != s2->type)
10014 return false;
10015
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010016 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010017 return false;
10018
Trond Myklebust2d2f24a2012-03-04 18:13:57 -050010019 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -050010020 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010021
Anna Schumaker045c5512017-01-11 16:59:48 -050010022 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -050010023}
10024
Andy Adamson557134a2009-04-01 09:21:53 -040010025#endif /* CONFIG_NFS_V4_1 */
10026
Trond Myklebust36281ca2012-03-04 18:13:56 -050010027static bool nfs4_match_stateid(const nfs4_stateid *s1,
10028 const nfs4_stateid *s2)
10029{
Trond Myklebustf597c532012-03-04 18:13:56 -050010030 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -050010031}
10032
10033
Trond Myklebust17280172012-03-11 13:11:00 -040010034static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010035 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010036 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010037 .recover_open = nfs4_open_reclaim,
10038 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -040010039 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -040010040 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010041};
10042
Andy Adamson591d71c2009-04-01 09:22:47 -040010043#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010044static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010045 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
10046 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
10047 .recover_open = nfs4_open_reclaim,
10048 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -050010049 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -050010050 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -040010051 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -040010052};
10053#endif /* CONFIG_NFS_V4_1 */
10054
Trond Myklebust17280172012-03-11 13:11:00 -040010055static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -050010056 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -050010057 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +030010058 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010059 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -040010060 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010061};
10062
Andy Adamson591d71c2009-04-01 09:22:47 -040010063#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010064static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -040010065 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
10066 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -040010067 .recover_open = nfs41_open_expired,
10068 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -050010069 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -040010070};
10071#endif /* CONFIG_NFS_V4_1 */
10072
Trond Myklebust17280172012-03-11 13:11:00 -040010073static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010074 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +110010075 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010076 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -040010077};
10078
10079#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -040010080static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -040010081 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +110010082 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -040010083 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -040010084};
10085#endif
10086
Chuck Leverec011fe2013-10-17 14:12:39 -040010087static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010088 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010089 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010090};
10091
10092#if defined(CONFIG_NFS_V4_1)
10093static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -040010094 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -040010095 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -040010096};
10097#endif /* CONFIG_NFS_V4_1 */
10098
Trond Myklebust97dc1352010-06-16 09:52:26 -040010099static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
10100 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010101 .init_caps = NFS_CAP_READDIRPLUS
10102 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010103 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010104 .init_client = nfs40_init_client,
10105 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010106 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010107 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010108 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -040010109 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010110 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -040010111 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010112 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
10113 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
10114 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010115 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010116};
10117
10118#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -050010119static struct nfs_seqid *
10120nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
10121{
10122 return NULL;
10123}
10124
Trond Myklebust97dc1352010-06-16 09:52:26 -040010125static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
10126 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -040010127 .init_caps = NFS_CAP_READDIRPLUS
10128 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -040010129 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -040010130 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -040010131 | NFS_CAP_ATOMIC_OPEN_V1
10132 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010133 .init_client = nfs41_init_client,
10134 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -050010135 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -040010136 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -040010137 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -040010138 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010139 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010140 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -040010141 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -040010142 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10143 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10144 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -040010145 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -040010146};
10147#endif
10148
Steve Dickson42c2c422013-05-22 12:50:38 -040010149#if defined(CONFIG_NFS_V4_2)
10150static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
10151 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -040010152 .init_caps = NFS_CAP_READDIRPLUS
10153 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -040010154 | NFS_CAP_POSIX_LOCK
10155 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -040010156 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -040010157 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -050010158 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -040010159 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -040010160 | NFS_CAP_OFFLOAD_CANCEL
Olga Kornievskaia04915672019-06-04 16:14:30 -040010161 | NFS_CAP_COPY_NOTIFY
Anna Schumaker624bd5b2014-11-25 13:18:16 -050010162 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -040010163 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +080010164 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -050010165 | NFS_CAP_CLONE
10166 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -040010167 .init_client = nfs41_init_client,
10168 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -040010169 .match_stateid = nfs41_match_stateid,
10170 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -040010171 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -040010172 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -040010173 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -050010174 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -040010175 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -040010176 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
10177 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
10178 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +080010179 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -040010180};
10181#endif
10182
Trond Myklebust97dc1352010-06-16 09:52:26 -040010183const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
10184 [0] = &nfs_v4_0_minor_ops,
10185#if defined(CONFIG_NFS_V4_1)
10186 [1] = &nfs_v4_1_minor_ops,
10187#endif
Steve Dickson42c2c422013-05-22 12:50:38 -040010188#if defined(CONFIG_NFS_V4_2)
10189 [2] = &nfs_v4_2_minor_ops,
10190#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -040010191};
10192
Trond Myklebust13997822016-07-24 17:10:52 -040010193static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010194{
Frank van der Linden012a2112020-06-23 22:39:03 +000010195 ssize_t error, error2, error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010196
10197 error = generic_listxattr(dentry, list, size);
10198 if (error < 0)
10199 return error;
10200 if (list) {
10201 list += error;
10202 size -= error;
10203 }
10204
10205 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
10206 if (error2 < 0)
10207 return error2;
Frank van der Linden012a2112020-06-23 22:39:03 +000010208
10209 if (list) {
10210 list += error2;
10211 size -= error2;
10212 }
10213
10214 error3 = nfs4_listxattr_nfs4_user(d_inode(dentry), list, size);
10215 if (error3 < 0)
10216 return error3;
10217
10218 return error + error2 + error3;
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010219}
10220
Trond Myklebust17f26b12013-08-21 15:48:42 -040010221static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -040010222 .create = nfs_create,
10223 .lookup = nfs_lookup,
10224 .atomic_open = nfs_atomic_open,
10225 .link = nfs_link,
10226 .unlink = nfs_unlink,
10227 .symlink = nfs_symlink,
10228 .mkdir = nfs_mkdir,
10229 .rmdir = nfs_rmdir,
10230 .mknod = nfs_mknod,
10231 .rename = nfs_rename,
10232 .permission = nfs_permission,
10233 .getattr = nfs_getattr,
10234 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010235 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -040010236};
10237
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -080010238static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010239 .permission = nfs_permission,
10240 .getattr = nfs_getattr,
10241 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +010010242 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010243};
10244
David Howells509de812006-08-22 20:06:11 -040010245const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -070010246 .version = 4, /* protocol version */
10247 .dentry_ops = &nfs4_dentry_operations,
10248 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +000010249 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -040010250 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010251 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -040010252 .submount = nfs4_submount,
David Howellsf2aedb72019-12-10 07:31:13 -050010253 .try_get_tree = nfs4_try_get_tree,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010254 .getattr = nfs4_proc_getattr,
10255 .setattr = nfs4_proc_setattr,
10256 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -070010257 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010258 .access = nfs4_proc_access,
10259 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010260 .create = nfs4_proc_create,
10261 .remove = nfs4_proc_remove,
10262 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -040010263 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010264 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -040010265 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -040010266 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -040010267 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010268 .link = nfs4_proc_link,
10269 .symlink = nfs4_proc_symlink,
10270 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -040010271 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010272 .readdir = nfs4_proc_readdir,
10273 .mknod = nfs4_proc_mknod,
10274 .statfs = nfs4_proc_statfs,
10275 .fsinfo = nfs4_proc_fsinfo,
10276 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -040010277 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010278 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -040010279 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010280 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -050010281 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010282 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010283 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010284 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -040010285 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -050010286 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010287 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +000010288 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -040010289 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -040010290 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -040010291 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -040010292 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +000010293 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -040010294 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -040010295 .create_server = nfs4_create_server,
10296 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -070010297};
10298
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010299static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +010010300 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010301 .list = nfs4_xattr_list_nfs4_acl,
10302 .get = nfs4_xattr_get_nfs4_acl,
10303 .set = nfs4_xattr_set_nfs4_acl,
10304};
10305
Frank van der Linden012a2112020-06-23 22:39:03 +000010306#ifdef CONFIG_NFS_V4_2
10307static const struct xattr_handler nfs4_xattr_nfs4_user_handler = {
10308 .prefix = XATTR_USER_PREFIX,
10309 .get = nfs4_xattr_get_nfs4_user,
10310 .set = nfs4_xattr_set_nfs4_user,
10311};
10312#endif
10313
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010314const struct xattr_handler *nfs4_xattr_handlers[] = {
10315 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -040010316#ifdef CONFIG_NFS_V4_SECURITY_LABEL
10317 &nfs4_xattr_nfs4_label_handler,
10318#endif
Frank van der Linden012a2112020-06-23 22:39:03 +000010319#ifdef CONFIG_NFS_V4_2
10320 &nfs4_xattr_nfs4_user_handler,
10321#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000010322 NULL
10323};
10324
Linus Torvalds1da177e2005-04-16 15:20:36 -070010325/*
10326 * Local variables:
10327 * c-basic-offset: 8
10328 * End:
10329 */