blob: d115d9973efc03efb78605b46da1099605acbfd7 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110096static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040097 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040099 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400100#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100102 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400103 struct nfs4_slot *slot,
104 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100106 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100108 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400109#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400110
111#ifdef CONFIG_NFS_V4_SECURITY_LABEL
112static inline struct nfs4_label *
113nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
115{
116 int err;
117
118 if (label == NULL)
119 return NULL;
120
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
123
David Quigleyaa9c2662013-05-22 12:50:44 -0400124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
128
129 return NULL;
130}
131static inline void
132nfs4_label_release_security(struct nfs4_label *label)
133{
134 if (label)
135 security_release_secctx(label->label, label->len);
136}
137static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{
139 if (label)
140 return server->attr_bitmask;
141
142 return server->attr_bitmask_nl;
143}
144#else
145static inline struct nfs4_label *
146nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148{ return NULL; }
149static inline void
150nfs4_label_release_security(struct nfs4_label *label)
151{ return; }
152static inline u32 *
153nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154{ return server->attr_bitmask; }
155#endif
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500158static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400168 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700181 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400184 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/*
188 * This is our standard bitmap for GETATTR requests.
189 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400190const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Trond Myklebust1549210f2012-06-05 09:16:47 -0400211static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500227#ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400230};
231
Andy Adamsone23008e2012-10-02 21:07:32 -0400232static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400234 | FATTR4_WORD0_FILEID,
235};
236
David Quigleya09df2c2013-05-22 12:50:41 -0400237const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
249 0
250};
251
Fred Isamandae100c2011-07-30 20:52:37 -0400252const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700256 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800259 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
David Quigleya09df2c2013-05-22 12:50:41 -0400262const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500263 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500268 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400276};
277
Trond Myklebust30846df2018-04-07 13:44:28 -0400278static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
280{
281 unsigned long cache_validity;
282
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
286
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
291
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
297}
298
299static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
301{
302 nfs4_bitmap_copy_adjust(dst, src, inode);
303}
304
Al Virobc4785c2006-10-19 23:28:51 -0700305static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct nfs4_readdir_arg *readdir)
307{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700309 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000312 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
315 }
316
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
321
322 /*
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
327 * instead of 1 or 2.
328 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (cookie == 0) {
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
337 p++;
338 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
350 p++;
351 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800359 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Trond Myklebust26d36302016-09-22 13:39:05 -0400362static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100364 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400365{
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368 ops->test_and_free_expired(server, stateid, cred);
369}
370
371static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100373 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400374{
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
377}
378
379static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100381 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400382{
383 nfs4_stateid tmp;
384
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
387}
388
NeilBrown8478eaa2014-09-18 16:09:27 +1000389static long nfs4_update_delay(long *timeout)
390{
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
401}
402
Trond Myklebust0688e642019-04-07 13:59:09 -0400403static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500404{
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 might_sleep();
406
NeilBrown8478eaa2014-09-18 16:09:27 +1000407 freezable_schedule_timeout_killable_unsafe(
408 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400409 if (!__fatal_signal_pending(current))
410 return 0;
411 return -EINTR;
412}
413
414static int nfs4_delay_interruptible(long *timeout)
415{
416 might_sleep();
417
418 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
419 if (!signal_pending(current))
420 return 0;
421 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
422}
423
424static int nfs4_delay(long *timeout, bool interruptible)
425{
426 if (interruptible)
427 return nfs4_delay_interruptible(timeout);
428 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500429}
430
431/* This is the error handling routine for processes that are allowed
432 * to sleep.
433 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400434static int nfs4_do_handle_exception(struct nfs_server *server,
435 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500436{
437 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500438 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400439 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500440 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500441 int ret = errorcode;
442
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400443 exception->delay = 0;
444 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500445 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400446
447 if (stateid == NULL && state != NULL)
448 stateid = &state->stateid;
449
Trond Myklebust65de8722008-12-23 15:21:44 -0500450 switch(errorcode) {
451 case 0:
452 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400453 case -NFS4ERR_BADHANDLE:
454 case -ESTALE:
455 if (inode != NULL && S_ISREG(inode->i_mode))
456 pnfs_destroy_layout(NFS_I(inode));
457 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400458 case -NFS4ERR_DELEG_REVOKED:
459 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400460 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400461 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400462 if (inode != NULL && stateid != NULL) {
463 nfs_inode_find_state_and_recover(inode,
464 stateid);
465 goto wait_on_recovery;
466 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500467 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400468 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400469 if (inode) {
470 int err;
471
472 err = nfs_async_inode_return_delegation(inode,
473 stateid);
474 if (err == 0)
475 goto wait_on_recovery;
476 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
477 exception->retry = 1;
478 break;
479 }
480 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500481 if (state == NULL)
482 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400483 ret = nfs4_schedule_stateid_recovery(server, state);
484 if (ret < 0)
485 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500486 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500487 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500488 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500489 nfs4_schedule_lease_recovery(clp);
490 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400491 case -NFS4ERR_MOVED:
492 ret = nfs4_schedule_migration_recovery(server);
493 if (ret < 0)
494 break;
495 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400496 case -NFS4ERR_LEASE_MOVED:
497 nfs4_schedule_lease_moved_recovery(clp);
498 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500499#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400500 case -NFS4ERR_BADSESSION:
501 case -NFS4ERR_BADSLOT:
502 case -NFS4ERR_BAD_HIGH_SLOT:
503 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
504 case -NFS4ERR_DEADSESSION:
505 case -NFS4ERR_SEQ_FALSE_RETRY:
506 case -NFS4ERR_SEQ_MISORDERED:
507 dprintk("%s ERROR: %d Reset session\n", __func__,
508 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400509 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400510 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500511#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500512 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500513 if (exception->timeout > HZ) {
514 /* We have retried a decent amount, time to
515 * fail
516 */
517 ret = -EBUSY;
518 break;
519 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500520 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500521 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400522 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500523 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400524 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400525 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400526 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400527 exception->delay = 1;
528 return 0;
529
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400530 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500531 case -NFS4ERR_OLD_STATEID:
532 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800533 break;
534 case -NFS4ERR_BADOWNER:
535 /* The following works around a Linux server bug! */
536 case -NFS4ERR_BADNAME:
537 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
538 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
539 exception->retry = 1;
540 printk(KERN_WARNING "NFS: v4 server %s "
541 "does not accept raw "
542 "uid/gids. "
543 "Reenabling the idmapper.\n",
544 server->nfs_client->cl_hostname);
545 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500546 }
547 /* We failed to handle the error */
548 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500549wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400550 exception->recovering = 1;
551 return 0;
552}
553
554/* This is the error handling routine for processes that are allowed
555 * to sleep.
556 */
557int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
558{
559 struct nfs_client *clp = server->nfs_client;
560 int ret;
561
562 ret = nfs4_do_handle_exception(server, errorcode, exception);
563 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400564 ret = nfs4_delay(&exception->timeout,
565 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400566 goto out_retry;
567 }
568 if (exception->recovering) {
569 ret = nfs4_wait_clnt_recover(clp);
570 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
571 return -EIO;
572 goto out_retry;
573 }
574 return ret;
575out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500576 if (ret == 0)
577 exception->retry = 1;
578 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500579}
580
Trond Myklebust037fc982015-09-20 15:51:00 -0400581static int
582nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
583 int errorcode, struct nfs4_exception *exception)
584{
585 struct nfs_client *clp = server->nfs_client;
586 int ret;
587
588 ret = nfs4_do_handle_exception(server, errorcode, exception);
589 if (exception->delay) {
590 rpc_delay(task, nfs4_update_delay(&exception->timeout));
591 goto out_retry;
592 }
593 if (exception->recovering) {
594 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
595 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
596 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
597 goto out_retry;
598 }
599 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
600 ret = -EIO;
601 return ret;
602out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500603 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400604 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500605 /*
606 * For NFS4ERR_MOVED, the client transport will need to
607 * be recomputed after migration recovery has completed.
608 */
609 if (errorcode == -NFS4ERR_MOVED)
610 rpc_task_release_transport(task);
611 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400612 return ret;
613}
614
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400615int
Trond Myklebust037fc982015-09-20 15:51:00 -0400616nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
617 struct nfs4_state *state, long *timeout)
618{
619 struct nfs4_exception exception = {
620 .state = state,
621 };
622
623 if (task->tk_status >= 0)
624 return 0;
625 if (timeout)
626 exception.timeout = *timeout;
627 task->tk_status = nfs4_async_handle_exception(task, server,
628 task->tk_status,
629 &exception);
630 if (exception.delay && timeout)
631 *timeout = exception.timeout;
632 if (exception.retry)
633 return -EAGAIN;
634 return 0;
635}
636
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400637/*
638 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
639 * or 'false' otherwise.
640 */
641static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
642{
643 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500644 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400645}
Trond Myklebust65de8722008-12-23 15:21:44 -0500646
Trond Myklebust452e9352010-07-31 14:29:06 -0400647static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700649 spin_lock(&clp->cl_lock);
650 if (time_before(clp->cl_last_renewal,timestamp))
651 clp->cl_last_renewal = timestamp;
652 spin_unlock(&clp->cl_lock);
653}
654
Trond Myklebust452e9352010-07-31 14:29:06 -0400655static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
656{
Trond Myklebustbe824162015-07-05 14:50:46 -0400657 struct nfs_client *clp = server->nfs_client;
658
659 if (!nfs4_has_session(clp))
660 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400661}
662
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400663struct nfs4_call_sync_data {
664 const struct nfs_server *seq_server;
665 struct nfs4_sequence_args *seq_args;
666 struct nfs4_sequence_res *seq_res;
667};
668
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800669void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400670 struct nfs4_sequence_res *res, int cache_reply,
671 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400672{
673 args->sa_slot = NULL;
674 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400675 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400676
677 res->sr_slot = NULL;
678}
679
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400680static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400681{
682 struct nfs4_slot *slot = res->sr_slot;
683 struct nfs4_slot_table *tbl;
684
Chuck Lever3bd23842013-08-09 12:49:19 -0400685 tbl = slot->table;
686 spin_lock(&tbl->slot_tbl_lock);
687 if (!nfs41_wake_and_assign_slot(tbl, slot))
688 nfs4_free_slot(tbl, slot);
689 spin_unlock(&tbl->slot_tbl_lock);
690
691 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400692}
693
694static int nfs40_sequence_done(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
696{
697 if (res->sr_slot != NULL)
698 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400699 return 1;
700}
701
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400702#if defined(CONFIG_NFS_V4_1)
703
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400704static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400705{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500706 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400707 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500708 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400709
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400710 if (!slot)
711 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500712 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500713 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500714
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400715 /* Bump the slot sequence number */
716 if (slot->seq_done)
717 slot->seq_nr++;
718 slot->seq_done = 0;
719
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500720 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500721 /* Be nice to the server: try to ensure that the last transmitted
722 * value for highest_user_slotid <= target_highest_slotid
723 */
724 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
725 send_new_highest_used_slotid = true;
726
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500727 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500728 send_new_highest_used_slotid = false;
729 goto out_unlock;
730 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500731 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500732
733 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
734 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500735out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500736 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500737 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400738 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400739 if (waitqueue_active(&tbl->slot_waitq))
740 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400741}
742
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400743static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
744{
745 nfs41_release_slot(res->sr_slot);
746 res->sr_slot = NULL;
747}
748
Trond Myklebust3453d572018-06-20 17:53:34 -0400749static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
750 u32 seqnr)
751{
752 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
753 slot->seq_nr_highest_sent = seqnr;
754}
755static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
756 u32 seqnr)
757{
758 slot->seq_nr_highest_sent = seqnr;
759 slot->seq_nr_last_acked = seqnr;
760}
761
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400762static int nfs41_sequence_process(struct rpc_task *task,
763 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400764{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500765 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500766 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400767 struct nfs_client *clp;
Trond Myklebust85563072012-12-11 10:31:12 -0500768 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400769
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500770 if (slot == NULL)
771 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400772 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500773 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400774 goto out;
775
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500776 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500777
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400778 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500779 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400780 switch (res->sr_status) {
781 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400782 /* Mark this sequence number as having been acked */
783 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400784 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400785 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500786 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500787 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500788 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400789 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
790 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500791 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400792 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500793 case 1:
794 /*
795 * sr_status remains 1 if an RPC level error occurred.
796 * The server may or may not have processed the sequence
797 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500798 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400799 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
800 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500801 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400802 case -NFS4ERR_DELAY:
803 /* The server detected a resend of the RPC call and
804 * returned NFS4ERR_DELAY as per Section 2.10.6.2
805 * of RFC5661.
806 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500807 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400808 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500809 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500810 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400811 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400812 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400813 case -NFS4ERR_RETRY_UNCACHED_REP:
814 case -NFS4ERR_SEQ_FALSE_RETRY:
815 /*
816 * The server thinks we tried to replay a request.
817 * Retry the call after bumping the sequence ID.
818 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400819 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400820 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500821 case -NFS4ERR_BADSLOT:
822 /*
823 * The slot id we used was probably retired. Try again
824 * using a different slot id.
825 */
Trond Myklebust99589102018-06-09 12:50:50 -0400826 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400827 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500828 goto retry_nowait;
829 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400830 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500831 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400832 * Were one or more calls using this slot interrupted?
833 * If the server never received the request, then our
834 * transmitted slot sequence number may be too high.
Trond Myklebustac20d162012-12-15 15:36:07 -0500835 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400836 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
837 slot->seq_nr--;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500838 goto retry_nowait;
839 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400840 /*
841 * RFC5661:
842 * A retry might be sent while the original request is
843 * still in progress on the replier. The replier SHOULD
844 * deal with the issue by returning NFS4ERR_DELAY as the
845 * reply to SEQUENCE or CB_SEQUENCE operation, but
846 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
847 *
848 * Restart the search after a delay.
849 */
850 slot->seq_nr = slot->seq_nr_highest_sent;
851 goto out_retry;
Trond Myklebust14516c32010-07-31 14:29:06 -0400852 default:
853 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400854 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400855 }
856out:
857 /* The session may be reset by one of the error handlers. */
858 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500859out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500860 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400861session_recover:
862 nfs4_schedule_session_recovery(session, res->sr_status);
863 goto retry_nowait;
864retry_new_seq:
865 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500866retry_nowait:
867 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400868 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500869 task->tk_status = 0;
870 ret = 0;
871 }
872 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400873out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400874 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400875 goto out;
876 rpc_delay(task, NFS4_POLL_RETRY_MAX);
877 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400878}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400879
880int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
881{
882 if (!nfs41_sequence_process(task, res))
883 return 0;
884 if (res->sr_slot != NULL)
885 nfs41_sequence_free_slot(res);
886 return 1;
887
888}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500889EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400890
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400891static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
892{
893 if (res->sr_slot == NULL)
894 return 1;
895 if (res->sr_slot->table->session != NULL)
896 return nfs41_sequence_process(task, res);
897 return nfs40_sequence_done(task, res);
898}
899
900static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
901{
902 if (res->sr_slot != NULL) {
903 if (res->sr_slot->table->session != NULL)
904 nfs41_sequence_free_slot(res);
905 else
906 nfs40_sequence_free_slot(res);
907 }
908}
909
Peng Tao2c4b1312014-06-11 05:24:16 +0800910int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400911{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500912 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400913 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400914 if (!res->sr_slot->table->session)
915 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400916 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400917}
Peng Tao2c4b1312014-06-11 05:24:16 +0800918EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400919
Andy Adamsonce5039c2009-04-01 09:22:13 -0400920static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
921{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400922 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400923
Trond Myklebust035168ab2010-06-16 09:52:26 -0400924 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
925
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500926 nfs4_setup_sequence(data->seq_server->nfs_client,
927 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400928}
929
Andy Adamson69ab40c2009-04-01 09:22:19 -0400930static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
931{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400932 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400933
Trond Myklebust14516c32010-07-31 14:29:06 -0400934 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400935}
936
Trond Myklebust17280172012-03-11 13:11:00 -0400937static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400938 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400939 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940};
941
Chuck Lever3bd23842013-08-09 12:49:19 -0400942#else /* !CONFIG_NFS_V4_1 */
943
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400944static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
945{
946 return nfs40_sequence_done(task, res);
947}
948
949static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
950{
951 if (res->sr_slot != NULL)
952 nfs40_sequence_free_slot(res);
953}
954
Peng Tao2c4b1312014-06-11 05:24:16 +0800955int nfs4_sequence_done(struct rpc_task *task,
956 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400957{
Chuck Lever3bd23842013-08-09 12:49:19 -0400958 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400959}
Peng Tao2c4b1312014-06-11 05:24:16 +0800960EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400961
962#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400963
Trond Myklebustc1dffe02019-03-01 12:13:34 -0500964static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
965{
966 res->sr_timestamp = jiffies;
967 res->sr_status_flags = 0;
968 res->sr_status = 1;
969}
970
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400971static
972void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
973 struct nfs4_sequence_res *res,
974 struct nfs4_slot *slot)
975{
976 if (!slot)
977 return;
978 slot->privileged = args->sa_privileged ? 1 : 0;
979 args->sa_slot = slot;
980
981 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400982}
983
984int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500985 struct nfs4_sequence_args *args,
986 struct nfs4_sequence_res *res,
987 struct rpc_task *task)
988{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500989 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500990 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500991 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500992
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500993 /* slot already allocated? */
994 if (res->sr_slot != NULL)
995 goto out_start;
996
Trond Myklebust6b2e6852019-04-07 13:58:49 -0400997 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -0500998 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -0500999
Trond Myklebust3453d572018-06-20 17:53:34 -04001000 spin_lock(&tbl->slot_tbl_lock);
1001 /* The state manager will wait until the slot table is empty */
1002 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1003 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001004
Trond Myklebust3453d572018-06-20 17:53:34 -04001005 slot = nfs4_alloc_slot(tbl);
1006 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001007 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001008 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001009 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001010 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001011 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001012
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001013 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001014
Anna Schumakerad05cc02017-01-11 13:37:06 -05001015 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001016out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001017 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001018 rpc_call_start(task);
1019 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001020out_sleep_timeout:
1021 /* Try again in 1/4 second */
1022 if (args->sa_privileged)
1023 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1024 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1025 else
1026 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1027 NULL, jiffies + (HZ >> 2));
1028 spin_unlock(&tbl->slot_tbl_lock);
1029 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001030out_sleep:
1031 if (args->sa_privileged)
1032 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001033 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001034 else
1035 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1036 spin_unlock(&tbl->slot_tbl_lock);
1037 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001038}
1039EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1040
Chuck Lever9915ea72013-08-09 12:48:27 -04001041static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1042{
1043 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001044 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001045 data->seq_args, data->seq_res, task);
1046}
1047
1048static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1049{
1050 struct nfs4_call_sync_data *data = calldata;
1051 nfs4_sequence_done(task, data->seq_res);
1052}
1053
1054static const struct rpc_call_ops nfs40_call_sync_ops = {
1055 .rpc_call_prepare = nfs40_call_sync_prepare,
1056 .rpc_call_done = nfs40_call_sync_done,
1057};
1058
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001059static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001060 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001061 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001062 struct nfs4_sequence_args *args,
1063 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001064{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001065 int ret;
1066 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001067 struct nfs_client *clp = server->nfs_client;
1068 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001069 .seq_server = server,
1070 .seq_args = args,
1071 .seq_res = res,
1072 };
1073 struct rpc_task_setup task_setup = {
1074 .rpc_client = clnt,
1075 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001076 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001077 .callback_data = &data
1078 };
1079
1080 task = rpc_run_task(&task_setup);
1081 if (IS_ERR(task))
1082 ret = PTR_ERR(task);
1083 else {
1084 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001085 rpc_put_task(task);
1086 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001087 return ret;
1088}
1089
Bryan Schumaker7c513052011-03-24 17:12:24 +00001090int nfs4_call_sync(struct rpc_clnt *clnt,
1091 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001092 struct rpc_message *msg,
1093 struct nfs4_sequence_args *args,
1094 struct nfs4_sequence_res *res,
1095 int cache_reply)
1096{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001097 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001098 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001099}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100
Trond Myklebust3c591172018-07-31 15:54:10 -04001101static void
1102nfs4_inc_nlink_locked(struct inode *inode)
1103{
1104 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1105 inc_nlink(inode);
1106}
1107
1108static void
1109nfs4_dec_nlink_locked(struct inode *inode)
1110{
1111 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1112 drop_nlink(inode);
1113}
1114
1115static void
1116update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001117 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118{
1119 struct nfs_inode *nfsi = NFS_I(dir);
1120
Trond Myklebust16e14372018-03-20 16:53:31 -04001121 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1122 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001123 | NFS_INO_INVALID_DATA
1124 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001125 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001126 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1127 nfsi->attrtimeo_timestamp = jiffies;
1128 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001129 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001130 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001131 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1132 NFS_INO_INVALID_ACL;
1133 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001134 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001135 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001136 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001137 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001138 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001139}
1140
1141static void
1142update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001143 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001144{
1145 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001146 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147 spin_unlock(&dir->i_lock);
1148}
1149
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001150struct nfs4_open_createattrs {
1151 struct nfs4_label *label;
1152 struct iattr *sattr;
1153 const __u32 verf[2];
1154};
1155
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001156static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1157 int err, struct nfs4_exception *exception)
1158{
1159 if (err != -EINVAL)
1160 return false;
1161 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1162 return false;
1163 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1164 exception->retry = 1;
1165 return true;
1166}
1167
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001168static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1169{
1170 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1171}
1172
1173static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1174{
1175 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1176
1177 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1178}
1179
Trond Myklebust6ae37332015-01-30 14:21:14 -05001180static u32
1181nfs4_map_atomic_open_share(struct nfs_server *server,
1182 fmode_t fmode, int openflags)
1183{
1184 u32 res = 0;
1185
1186 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1187 case FMODE_READ:
1188 res = NFS4_SHARE_ACCESS_READ;
1189 break;
1190 case FMODE_WRITE:
1191 res = NFS4_SHARE_ACCESS_WRITE;
1192 break;
1193 case FMODE_READ|FMODE_WRITE:
1194 res = NFS4_SHARE_ACCESS_BOTH;
1195 }
1196 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1197 goto out;
1198 /* Want no delegation if we're using O_DIRECT */
1199 if (openflags & O_DIRECT)
1200 res |= NFS4_SHARE_WANT_NO_DELEG;
1201out:
1202 return res;
1203}
1204
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001205static enum open_claim_type4
1206nfs4_map_atomic_open_claim(struct nfs_server *server,
1207 enum open_claim_type4 claim)
1208{
1209 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1210 return claim;
1211 switch (claim) {
1212 default:
1213 return claim;
1214 case NFS4_OPEN_CLAIM_FH:
1215 return NFS4_OPEN_CLAIM_NULL;
1216 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1217 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1218 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1219 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1220 }
1221}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222
1223static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001224{
1225 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001226 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227 p->o_res.seqid = p->o_arg.seqid;
1228 p->c_res.seqid = p->c_arg.seqid;
1229 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001230 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001231 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001232 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001233}
1234
Al Viro82a2c1b2011-06-22 18:30:55 -04001235static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001236 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001237 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001238 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001239 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001240{
Al Viro82a2c1b2011-06-22 18:30:55 -04001241 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001242 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001243 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001244 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001245 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246 struct nfs4_opendata *p;
1247
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001248 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001249 if (p == NULL)
1250 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001251
1252 p->f_label = nfs4_label_alloc(server, gfp_mask);
1253 if (IS_ERR(p->f_label))
1254 goto err_free_p;
1255
Kinglong Meea49c2692015-07-27 15:31:38 +08001256 p->a_label = nfs4_label_alloc(server, gfp_mask);
1257 if (IS_ERR(p->a_label))
1258 goto err_free_f;
1259
Trond Myklebust63f5f792015-01-23 19:19:25 -05001260 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1261 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001262 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001263 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001264 nfs_sb_active(dentry->d_sb);
1265 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 p->dir = parent;
1267 p->owner = sp;
1268 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001269 p->o_arg.open_flags = flags;
1270 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001271 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001272 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1273 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001274 if (flags & O_CREAT) {
1275 p->o_arg.umask = current_umask();
1276 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1277 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1278 p->o_arg.u.attrs = &p->attrs;
1279 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1280
1281 memcpy(p->o_arg.u.verifier.data, c->verf,
1282 sizeof(p->o_arg.u.verifier.data));
1283 }
1284 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001285 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1286 * will return permission denied for all bits until close */
1287 if (!(flags & O_EXCL)) {
1288 /* ask server to check for all possible rights as results
1289 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001290 switch (p->o_arg.claim) {
1291 default:
1292 break;
1293 case NFS4_OPEN_CLAIM_NULL:
1294 case NFS4_OPEN_CLAIM_FH:
1295 p->o_arg.access = NFS4_ACCESS_READ |
1296 NFS4_ACCESS_MODIFY |
1297 NFS4_ACCESS_EXTEND |
1298 NFS4_ACCESS_EXECUTE;
1299 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001300 }
David Howells7539bba2006-08-22 20:06:09 -04001301 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001302 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1303 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001304 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001305 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001306 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001307 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001308 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001309 case NFS4_OPEN_CLAIM_NULL:
1310 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1311 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1312 p->o_arg.fh = NFS_FH(dir);
1313 break;
1314 case NFS4_OPEN_CLAIM_PREVIOUS:
1315 case NFS4_OPEN_CLAIM_FH:
1316 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1317 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001318 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001319 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001320 p->c_arg.fh = &p->o_res.fh;
1321 p->c_arg.stateid = &p->o_res.stateid;
1322 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001323 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001324 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001325 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001326
1327err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001328 nfs4_label_free(p->a_label);
1329err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001330 nfs4_label_free(p->f_label);
1331err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001332 kfree(p);
1333err:
1334 dput(parent);
1335 return NULL;
1336}
1337
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001338static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001339{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001340 struct nfs4_opendata *p = container_of(kref,
1341 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001342 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001343
Fred Isaman30ae2412016-10-18 13:39:51 -04001344 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001345 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001346 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001347 if (p->state != NULL)
1348 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001349 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001350
Kinglong Meea49c2692015-07-27 15:31:38 +08001351 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001352 nfs4_label_free(p->f_label);
1353
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001354 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001355 dput(p->dentry);
1356 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001357 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001358 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001359 kfree(p);
1360}
1361
1362static void nfs4_opendata_put(struct nfs4_opendata *p)
1363{
1364 if (p != NULL)
1365 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001366}
1367
Trond Myklebust24311f82015-09-20 10:50:17 -04001368static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1369 fmode_t fmode)
1370{
1371 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1372 case FMODE_READ|FMODE_WRITE:
1373 return state->n_rdwr != 0;
1374 case FMODE_WRITE:
1375 return state->n_wronly != 0;
1376 case FMODE_READ:
1377 return state->n_rdonly != 0;
1378 }
1379 WARN_ON_ONCE(1);
1380 return false;
1381}
1382
Trond Myklebustbe189f72018-09-27 17:12:33 -04001383static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1384 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001385{
1386 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001387
Trond Myklebust536e43d2012-01-17 22:04:26 -05001388 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001389 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001390 switch (claim) {
1391 case NFS4_OPEN_CLAIM_NULL:
1392 case NFS4_OPEN_CLAIM_FH:
1393 goto out;
1394 default:
1395 break;
1396 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001397 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001398 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001399 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1400 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001401 break;
1402 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001403 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1404 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001405 break;
1406 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001407 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1408 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001409 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001410out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001411 return ret;
1412}
1413
Trond Myklebust2a606182015-08-19 22:30:00 -05001414static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1415 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001416{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001417 if (delegation == NULL)
1418 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001420 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001421 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1422 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001423 switch (claim) {
1424 case NFS4_OPEN_CLAIM_NULL:
1425 case NFS4_OPEN_CLAIM_FH:
1426 break;
1427 case NFS4_OPEN_CLAIM_PREVIOUS:
1428 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1429 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001430 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001431 default:
1432 return 0;
1433 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001434 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001435 return 1;
1436}
1437
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001438static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001439{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001440 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001441 case FMODE_WRITE:
1442 state->n_wronly++;
1443 break;
1444 case FMODE_READ:
1445 state->n_rdonly++;
1446 break;
1447 case FMODE_READ|FMODE_WRITE:
1448 state->n_rdwr++;
1449 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001450 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001451}
1452
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001453#ifdef CONFIG_NFS_V4_1
1454static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1455{
1456 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1457 return true;
1458 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1459 return true;
1460 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1461 return true;
1462 return false;
1463}
1464#endif /* CONFIG_NFS_V4_1 */
1465
Trond Myklebustc9399f22017-11-06 15:28:01 -05001466static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1467{
1468 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1469 wake_up_all(&state->waitq);
1470}
1471
1472static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1473 const nfs4_stateid *stateid)
1474{
1475 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1476 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1477
1478 if (stateid_seqid == state_seqid + 1U ||
1479 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1480 nfs_state_log_update_open_stateid(state);
1481 else
1482 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1483}
1484
Trond Myklebust4f14c192014-02-12 19:15:06 -05001485static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001486{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001487 struct nfs_client *clp = state->owner->so_server->nfs_client;
1488 bool need_recover = false;
1489
1490 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1491 need_recover = true;
1492 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1493 need_recover = true;
1494 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1495 need_recover = true;
1496 if (need_recover)
1497 nfs4_state_mark_reclaim_nograce(clp, state);
1498}
1499
Trond Myklebustc9399f22017-11-06 15:28:01 -05001500/*
1501 * Check for whether or not the caller may update the open stateid
1502 * to the value passed in by stateid.
1503 *
1504 * Note: This function relies heavily on the server implementing
1505 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1506 * correctly.
1507 * i.e. The stateid seqids have to be initialised to 1, and
1508 * are then incremented on every state transition.
1509 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001510static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001511 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001512{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001513 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1514 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1515 if (stateid->seqid == cpu_to_be32(1))
1516 nfs_state_log_update_open_stateid(state);
1517 else
1518 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001519 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001520 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001521
1522 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1523 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001524 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001525 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001526 return false;
1527}
1528
Trond Myklebustf95549c2015-01-23 18:06:09 -05001529static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1530{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001531 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1532 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001533 if (state->n_wronly)
1534 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1535 if (state->n_rdonly)
1536 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1537 if (state->n_rdwr)
1538 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001539 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001540}
1541
Trond Myklebust226056c2014-02-11 10:41:07 -05001542static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1543 nfs4_stateid *stateid, fmode_t fmode)
1544{
1545 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1546 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1547 case FMODE_WRITE:
1548 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1549 break;
1550 case FMODE_READ:
1551 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1552 break;
1553 case 0:
1554 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1555 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1556 clear_bit(NFS_OPEN_STATE, &state->flags);
1557 }
1558 if (stateid == NULL)
1559 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001560 /* Handle OPEN+OPEN_DOWNGRADE races */
1561 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1562 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001563 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001564 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001565 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001566 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001567 nfs4_stateid_copy(&state->stateid, stateid);
1568 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001569 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001570out:
1571 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001572}
1573
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001574static void nfs_clear_open_stateid(struct nfs4_state *state,
1575 nfs4_stateid *arg_stateid,
1576 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001577{
1578 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001579 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1580 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1581 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001582 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001583 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1584 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001585}
1586
Trond Myklebust1393d962016-09-22 13:39:13 -04001587static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001588 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001589 __must_hold(&state->owner->so_lock)
1590 __must_hold(&state->seqlock)
1591 __must_hold(RCU)
1592
Trond Myklebust003707c2007-07-05 18:07:55 -04001593{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001594 DEFINE_WAIT(wait);
1595 int status = 0;
1596 for (;;) {
1597
1598 if (!nfs_need_update_open_stateid(state, stateid))
1599 return;
1600 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001601 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001602 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001603 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001604 /* Rely on seqids for serialisation with NFSv4.0 */
1605 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1606 break;
1607
1608 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1609 /*
1610 * Ensure we process the state changes in the same order
1611 * in which the server processed them by delaying the
1612 * update of the stateid until we are in sequence.
1613 */
1614 write_sequnlock(&state->seqlock);
1615 spin_unlock(&state->owner->so_lock);
1616 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001617 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001618 if (!signal_pending(current)) {
1619 if (schedule_timeout(5*HZ) == 0)
1620 status = -EAGAIN;
1621 else
1622 status = 0;
1623 } else
1624 status = -EINTR;
1625 finish_wait(&state->waitq, &wait);
1626 rcu_read_lock();
1627 spin_lock(&state->owner->so_lock);
1628 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001629 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001630
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001631 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1632 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001633 nfs4_stateid_copy(freeme, &state->open_stateid);
1634 nfs_test_and_clear_all_open_stateid(state);
1635 }
1636
Trond Myklebuste999e802014-02-10 18:20:47 -05001637 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1638 nfs4_stateid_copy(&state->stateid, stateid);
1639 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001640 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001641 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001642}
1643
Trond Myklebustc9399f22017-11-06 15:28:01 -05001644static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001645 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001646 fmode_t fmode,
1647 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001649 /*
1650 * Protect the call to nfs4_state_set_mode_locked and
1651 * serialise the stateid update
1652 */
1653 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001654 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1655 switch (fmode) {
1656 case FMODE_READ:
1657 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1658 break;
1659 case FMODE_WRITE:
1660 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1661 break;
1662 case FMODE_READ|FMODE_WRITE:
1663 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001664 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001665 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001666 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001667}
1668
1669static void nfs_state_set_delegation(struct nfs4_state *state,
1670 const nfs4_stateid *deleg_stateid,
1671 fmode_t fmode)
1672{
1673 /*
1674 * Protect the call to nfs4_state_set_mode_locked and
1675 * serialise the stateid update
1676 */
1677 write_seqlock(&state->seqlock);
1678 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1679 set_bit(NFS_DELEGATED_STATE, &state->flags);
1680 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681}
1682
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001683static void nfs_state_clear_delegation(struct nfs4_state *state)
1684{
1685 write_seqlock(&state->seqlock);
1686 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1687 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1688 write_sequnlock(&state->seqlock);
1689}
1690
Trond Myklebust1393d962016-09-22 13:39:13 -04001691static int update_open_stateid(struct nfs4_state *state,
1692 const nfs4_stateid *open_stateid,
1693 const nfs4_stateid *delegation,
1694 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001695{
Trond Myklebust1393d962016-09-22 13:39:13 -04001696 struct nfs_server *server = NFS_SERVER(state->inode);
1697 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001698 struct nfs_inode *nfsi = NFS_I(state->inode);
1699 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001700 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001701 int ret = 0;
1702
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001703 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001704
1705 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001706 spin_lock(&state->owner->so_lock);
1707 if (open_stateid != NULL) {
1708 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1709 ret = 1;
1710 }
1711
Trond Myklebust34310432008-12-23 15:21:38 -05001712 deleg_cur = rcu_dereference(nfsi->delegation);
1713 if (deleg_cur == NULL)
1714 goto no_delegation;
1715
1716 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001717 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001718 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001719 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001720 goto no_delegation_unlock;
1721
1722 if (delegation == NULL)
1723 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001724 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001725 goto no_delegation_unlock;
1726
Trond Myklebustb7391f42008-12-23 15:21:52 -05001727 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001728 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001729 ret = 1;
1730no_delegation_unlock:
1731 spin_unlock(&deleg_cur->lock);
1732no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001733 if (ret)
1734 update_open_stateflags(state, fmode);
1735 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001736 rcu_read_unlock();
1737
Trond Myklebust4f14c192014-02-12 19:15:06 -05001738 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001739 nfs4_schedule_state_manager(clp);
1740 if (freeme.type != 0)
1741 nfs4_test_and_free_stateid(server, &freeme,
1742 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001743
1744 return ret;
1745}
1746
Trond Myklebust39071e62015-01-24 15:07:56 -05001747static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1748 const nfs4_stateid *stateid)
1749{
1750 struct nfs4_state *state = lsp->ls_state;
1751 bool ret = false;
1752
1753 spin_lock(&state->state_lock);
1754 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1755 goto out_noupdate;
1756 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1757 goto out_noupdate;
1758 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1759 ret = true;
1760out_noupdate:
1761 spin_unlock(&state->state_lock);
1762 return ret;
1763}
Trond Myklebust34310432008-12-23 15:21:38 -05001764
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001765static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001766{
1767 struct nfs_delegation *delegation;
1768
Trond Myklebustf5086242018-03-20 16:43:13 -04001769 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001770 rcu_read_lock();
1771 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001772 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001773 rcu_read_unlock();
1774 return;
1775 }
1776 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001777 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001778}
1779
Trond Myklebust6ee41262007-07-08 14:11:36 -04001780static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001781{
1782 struct nfs4_state *state = opendata->state;
1783 struct nfs_inode *nfsi = NFS_I(state->inode);
1784 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001785 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001786 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001787 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001788 nfs4_stateid stateid;
1789 int ret = -EAGAIN;
1790
Trond Myklebustaac00a82007-07-05 19:02:21 -04001791 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001792 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001793 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001794 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001795 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001796 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001797 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001798 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001799 rcu_read_lock();
1800 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001801 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001802 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001803 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001804 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001805 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001806 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001807 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001808 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001809 if (!opendata->is_recover) {
1810 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1811 if (ret != 0)
1812 goto out;
1813 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001814 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001815
1816 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001817 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001818 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001819 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001820out:
1821 return ERR_PTR(ret);
1822out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001823 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001824 return state;
1825}
1826
Andy Adamsone23008e2012-10-02 21:07:32 -04001827static void
1828nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1829{
1830 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1831 struct nfs_delegation *delegation;
1832 int delegation_flags = 0;
1833
1834 rcu_read_lock();
1835 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1836 if (delegation)
1837 delegation_flags = delegation->flags;
1838 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001839 switch (data->o_arg.claim) {
1840 default:
1841 break;
1842 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1843 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001844 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1845 "returning a delegation for "
1846 "OPEN(CLAIM_DELEGATE_CUR)\n",
1847 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001848 return;
1849 }
1850 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001851 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001852 data->owner->so_cred,
1853 data->o_res.delegation_type,
1854 &data->o_res.delegation,
1855 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001856 else
1857 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001858 data->owner->so_cred,
1859 data->o_res.delegation_type,
1860 &data->o_res.delegation,
1861 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001862
1863 if (data->o_res.do_recall)
1864 nfs_async_inode_return_delegation(state->inode,
1865 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001866}
1867
1868/*
1869 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1870 * and update the nfs4_state.
1871 */
1872static struct nfs4_state *
1873_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1874{
1875 struct inode *inode = data->state->inode;
1876 struct nfs4_state *state = data->state;
1877 int ret;
1878
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001879 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001880 if (data->rpc_status)
1881 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001882 /* cached opens have already been processed */
1883 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001884 }
1885
Andy Adamsone23008e2012-10-02 21:07:32 -04001886 ret = nfs_refresh_inode(inode, &data->f_attr);
1887 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001888 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001889
1890 if (data->o_res.delegation_type != 0)
1891 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001892update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001893 update_open_stateid(state, &data->o_res.stateid, NULL,
1894 data->o_arg.fmode);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001895 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001896
1897 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001898}
1899
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001900static struct inode *
1901nfs4_opendata_get_inode(struct nfs4_opendata *data)
1902{
1903 struct inode *inode;
1904
1905 switch (data->o_arg.claim) {
1906 case NFS4_OPEN_CLAIM_NULL:
1907 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1908 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1909 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1910 return ERR_PTR(-EAGAIN);
1911 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1912 &data->f_attr, data->f_label);
1913 break;
1914 default:
1915 inode = d_inode(data->dentry);
1916 ihold(inode);
1917 nfs_refresh_inode(inode, &data->f_attr);
1918 }
1919 return inode;
1920}
1921
Andy Adamsone23008e2012-10-02 21:07:32 -04001922static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001923nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1924{
1925 struct nfs4_state *state;
1926 struct inode *inode;
1927
1928 inode = nfs4_opendata_get_inode(data);
1929 if (IS_ERR(inode))
1930 return ERR_CAST(inode);
1931 if (data->state != NULL && data->state->inode == inode) {
1932 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001933 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001934 } else
1935 state = nfs4_get_open_state(inode, data->owner);
1936 iput(inode);
1937 if (state == NULL)
1938 state = ERR_PTR(-ENOMEM);
1939 return state;
1940}
1941
Andy Adamsone23008e2012-10-02 21:07:32 -04001942static struct nfs4_state *
1943_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001944{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001945 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001946
Trond Myklebustaac00a82007-07-05 19:02:21 -04001947 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001948 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001949 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001950 goto out;
1951 }
1952
Trond Myklebust75e8c482017-08-08 10:38:07 -04001953 state = nfs4_opendata_find_nfs4_state(data);
1954 if (IS_ERR(state))
1955 goto out;
1956
Andy Adamsone23008e2012-10-02 21:07:32 -04001957 if (data->o_res.delegation_type != 0)
1958 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001959 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001960 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001961out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001962 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001963 return state;
1964}
1965
Andy Adamsone23008e2012-10-02 21:07:32 -04001966static struct nfs4_state *
1967nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1968{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001969 struct nfs4_state *ret;
1970
Andy Adamsone23008e2012-10-02 21:07:32 -04001971 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001972 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1973 else
1974 ret = _nfs4_opendata_to_nfs4_state(data);
1975 nfs4_sequence_free_slot(&data->o_res.seq_res);
1976 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001977}
1978
Trond Myklebust0de43972018-09-02 15:57:01 -04001979static struct nfs_open_context *
1980nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001981{
1982 struct nfs_inode *nfsi = NFS_I(state->inode);
1983 struct nfs_open_context *ctx;
1984
Trond Myklebust0de43972018-09-02 15:57:01 -04001985 rcu_read_lock();
1986 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01001987 if (ctx->state != state)
1988 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04001989 if ((ctx->mode & mode) != mode)
1990 continue;
1991 if (!get_nfs_open_context(ctx))
1992 continue;
1993 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001994 return ctx;
1995 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001996 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001997 return ERR_PTR(-ENOENT);
1998}
1999
Trond Myklebust0de43972018-09-02 15:57:01 -04002000static struct nfs_open_context *
2001nfs4_state_find_open_context(struct nfs4_state *state)
2002{
2003 struct nfs_open_context *ctx;
2004
2005 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2006 if (!IS_ERR(ctx))
2007 return ctx;
2008 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2009 if (!IS_ERR(ctx))
2010 return ctx;
2011 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2012}
2013
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002014static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2015 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002016{
2017 struct nfs4_opendata *opendata;
2018
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002019 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002020 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002021 if (opendata == NULL)
2022 return ERR_PTR(-ENOMEM);
2023 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002024 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002025 return opendata;
2026}
2027
Trond Myklebust24311f82015-09-20 10:50:17 -04002028static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2029 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002030{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002031 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002032 int ret;
2033
Trond Myklebust24311f82015-09-20 10:50:17 -04002034 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002035 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002036 opendata->o_arg.open_flags = 0;
2037 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002038 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2039 NFS_SB(opendata->dentry->d_sb),
2040 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002041 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2042 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2043 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002044 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002045 if (ret != 0)
2046 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002047 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002048 if (IS_ERR(newstate))
2049 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002050 if (newstate != opendata->state)
2051 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002052 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002053 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002054}
2055
2056static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2057{
Trond Myklebust864472e2006-01-03 09:55:15 +01002058 int ret;
2059
Trond Myklebust4f14c192014-02-12 19:15:06 -05002060 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2061 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2062 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2063 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002064 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002065 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002066 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002067 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002068 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2069 if (ret != 0)
2070 return ret;
2071 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2072 if (ret != 0)
2073 return ret;
2074 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2075 if (ret != 0)
2076 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002077 /*
2078 * We may have performed cached opens for all three recoveries.
2079 * Check if we need to update the current stateid.
2080 */
2081 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002082 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002083 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002084 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002085 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002086 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002087 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002088 return 0;
2089}
2090
Linus Torvalds1da177e2005-04-16 15:20:36 -07002091/*
2092 * OPEN_RECLAIM:
2093 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002094 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002095static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002097 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002098 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002099 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002100 int status;
2101
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002102 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2103 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002104 if (IS_ERR(opendata))
2105 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002106 rcu_read_lock();
2107 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002108 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002109 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002110 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002111 opendata->o_arg.u.delegation_type = delegation_type;
2112 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002113 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 return status;
2115}
2116
Trond Myklebust539cd032007-06-05 11:46:42 -04002117static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118{
2119 struct nfs_server *server = NFS_SERVER(state->inode);
2120 struct nfs4_exception exception = { };
2121 int err;
2122 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002123 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002124 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002125 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2126 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002127 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002128 break;
2129 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002130 } while (exception.retry);
2131 return err;
2132}
2133
Trond Myklebust864472e2006-01-03 09:55:15 +01002134static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2135{
2136 struct nfs_open_context *ctx;
2137 int ret;
2138
2139 ctx = nfs4_state_find_open_context(state);
2140 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002141 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002142 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002143 put_nfs_open_context(ctx);
2144 return ret;
2145}
2146
NeilBrowndce26302017-12-13 09:57:09 +11002147static 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 -07002148{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002149 switch (err) {
2150 default:
2151 printk(KERN_ERR "NFS: %s: unhandled error "
2152 "%d.\n", __func__, err);
2153 case 0:
2154 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002155 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002156 case -ESTALE:
2157 break;
2158 case -NFS4ERR_BADSESSION:
2159 case -NFS4ERR_BADSLOT:
2160 case -NFS4ERR_BAD_HIGH_SLOT:
2161 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2162 case -NFS4ERR_DEADSESSION:
2163 set_bit(NFS_DELEGATED_STATE, &state->flags);
2164 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2165 return -EAGAIN;
2166 case -NFS4ERR_STALE_CLIENTID:
2167 case -NFS4ERR_STALE_STATEID:
2168 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002169 /* Don't recall a delegation if it was lost */
2170 nfs4_schedule_lease_recovery(server->nfs_client);
2171 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002172 case -NFS4ERR_MOVED:
2173 nfs4_schedule_migration_recovery(server);
2174 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002175 case -NFS4ERR_LEASE_MOVED:
2176 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2177 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002178 case -NFS4ERR_DELEG_REVOKED:
2179 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002180 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002181 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002182 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002183 nfs_inode_find_state_and_recover(state->inode,
2184 stateid);
2185 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002186 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002187 case -NFS4ERR_DELAY:
2188 case -NFS4ERR_GRACE:
2189 set_bit(NFS_DELEGATED_STATE, &state->flags);
2190 ssleep(1);
2191 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002192 case -ENOMEM:
2193 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002194 if (fl) {
2195 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2196 if (lsp)
2197 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2198 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002199 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002200 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return err;
2202}
2203
Trond Myklebust24311f82015-09-20 10:50:17 -04002204int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2205 struct nfs4_state *state, const nfs4_stateid *stateid,
2206 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002207{
2208 struct nfs_server *server = NFS_SERVER(state->inode);
2209 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002210 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002211
2212 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2213 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2214 if (IS_ERR(opendata))
2215 return PTR_ERR(opendata);
2216 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002217 nfs_state_clear_delegation(state);
Trond Myklebust24311f82015-09-20 10:50:17 -04002218 switch (type & (FMODE_READ|FMODE_WRITE)) {
2219 case FMODE_READ|FMODE_WRITE:
2220 case FMODE_WRITE:
2221 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2222 if (err)
2223 break;
2224 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2225 if (err)
2226 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002227 /* Fall through */
Trond Myklebust24311f82015-09-20 10:50:17 -04002228 case FMODE_READ:
2229 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2230 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002231 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002232 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002233}
2234
Chuck Leverbe05c862013-08-09 12:49:47 -04002235static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2236{
2237 struct nfs4_opendata *data = calldata;
2238
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002239 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2240 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002241}
2242
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002243static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2244{
2245 struct nfs4_opendata *data = calldata;
2246
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002247 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002248
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002249 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002250 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002251 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002252 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002253 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002254 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002255 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002256}
2257
2258static void nfs4_open_confirm_release(void *calldata)
2259{
2260 struct nfs4_opendata *data = calldata;
2261 struct nfs4_state *state = NULL;
2262
2263 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002264 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002265 goto out_free;
2266 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002267 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002268 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002269 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002270 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002271 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002272out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002273 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002274}
2275
2276static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002277 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002278 .rpc_call_done = nfs4_open_confirm_done,
2279 .rpc_release = nfs4_open_confirm_release,
2280};
2281
2282/*
2283 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2284 */
2285static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2286{
David Howells2b0143b2015-03-17 22:25:59 +00002287 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002288 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002289 struct rpc_message msg = {
2290 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2291 .rpc_argp = &data->c_arg,
2292 .rpc_resp = &data->c_res,
2293 .rpc_cred = data->owner->so_cred,
2294 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002295 struct rpc_task_setup task_setup_data = {
2296 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002297 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002298 .callback_ops = &nfs4_open_confirm_ops,
2299 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002300 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002301 .flags = RPC_TASK_ASYNC,
2302 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 int status;
2304
Anna Schumakerfba83f32018-05-04 16:22:50 -04002305 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2306 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002307 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002308 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002309 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002310 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002311 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002312 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002313 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002314 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002315 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002316 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002317 smp_wmb();
2318 } else
2319 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002320 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002321 return status;
2322}
2323
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002324static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002325{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002326 struct nfs4_opendata *data = calldata;
2327 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002328 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002329 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002330
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002331 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002332 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002333 /*
2334 * Check if we still need to send an OPEN call, or if we can use
2335 * a delegation instead.
2336 */
2337 if (data->state != NULL) {
2338 struct nfs_delegation *delegation;
2339
Trond Myklebustbe189f72018-09-27 17:12:33 -04002340 if (can_open_cached(data->state, data->o_arg.fmode,
2341 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002342 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002343 rcu_read_lock();
2344 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002345 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002346 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002347 rcu_read_unlock();
2348 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002349 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002350 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002351 switch (claim) {
2352 default:
2353 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002354 case NFS4_OPEN_CLAIM_PREVIOUS:
2355 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2356 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002357 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002358 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002359 case NFS4_OPEN_CLAIM_FH:
2360 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002361 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002362 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002363 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002364 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002365 &data->o_res.seq_res,
2366 task) != 0)
2367 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002368
2369 /* Set the create mode (note dependency on the session type) */
2370 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2371 if (data->o_arg.open_flags & O_EXCL) {
2372 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2373 if (nfs4_has_persistent_session(clp))
2374 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2375 else if (clp->cl_mvops->minor_version > 0)
2376 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2377 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002378 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002379unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002380 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002381 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002382out_no_action:
2383 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002384out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002385 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002386}
2387
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002388static void nfs4_open_done(struct rpc_task *task, void *calldata)
2389{
2390 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002391
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002392 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002393
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002394 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002395 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002396
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002397 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002398 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2399 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002400 case S_IFREG:
2401 break;
2402 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002403 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002404 break;
2405 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002406 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002407 break;
2408 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002409 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002410 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002411 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002412 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002413 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2414 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002415 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002416 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002417}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002418
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002419static void nfs4_open_release(void *calldata)
2420{
2421 struct nfs4_opendata *data = calldata;
2422 struct nfs4_state *state = NULL;
2423
2424 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002425 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002426 goto out_free;
2427 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002428 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002429 goto out_free;
2430 /* In case we need an open_confirm, no cleanup! */
2431 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2432 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002433 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002434 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002435 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002436out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002437 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002438}
2439
2440static const struct rpc_call_ops nfs4_open_ops = {
2441 .rpc_call_prepare = nfs4_open_prepare,
2442 .rpc_call_done = nfs4_open_done,
2443 .rpc_release = nfs4_open_release,
2444};
2445
Fred Isaman3b65a302016-09-19 10:06:49 -04002446static int nfs4_run_open_task(struct nfs4_opendata *data,
2447 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002448{
David Howells2b0143b2015-03-17 22:25:59 +00002449 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002450 struct nfs_server *server = NFS_SERVER(dir);
2451 struct nfs_openargs *o_arg = &data->o_arg;
2452 struct nfs_openres *o_res = &data->o_res;
2453 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002454 struct rpc_message msg = {
2455 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2456 .rpc_argp = o_arg,
2457 .rpc_resp = o_res,
2458 .rpc_cred = data->owner->so_cred,
2459 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002460 struct rpc_task_setup task_setup_data = {
2461 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002462 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002463 .callback_ops = &nfs4_open_ops,
2464 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002465 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002466 .flags = RPC_TASK_ASYNC,
2467 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002468 int status;
2469
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002470 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002471 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002472 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002473 data->cancelled = false;
2474 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002475 if (!ctx) {
2476 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002477 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002478 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002479 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002480 pnfs_lgopen_prepare(data, ctx);
2481 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002482 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002483 if (IS_ERR(task))
2484 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002485 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002486 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002487 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002488 smp_wmb();
2489 } else
2490 status = data->rpc_status;
2491 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002492
2493 return status;
2494}
2495
2496static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2497{
David Howells2b0143b2015-03-17 22:25:59 +00002498 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002499 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002500 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002501
Fred Isaman3b65a302016-09-19 10:06:49 -04002502 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002503 if (status != 0 || !data->rpc_done)
2504 return status;
2505
Trond Myklebust6926afd2012-01-07 13:22:46 -05002506 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2507
Anna Schumakerd7e98252017-01-11 16:13:29 -05002508 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002509 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002510
2511 return status;
2512}
2513
Trond Myklebustf3792d62014-07-10 08:54:32 -04002514/*
2515 * Additional permission checks in order to distinguish between an
2516 * open for read, and an open for execute. This works around the
2517 * fact that NFSv4 OPEN treats read and execute permissions as being
2518 * the same.
2519 * Note that in the non-execute case, we want to turn off permission
2520 * checking if we just created a new file (POSIX open() semantics).
2521 */
NeilBrowna52458b2018-12-03 11:30:31 +11002522static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002523 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002524 struct nfs4_state *state, fmode_t fmode,
2525 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002526{
2527 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002528 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002529
2530 /* access call failed or for some reason the server doesn't
2531 * support any access modes -- defer access call until later */
2532 if (opendata->o_res.access_supported == 0)
2533 return 0;
2534
2535 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002536 /*
2537 * Use openflags to check for exec, because fmode won't
2538 * always have FMODE_EXEC set when file open for exec.
2539 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002540 if (openflags & __FMODE_EXEC) {
2541 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002542 if (S_ISDIR(state->inode->i_mode))
2543 mask = NFS4_ACCESS_LOOKUP;
2544 else
2545 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002546 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002547 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002548
2549 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002550 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2551 nfs_access_add_cache(state->inode, &cache);
2552
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002553 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2554 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002555 return 0;
2556
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002557 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002558}
2559
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002560/*
2561 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2562 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002563static int _nfs4_proc_open(struct nfs4_opendata *data,
2564 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002565{
David Howells2b0143b2015-03-17 22:25:59 +00002566 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002567 struct nfs_server *server = NFS_SERVER(dir);
2568 struct nfs_openargs *o_arg = &data->o_arg;
2569 struct nfs_openres *o_res = &data->o_res;
2570 int status;
2571
Fred Isaman3b65a302016-09-19 10:06:49 -04002572 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002573 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002574 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002575 if (status != 0) {
2576 if (status == -NFS4ERR_BADNAME &&
2577 !(o_arg->open_flags & O_CREAT))
2578 return -ENOENT;
2579 return status;
2580 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002581
Trond Myklebust6926afd2012-01-07 13:22:46 -05002582 nfs_fattr_map_and_free_names(server, &data->f_attr);
2583
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002584 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002585 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002586 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002587 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002588 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002589 if (data->file_created ||
2590 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002591 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002592 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002593 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002594 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2595 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002596 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002597 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002598 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002599 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002600 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002601 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2602 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002603 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2604 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002605 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002606 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002607}
2608
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609/*
2610 * OPEN_EXPIRED:
2611 * reclaim state on the server after a network partition.
2612 * Assumes caller holds the appropriate lock
2613 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002614static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002616 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002617 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002618
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002619 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002620 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002621 if (IS_ERR(opendata))
2622 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002623 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002624 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002625 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002626 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002627 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002628}
2629
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002630static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002631{
Trond Myklebust539cd032007-06-05 11:46:42 -04002632 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002633 struct nfs4_exception exception = { };
2634 int err;
2635
2636 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002637 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002638 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002639 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2640 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002641 switch (err) {
2642 default:
2643 goto out;
2644 case -NFS4ERR_GRACE:
2645 case -NFS4ERR_DELAY:
2646 nfs4_handle_exception(server, err, &exception);
2647 err = 0;
2648 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002649 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002650out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002651 return err;
2652}
2653
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2655{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002657 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658
Trond Myklebust864472e2006-01-03 09:55:15 +01002659 ctx = nfs4_state_find_open_context(state);
2660 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002661 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002662 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002663 put_nfs_open_context(ctx);
2664 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665}
2666
Trond Myklebust41020b62016-09-22 13:38:58 -04002667static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2668 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002669{
Trond Myklebust41020b62016-09-22 13:38:58 -04002670 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002671 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002672}
2673
2674static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2675{
2676 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002677 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002678}
2679
2680static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2681{
2682 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2683 nfs40_clear_delegation_stateid(state);
2684 return nfs4_open_expired(sp, state);
2685}
2686
Trond Myklebust45870d62016-09-22 13:38:59 -04002687static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2688 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002689 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002690{
2691 return -NFS4ERR_BAD_STATEID;
2692}
2693
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002694#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002695static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2696 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002697 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002698{
2699 int status;
2700
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002701 switch (stateid->type) {
2702 default:
2703 break;
2704 case NFS4_INVALID_STATEID_TYPE:
2705 case NFS4_SPECIAL_STATEID_TYPE:
2706 return -NFS4ERR_BAD_STATEID;
2707 case NFS4_REVOKED_STATEID_TYPE:
2708 goto out_free;
2709 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002710
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002711 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002712 switch (status) {
2713 case -NFS4ERR_EXPIRED:
2714 case -NFS4ERR_ADMIN_REVOKED:
2715 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002716 break;
2717 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002718 return status;
2719 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002720out_free:
2721 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002722 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002723 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002724}
2725
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002726static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002727{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002728 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002729 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002730 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002731 const struct cred *cred = NULL;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002732 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002733
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002734 /* Get the delegation credential for use by test/free_stateid */
2735 rcu_read_lock();
2736 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002737 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002738 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002739 nfs_state_clear_delegation(state);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002740 return;
2741 }
2742
2743 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002744 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002745 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002746 nfs_state_clear_delegation(state);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002747 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002748 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002749
Trond Myklebust994b15b2018-09-05 14:07:14 -04002750 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2751 &delegation->flags)) {
2752 rcu_read_unlock();
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002753 return;
2754 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002755
NeilBrowna52458b2018-12-03 11:30:31 +11002756 if (delegation->cred)
2757 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002758 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002759 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002760 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002761 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002762 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002763
NeilBrowna52458b2018-12-03 11:30:31 +11002764 if (delegation->cred)
2765 put_cred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002766}
2767
2768/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002769 * nfs41_check_expired_locks - possibly free a lock stateid
2770 *
2771 * @state: NFSv4 state for an inode
2772 *
2773 * Returns NFS_OK if recovery for this stateid is now finished.
2774 * Otherwise a negative NFS4ERR value is returned.
2775 */
2776static int nfs41_check_expired_locks(struct nfs4_state *state)
2777{
2778 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002779 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002780 struct nfs_server *server = NFS_SERVER(state->inode);
2781
2782 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2783 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002784
2785 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002786 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2787 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002788 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002789
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002790 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002791 spin_unlock(&state->state_lock);
2792
2793 nfs4_put_lock_state(prev);
2794 prev = lsp;
2795
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002796 status = nfs41_test_and_free_expired_stateid(server,
2797 &lsp->ls_stateid,
2798 cred);
2799 trace_nfs4_test_lock_stateid(state, lsp, status);
2800 if (status == -NFS4ERR_EXPIRED ||
2801 status == -NFS4ERR_BAD_STATEID) {
2802 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002803 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002804 if (!recover_lost_locks)
2805 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2806 } else if (status != NFS_OK) {
2807 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002808 nfs4_put_lock_state(prev);
2809 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002810 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002811 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002812 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002813 }
2814 spin_unlock(&state->state_lock);
2815 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002816out:
2817 return ret;
2818}
2819
2820/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002821 * nfs41_check_open_stateid - possibly free an open stateid
2822 *
2823 * @state: NFSv4 state for an inode
2824 *
2825 * Returns NFS_OK if recovery for this stateid is now finished.
2826 * Otherwise a negative NFS4ERR value is returned.
2827 */
2828static int nfs41_check_open_stateid(struct nfs4_state *state)
2829{
2830 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002831 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002832 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002833 int status;
2834
Trond Myklebustb134fc42016-09-22 13:39:16 -04002835 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002836 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2837 if (nfs4_have_delegation(state->inode, state->state))
2838 return NFS_OK;
2839 return -NFS4ERR_OPENMODE;
2840 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002841 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002842 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002843 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002844 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002845 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002846 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2847 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2848 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002849 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002850 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002851 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002852 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002853 if (nfs_open_stateid_recover_openmode(state))
2854 return -NFS4ERR_OPENMODE;
2855 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002856}
2857
2858static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2859{
Chuck Levereb64cf92012-07-11 16:30:05 -04002860 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002861
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002862 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002863 status = nfs41_check_expired_locks(state);
2864 if (status != NFS_OK)
2865 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002866 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002867 if (status != NFS_OK)
2868 status = nfs4_open_expired(sp, state);
2869 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002870}
2871#endif
2872
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002874 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2875 * fields corresponding to attributes that were used to store the verifier.
2876 * Make sure we clobber those fields in the later setattr call
2877 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002878static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002879 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002880{
Trond Myklebust609339c2018-03-28 16:18:17 -04002881 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2882 __u32 attrset[3];
2883 unsigned ret;
2884 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002885
Trond Myklebust609339c2018-03-28 16:18:17 -04002886 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2887 attrset[i] = opendata->o_res.attrset[i];
2888 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2889 attrset[i] &= ~bitmask[i];
2890 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002891
Trond Myklebust609339c2018-03-28 16:18:17 -04002892 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2893 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002894
Trond Myklebust609339c2018-03-28 16:18:17 -04002895 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2896 if (sattr->ia_valid & ATTR_ATIME_SET)
2897 ret |= ATTR_ATIME_SET;
2898 else
2899 ret |= ATTR_ATIME;
2900 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002901
Trond Myklebust609339c2018-03-28 16:18:17 -04002902 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2903 if (sattr->ia_valid & ATTR_MTIME_SET)
2904 ret |= ATTR_MTIME_SET;
2905 else
2906 ret |= ATTR_MTIME;
2907 }
2908
2909 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002910 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002911 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002912}
2913
Trond Myklebustc21443c2013-02-07 14:26:21 -05002914static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002915 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002916{
2917 struct nfs4_state_owner *sp = opendata->owner;
2918 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002919 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002920 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002921 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002922 unsigned int seq;
2923 int ret;
2924
2925 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2926
Fred Isaman3b65a302016-09-19 10:06:49 -04002927 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002928 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002929 goto out;
2930
Trond Myklebustae55e592018-05-22 11:17:16 -04002931 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002932 ret = PTR_ERR(state);
2933 if (IS_ERR(state))
2934 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002935 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002936 if (server->caps & NFS_CAP_POSIX_LOCK)
2937 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002938 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2939 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002940
Trond Myklebust275bb302013-05-29 13:11:28 -04002941 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002942 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002943 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002944 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002945 alias = d_exact_alias(dentry, state->inode);
2946 if (!alias)
2947 alias = d_splice_alias(igrab(state->inode), dentry);
2948 /* d_splice_alias() can't fail here - it's a non-directory */
2949 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002950 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002951 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002952 }
2953 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002954 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002955 }
2956
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002957 /* Parse layoutget results before we check for access */
2958 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2959
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002960 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2961 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002962 if (ret != 0)
2963 goto out;
2964
David Howells2b0143b2015-03-17 22:25:59 +00002965 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002966 nfs_inode_attach_open_context(ctx);
2967 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2968 nfs4_schedule_stateid_recovery(server, state);
2969 }
Fred Isaman2409a972016-10-06 12:11:21 -04002970
Trond Myklebustc21443c2013-02-07 14:26:21 -05002971out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04002972 if (!opendata->cancelled)
2973 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002974 return ret;
2975}
2976
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002977/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002978 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002979 */
Andy Adamson82be4172012-05-23 05:02:35 -04002980static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002981 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002982 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002983 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002984 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002985{
2986 struct nfs4_state_owner *sp;
2987 struct nfs4_state *state = NULL;
2988 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002989 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002990 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11002991 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04002992 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002993 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002994 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002995 struct iattr *sattr = c->sattr;
2996 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002997 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002998 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999
3000 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003002 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3003 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003004 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3005 goto out_err;
3006 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003007 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003008 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003009 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003010 if (d_really_is_positive(dentry))
3011 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003012 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003013 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003014 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003015 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3016 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003017 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003018 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003019
David Quigley14c43f72013-05-22 12:50:43 -04003020 if (label) {
3021 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3022 if (IS_ERR(olabel)) {
3023 status = PTR_ERR(olabel);
3024 goto err_opendata_put;
3025 }
3026 }
3027
Trond Myklebuste911b812014-03-26 13:24:37 -07003028 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3029 if (!opendata->f_attr.mdsthreshold) {
3030 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3031 if (!opendata->f_attr.mdsthreshold)
3032 goto err_free_label;
3033 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003034 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003035 }
David Howells2b0143b2015-03-17 22:25:59 +00003036 if (d_really_is_positive(dentry))
3037 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003038
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003039 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003040 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003041 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003042 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003043
NeilBrownefcbc042015-07-30 13:00:56 +10003044 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003045 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003046 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003047 /*
3048 * send create attributes which was not set by open
3049 * with an extra setattr.
3050 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003051 if (attrs || label) {
3052 unsigned ia_old = sattr->ia_valid;
3053
3054 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003055 nfs_fattr_init(opendata->o_res.f_attr);
3056 status = nfs4_do_setattr(state->inode, cred,
3057 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003058 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003059 if (status == 0) {
3060 nfs_setattr_update_inode(state->inode, sattr,
3061 opendata->o_res.f_attr);
3062 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3063 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003064 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003065 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003066 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003067 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003068 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003069
Trond Myklebuste911b812014-03-26 13:24:37 -07003070 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003071 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003072 opendata->f_attr.mdsthreshold = NULL;
3073 }
Andy Adamson82be4172012-05-23 05:02:35 -04003074
David Quigley14c43f72013-05-22 12:50:43 -04003075 nfs4_label_free(olabel);
3076
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003077 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003078 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003080err_free_label:
3081 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003082err_opendata_put:
3083 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003084err_put_state_owner:
3085 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 return status;
3088}
3089
3090
Andy Adamson82be4172012-05-23 05:02:35 -04003091static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003092 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003093 int flags,
3094 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003095 struct nfs4_label *label,
3096 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003098 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003099 struct nfs4_exception exception = {
3100 .interruptible = true,
3101 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003102 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003103 struct nfs4_open_createattrs c = {
3104 .label = label,
3105 .sattr = sattr,
3106 .verf = {
3107 [0] = (__u32)jiffies,
3108 [1] = (__u32)current->pid,
3109 },
3110 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003111 int status;
3112
3113 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003114 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003115 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003116 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003117 if (status == 0)
3118 break;
3119 /* NOTE: BAD_SEQID means the server and client disagree about the
3120 * book-keeping w.r.t. state-changing operations
3121 * (OPEN/CLOSE/LOCK/LOCKU...)
3122 * It is actually a sign of a bug on the client or on the server.
3123 *
3124 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003125 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 * have unhashed the old state_owner for us, and that we can
3127 * therefore safely retry using a new one. We should still warn
3128 * the user though...
3129 */
3130 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003131 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003132 " returned a bad sequence-id error!\n",
3133 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134 exception.retry = 1;
3135 continue;
3136 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003137 /*
3138 * BAD_STATEID on OPEN means that the server cancelled our
3139 * state before it received the OPEN_CONFIRM.
3140 * Recover by retrying the request as per the discussion
3141 * on Page 181 of RFC3530.
3142 */
3143 if (status == -NFS4ERR_BAD_STATEID) {
3144 exception.retry = 1;
3145 continue;
3146 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003147 if (status == -EAGAIN) {
3148 /* We must have found a delegation */
3149 exception.retry = 1;
3150 continue;
3151 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003152 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3153 continue;
3154 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 status, &exception));
3156 } while (exception.retry);
3157 return res;
3158}
3159
Trond Myklebust8487c472016-06-26 08:44:35 -04003160static int _nfs4_do_setattr(struct inode *inode,
3161 struct nfs_setattrargs *arg,
3162 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003163 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003164 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003165{
3166 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003167 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003168 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3169 .rpc_argp = arg,
3170 .rpc_resp = res,
3171 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003172 };
NeilBrowna52458b2018-12-03 11:30:31 +11003173 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003174 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003175 bool truncate;
3176 int status;
3177
3178 nfs_fattr_init(res->fattr);
3179
3180 /* Servers should only apply open mode checks for file size changes */
3181 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003182 if (!truncate)
3183 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003184
Trond Myklebust991eedb2018-04-09 11:15:30 -04003185 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003186 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003187 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003188 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003189 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003190 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003191 l_ctx = nfs_get_lock_context(ctx);
3192 if (IS_ERR(l_ctx))
3193 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003194 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3195 &arg->stateid, &delegation_cred);
3196 nfs_put_lock_context(l_ctx);
3197 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003198 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003199 } else {
3200zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003201 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003202 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003203 if (delegation_cred)
3204 msg.rpc_cred = delegation_cred;
3205
3206 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3207
NeilBrowna52458b2018-12-03 11:30:31 +11003208 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003209 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003210 renew_lease(server, timestamp);
3211 trace_nfs4_setattr(inode, &arg->stateid, status);
3212 return status;
3213}
3214
NeilBrowna52458b2018-12-03 11:30:31 +11003215static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003216 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003217 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003218 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003220 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003221 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003222 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003223 struct nfs_setattrargs arg = {
3224 .fh = NFS_FH(inode),
3225 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003226 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003227 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003228 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003229 };
3230 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003231 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003232 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003234 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003235 struct nfs4_exception exception = {
3236 .state = state,
3237 .inode = inode,
3238 .stateid = &arg.stateid,
3239 };
3240 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003243 nfs4_bitmap_copy_adjust_setattr(bitmask,
3244 nfs4_bitmask(server, olabel),
3245 inode);
3246
NeilBrown29b59f92016-10-13 15:26:47 +11003247 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003248 switch (err) {
3249 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003250 if (!(sattr->ia_valid & ATTR_SIZE)) {
3251 pr_warn_once("NFSv4: server %s is incorrectly "
3252 "applying open mode checks to "
3253 "a SETATTR that is not "
3254 "changing file size.\n",
3255 server->nfs_client->cl_hostname);
3256 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003257 if (state && !(state->state & FMODE_WRITE)) {
3258 err = -EBADF;
3259 if (sattr->ia_valid & ATTR_OPEN)
3260 err = -EACCES;
3261 goto out;
3262 }
3263 }
3264 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003266out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003267 return err;
3268}
3269
Peng Tao500d7012015-09-22 11:35:22 +08003270static bool
3271nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3272{
3273 if (inode == NULL || !nfs_have_layout(inode))
3274 return false;
3275
3276 return pnfs_wait_on_layoutreturn(inode, task);
3277}
3278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279struct nfs4_closedata {
3280 struct inode *inode;
3281 struct nfs4_state *state;
3282 struct nfs_closeargs arg;
3283 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003284 struct {
3285 struct nfs4_layoutreturn_args arg;
3286 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003287 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003288 u32 roc_barrier;
3289 bool roc;
3290 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003291 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003292 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003293};
3294
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003295static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003296{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003297 struct nfs4_closedata *calldata = data;
3298 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003299 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003300
Trond Myklebustcf805162016-11-15 14:56:07 -05003301 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003302 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3303 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003304 nfs4_put_open_state(calldata->state);
3305 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003306 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003307 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003308 kfree(calldata);
3309}
3310
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003311static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003312{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003313 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003314 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003315 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003316 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003317 struct nfs4_exception exception = {
3318 .state = state,
3319 .inode = calldata->inode,
3320 .stateid = &calldata->arg.stateid,
3321 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003322
Chuck Levera3ca5652012-03-01 17:00:40 -05003323 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003324 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3325 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003326 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003327
3328 /* Handle Layoutreturn errors */
3329 if (calldata->arg.lr_args && task->tk_status != 0) {
3330 switch (calldata->res.lr_ret) {
3331 default:
3332 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3333 break;
3334 case 0:
3335 calldata->arg.lr_args = NULL;
3336 calldata->res.lr_res = NULL;
3337 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003338 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003339 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04003340 &calldata->arg.lr_args->range,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003341 calldata->inode))
3342 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003343 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003344 case -NFS4ERR_ADMIN_REVOKED:
3345 case -NFS4ERR_DELEG_REVOKED:
3346 case -NFS4ERR_EXPIRED:
3347 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003348 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3349 case -NFS4ERR_WRONG_CRED:
3350 calldata->arg.lr_args = NULL;
3351 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003352 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003353 }
3354 }
3355
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003356 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003357 * the state_owner. we keep this around to process errors
3358 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359 switch (task->tk_status) {
3360 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003361 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003362 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003363 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003364 case -NFS4ERR_ACCESS:
3365 if (calldata->arg.bitmask != NULL) {
3366 calldata->arg.bitmask = NULL;
3367 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003368 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003369
3370 }
3371 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003372 case -NFS4ERR_OLD_STATEID:
3373 /* Did we race with OPEN? */
3374 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003375 state))
3376 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003377 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003378 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003380 case -NFS4ERR_EXPIRED:
3381 nfs4_free_revoked_stateid(server,
3382 &calldata->arg.stateid,
3383 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003384 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003385 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003386 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003388 task->tk_status = nfs4_async_handle_exception(task,
3389 server, task->tk_status, &exception);
3390 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003391 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003392 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003393 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3394 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003395out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003396 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003397 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003398 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003399 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003400 return;
3401lr_restart:
3402 calldata->res.lr_ret = 0;
3403out_restart:
3404 task->tk_status = 0;
3405 rpc_restart_call_prepare(task);
3406 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003407}
3408
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003409static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003410{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003411 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003412 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003413 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003414 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003415 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003416 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003417
Chuck Levera3ca5652012-03-01 17:00:40 -05003418 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003419 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003420 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003421
Trond Myklebust88069f72009-12-08 08:33:16 -05003422 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003423 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003424 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3425 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3426 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003427 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003428 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003429 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003430 if (state->n_rdonly == 0)
3431 call_close |= is_rdonly;
3432 else if (is_rdonly)
3433 calldata->arg.fmode |= FMODE_READ;
3434 if (state->n_wronly == 0)
3435 call_close |= is_wronly;
3436 else if (is_wronly)
3437 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003438 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3439 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003440 } else if (is_rdwr)
3441 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3442
Trond Myklebust5cc78612016-11-14 11:19:56 -05003443 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003444 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003445 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003446 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003447
3448 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003449 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003450 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003451 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003452
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003453 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003454 nfs_release_seqid(calldata->arg.seqid);
3455 goto out_wait;
3456 }
3457
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003458 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3459 if (lo && !pnfs_layout_is_valid(lo)) {
3460 calldata->arg.lr_args = NULL;
3461 calldata->res.lr_res = NULL;
3462 }
3463
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003464 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003465 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003466
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003467 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003468 /* Close-to-open cache consistency revalidation */
3469 if (!nfs4_have_delegation(inode, FMODE_READ))
3470 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3471 else
3472 calldata->arg.bitmask = NULL;
3473 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003474
Trond Myklebust6ae37332015-01-30 14:21:14 -05003475 calldata->arg.share_access =
3476 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3477 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003478
Trond Myklebustd8d84982016-12-19 12:14:44 -05003479 if (calldata->res.fattr == NULL)
3480 calldata->arg.bitmask = NULL;
3481 else if (calldata->arg.bitmask == NULL)
3482 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003483 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003484 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003485 &calldata->arg.seq_args,
3486 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003487 task) != 0)
3488 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003489 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003490 return;
3491out_no_action:
3492 task->tk_action = NULL;
3493out_wait:
3494 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003495}
3496
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003497static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003498 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003499 .rpc_call_done = nfs4_close_done,
3500 .rpc_release = nfs4_free_closedata,
3501};
3502
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503/*
3504 * It is possible for data to be read/written from a mem-mapped file
3505 * after the sys_close call (which hits the vfs layer as a flush).
3506 * This means that we can't safely call nfsv4 close on a file until
3507 * the inode is cleared. This in turn means that we are not good
3508 * NFSv4 citizens - we do not indicate to the server to update the file's
3509 * share state even when we are done with one of the three share
3510 * stateid's in the inode.
3511 *
3512 * NOTE: Caller must be holding the sp->so_owner semaphore!
3513 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003514int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003515{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003516 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003517 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003518 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003519 struct nfs4_state_owner *sp = state->owner;
3520 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003521 struct rpc_message msg = {
3522 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3523 .rpc_cred = state->owner->so_cred,
3524 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003525 struct rpc_task_setup task_setup_data = {
3526 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003527 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003528 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003529 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003530 .flags = RPC_TASK_ASYNC,
3531 };
Trond Myklebust95121352005-10-18 14:20:12 -07003532 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003533
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003534 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3535 &task_setup_data.rpc_client, &msg);
3536
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003537 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003539 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003540 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003541 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003543 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003544 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3545 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003547 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3548 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003549 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003550 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003551 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003552 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003553 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003554 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003555 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003556 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003557 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003558 calldata->lr.roc = pnfs_roc(state->inode,
3559 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3560 if (calldata->lr.roc) {
3561 calldata->arg.lr_args = &calldata->lr.arg;
3562 calldata->res.lr_res = &calldata->lr.res;
3563 }
Al Viro643168c2011-06-22 18:20:23 -04003564 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003565
Trond Myklebust1174dd12010-12-21 10:52:24 -05003566 msg.rpc_argp = &calldata->arg;
3567 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003568 task_setup_data.callback_data = calldata;
3569 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003570 if (IS_ERR(task))
3571 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003572 status = 0;
3573 if (wait)
3574 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003575 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003576 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003577out_free_calldata:
3578 kfree(calldata);
3579out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003580 nfs4_put_open_state(state);
3581 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003582 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583}
3584
Trond Myklebust2b484292010-09-17 10:56:51 -04003585static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003586nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3587 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003588{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003589 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003590 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3591
3592 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003593
Trond Myklebust565277f2007-10-15 18:17:53 -04003594 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003595 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003596
3597 nfs4_label_release_security(label);
3598
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003599 if (IS_ERR(state))
3600 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003601 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
Trond Myklebust1185a552009-12-03 15:54:02 -05003604static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003605{
3606 if (ctx->state == NULL)
3607 return;
3608 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003609 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003610 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003611 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003612}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003613
Trond Myklebustb944dba2013-11-04 15:20:20 -05003614#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3615#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003616#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003617
Linus Torvalds1da177e2005-04-16 15:20:36 -07003618static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3619{
Kinglong Mee8c612822015-08-26 21:12:58 +08003620 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003621 struct nfs4_server_caps_arg args = {
3622 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003623 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003624 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625 struct nfs4_server_caps_res res = {};
3626 struct rpc_message msg = {
3627 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003628 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003629 .rpc_resp = &res,
3630 };
3631 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003632 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633
Kinglong Mee8c612822015-08-26 21:12:58 +08003634 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3635 FATTR4_WORD0_FH_EXPIRE_TYPE |
3636 FATTR4_WORD0_LINK_SUPPORT |
3637 FATTR4_WORD0_SYMLINK_SUPPORT |
3638 FATTR4_WORD0_ACLSUPPORT;
3639 if (minorversion)
3640 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3641
Bryan Schumaker7c513052011-03-24 17:12:24 +00003642 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003643 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003644 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003645 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003646 case 0:
3647 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3648 res.attr_bitmask[2] = 0;
3649 break;
3650 case 1:
3651 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3652 break;
3653 case 2:
3654 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3655 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003657 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3658 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3659 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3660 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003661 NFS_CAP_CTIME|NFS_CAP_MTIME|
3662 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003663 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3664 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665 server->caps |= NFS_CAP_ACLS;
3666 if (res.has_links != 0)
3667 server->caps |= NFS_CAP_HARDLINKS;
3668 if (res.has_symlinks != 0)
3669 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003670 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3671 server->caps |= NFS_CAP_FILEID;
3672 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3673 server->caps |= NFS_CAP_MODE;
3674 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3675 server->caps |= NFS_CAP_NLINK;
3676 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3677 server->caps |= NFS_CAP_OWNER;
3678 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3679 server->caps |= NFS_CAP_OWNER_GROUP;
3680 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3681 server->caps |= NFS_CAP_ATIME;
3682 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3683 server->caps |= NFS_CAP_CTIME;
3684 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3685 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003686#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3687 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3688 server->caps |= NFS_CAP_SECURITY_LABEL;
3689#endif
3690 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3691 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003692 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003693
Trond Myklebusta65318b2009-03-11 14:10:28 -04003694 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3695 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3696 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003697 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003698
3699 /* Avoid a regression due to buggy server */
3700 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3701 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003702 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3703 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003704
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003706 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003707 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003708
Linus Torvalds1da177e2005-04-16 15:20:36 -07003709 return status;
3710}
3711
Trond Myklebust55a97592006-06-09 09:34:19 -04003712int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713{
Trond Myklebust0688e642019-04-07 13:59:09 -04003714 struct nfs4_exception exception = {
3715 .interruptible = true,
3716 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717 int err;
3718 do {
3719 err = nfs4_handle_exception(server,
3720 _nfs4_server_capabilities(server, fhandle),
3721 &exception);
3722 } while (exception.retry);
3723 return err;
3724}
3725
3726static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3727 struct nfs_fsinfo *info)
3728{
David Quigleyaa9c2662013-05-22 12:50:44 -04003729 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003730 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003731 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003732 };
3733 struct nfs4_lookup_res res = {
3734 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003735 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003736 .fh = fhandle,
3737 };
3738 struct rpc_message msg = {
3739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3740 .rpc_argp = &args,
3741 .rpc_resp = &res,
3742 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003743
David Quigleyaa9c2662013-05-22 12:50:44 -04003744 bitmask[0] = nfs4_fattr_bitmap[0];
3745 bitmask[1] = nfs4_fattr_bitmap[1];
3746 /*
3747 * Process the label in the upcoming getfattr
3748 */
3749 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3750
Trond Myklebust0e574af2005-10-27 22:12:38 -04003751 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003752 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003753}
3754
3755static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3756 struct nfs_fsinfo *info)
3757{
Trond Myklebust0688e642019-04-07 13:59:09 -04003758 struct nfs4_exception exception = {
3759 .interruptible = true,
3760 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003761 int err;
3762 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003763 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003764 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003765 switch (err) {
3766 case 0:
3767 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003768 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003769 default:
3770 err = nfs4_handle_exception(server, err, &exception);
3771 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003772 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003773out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003774 return err;
3775}
3776
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003777static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3778 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3779{
Trond Myklebustc2190662013-08-26 19:23:04 -04003780 struct rpc_auth_create_args auth_args = {
3781 .pseudoflavor = flavor,
3782 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003783 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003784
Trond Myklebustc2190662013-08-26 19:23:04 -04003785 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003786 if (IS_ERR(auth))
3787 return -EACCES;
3788 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003789}
3790
Chuck Lever9a744ba2013-03-16 15:56:02 -04003791/*
3792 * Retry pseudoroot lookup with various security flavors. We do this when:
3793 *
3794 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3795 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3796 *
3797 * Returns zero on success, or a negative NFS4ERR value, or a
3798 * negative errno value.
3799 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003800static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003801 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003803 /* Per 3530bis 15.33.5 */
3804 static const rpc_authflavor_t flav_array[] = {
3805 RPC_AUTH_GSS_KRB5P,
3806 RPC_AUTH_GSS_KRB5I,
3807 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003808 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003809 RPC_AUTH_NULL,
3810 };
3811 int status = -EPERM;
3812 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003813
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003814 if (server->auth_info.flavor_len > 0) {
3815 /* try each flavor specified by user */
3816 for (i = 0; i < server->auth_info.flavor_len; i++) {
3817 status = nfs4_lookup_root_sec(server, fhandle, info,
3818 server->auth_info.flavors[i]);
3819 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3820 continue;
3821 break;
3822 }
3823 } else {
3824 /* no flavors specified by user, try default list */
3825 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3826 status = nfs4_lookup_root_sec(server, fhandle, info,
3827 flav_array[i]);
3828 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3829 continue;
3830 break;
3831 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003832 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003833
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003834 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003835 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003836 * to access the mount. It could also mean that we tried to mount
3837 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3838 * existing mount programs don't handle -EACCES very well so it should
3839 * be mapped to -EPERM instead.
3840 */
3841 if (status == -EACCES)
3842 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003843 return status;
3844}
3845
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003846/**
3847 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3848 * @server: initialized nfs_server handle
3849 * @fhandle: we fill in the pseudo-fs root file handle
3850 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003851 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003852 *
3853 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003854 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003855int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003856 struct nfs_fsinfo *info,
3857 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003858{
Andre Przywarac7757072015-04-23 17:17:40 +01003859 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003860
Andre Przywarac7757072015-04-23 17:17:40 +01003861 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003862 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003863
3864 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003865 status = server->nfs_client->cl_mvops->find_root_sec(server,
3866 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003867
Linus Torvalds1da177e2005-04-16 15:20:36 -07003868 if (status == 0)
3869 status = nfs4_server_capabilities(server, fhandle);
3870 if (status == 0)
3871 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003872
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003873 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003874}
3875
Bryan Schumakerbae36242012-05-10 15:07:31 -04003876static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3877 struct nfs_fsinfo *info)
3878{
3879 int error;
3880 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003881 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003882
3883 error = nfs4_server_capabilities(server, mntfh);
3884 if (error < 0) {
3885 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3886 return error;
3887 }
3888
David Quigley14c43f72013-05-22 12:50:43 -04003889 label = nfs4_label_alloc(server, GFP_KERNEL);
3890 if (IS_ERR(label))
3891 return PTR_ERR(label);
3892
Trond Myklebusta841b542018-04-07 13:50:59 -04003893 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003894 if (error < 0) {
3895 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003896 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003897 }
3898
3899 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3900 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3901 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3902
David Quigley14c43f72013-05-22 12:50:43 -04003903err_free_label:
3904 nfs4_label_free(label);
3905
Bryan Schumakerbae36242012-05-10 15:07:31 -04003906 return error;
3907}
3908
Manoj Naik6b97fd32006-06-09 09:34:29 -04003909/*
3910 * Get locations and (maybe) other attributes of a referral.
3911 * Note that we'll actually follow the referral later when
3912 * we detect fsid mismatch in inode revalidation
3913 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003914static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3915 const struct qstr *name, struct nfs_fattr *fattr,
3916 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003917{
3918 int status = -ENOMEM;
3919 struct page *page = NULL;
3920 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003921
3922 page = alloc_page(GFP_KERNEL);
3923 if (page == NULL)
3924 goto out;
3925 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3926 if (locations == NULL)
3927 goto out;
3928
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003929 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003930 if (status != 0)
3931 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003932
3933 /*
3934 * If the fsid didn't change, this is a migration event, not a
3935 * referral. Cause us to drop into the exception handler, which
3936 * will kick off migration recovery.
3937 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003938 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003939 dprintk("%s: server did not return a different fsid for"
3940 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003941 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003942 goto out;
3943 }
Andy Adamson533eb462011-06-13 18:25:56 -04003944 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3945 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003946
Andy Adamson533eb462011-06-13 18:25:56 -04003947 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003948 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003949 memset(fhandle, 0, sizeof(struct nfs_fh));
3950out:
3951 if (page)
3952 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003953 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003954 return status;
3955}
3956
David Quigley1775fd32013-05-22 12:50:42 -04003957static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003958 struct nfs_fattr *fattr, struct nfs4_label *label,
3959 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003960{
Trond Myklebust771734f2018-04-07 13:54:23 -04003961 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003962 struct nfs4_getattr_arg args = {
3963 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003964 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003965 };
3966 struct nfs4_getattr_res res = {
3967 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003968 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003969 .server = server,
3970 };
3971 struct rpc_message msg = {
3972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3973 .rpc_argp = &args,
3974 .rpc_resp = &res,
3975 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003976
Trond Myklebust771734f2018-04-07 13:54:23 -04003977 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003978
Trond Myklebust0e574af2005-10-27 22:12:38 -04003979 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003980 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003981}
3982
David Quigley1775fd32013-05-22 12:50:42 -04003983static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003984 struct nfs_fattr *fattr, struct nfs4_label *label,
3985 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003986{
Trond Myklebust0688e642019-04-07 13:59:09 -04003987 struct nfs4_exception exception = {
3988 .interruptible = true,
3989 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990 int err;
3991 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003992 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003993 trace_nfs4_getattr(server, fhandle, fattr, err);
3994 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003995 &exception);
3996 } while (exception.retry);
3997 return err;
3998}
3999
4000/*
4001 * The file is not closed if it is opened due to the a request to change
4002 * the size of the file. The open call will not be needed once the
4003 * VFS layer lookup-intents are implemented.
4004 *
4005 * Close is called when the inode is destroyed.
4006 * If we haven't opened the file for O_WRONLY, we
4007 * need to in the size_change case to obtain a stateid.
4008 *
4009 * Got race?
4010 * Because OPEN is always done by name in nfsv4, it is
4011 * possible that we opened a different file by the same
4012 * name. We can recognize this race condition, but we
4013 * can't do anything about it besides returning an error.
4014 *
4015 * This will be fixed with VFS changes (lookup-intent).
4016 */
4017static int
4018nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4019 struct iattr *sattr)
4020{
David Howells2b0143b2015-03-17 22:25:59 +00004021 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004022 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004023 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004024 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004025 int status;
4026
Peng Tao88ac8152014-09-12 11:04:10 +08004027 if (pnfs_ld_layoutret_on_setattr(inode) &&
4028 sattr->ia_valid & ATTR_SIZE &&
4029 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004030 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004031
Trond Myklebust0e574af2005-10-27 22:12:38 -04004032 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004033
Andy Adamson26699402012-05-30 16:12:24 -04004034 /* Deal with open(O_TRUNC) */
4035 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004036 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004037
4038 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004039 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004040 return 0;
4041
Trond Myklebustd5308382005-11-04 15:33:38 -05004042 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004043 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004044
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004045 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004046 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004047 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004048 }
4049
David Quigley14c43f72013-05-22 12:50:43 -04004050 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4051 if (IS_ERR(label))
4052 return PTR_ERR(label);
4053
Trond Myklebust199366f2018-03-20 16:43:18 -04004054 /* Return any delegations if we're going to change ACLs */
4055 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004056 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004057
NeilBrown29b59f92016-10-13 15:26:47 +11004058 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004059 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004060 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004061 nfs_setsecurity(inode, fattr, label);
4062 }
David Quigley14c43f72013-05-22 12:50:43 -04004063 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 return status;
4065}
4066
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004067static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4068 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004069 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004070{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004071 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004072 int status;
4073 struct nfs4_lookup_arg args = {
4074 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004075 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004076 .name = name,
4077 };
4078 struct nfs4_lookup_res res = {
4079 .server = server,
4080 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004081 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004082 .fh = fhandle,
4083 };
4084 struct rpc_message msg = {
4085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4086 .rpc_argp = &args,
4087 .rpc_resp = &res,
4088 };
4089
David Quigleyaa9c2662013-05-22 12:50:44 -04004090 args.bitmask = nfs4_bitmask(server, label);
4091
David Howells2b3de442006-08-22 20:06:09 -04004092 nfs_fattr_init(fattr);
4093
Linus Torvalds1da177e2005-04-16 15:20:36 -07004094 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004095 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004096 dprintk("NFS reply lookup: %d\n", status);
4097 return status;
4098}
4099
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004100static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004101{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004102 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004103 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004104 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4105 fattr->nlink = 2;
4106}
4107
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004108static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004109 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004110 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004111{
Trond Myklebust0688e642019-04-07 13:59:09 -04004112 struct nfs4_exception exception = {
4113 .interruptible = true,
4114 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004115 struct rpc_clnt *client = *clnt;
4116 int err;
4117 do {
David Quigley1775fd32013-05-22 12:50:42 -04004118 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004119 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004120 switch (err) {
4121 case -NFS4ERR_BADNAME:
4122 err = -ENOENT;
4123 goto out;
4124 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004125 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004126 if (err == -NFS4ERR_MOVED)
4127 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004128 goto out;
4129 case -NFS4ERR_WRONGSEC:
4130 err = -EPERM;
4131 if (client != *clnt)
4132 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004133 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004134 if (IS_ERR(client))
4135 return PTR_ERR(client);
4136
4137 exception.retry = 1;
4138 break;
4139 default:
4140 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4141 }
4142 } while (exception.retry);
4143
4144out:
4145 if (err == 0)
4146 *clnt = client;
4147 else if (client != *clnt)
4148 rpc_shutdown_client(client);
4149
4150 return err;
4151}
4152
Al Virobeffb8f2016-07-20 16:34:42 -04004153static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004154 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4155 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004157 int status;
4158 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004159
David Quigley1775fd32013-05-22 12:50:42 -04004160 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004161 if (client != NFS_CLIENT(dir)) {
4162 rpc_shutdown_client(client);
4163 nfs_fixup_secinfo_attributes(fattr);
4164 }
4165 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004166}
4167
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004168struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004169nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004170 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4171{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004172 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004173 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004174
David Quigley1775fd32013-05-22 12:50:42 -04004175 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004176 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004177 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004178 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004179}
4180
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004181static int _nfs4_proc_lookupp(struct inode *inode,
4182 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4183 struct nfs4_label *label)
4184{
4185 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4186 struct nfs_server *server = NFS_SERVER(inode);
4187 int status;
4188 struct nfs4_lookupp_arg args = {
4189 .bitmask = server->attr_bitmask,
4190 .fh = NFS_FH(inode),
4191 };
4192 struct nfs4_lookupp_res res = {
4193 .server = server,
4194 .fattr = fattr,
4195 .label = label,
4196 .fh = fhandle,
4197 };
4198 struct rpc_message msg = {
4199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4200 .rpc_argp = &args,
4201 .rpc_resp = &res,
4202 };
4203
4204 args.bitmask = nfs4_bitmask(server, label);
4205
4206 nfs_fattr_init(fattr);
4207
4208 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4209 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4210 &res.seq_res, 0);
4211 dprintk("NFS reply lookupp: %d\n", status);
4212 return status;
4213}
4214
4215static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4216 struct nfs_fattr *fattr, struct nfs4_label *label)
4217{
Trond Myklebust0688e642019-04-07 13:59:09 -04004218 struct nfs4_exception exception = {
4219 .interruptible = true,
4220 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004221 int err;
4222 do {
4223 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4224 trace_nfs4_lookupp(inode, err);
4225 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4226 &exception);
4227 } while (exception.retry);
4228 return err;
4229}
4230
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4232{
Trond Myklebust76b32992007-08-10 17:45:11 -04004233 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004234 struct nfs4_accessargs args = {
4235 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004236 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004237 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004238 struct nfs4_accessres res = {
4239 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004240 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 struct rpc_message msg = {
4242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4243 .rpc_argp = &args,
4244 .rpc_resp = &res,
4245 .rpc_cred = entry->cred,
4246 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004247 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248
Trond Myklebust7c672652018-06-04 15:00:53 -04004249 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004250 res.fattr = nfs_alloc_fattr();
4251 if (res.fattr == NULL)
4252 return -ENOMEM;
4253 args.bitmask = server->cache_consistency_bitmask;
4254 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004255 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004257 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004258 if (res.fattr)
4259 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004261 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262 return status;
4263}
4264
4265static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4266{
Trond Myklebust0688e642019-04-07 13:59:09 -04004267 struct nfs4_exception exception = {
4268 .interruptible = true,
4269 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 int err;
4271 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004272 err = _nfs4_proc_access(inode, entry);
4273 trace_nfs4_access(inode, err);
4274 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 &exception);
4276 } while (exception.retry);
4277 return err;
4278}
4279
4280/*
4281 * TODO: For the time being, we don't try to get any attributes
4282 * along with any of the zero-copy operations READ, READDIR,
4283 * READLINK, WRITE.
4284 *
4285 * In the case of the first three, we want to put the GETATTR
4286 * after the read-type operation -- this is because it is hard
4287 * to predict the length of a GETATTR response in v4, and thus
4288 * align the READ data correctly. This means that the GETATTR
4289 * may end up partially falling into the page cache, and we should
4290 * shift it into the 'tail' of the xdr_buf before processing.
4291 * To do this efficiently, we need to know the total length
4292 * of data received, which doesn't seem to be available outside
4293 * of the RPC layer.
4294 *
4295 * In the case of WRITE, we also want to put the GETATTR after
4296 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004297 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 *
4299 * Both of these changes to the XDR layer would in fact be quite
4300 * minor, but I decided to leave them for a subsequent patch.
4301 */
4302static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4303 unsigned int pgbase, unsigned int pglen)
4304{
4305 struct nfs4_readlink args = {
4306 .fh = NFS_FH(inode),
4307 .pgbase = pgbase,
4308 .pglen = pglen,
4309 .pages = &page,
4310 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004311 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 struct rpc_message msg = {
4313 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4314 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004315 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004316 };
4317
Bryan Schumaker7c513052011-03-24 17:12:24 +00004318 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 -07004319}
4320
4321static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4322 unsigned int pgbase, unsigned int pglen)
4323{
Trond Myklebust0688e642019-04-07 13:59:09 -04004324 struct nfs4_exception exception = {
4325 .interruptible = true,
4326 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004327 int err;
4328 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004329 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4330 trace_nfs4_readlink(inode, err);
4331 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004332 &exception);
4333 } while (exception.retry);
4334 return err;
4335}
4336
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004338 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004340static int
4341nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004342 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004343{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004344 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004345 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004346 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 int status = 0;
4349
NeilBrown532d4de2016-10-13 15:26:47 +11004350 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004351 if (IS_ERR(ctx))
4352 return PTR_ERR(ctx);
4353
David Quigleyaa9c2662013-05-22 12:50:44 -04004354 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4355
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004356 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4357 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004358 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004359 if (IS_ERR(state)) {
4360 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004361 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004364 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004365 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366 return status;
4367}
4368
Trond Myklebust3c591172018-07-31 15:54:10 -04004369static int
4370_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371{
Trond Myklebust16e42952005-10-27 22:12:44 -04004372 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004373 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004375 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004377 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004378 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004379 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004381 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4382 .rpc_argp = &args,
4383 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004385 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004386 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004387
Bryan Schumaker7c513052011-03-24 17:12:24 +00004388 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004389 if (status == 0) {
4390 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004391 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004392 /* Removing a directory decrements nlink in the parent */
4393 if (ftype == NF4DIR && dir->i_nlink > 2)
4394 nfs4_dec_nlink_locked(dir);
4395 spin_unlock(&dir->i_lock);
4396 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397 return status;
4398}
4399
Trond Myklebust912678d2018-03-20 16:43:15 -04004400static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4401{
Trond Myklebust0688e642019-04-07 13:59:09 -04004402 struct nfs4_exception exception = {
4403 .interruptible = true,
4404 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004405 struct inode *inode = d_inode(dentry);
4406 int err;
4407
Trond Myklebustc01d3642018-03-20 16:43:20 -04004408 if (inode) {
4409 if (inode->i_nlink == 1)
4410 nfs4_inode_return_delegation(inode);
4411 else
4412 nfs4_inode_make_writeable(inode);
4413 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004414 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004415 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004416 trace_nfs4_remove(dir, &dentry->d_name, err);
4417 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4418 &exception);
4419 } while (exception.retry);
4420 return err;
4421}
4422
4423static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004424{
Trond Myklebust0688e642019-04-07 13:59:09 -04004425 struct nfs4_exception exception = {
4426 .interruptible = true,
4427 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004428 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004429
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004431 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004432 trace_nfs4_remove(dir, name, err);
4433 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 &exception);
4435 } while (exception.retry);
4436 return err;
4437}
4438
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004439static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4440 struct dentry *dentry,
4441 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004442{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004443 struct nfs_removeargs *args = msg->rpc_argp;
4444 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004445
Trond Myklebust977fcc22018-03-20 16:43:17 -04004446 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004448 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004449
4450 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004451
4452 if (inode)
4453 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454}
4455
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004456static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4457{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004458 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004459 &data->args.seq_args,
4460 &data->res.seq_res,
4461 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004462}
4463
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004464static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004466 struct nfs_unlinkdata *data = task->tk_calldata;
4467 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004468
Trond Myklebust14516c32010-07-31 14:29:06 -04004469 if (!nfs4_sequence_done(task, &res->seq_res))
4470 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004471 if (nfs4_async_handle_error(task, res->server, NULL,
4472 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004473 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004474 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004475 update_changeattr(dir, &res->cinfo,
4476 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004477 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478}
4479
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004480static void nfs4_proc_rename_setup(struct rpc_message *msg,
4481 struct dentry *old_dentry,
4482 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004483{
Jeff Laytond3d41522010-09-17 17:31:57 -04004484 struct nfs_renameargs *arg = msg->rpc_argp;
4485 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004486 struct inode *old_inode = d_inode(old_dentry);
4487 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004488
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004489 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004490 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004491 if (new_inode)
4492 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004493 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004494 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004495 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004496}
4497
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004498static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4499{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004500 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004501 &data->args.seq_args,
4502 &data->res.seq_res,
4503 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004504}
4505
4506static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4507 struct inode *new_dir)
4508{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004509 struct nfs_renamedata *data = task->tk_calldata;
4510 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004511
4512 if (!nfs4_sequence_done(task, &res->seq_res))
4513 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004514 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004515 return 0;
4516
Trond Myklebustc733c492017-01-11 12:32:26 -05004517 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004518 if (new_dir != old_dir) {
4519 /* Note: If we moved a directory, nlink will change */
4520 update_changeattr(old_dir, &res->old_cinfo,
4521 res->old_fattr->time_start,
4522 NFS_INO_INVALID_OTHER);
4523 update_changeattr(new_dir, &res->new_cinfo,
4524 res->new_fattr->time_start,
4525 NFS_INO_INVALID_OTHER);
4526 } else
4527 update_changeattr(old_dir, &res->old_cinfo,
4528 res->old_fattr->time_start,
4529 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004530 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004531 return 1;
4532}
4533
Al Virobeffb8f2016-07-20 16:34:42 -04004534static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004536 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004537 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 struct nfs4_link_arg arg = {
4539 .fh = NFS_FH(inode),
4540 .dir_fh = NFS_FH(dir),
4541 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004542 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004544 struct nfs4_link_res res = {
4545 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004546 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004547 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 struct rpc_message msg = {
4549 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4550 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004551 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004553 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554
Trond Myklebust136f2622010-04-16 16:22:49 -04004555 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004556 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004557 goto out;
4558
David Quigley14c43f72013-05-22 12:50:43 -04004559 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4560 if (IS_ERR(res.label)) {
4561 status = PTR_ERR(res.label);
4562 goto out;
4563 }
4564
Trond Myklebustc01d3642018-03-20 16:43:20 -04004565 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004566 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004567
Bryan Schumaker7c513052011-03-24 17:12:24 +00004568 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004569 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004570 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004571 status = nfs_post_op_update_inode(inode, res.fattr);
4572 if (!status)
4573 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004574 }
David Quigley14c43f72013-05-22 12:50:43 -04004575
4576
4577 nfs4_label_free(res.label);
4578
Trond Myklebust136f2622010-04-16 16:22:49 -04004579out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004580 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 return status;
4582}
4583
Al Virobeffb8f2016-07-20 16:34:42 -04004584static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585{
Trond Myklebust0688e642019-04-07 13:59:09 -04004586 struct nfs4_exception exception = {
4587 .interruptible = true,
4588 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 int err;
4590 do {
4591 err = nfs4_handle_exception(NFS_SERVER(inode),
4592 _nfs4_proc_link(inode, dir, name),
4593 &exception);
4594 } while (exception.retry);
4595 return err;
4596}
4597
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004598struct nfs4_createdata {
4599 struct rpc_message msg;
4600 struct nfs4_create_arg arg;
4601 struct nfs4_create_res res;
4602 struct nfs_fh fh;
4603 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004604 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004605};
4606
4607static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004608 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004609{
4610 struct nfs4_createdata *data;
4611
4612 data = kzalloc(sizeof(*data), GFP_KERNEL);
4613 if (data != NULL) {
4614 struct nfs_server *server = NFS_SERVER(dir);
4615
David Quigley14c43f72013-05-22 12:50:43 -04004616 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4617 if (IS_ERR(data->label))
4618 goto out_free;
4619
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004620 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4621 data->msg.rpc_argp = &data->arg;
4622 data->msg.rpc_resp = &data->res;
4623 data->arg.dir_fh = NFS_FH(dir);
4624 data->arg.server = server;
4625 data->arg.name = name;
4626 data->arg.attrs = sattr;
4627 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004628 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004629 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004630 data->res.server = server;
4631 data->res.fh = &data->fh;
4632 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004633 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004634 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004635 }
4636 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004637out_free:
4638 kfree(data);
4639 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004640}
4641
4642static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4643{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004644 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004645 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004646 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004647 spin_lock(&dir->i_lock);
4648 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004649 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004650 /* Creating a directory bumps nlink in the parent */
4651 if (data->arg.ftype == NF4DIR)
4652 nfs4_inc_nlink_locked(dir);
4653 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004654 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004655 }
4656 return status;
4657}
4658
4659static void nfs4_free_createdata(struct nfs4_createdata *data)
4660{
David Quigley14c43f72013-05-22 12:50:43 -04004661 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004662 kfree(data);
4663}
4664
Chuck Lever4f390c12006-08-22 20:06:22 -04004665static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004666 struct page *page, unsigned int len, struct iattr *sattr,
4667 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004669 struct nfs4_createdata *data;
4670 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004671
Chuck Lever94a6d752006-08-22 20:06:23 -04004672 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004673 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004674
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004675 status = -ENOMEM;
4676 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4677 if (data == NULL)
4678 goto out;
4679
4680 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4681 data->arg.u.symlink.pages = &page;
4682 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004683 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004685 status = nfs4_do_create(dir, dentry, data);
4686
4687 nfs4_free_createdata(data);
4688out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 return status;
4690}
4691
Chuck Lever4f390c12006-08-22 20:06:22 -04004692static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004693 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694{
Trond Myklebust0688e642019-04-07 13:59:09 -04004695 struct nfs4_exception exception = {
4696 .interruptible = true,
4697 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004698 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004699 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004700
4701 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4702
Linus Torvalds1da177e2005-04-16 15:20:36 -07004703 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004704 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4705 trace_nfs4_symlink(dir, &dentry->d_name, err);
4706 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 &exception);
4708 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004709
4710 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 return err;
4712}
4713
4714static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004715 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004717 struct nfs4_createdata *data;
4718 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004720 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4721 if (data == NULL)
4722 goto out;
4723
David Quigley1775fd32013-05-22 12:50:42 -04004724 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004725 status = nfs4_do_create(dir, dentry, data);
4726
4727 nfs4_free_createdata(data);
4728out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729 return status;
4730}
4731
4732static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4733 struct iattr *sattr)
4734{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004735 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004736 struct nfs4_exception exception = {
4737 .interruptible = true,
4738 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004739 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004741
David Quigleyaa9c2662013-05-22 12:50:44 -04004742 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4743
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004744 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4745 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004746 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004747 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4748 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4749 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004750 &exception);
4751 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004752 nfs4_label_release_security(label);
4753
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754 return err;
4755}
4756
NeilBrown684f39b2018-12-03 11:30:30 +11004757static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004758 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759{
David Howells2b0143b2015-03-17 22:25:59 +00004760 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761 struct nfs4_readdir_arg args = {
4762 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004763 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 .pgbase = 0,
4765 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004766 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004767 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 };
4769 struct nfs4_readdir_res res;
4770 struct rpc_message msg = {
4771 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4772 .rpc_argp = &args,
4773 .rpc_resp = &res,
4774 .rpc_cred = cred,
4775 };
4776 int status;
4777
Al Viro6de14722013-09-16 10:53:17 -04004778 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4779 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004780 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004781 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004782 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004783 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 -05004784 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004785 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004786 status += args.pgbase;
4787 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004788
4789 nfs_invalidate_atime(dir);
4790
Harvey Harrison3110ff82008-05-02 13:42:44 -07004791 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004792 return status;
4793}
4794
NeilBrown684f39b2018-12-03 11:30:30 +11004795static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004796 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004797{
Trond Myklebust0688e642019-04-07 13:59:09 -04004798 struct nfs4_exception exception = {
4799 .interruptible = true,
4800 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 int err;
4802 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004803 err = _nfs4_proc_readdir(dentry, cred, cookie,
4804 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004805 trace_nfs4_readdir(d_inode(dentry), err);
4806 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 &exception);
4808 } while (exception.retry);
4809 return err;
4810}
4811
4812static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004813 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004814{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004815 struct nfs4_createdata *data;
4816 int mode = sattr->ia_mode;
4817 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004818
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004819 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4820 if (data == NULL)
4821 goto out;
4822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004824 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004826 data->arg.ftype = NF4BLK;
4827 data->arg.u.device.specdata1 = MAJOR(rdev);
4828 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 }
4830 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004831 data->arg.ftype = NF4CHR;
4832 data->arg.u.device.specdata1 = MAJOR(rdev);
4833 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004834 } else if (!S_ISSOCK(mode)) {
4835 status = -EINVAL;
4836 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004837 }
David Quigley1775fd32013-05-22 12:50:42 -04004838
David Quigleyaa9c2662013-05-22 12:50:44 -04004839 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004840 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004841out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004842 nfs4_free_createdata(data);
4843out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 return status;
4845}
4846
4847static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4848 struct iattr *sattr, dev_t rdev)
4849{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004850 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004851 struct nfs4_exception exception = {
4852 .interruptible = true,
4853 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004854 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004856
David Quigleyaa9c2662013-05-22 12:50:44 -04004857 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4858
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004859 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4860 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004861 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004862 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4863 trace_nfs4_mknod(dir, &dentry->d_name, err);
4864 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865 &exception);
4866 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004867
4868 nfs4_label_release_security(label);
4869
Linus Torvalds1da177e2005-04-16 15:20:36 -07004870 return err;
4871}
4872
4873static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4874 struct nfs_fsstat *fsstat)
4875{
4876 struct nfs4_statfs_arg args = {
4877 .fh = fhandle,
4878 .bitmask = server->attr_bitmask,
4879 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004880 struct nfs4_statfs_res res = {
4881 .fsstat = fsstat,
4882 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004883 struct rpc_message msg = {
4884 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4885 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004886 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004887 };
4888
Trond Myklebust0e574af2005-10-27 22:12:38 -04004889 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004890 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
4892
4893static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4894{
Trond Myklebust0688e642019-04-07 13:59:09 -04004895 struct nfs4_exception exception = {
4896 .interruptible = true,
4897 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 int err;
4899 do {
4900 err = nfs4_handle_exception(server,
4901 _nfs4_proc_statfs(server, fhandle, fsstat),
4902 &exception);
4903 } while (exception.retry);
4904 return err;
4905}
4906
4907static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4908 struct nfs_fsinfo *fsinfo)
4909{
4910 struct nfs4_fsinfo_arg args = {
4911 .fh = fhandle,
4912 .bitmask = server->attr_bitmask,
4913 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004914 struct nfs4_fsinfo_res res = {
4915 .fsinfo = fsinfo,
4916 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004917 struct rpc_message msg = {
4918 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4919 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004920 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004921 };
4922
Bryan Schumaker7c513052011-03-24 17:12:24 +00004923 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924}
4925
4926static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4927{
Trond Myklebust0688e642019-04-07 13:59:09 -04004928 struct nfs4_exception exception = {
4929 .interruptible = true,
4930 };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004931 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 int err;
4933
4934 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004935 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004936 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004937 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004938 nfs4_set_lease_period(server->nfs_client,
4939 fsinfo->lease_time * HZ,
4940 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004941 break;
4942 }
4943 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004944 } while (exception.retry);
4945 return err;
4946}
4947
4948static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4949{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004950 int error;
4951
Trond Myklebust0e574af2005-10-27 22:12:38 -04004952 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004953 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004954 if (error == 0) {
4955 /* block layout checks this! */
4956 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004957 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004958 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004959
4960 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961}
4962
4963static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4964 struct nfs_pathconf *pathconf)
4965{
4966 struct nfs4_pathconf_arg args = {
4967 .fh = fhandle,
4968 .bitmask = server->attr_bitmask,
4969 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004970 struct nfs4_pathconf_res res = {
4971 .pathconf = pathconf,
4972 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973 struct rpc_message msg = {
4974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4975 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004976 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 };
4978
4979 /* None of the pathconf attributes are mandatory to implement */
4980 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4981 memset(pathconf, 0, sizeof(*pathconf));
4982 return 0;
4983 }
4984
Trond Myklebust0e574af2005-10-27 22:12:38 -04004985 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004986 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987}
4988
4989static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4990 struct nfs_pathconf *pathconf)
4991{
Trond Myklebust0688e642019-04-07 13:59:09 -04004992 struct nfs4_exception exception = {
4993 .interruptible = true,
4994 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004995 int err;
4996
4997 do {
4998 err = nfs4_handle_exception(server,
4999 _nfs4_proc_pathconf(server, fhandle, pathconf),
5000 &exception);
5001 } while (exception.retry);
5002 return err;
5003}
5004
Trond Myklebust5521abf2013-03-16 20:54:34 -04005005int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005006 const struct nfs_open_context *ctx,
5007 const struct nfs_lock_context *l_ctx,
5008 fmode_t fmode)
5009{
NeilBrown17393472016-10-13 15:26:47 +11005010 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005011}
5012EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5013
Trond Myklebust5521abf2013-03-16 20:54:34 -04005014static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5015 const struct nfs_open_context *ctx,
5016 const struct nfs_lock_context *l_ctx,
5017 fmode_t fmode)
5018{
5019 nfs4_stateid current_stateid;
5020
Trond Myklebuste1253be2014-03-05 08:44:23 -05005021 /* If the current stateid represents a lost lock, then exit */
5022 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
5023 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005024 return nfs4_stateid_match(stateid, &current_stateid);
5025}
5026
5027static bool nfs4_error_stateid_expired(int err)
5028{
5029 switch (err) {
5030 case -NFS4ERR_DELEG_REVOKED:
5031 case -NFS4ERR_ADMIN_REVOKED:
5032 case -NFS4ERR_BAD_STATEID:
5033 case -NFS4ERR_STALE_STATEID:
5034 case -NFS4ERR_OLD_STATEID:
5035 case -NFS4ERR_OPENMODE:
5036 case -NFS4ERR_EXPIRED:
5037 return true;
5038 }
5039 return false;
5040}
5041
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005042static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005043{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005044 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005045
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005046 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005047 if (task->tk_status < 0) {
5048 struct nfs4_exception exception = {
5049 .inode = hdr->inode,
5050 .state = hdr->args.context->state,
5051 .stateid = &hdr->args.stateid,
5052 };
5053 task->tk_status = nfs4_async_handle_exception(task,
5054 server, task->tk_status, &exception);
5055 if (exception.retry) {
5056 rpc_restart_call_prepare(task);
5057 return -EAGAIN;
5058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005060
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005062 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005063 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064}
5065
Trond Myklebust5521abf2013-03-16 20:54:34 -04005066static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005067 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005068{
5069
5070 if (!nfs4_error_stateid_expired(task->tk_status) ||
5071 nfs4_stateid_is_current(&args->stateid,
5072 args->context,
5073 args->lock_context,
5074 FMODE_READ))
5075 return false;
5076 rpc_restart_call_prepare(task);
5077 return true;
5078}
5079
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005080static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005081{
5082
5083 dprintk("--> %s\n", __func__);
5084
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005085 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005086 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005087 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005088 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005089 if (task->tk_status > 0)
5090 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005091 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5092 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005093}
5094
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005095static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5096 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005098 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005099 if (!hdr->pgio_done_cb)
5100 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005101 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005102 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005103}
5104
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005105static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5106 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005107{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005108 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005109 &hdr->args.seq_args,
5110 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005111 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005112 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005113 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5114 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005115 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005116 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005117 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005118 return -EIO;
5119 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120}
5121
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005122static int nfs4_write_done_cb(struct rpc_task *task,
5123 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005124{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005125 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005126
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005127 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005128 if (task->tk_status < 0) {
5129 struct nfs4_exception exception = {
5130 .inode = hdr->inode,
5131 .state = hdr->args.context->state,
5132 .stateid = &hdr->args.stateid,
5133 };
5134 task->tk_status = nfs4_async_handle_exception(task,
5135 NFS_SERVER(inode), task->tk_status,
5136 &exception);
5137 if (exception.retry) {
5138 rpc_restart_call_prepare(task);
5139 return -EAGAIN;
5140 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005141 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005142 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005143 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005144 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005145 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005146 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005147}
5148
Trond Myklebust5521abf2013-03-16 20:54:34 -04005149static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005150 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005151{
5152
5153 if (!nfs4_error_stateid_expired(task->tk_status) ||
5154 nfs4_stateid_is_current(&args->stateid,
5155 args->context,
5156 args->lock_context,
5157 FMODE_WRITE))
5158 return false;
5159 rpc_restart_call_prepare(task);
5160 return true;
5161}
5162
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005163static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005164{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005165 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005166 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005167 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005168 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005169 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5170 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005171}
5172
Trond Myklebust5a37f852012-04-28 14:55:16 -04005173static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005174bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005175{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005176 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005177 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005178 return false;
5179 /* Otherwise, request attributes if and only if we don't hold
5180 * a delegation
5181 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005182 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005183}
Fred Isamana69aef12011-03-03 15:13:47 +00005184
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005185static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005186 struct rpc_message *msg,
5187 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005188{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005189 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005190
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005191 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5192 hdr->args.bitmask = NULL;
5193 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005194 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005195 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005196
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005197 if (!hdr->pgio_done_cb)
5198 hdr->pgio_done_cb = nfs4_write_done_cb;
5199 hdr->res.server = server;
5200 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005201
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005202 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005203 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005204 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205}
5206
Fred Isaman0b7c0152012-04-20 14:47:39 -04005207static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5208{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005209 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005210 &data->args.seq_args,
5211 &data->res.seq_res,
5212 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005213}
5214
Fred Isaman0b7c0152012-04-20 14:47:39 -04005215static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005216{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005217 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005218
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005219 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005220 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5221 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005222 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005223 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005224 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005225 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005226}
5227
Fred Isaman0b7c0152012-04-20 14:47:39 -04005228static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005229{
5230 if (!nfs4_sequence_done(task, &data->res.seq_res))
5231 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005232 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005233}
5234
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005235static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5236 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005237{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005238 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005239
Fred Isaman0b7c0152012-04-20 14:47:39 -04005240 if (data->commit_done_cb == NULL)
5241 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005242 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005243 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005244 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005245 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005246}
5247
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005248static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5249 struct nfs_commitres *res)
5250{
5251 struct inode *dst_inode = file_inode(dst);
5252 struct nfs_server *server = NFS_SERVER(dst_inode);
5253 struct rpc_message msg = {
5254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5255 .rpc_argp = args,
5256 .rpc_resp = res,
5257 };
5258
5259 args->fh = NFS_FH(dst_inode);
5260 return nfs4_call_sync(server->client, server, &msg,
5261 &args->seq_args, &res->seq_res, 1);
5262}
5263
5264int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5265{
5266 struct nfs_commitargs args = {
5267 .offset = offset,
5268 .count = count,
5269 };
5270 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5271 struct nfs4_exception exception = { };
5272 int status;
5273
5274 do {
5275 status = _nfs4_proc_commit(dst, &args, res);
5276 status = nfs4_handle_exception(dst_server, status, &exception);
5277 } while (exception.retry);
5278
5279 return status;
5280}
5281
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005282struct nfs4_renewdata {
5283 struct nfs_client *client;
5284 unsigned long timestamp;
5285};
5286
Linus Torvalds1da177e2005-04-16 15:20:36 -07005287/*
5288 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5289 * standalone procedure for queueing an asynchronous RENEW.
5290 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005291static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005292{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005293 struct nfs4_renewdata *data = calldata;
5294 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005295
Elena Reshetova212bf412017-10-20 12:53:38 +03005296 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005297 nfs4_schedule_state_renewal(clp);
5298 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005299 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005300}
5301
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005302static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005303{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005304 struct nfs4_renewdata *data = calldata;
5305 struct nfs_client *clp = data->client;
5306 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005307
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005308 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005309 switch (task->tk_status) {
5310 case 0:
5311 break;
5312 case -NFS4ERR_LEASE_MOVED:
5313 nfs4_schedule_lease_moved_recovery(clp);
5314 break;
5315 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005316 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005317 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5318 return;
5319 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005320 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005321 return;
5322 }
5323 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005325 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005326}
5327
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005328static const struct rpc_call_ops nfs4_renew_ops = {
5329 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005330 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005331};
5332
NeilBrowna52458b2018-12-03 11:30:31 +11005333static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005334{
5335 struct rpc_message msg = {
5336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5337 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005338 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005339 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005340 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005341
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005342 if (renew_flags == 0)
5343 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005344 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005345 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005346 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005347 if (data == NULL) {
5348 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005349 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005350 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005351 data->client = clp;
5352 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005353 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005354 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005355}
5356
NeilBrowna52458b2018-12-03 11:30:31 +11005357static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005358{
5359 struct rpc_message msg = {
5360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5361 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005362 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363 };
5364 unsigned long now = jiffies;
5365 int status;
5366
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005367 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005368 if (status < 0)
5369 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005370 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371 return 0;
5372}
5373
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005374static inline int nfs4_server_supports_acls(struct nfs_server *server)
5375{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005376 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005377}
5378
Trond Myklebust21f498c2012-08-24 10:59:25 -04005379/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5380 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005381 * the stack.
5382 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005383#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005384
Neil Hormane9e3d722011-03-04 19:26:03 -05005385static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005386 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005387{
5388 struct page *newpage, **spages;
5389 int rc = 0;
5390 size_t len;
5391 spages = pages;
5392
5393 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005394 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005395 newpage = alloc_page(GFP_KERNEL);
5396
5397 if (newpage == NULL)
5398 goto unwind;
5399 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005400 buf += len;
5401 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005402 *pages++ = newpage;
5403 rc++;
5404 } while (buflen != 0);
5405
5406 return rc;
5407
5408unwind:
5409 for(; rc > 0; rc--)
5410 __free_page(spages[rc-1]);
5411 return -ENOMEM;
5412}
5413
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005414struct nfs4_cached_acl {
5415 int cached;
5416 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005417 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005418};
5419
5420static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005421{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005422 struct nfs_inode *nfsi = NFS_I(inode);
5423
5424 spin_lock(&inode->i_lock);
5425 kfree(nfsi->nfs4_acl);
5426 nfsi->nfs4_acl = acl;
5427 spin_unlock(&inode->i_lock);
5428}
5429
5430static void nfs4_zap_acl_attr(struct inode *inode)
5431{
5432 nfs4_set_cached_acl(inode, NULL);
5433}
5434
5435static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5436{
5437 struct nfs_inode *nfsi = NFS_I(inode);
5438 struct nfs4_cached_acl *acl;
5439 int ret = -ENOENT;
5440
5441 spin_lock(&inode->i_lock);
5442 acl = nfsi->nfs4_acl;
5443 if (acl == NULL)
5444 goto out;
5445 if (buf == NULL) /* user is just asking for length */
5446 goto out_len;
5447 if (acl->cached == 0)
5448 goto out;
5449 ret = -ERANGE; /* see getxattr(2) man page */
5450 if (acl->len > buflen)
5451 goto out;
5452 memcpy(buf, acl->data, acl->len);
5453out_len:
5454 ret = acl->len;
5455out:
5456 spin_unlock(&inode->i_lock);
5457 return ret;
5458}
5459
Sachin Prabhu5794d212012-04-17 14:36:40 +01005460static 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 +00005461{
5462 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005463 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005464
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005465 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005466 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005467 if (acl == NULL)
5468 goto out;
5469 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005470 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005471 } else {
5472 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5473 if (acl == NULL)
5474 goto out;
5475 acl->cached = 0;
5476 }
5477 acl->len = acl_len;
5478out:
5479 nfs4_set_cached_acl(inode, acl);
5480}
5481
Andy Adamsonbf118a32011-12-07 11:55:27 -05005482/*
5483 * The getxattr API returns the required buffer length when called with a
5484 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5485 * the required buf. On a NULL buf, we send a page of data to the server
5486 * guessing that the ACL request can be serviced by a page. If so, we cache
5487 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5488 * the cache. If not so, we throw away the page, and cache the required
5489 * length. The next getxattr call will then produce another round trip to
5490 * the server, this time with the input buf of the required size.
5491 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005492static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005493{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005494 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005495 struct nfs_getaclargs args = {
5496 .fh = NFS_FH(inode),
5497 .acl_pages = pages,
5498 .acl_len = buflen,
5499 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005500 struct nfs_getaclres res = {
5501 .acl_len = buflen,
5502 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005503 struct rpc_message msg = {
5504 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5505 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005506 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005507 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005508 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005509 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005510
Trond Myklebust21f498c2012-08-24 10:59:25 -04005511 if (npages > ARRAY_SIZE(pages))
5512 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005513
Andy Adamsonbf118a32011-12-07 11:55:27 -05005514 for (i = 0; i < npages; i++) {
5515 pages[i] = alloc_page(GFP_KERNEL);
5516 if (!pages[i])
5517 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005518 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005519
5520 /* for decoding across pages */
5521 res.acl_scratch = alloc_page(GFP_KERNEL);
5522 if (!res.acl_scratch)
5523 goto out_free;
5524
Andy Adamsonbf118a32011-12-07 11:55:27 -05005525 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005526
Peng Taode040be2012-01-10 22:42:47 +08005527 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005528 __func__, buf, buflen, npages, args.acl_len);
5529 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5530 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005531 if (ret)
5532 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005533
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005534 /* Handle the case where the passed-in buffer is too short */
5535 if (res.acl_flags & NFS4_ACL_TRUNC) {
5536 /* Did the user only issue a request for the acl length? */
5537 if (buf == NULL)
5538 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005539 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005540 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005541 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005542 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005543 if (buf) {
5544 if (res.acl_len > buflen) {
5545 ret = -ERANGE;
5546 goto out_free;
5547 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005548 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005549 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005550out_ok:
5551 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005552out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005553 for (i = 0; i < npages; i++)
5554 if (pages[i])
5555 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005556 if (res.acl_scratch)
5557 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005558 return ret;
5559}
5560
Trond Myklebust16b42892006-08-24 12:27:15 -04005561static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5562{
Trond Myklebust0688e642019-04-07 13:59:09 -04005563 struct nfs4_exception exception = {
5564 .interruptible = true,
5565 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005566 ssize_t ret;
5567 do {
5568 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005569 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005570 if (ret >= 0)
5571 break;
5572 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5573 } while (exception.retry);
5574 return ret;
5575}
5576
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005577static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5578{
5579 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005580 int ret;
5581
5582 if (!nfs4_server_supports_acls(server))
5583 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005584 ret = nfs_revalidate_inode(server, inode);
5585 if (ret < 0)
5586 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005587 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5588 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005589 ret = nfs4_read_cached_acl(inode, buf, buflen);
5590 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005591 /* -ENOENT is returned if there is no ACL or if there is an ACL
5592 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005593 return ret;
5594 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005595}
5596
Trond Myklebust16b42892006-08-24 12:27:15 -04005597static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005598{
5599 struct nfs_server *server = NFS_SERVER(inode);
5600 struct page *pages[NFS4ACL_MAXPAGES];
5601 struct nfs_setaclargs arg = {
5602 .fh = NFS_FH(inode),
5603 .acl_pages = pages,
5604 .acl_len = buflen,
5605 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005606 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005607 struct rpc_message msg = {
5608 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5609 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005610 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005611 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005612 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005613 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005614
5615 if (!nfs4_server_supports_acls(server))
5616 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005617 if (npages > ARRAY_SIZE(pages))
5618 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005619 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005620 if (i < 0)
5621 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005622 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005623 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005624
5625 /*
5626 * Free each page after tx, so the only ref left is
5627 * held by the network stack
5628 */
5629 for (; i > 0; i--)
5630 put_page(pages[i-1]);
5631
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005632 /*
5633 * Acl update can result in inode attribute update.
5634 * so mark the attribute cache invalid.
5635 */
5636 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005637 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005638 | NFS_INO_INVALID_CTIME
5639 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005640 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005641 nfs_access_zap_cache(inode);
5642 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005643 return ret;
5644}
5645
Trond Myklebust16b42892006-08-24 12:27:15 -04005646static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5647{
5648 struct nfs4_exception exception = { };
5649 int err;
5650 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005651 err = __nfs4_proc_set_acl(inode, buf, buflen);
5652 trace_nfs4_set_acl(inode, err);
5653 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005654 &exception);
5655 } while (exception.retry);
5656 return err;
5657}
5658
David Quigleyaa9c2662013-05-22 12:50:44 -04005659#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5660static int _nfs4_get_security_label(struct inode *inode, void *buf,
5661 size_t buflen)
5662{
5663 struct nfs_server *server = NFS_SERVER(inode);
5664 struct nfs_fattr fattr;
5665 struct nfs4_label label = {0, 0, buflen, buf};
5666
5667 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005668 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005669 .fh = NFS_FH(inode),
5670 .bitmask = bitmask,
5671 };
5672 struct nfs4_getattr_res res = {
5673 .fattr = &fattr,
5674 .label = &label,
5675 .server = server,
5676 };
5677 struct rpc_message msg = {
5678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005679 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005680 .rpc_resp = &res,
5681 };
5682 int ret;
5683
5684 nfs_fattr_init(&fattr);
5685
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005686 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005687 if (ret)
5688 return ret;
5689 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5690 return -ENOENT;
5691 if (buflen < label.len)
5692 return -ERANGE;
5693 return 0;
5694}
5695
5696static int nfs4_get_security_label(struct inode *inode, void *buf,
5697 size_t buflen)
5698{
Trond Myklebust0688e642019-04-07 13:59:09 -04005699 struct nfs4_exception exception = {
5700 .interruptible = true,
5701 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005702 int err;
5703
5704 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5705 return -EOPNOTSUPP;
5706
5707 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005708 err = _nfs4_get_security_label(inode, buf, buflen);
5709 trace_nfs4_get_security_label(inode, err);
5710 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005711 &exception);
5712 } while (exception.retry);
5713 return err;
5714}
5715
5716static int _nfs4_do_set_security_label(struct inode *inode,
5717 struct nfs4_label *ilabel,
5718 struct nfs_fattr *fattr,
5719 struct nfs4_label *olabel)
5720{
5721
5722 struct iattr sattr = {0};
5723 struct nfs_server *server = NFS_SERVER(inode);
5724 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005725 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005726 .fh = NFS_FH(inode),
5727 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005728 .server = server,
5729 .bitmask = bitmask,
5730 .label = ilabel,
5731 };
5732 struct nfs_setattrres res = {
5733 .fattr = fattr,
5734 .label = olabel,
5735 .server = server,
5736 };
5737 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5739 .rpc_argp = &arg,
5740 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005741 };
5742 int status;
5743
Jeff Layton12207f62013-11-01 10:49:32 -04005744 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005745
Jeff Layton12207f62013-11-01 10:49:32 -04005746 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005747 if (status)
5748 dprintk("%s failed: %d\n", __func__, status);
5749
5750 return status;
5751}
5752
5753static int nfs4_do_set_security_label(struct inode *inode,
5754 struct nfs4_label *ilabel,
5755 struct nfs_fattr *fattr,
5756 struct nfs4_label *olabel)
5757{
5758 struct nfs4_exception exception = { };
5759 int err;
5760
5761 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005762 err = _nfs4_do_set_security_label(inode, ilabel,
5763 fattr, olabel);
5764 trace_nfs4_set_security_label(inode, err);
5765 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005766 &exception);
5767 } while (exception.retry);
5768 return err;
5769}
5770
5771static int
Al Viro59301222016-05-27 10:19:30 -04005772nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005773{
5774 struct nfs4_label ilabel, *olabel = NULL;
5775 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005776 int status;
5777
5778 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5779 return -EOPNOTSUPP;
5780
5781 nfs_fattr_init(&fattr);
5782
5783 ilabel.pi = 0;
5784 ilabel.lfs = 0;
5785 ilabel.label = (char *)buf;
5786 ilabel.len = buflen;
5787
David Quigleyaa9c2662013-05-22 12:50:44 -04005788 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5789 if (IS_ERR(olabel)) {
5790 status = -PTR_ERR(olabel);
5791 goto out;
5792 }
5793
5794 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5795 if (status == 0)
5796 nfs_setsecurity(inode, &fattr, olabel);
5797
5798 nfs4_label_free(olabel);
5799out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005800 return status;
5801}
5802#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5803
5804
Chuck Leverf0920752012-05-21 22:45:41 -04005805static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5806 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005807{
5808 __be32 verf[2];
5809
Chuck Lever2c820d92012-05-21 22:45:33 -04005810 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5811 /* An impossible timestamp guarantees this value
5812 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005813 verf[0] = cpu_to_be32(U32_MAX);
5814 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005815 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005816 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005817 u64 ns = ktime_to_ns(nn->boot_time);
5818
5819 verf[0] = cpu_to_be32(ns >> 32);
5820 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005821 }
Chuck Levercd937102012-03-02 17:14:31 -05005822 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5823}
5824
Jeff Laytona3192682015-06-09 19:43:59 -04005825static int
5826nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005827{
Jeff Laytona3192682015-06-09 19:43:59 -04005828 size_t len;
5829 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005830
Trond Myklebustceb3a162015-01-03 15:16:04 -05005831 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005832 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005833
Jeff Laytona3192682015-06-09 19:43:59 -04005834 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005835 len = 14 +
5836 strlen(clp->cl_rpcclient->cl_nodename) +
5837 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005838 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005839 1;
5840 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005841 if (nfs4_client_id_uniquifier[0] != '\0')
5842 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005843 if (len > NFS4_OPAQUE_LIMIT + 1)
5844 return -EINVAL;
5845
5846 /*
5847 * Since this string is allocated at mount time, and held until the
5848 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5849 * about a memory-reclaim deadlock.
5850 */
5851 str = kmalloc(len, GFP_KERNEL);
5852 if (!str)
5853 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005854
Chuck Levere984a552012-09-14 17:24:21 -04005855 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005856 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005857 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005858 clp->cl_rpcclient->cl_nodename,
5859 nfs4_client_id_uniquifier,
5860 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005861 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005862 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005863 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005864 clp->cl_rpcclient->cl_nodename,
5865 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005866 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005867 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005868
Jeff Laytona3192682015-06-09 19:43:59 -04005869 clp->cl_owner_id = str;
5870 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005871}
5872
Jeff Layton873e3852015-06-09 19:44:00 -04005873static int
5874nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005875{
Jeff Layton873e3852015-06-09 19:44:00 -04005876 size_t len;
5877 char *str;
5878
5879 len = 10 + 10 + 1 + 10 + 1 +
5880 strlen(nfs4_client_id_uniquifier) + 1 +
5881 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5882
5883 if (len > NFS4_OPAQUE_LIMIT + 1)
5884 return -EINVAL;
5885
5886 /*
5887 * Since this string is allocated at mount time, and held until the
5888 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5889 * about a memory-reclaim deadlock.
5890 */
5891 str = kmalloc(len, GFP_KERNEL);
5892 if (!str)
5893 return -ENOMEM;
5894
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005895 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005896 clp->rpc_ops->version, clp->cl_minorversion,
5897 nfs4_client_id_uniquifier,
5898 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005899 clp->cl_owner_id = str;
5900 return 0;
5901}
5902
5903static int
5904nfs4_init_uniform_client_string(struct nfs_client *clp)
5905{
Jeff Layton873e3852015-06-09 19:44:00 -04005906 size_t len;
5907 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005908
5909 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005910 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005911
5912 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005913 return nfs4_init_uniquifier_client_string(clp);
5914
5915 len = 10 + 10 + 1 + 10 + 1 +
5916 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5917
5918 if (len > NFS4_OPAQUE_LIMIT + 1)
5919 return -EINVAL;
5920
5921 /*
5922 * Since this string is allocated at mount time, and held until the
5923 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5924 * about a memory-reclaim deadlock.
5925 */
5926 str = kmalloc(len, GFP_KERNEL);
5927 if (!str)
5928 return -ENOMEM;
5929
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005930 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005931 clp->rpc_ops->version, clp->cl_minorversion,
5932 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005933 clp->cl_owner_id = str;
5934 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005935}
5936
Chuck Lever706cb8d2014-03-12 12:51:47 -04005937/*
5938 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5939 * services. Advertise one based on the address family of the
5940 * clientaddr.
5941 */
5942static unsigned int
5943nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5944{
5945 if (strchr(clp->cl_ipaddr, ':') != NULL)
5946 return scnprintf(buf, len, "tcp6");
5947 else
5948 return scnprintf(buf, len, "tcp");
5949}
5950
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005951static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5952{
5953 struct nfs4_setclientid *sc = calldata;
5954
5955 if (task->tk_status == 0)
5956 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5957}
5958
5959static const struct rpc_call_ops nfs4_setclientid_ops = {
5960 .rpc_call_done = nfs4_setclientid_done,
5961};
5962
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005963/**
5964 * nfs4_proc_setclientid - Negotiate client ID
5965 * @clp: state data structure
5966 * @program: RPC program for NFSv4 callback service
5967 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11005968 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005969 * @res: where to place the result
5970 *
5971 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5972 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005973int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11005974 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005975 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976{
5977 nfs4_verifier sc_verifier;
5978 struct nfs4_setclientid setclientid = {
5979 .sc_verifier = &sc_verifier,
5980 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005981 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 };
5983 struct rpc_message msg = {
5984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5985 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005986 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005987 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005989 struct rpc_task *task;
5990 struct rpc_task_setup task_setup_data = {
5991 .rpc_client = clp->cl_rpcclient,
5992 .rpc_message = &msg,
5993 .callback_ops = &nfs4_setclientid_ops,
5994 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10005995 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005996 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005997 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998
Chuck Leverde734832012-07-11 16:30:50 -04005999 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006000 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006001
6002 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6003 status = nfs4_init_uniform_client_string(clp);
6004 else
Jeff Laytona3192682015-06-09 19:43:59 -04006005 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006006
6007 if (status)
6008 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006009
Chuck Leverde734832012-07-11 16:30:50 -04006010 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006011 setclientid.sc_netid_len =
6012 nfs4_init_callback_netid(clp,
6013 setclientid.sc_netid,
6014 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006015 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006016 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 clp->cl_ipaddr, port >> 8, port & 255);
6018
Jeff Layton3a6bb732015-06-09 19:43:57 -04006019 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006020 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006021 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006022 task = rpc_run_task(&task_setup_data);
6023 if (IS_ERR(task)) {
6024 status = PTR_ERR(task);
6025 goto out;
6026 }
6027 status = task->tk_status;
6028 if (setclientid.sc_cred) {
6029 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6030 put_rpccred(setclientid.sc_cred);
6031 }
6032 rpc_put_task(task);
6033out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006034 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006035 dprintk("NFS reply setclientid: %d\n", status);
6036 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006037}
6038
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006039/**
6040 * nfs4_proc_setclientid_confirm - Confirm client ID
6041 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006042 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006043 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006044 *
6045 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6046 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006047int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006048 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006049 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006050{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 struct rpc_message msg = {
6052 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006053 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006054 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 int status;
6057
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006058 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6059 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6060 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006061 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6062 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006063 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006064 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006065 return status;
6066}
6067
Trond Myklebustfe650402006-01-03 09:55:18 +01006068struct nfs4_delegreturndata {
6069 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006070 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006071 struct nfs_fh fh;
6072 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006073 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006074 struct {
6075 struct nfs4_layoutreturn_args arg;
6076 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006077 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006078 u32 roc_barrier;
6079 bool roc;
6080 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006081 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006082 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006083 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006084};
6085
Trond Myklebustfe650402006-01-03 09:55:18 +01006086static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6087{
6088 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006089 struct nfs4_exception exception = {
6090 .inode = data->inode,
6091 .stateid = &data->stateid,
6092 };
Andy Adamson938e1012009-04-01 09:22:28 -04006093
Trond Myklebust14516c32010-07-31 14:29:06 -04006094 if (!nfs4_sequence_done(task, &data->res.seq_res))
6095 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006096
Trond Myklebustca8acf82013-08-13 10:36:56 -04006097 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006098
6099 /* Handle Layoutreturn errors */
6100 if (data->args.lr_args && task->tk_status != 0) {
6101 switch(data->res.lr_ret) {
6102 default:
6103 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6104 break;
6105 case 0:
6106 data->args.lr_args = NULL;
6107 data->res.lr_res = NULL;
6108 break;
Trond Myklebust73800202017-11-06 15:28:07 -05006109 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04006110 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04006111 &data->args.lr_args->range,
Trond Myklebust140087fd2017-11-06 15:28:10 -05006112 data->inode))
6113 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05006114 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05006115 case -NFS4ERR_ADMIN_REVOKED:
6116 case -NFS4ERR_DELEG_REVOKED:
6117 case -NFS4ERR_EXPIRED:
6118 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05006119 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6120 case -NFS4ERR_WRONG_CRED:
6121 data->args.lr_args = NULL;
6122 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006123 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006124 }
6125 }
6126
Ricardo Labiaga79708862009-12-07 09:23:21 -05006127 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006128 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006129 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006130 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006131 case -NFS4ERR_ADMIN_REVOKED:
6132 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006133 case -NFS4ERR_EXPIRED:
6134 nfs4_free_revoked_stateid(data->res.server,
6135 data->args.stateid,
6136 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006137 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006138 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006139 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006140 task->tk_status = 0;
6141 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006142 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006143 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6144 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006145 task->tk_status = 0;
6146 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006147 case -NFS4ERR_ACCESS:
6148 if (data->args.bitmask) {
6149 data->args.bitmask = NULL;
6150 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006151 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006152 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006153 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006154 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006155 task->tk_status = nfs4_async_handle_exception(task,
6156 data->res.server, task->tk_status,
6157 &exception);
6158 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006159 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006160 }
6161 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006162 return;
6163lr_restart:
6164 data->res.lr_ret = 0;
6165out_restart:
6166 task->tk_status = 0;
6167 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006168}
6169
6170static void nfs4_delegreturn_release(void *calldata)
6171{
Peng Tao039b7562014-07-03 13:05:02 +08006172 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006173 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006174
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006175 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006176 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006177 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6178 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006179 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006180 nfs_iput_and_deactive(inode);
6181 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006182 kfree(calldata);
6183}
6184
Andy Adamson938e1012009-04-01 09:22:28 -04006185static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6186{
6187 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006188 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006189
6190 d_data = (struct nfs4_delegreturndata *)data;
6191
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006192 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006193 return;
6194
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006195 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6196 if (lo && !pnfs_layout_is_valid(lo)) {
6197 d_data->args.lr_args = NULL;
6198 d_data->res.lr_res = NULL;
6199 }
6200
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006201 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006202 &d_data->args.seq_args,
6203 &d_data->res.seq_res,
6204 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006205}
Andy Adamson938e1012009-04-01 09:22:28 -04006206
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006207static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006208 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006209 .rpc_call_done = nfs4_delegreturn_done,
6210 .rpc_release = nfs4_delegreturn_release,
6211};
6212
NeilBrowna52458b2018-12-03 11:30:31 +11006213static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006214{
6215 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006216 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006217 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006218 struct rpc_message msg = {
6219 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6220 .rpc_cred = cred,
6221 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006222 struct rpc_task_setup task_setup_data = {
6223 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006224 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006225 .callback_ops = &nfs4_delegreturn_ops,
6226 .flags = RPC_TASK_ASYNC,
6227 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006228 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006229
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006230 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006231 if (data == NULL)
6232 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006233 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006234
6235 nfs4_state_protect(server->nfs_client,
6236 NFS_SP4_MACH_CRED_CLEANUP,
6237 &task_setup_data.rpc_client, &msg);
6238
Trond Myklebustfe650402006-01-03 09:55:18 +01006239 data->args.fhandle = &data->fh;
6240 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006241 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006242 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006243 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006244 data->res.fattr = &data->fattr;
6245 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006246 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006247 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006248 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006249 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006250 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006251 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006252 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006253 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006254 if (data->lr.roc) {
6255 data->args.lr_args = &data->lr.arg;
6256 data->res.lr_res = &data->lr.res;
6257 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006258 } else if (data->lr.roc) {
6259 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6260 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006261 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006262
Trond Myklebustc970aa82007-07-14 15:39:59 -04006263 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006264 msg.rpc_argp = &data->args;
6265 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006266 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006267 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006268 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006269 if (!issync)
6270 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006271 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006272 if (status != 0)
6273 goto out;
6274 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006275out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006276 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006277 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006278}
6279
NeilBrowna52458b2018-12-03 11:30:31 +11006280int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006281{
6282 struct nfs_server *server = NFS_SERVER(inode);
6283 struct nfs4_exception exception = { };
6284 int err;
6285 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006286 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006287 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006288 switch (err) {
6289 case -NFS4ERR_STALE_STATEID:
6290 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006291 case 0:
6292 return 0;
6293 }
6294 err = nfs4_handle_exception(server, err, &exception);
6295 } while (exception.retry);
6296 return err;
6297}
6298
Linus Torvalds1da177e2005-04-16 15:20:36 -07006299static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6300{
6301 struct inode *inode = state->inode;
6302 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006303 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006304 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006305 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006306 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006307 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006308 struct nfs_lockt_res res = {
6309 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006310 };
6311 struct rpc_message msg = {
6312 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006313 .rpc_argp = &arg,
6314 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006315 .rpc_cred = state->owner->so_cred,
6316 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006317 struct nfs4_lock_state *lsp;
6318 int status;
6319
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006320 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006321 status = nfs4_set_lock_state(state, request);
6322 if (status != 0)
6323 goto out;
6324 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006325 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006326 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006327 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006328 switch (status) {
6329 case 0:
6330 request->fl_type = F_UNLCK;
6331 break;
6332 case -NFS4ERR_DENIED:
6333 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006334 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006335 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006336 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006337out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 return status;
6339}
6340
6341static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6342{
Trond Myklebust0688e642019-04-07 13:59:09 -04006343 struct nfs4_exception exception = {
6344 .interruptible = true,
6345 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 int err;
6347
6348 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006349 err = _nfs4_proc_getlk(state, cmd, request);
6350 trace_nfs4_get_lock(request, state, cmd, err);
6351 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006352 &exception);
6353 } while (exception.retry);
6354 return err;
6355}
6356
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006357struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006358 struct nfs_locku_args arg;
6359 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006360 struct nfs4_lock_state *lsp;
6361 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006362 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006363 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006364 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006365 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006366};
6367
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006368static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6369 struct nfs_open_context *ctx,
6370 struct nfs4_lock_state *lsp,
6371 struct nfs_seqid *seqid)
6372{
6373 struct nfs4_unlockdata *p;
6374 struct inode *inode = lsp->ls_state->inode;
6375
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006376 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006377 if (p == NULL)
6378 return NULL;
6379 p->arg.fh = NFS_FH(inode);
6380 p->arg.fl = &p->fl;
6381 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006382 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006383 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006384 /* Ensure we don't close file until we're done freeing locks! */
6385 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006386 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006387 locks_init_lock(&p->fl);
6388 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006389 p->server = NFS_SERVER(inode);
6390 return p;
6391}
6392
Trond Myklebust06f814a2006-01-03 09:55:07 +01006393static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006394{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006395 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006396 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006397 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006398 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006399 put_nfs_open_context(calldata->ctx);
6400 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006401}
6402
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006403static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006404{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006405 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006406 struct nfs4_exception exception = {
6407 .inode = calldata->lsp->ls_state->inode,
6408 .stateid = &calldata->arg.stateid,
6409 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006410
Trond Myklebust14516c32010-07-31 14:29:06 -04006411 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6412 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006413 switch (task->tk_status) {
6414 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006415 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006416 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006417 if (nfs4_update_lock_stateid(calldata->lsp,
6418 &calldata->res.stateid))
6419 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006420 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006421 case -NFS4ERR_ADMIN_REVOKED:
6422 case -NFS4ERR_EXPIRED:
6423 nfs4_free_revoked_stateid(calldata->server,
6424 &calldata->arg.stateid,
6425 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006426 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006427 case -NFS4ERR_BAD_STATEID:
6428 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006429 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006430 if (!nfs4_stateid_match(&calldata->arg.stateid,
6431 &calldata->lsp->ls_stateid))
6432 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006433 break;
6434 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006435 task->tk_status = nfs4_async_handle_exception(task,
6436 calldata->server, task->tk_status,
6437 &exception);
6438 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006439 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006440 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006441 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006442}
6443
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006444static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006445{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006446 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006448 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6449 nfs_async_iocounter_wait(task, calldata->l_ctx))
6450 return;
6451
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006452 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006453 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006454 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006455 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006456 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006457 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006458 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006459 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006460 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006461 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006462 &calldata->res.seq_res,
6463 task) != 0)
6464 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006465 return;
6466out_no_action:
6467 task->tk_action = NULL;
6468out_wait:
6469 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006470}
6471
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006472static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006473 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006474 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006475 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006476};
6477
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006478static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6479 struct nfs_open_context *ctx,
6480 struct nfs4_lock_state *lsp,
6481 struct nfs_seqid *seqid)
6482{
6483 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006484 struct rpc_message msg = {
6485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6486 .rpc_cred = ctx->cred,
6487 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006488 struct rpc_task_setup task_setup_data = {
6489 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006490 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006491 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006492 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006493 .flags = RPC_TASK_ASYNC,
6494 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006495
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006496 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6497 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6498
Frank Filz137d6ac2007-07-09 15:32:29 -07006499 /* Ensure this is an unlock - when canceling a lock, the
6500 * canceled lock is passed in, and it won't be an unlock.
6501 */
6502 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006503 if (fl->fl_flags & FL_CLOSE)
6504 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006505
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006506 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6507 if (data == NULL) {
6508 nfs_free_seqid(seqid);
6509 return ERR_PTR(-ENOMEM);
6510 }
6511
Anna Schumakerfba83f32018-05-04 16:22:50 -04006512 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006513 msg.rpc_argp = &data->arg;
6514 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006515 task_setup_data.callback_data = data;
6516 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006517}
6518
Linus Torvalds1da177e2005-04-16 15:20:36 -07006519static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6520{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006521 struct inode *inode = state->inode;
6522 struct nfs4_state_owner *sp = state->owner;
6523 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006524 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006525 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006526 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006527 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006528 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006529 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006530
Trond Myklebust9b073572006-06-29 16:38:34 -04006531 status = nfs4_set_lock_state(state, request);
6532 /* Unlock _before_ we do the RPC call */
6533 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006534 /* Exclude nfs_delegation_claim_locks() */
6535 mutex_lock(&sp->so_delegreturn_mutex);
6536 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006537 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006538 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006539 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006540 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006541 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006542 }
6543 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006544 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006545 if (status != 0)
6546 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006547 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006548 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006549 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6550 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006551 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6552 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006553 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006554 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006555 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006556 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006557 status = PTR_ERR(task);
6558 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006559 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006560 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006561 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006562out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006563 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006564 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006565 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006566}
6567
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006568struct nfs4_lockdata {
6569 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006570 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006571 struct nfs4_lock_state *lsp;
6572 struct nfs_open_context *ctx;
6573 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006574 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006575 int rpc_status;
6576 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006577 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006578};
6579
6580static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006581 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6582 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006583{
6584 struct nfs4_lockdata *p;
6585 struct inode *inode = lsp->ls_state->inode;
6586 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006587 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006588
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006589 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006590 if (p == NULL)
6591 return NULL;
6592
6593 p->arg.fh = NFS_FH(inode);
6594 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006595 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006596 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006597 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006598 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6599 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006600 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006601 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006602 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006603 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006604 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006605 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006606 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006607 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006608 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006609 locks_init_lock(&p->fl);
6610 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006611 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006612out_free_seqid:
6613 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006614out_free:
6615 kfree(p);
6616 return NULL;
6617}
6618
6619static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6620{
6621 struct nfs4_lockdata *data = calldata;
6622 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006623
Harvey Harrison3110ff82008-05-02 13:42:44 -07006624 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006625 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006626 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006627 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006628 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006629 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006630 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006631 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006632 nfs4_stateid_copy(&data->arg.open_stateid,
6633 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006634 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006635 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006636 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006637 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006638 nfs4_stateid_copy(&data->arg.lock_stateid,
6639 &data->lsp->ls_stateid);
6640 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006641 if (!nfs4_valid_open_stateid(state)) {
6642 data->rpc_status = -EBADF;
6643 task->tk_action = NULL;
6644 goto out_release_open_seqid;
6645 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006646 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006647 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006648 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006649 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006650 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006651 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006652out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006653 nfs_release_seqid(data->arg.open_seqid);
6654out_release_lock_seqid:
6655 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006656out_wait:
6657 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006658 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006659}
6660
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006661static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6662{
6663 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006664 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006665
Harvey Harrison3110ff82008-05-02 13:42:44 -07006666 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006667
Trond Myklebust14516c32010-07-31 14:29:06 -04006668 if (!nfs4_sequence_done(task, &data->res.seq_res))
6669 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006670
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006671 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006672 switch (task->tk_status) {
6673 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006674 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006675 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006676 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006677 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006678 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006679 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006680 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006681 if (data->arg.new_lock_owner != 0) {
6682 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6683 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6684 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006685 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6686 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006687 break;
6688 case -NFS4ERR_BAD_STATEID:
6689 case -NFS4ERR_OLD_STATEID:
6690 case -NFS4ERR_STALE_STATEID:
6691 case -NFS4ERR_EXPIRED:
6692 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006693 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006694 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006695 goto out_restart;
6696 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006697 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006698 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006699 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006700out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006701 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006702 return;
6703out_restart:
6704 if (!data->cancelled)
6705 rpc_restart_call_prepare(task);
6706 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006707}
6708
6709static void nfs4_lock_release(void *calldata)
6710{
6711 struct nfs4_lockdata *data = calldata;
6712
Harvey Harrison3110ff82008-05-02 13:42:44 -07006713 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006714 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006715 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006716 struct rpc_task *task;
6717 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6718 data->arg.lock_seqid);
6719 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006720 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006721 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006722 } else
6723 nfs_free_seqid(data->arg.lock_seqid);
6724 nfs4_put_lock_state(data->lsp);
6725 put_nfs_open_context(data->ctx);
6726 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006727 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006728}
6729
6730static const struct rpc_call_ops nfs4_lock_ops = {
6731 .rpc_call_prepare = nfs4_lock_prepare,
6732 .rpc_call_done = nfs4_lock_done,
6733 .rpc_release = nfs4_lock_release,
6734};
6735
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006736static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6737{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006738 switch (error) {
6739 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006740 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006741 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006742 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006743 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006744 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006745 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006746 break;
6747 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006748 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006749 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006750 };
6751}
6752
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006753static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006754{
6755 struct nfs4_lockdata *data;
6756 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006757 struct rpc_message msg = {
6758 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6759 .rpc_cred = state->owner->so_cred,
6760 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006761 struct rpc_task_setup task_setup_data = {
6762 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006763 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006764 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006765 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006766 .flags = RPC_TASK_ASYNC,
6767 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006768 int ret;
6769
Harvey Harrison3110ff82008-05-02 13:42:44 -07006770 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006771 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006772 fl->fl_u.nfs4_fl.owner,
6773 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006774 if (data == NULL)
6775 return -ENOMEM;
6776 if (IS_SETLKW(cmd))
6777 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006778 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6779 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006780 msg.rpc_argp = &data->arg;
6781 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006782 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006783 if (recovery_type > NFS_LOCK_NEW) {
6784 if (recovery_type == NFS_LOCK_RECLAIM)
6785 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006786 } else
6787 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006788 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006789 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006790 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006791 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006792 if (ret == 0) {
6793 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006794 if (ret)
6795 nfs4_handle_setlk_error(data->server, data->lsp,
6796 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006797 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006798 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006799 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006800 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006801 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006802 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006803}
6804
6805static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6806{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006807 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006808 struct nfs4_exception exception = {
6809 .inode = state->inode,
6810 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006811 int err;
6812
6813 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006814 /* Cache the lock if possible... */
6815 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6816 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006817 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006818 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006819 break;
6820 nfs4_handle_exception(server, err, &exception);
6821 } while (exception.retry);
6822 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823}
6824
6825static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6826{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006827 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006828 struct nfs4_exception exception = {
6829 .inode = state->inode,
6830 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006831 int err;
6832
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006833 err = nfs4_set_lock_state(state, request);
6834 if (err != 0)
6835 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006836 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006837 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6838 return 0;
6839 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006840 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006841 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6842 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006843 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006844 switch (err) {
6845 default:
6846 goto out;
6847 case -NFS4ERR_GRACE:
6848 case -NFS4ERR_DELAY:
6849 nfs4_handle_exception(server, err, &exception);
6850 err = 0;
6851 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006852 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006853out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006854 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006855}
6856
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006857#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006858static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6859{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006860 struct nfs4_lock_state *lsp;
6861 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006862
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006863 status = nfs4_set_lock_state(state, request);
6864 if (status != 0)
6865 return status;
6866 lsp = request->fl_u.nfs4_fl.owner;
6867 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6868 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6869 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006870 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006871}
6872#endif
6873
Linus Torvalds1da177e2005-04-16 15:20:36 -07006874static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6875{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006876 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006877 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006878 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006879 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006880
Trond Myklebust01c3b862006-06-29 16:38:39 -04006881 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006882 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006883 if (status < 0)
6884 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006885 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006886 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006887 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006888 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006889 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006890 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006891 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006892 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006893 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006894 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006895 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006896 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006897 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006898 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006899out:
6900 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006901 return status;
6902}
6903
6904static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6905{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006906 struct nfs4_exception exception = {
6907 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006908 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04006909 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006910 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911 int err;
6912
6913 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006914 err = _nfs4_proc_setlk(state, cmd, request);
6915 if (err == -NFS4ERR_DENIED)
6916 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006917 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006918 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006919 } while (exception.retry);
6920 return err;
6921}
6922
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006923#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6924#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6925
6926static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006927nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6928 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006929{
6930 int status = -ERESTARTSYS;
6931 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6932
6933 while(!signalled()) {
6934 status = nfs4_proc_setlk(state, cmd, request);
6935 if ((status != -EAGAIN) || IS_SETLK(cmd))
6936 break;
6937 freezable_schedule_timeout_interruptible(timeout);
6938 timeout *= 2;
6939 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6940 status = -ERESTARTSYS;
6941 }
6942 return status;
6943}
6944
Jeff Laytona1d617d82016-09-17 18:17:39 -04006945#ifdef CONFIG_NFS_V4_1
6946struct nfs4_lock_waiter {
6947 struct task_struct *task;
6948 struct inode *inode;
6949 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006950};
6951
6952static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006953nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006954{
6955 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006956 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006957
Jeff Layton57174592018-03-18 08:37:03 -04006958 /* NULL key means to wake up everyone */
6959 if (key) {
6960 struct cb_notify_lock_args *cbnl = key;
6961 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6962 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006963
Jeff Layton57174592018-03-18 08:37:03 -04006964 /* Only wake if the callback was for the same owner. */
6965 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6966 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006967
Jeff Layton57174592018-03-18 08:37:03 -04006968 /* Make sure it's for the right inode */
6969 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6970 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04006971 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006972
6973 /* override "private" so we can use default_wake_function */
6974 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08006975 ret = woken_wake_function(wait, mode, flags, key);
6976 if (ret)
6977 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006978 wait->private = waiter;
6979 return ret;
6980}
6981
6982static int
6983nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6984{
6985 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006986 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6987 struct nfs_server *server = NFS_SERVER(state->inode);
6988 struct nfs_client *clp = server->nfs_client;
6989 wait_queue_head_t *q = &clp->cl_lock_waitq;
6990 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6991 .id = lsp->ls_seqid.owner_id,
6992 .s_dev = server->s_dev };
6993 struct nfs4_lock_waiter waiter = { .task = current,
6994 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08006995 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02006996 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006997
6998 /* Don't bother with waitqueue if we don't expect a callback */
6999 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7000 return nfs4_retry_setlk_simple(state, cmd, request);
7001
7002 init_wait(&wait);
7003 wait.private = &waiter;
7004 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007005
7006 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007007 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007008 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007009 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7010 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007011 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007012 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007013
7014 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007015 freezer_do_not_count();
7016 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7017 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007018 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007019 }
7020
Jeff Laytona1d617d82016-09-17 18:17:39 -04007021 return status;
7022}
7023#else /* !CONFIG_NFS_V4_1 */
7024static inline int
7025nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7026{
7027 return nfs4_retry_setlk_simple(state, cmd, request);
7028}
7029#endif
7030
Linus Torvalds1da177e2005-04-16 15:20:36 -07007031static int
7032nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7033{
7034 struct nfs_open_context *ctx;
7035 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007036 int status;
7037
7038 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007039 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007040 state = ctx->state;
7041
Trond Myklebustd9531262009-07-21 19:22:38 -04007042 if (IS_GETLK(cmd)) {
7043 if (state != NULL)
7044 return nfs4_proc_getlk(state, F_GETLK, request);
7045 return 0;
7046 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007047
7048 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7049 return -EINVAL;
7050
Trond Myklebustd9531262009-07-21 19:22:38 -04007051 if (request->fl_type == F_UNLCK) {
7052 if (state != NULL)
7053 return nfs4_proc_unlck(state, cmd, request);
7054 return 0;
7055 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007056
Trond Myklebustd9531262009-07-21 19:22:38 -04007057 if (state == NULL)
7058 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007059
7060 if ((request->fl_flags & FL_POSIX) &&
7061 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7062 return -ENOLCK;
7063
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007064 /*
7065 * Don't rely on the VFS having checked the file open mode,
7066 * since it won't do this for flock() locks.
7067 */
7068 switch (request->fl_type) {
7069 case F_RDLCK:
7070 if (!(filp->f_mode & FMODE_READ))
7071 return -EBADF;
7072 break;
7073 case F_WRLCK:
7074 if (!(filp->f_mode & FMODE_WRITE))
7075 return -EBADF;
7076 }
7077
Jeff Layton1ea67db2016-09-17 18:17:37 -04007078 status = nfs4_set_lock_state(state, request);
7079 if (status != 0)
7080 return status;
7081
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007082 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007083}
7084
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007085int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007086{
7087 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007088 int err;
7089
7090 err = nfs4_set_lock_state(state, fl);
7091 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007092 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007093 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007094 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007095}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007096
Trond Myklebustcf470c32012-03-07 13:49:12 -05007097struct nfs_release_lockowner_data {
7098 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007099 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007100 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007101 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007102 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007103};
7104
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007105static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7106{
7107 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007108 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007109 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7110 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007111 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007112 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007113}
7114
7115static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7116{
7117 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007118 struct nfs_server *server = data->server;
7119
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007120 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007121
7122 switch (task->tk_status) {
7123 case 0:
7124 renew_lease(server, data->timestamp);
7125 break;
7126 case -NFS4ERR_STALE_CLIENTID:
7127 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007128 nfs4_schedule_lease_recovery(server->nfs_client);
7129 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007130 case -NFS4ERR_LEASE_MOVED:
7131 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007132 if (nfs4_async_handle_error(task, server,
7133 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007134 rpc_restart_call_prepare(task);
7135 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007136}
7137
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007138static void nfs4_release_lockowner_release(void *calldata)
7139{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007140 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007141 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007142 kfree(calldata);
7143}
7144
Trond Myklebust17280172012-03-11 13:11:00 -04007145static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007146 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7147 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007148 .rpc_release = nfs4_release_lockowner_release,
7149};
7150
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007151static void
7152nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007153{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007154 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007155 struct rpc_message msg = {
7156 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7157 };
7158
7159 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007160 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007161
Trond Myklebustcf470c32012-03-07 13:49:12 -05007162 data = kmalloc(sizeof(*data), GFP_NOFS);
7163 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007164 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007165 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007166 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007167 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7168 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7169 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007170
Trond Myklebustcf470c32012-03-07 13:49:12 -05007171 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007172 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007173 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007174 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007175}
7176
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007177#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7178
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007179static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007180 struct dentry *unused, struct inode *inode,
7181 const char *key, const void *buf,
7182 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007183{
Al Viro59301222016-05-27 10:19:30 -04007184 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007185}
7186
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007187static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007188 struct dentry *unused, struct inode *inode,
7189 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007190{
Al Virob2968212016-04-10 20:48:24 -04007191 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007192}
7193
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007194static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007195{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007196 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007197}
7198
David Quigleyc9bccef2013-05-22 12:50:45 -04007199#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007200
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007201static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007202 struct dentry *unused, struct inode *inode,
7203 const char *key, const void *buf,
7204 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007205{
7206 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007207 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007208
7209 return -EOPNOTSUPP;
7210}
7211
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007212static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007213 struct dentry *unused, struct inode *inode,
7214 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007215{
7216 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007217 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007218 return -EOPNOTSUPP;
7219}
7220
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007221static ssize_t
7222nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007223{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007224 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007225
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007226 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7227 len = security_inode_listsecurity(inode, list, list_len);
7228 if (list_len && len > list_len)
7229 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007230 }
7231 return len;
7232}
7233
7234static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7235 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007236 .get = nfs4_xattr_get_nfs4_label,
7237 .set = nfs4_xattr_set_nfs4_label,
7238};
David Quigleyc9bccef2013-05-22 12:50:45 -04007239
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007240#else
7241
7242static ssize_t
7243nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7244{
7245 return 0;
7246}
7247
7248#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007249
Andy Adamson533eb462011-06-13 18:25:56 -04007250/*
7251 * nfs_fhget will use either the mounted_on_fileid or the fileid
7252 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007253static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7254{
Andy Adamson533eb462011-06-13 18:25:56 -04007255 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7256 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7257 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007258 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007259 return;
7260
7261 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007262 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007263 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7264 fattr->nlink = 2;
7265}
7266
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007267static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7268 const struct qstr *name,
7269 struct nfs4_fs_locations *fs_locations,
7270 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007271{
7272 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007273 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007274 struct nfs4_fs_locations_arg args = {
7275 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007276 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007277 .page = page,
7278 .bitmask = bitmask,
7279 };
Benny Halevy22958462009-04-01 09:22:02 -04007280 struct nfs4_fs_locations_res res = {
7281 .fs_locations = fs_locations,
7282 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007283 struct rpc_message msg = {
7284 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7285 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007286 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007287 };
7288 int status;
7289
Harvey Harrison3110ff82008-05-02 13:42:44 -07007290 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007291
Chuck Leverc05cefc2017-11-05 15:45:22 -05007292 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7293 bitmask[1] = nfs4_fattr_bitmap[1];
7294
Andy Adamson533eb462011-06-13 18:25:56 -04007295 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7296 * is not supported */
7297 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007298 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007299 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007300 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007301
Trond Myklebustc228fd32007-01-13 02:28:11 -05007302 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007303 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007304 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007305 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007306 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007307 return status;
7308}
7309
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007310int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7311 const struct qstr *name,
7312 struct nfs4_fs_locations *fs_locations,
7313 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007314{
Trond Myklebust0688e642019-04-07 13:59:09 -04007315 struct nfs4_exception exception = {
7316 .interruptible = true,
7317 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007318 int err;
7319 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007320 err = _nfs4_proc_fs_locations(client, dir, name,
7321 fs_locations, page);
7322 trace_nfs4_get_fs_locations(dir, name, err);
7323 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007324 &exception);
7325 } while (exception.retry);
7326 return err;
7327}
7328
Chuck Leverb03d7352013-10-17 14:12:50 -04007329/*
7330 * This operation also signals the server that this client is
7331 * performing migration recovery. The server can stop returning
7332 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7333 * appended to this compound to identify the client ID which is
7334 * performing recovery.
7335 */
7336static int _nfs40_proc_get_locations(struct inode *inode,
7337 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007338 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007339{
7340 struct nfs_server *server = NFS_SERVER(inode);
7341 struct rpc_clnt *clnt = server->client;
7342 u32 bitmask[2] = {
7343 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7344 };
7345 struct nfs4_fs_locations_arg args = {
7346 .clientid = server->nfs_client->cl_clientid,
7347 .fh = NFS_FH(inode),
7348 .page = page,
7349 .bitmask = bitmask,
7350 .migration = 1, /* skip LOOKUP */
7351 .renew = 1, /* append RENEW */
7352 };
7353 struct nfs4_fs_locations_res res = {
7354 .fs_locations = locations,
7355 .migration = 1,
7356 .renew = 1,
7357 };
7358 struct rpc_message msg = {
7359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7360 .rpc_argp = &args,
7361 .rpc_resp = &res,
7362 .rpc_cred = cred,
7363 };
7364 unsigned long now = jiffies;
7365 int status;
7366
7367 nfs_fattr_init(&locations->fattr);
7368 locations->server = server;
7369 locations->nlocations = 0;
7370
Anna Schumakerfba83f32018-05-04 16:22:50 -04007371 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007372 status = nfs4_call_sync_sequence(clnt, server, &msg,
7373 &args.seq_args, &res.seq_res);
7374 if (status)
7375 return status;
7376
7377 renew_lease(server, now);
7378 return 0;
7379}
7380
7381#ifdef CONFIG_NFS_V4_1
7382
7383/*
7384 * This operation also signals the server that this client is
7385 * performing migration recovery. The server can stop asserting
7386 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7387 * performing this operation is identified in the SEQUENCE
7388 * operation in this compound.
7389 *
7390 * When the client supports GETATTR(fs_locations_info), it can
7391 * be plumbed in here.
7392 */
7393static int _nfs41_proc_get_locations(struct inode *inode,
7394 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007395 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007396{
7397 struct nfs_server *server = NFS_SERVER(inode);
7398 struct rpc_clnt *clnt = server->client;
7399 u32 bitmask[2] = {
7400 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7401 };
7402 struct nfs4_fs_locations_arg args = {
7403 .fh = NFS_FH(inode),
7404 .page = page,
7405 .bitmask = bitmask,
7406 .migration = 1, /* skip LOOKUP */
7407 };
7408 struct nfs4_fs_locations_res res = {
7409 .fs_locations = locations,
7410 .migration = 1,
7411 };
7412 struct rpc_message msg = {
7413 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7414 .rpc_argp = &args,
7415 .rpc_resp = &res,
7416 .rpc_cred = cred,
7417 };
7418 int status;
7419
7420 nfs_fattr_init(&locations->fattr);
7421 locations->server = server;
7422 locations->nlocations = 0;
7423
Anna Schumakerfba83f32018-05-04 16:22:50 -04007424 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007425 status = nfs4_call_sync_sequence(clnt, server, &msg,
7426 &args.seq_args, &res.seq_res);
7427 if (status == NFS4_OK &&
7428 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7429 status = -NFS4ERR_LEASE_MOVED;
7430 return status;
7431}
7432
7433#endif /* CONFIG_NFS_V4_1 */
7434
7435/**
7436 * nfs4_proc_get_locations - discover locations for a migrated FSID
7437 * @inode: inode on FSID that is migrating
7438 * @locations: result of query
7439 * @page: buffer
7440 * @cred: credential to use for this operation
7441 *
7442 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7443 * operation failed, or a negative errno if a local error occurred.
7444 *
7445 * On success, "locations" is filled in, but if the server has
7446 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7447 * asserted.
7448 *
7449 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7450 * from this client that require migration recovery.
7451 */
7452int nfs4_proc_get_locations(struct inode *inode,
7453 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007454 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007455{
7456 struct nfs_server *server = NFS_SERVER(inode);
7457 struct nfs_client *clp = server->nfs_client;
7458 const struct nfs4_mig_recovery_ops *ops =
7459 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007460 struct nfs4_exception exception = {
7461 .interruptible = true,
7462 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007463 int status;
7464
7465 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7466 (unsigned long long)server->fsid.major,
7467 (unsigned long long)server->fsid.minor,
7468 clp->cl_hostname);
7469 nfs_display_fhandle(NFS_FH(inode), __func__);
7470
7471 do {
7472 status = ops->get_locations(inode, locations, page, cred);
7473 if (status != -NFS4ERR_DELAY)
7474 break;
7475 nfs4_handle_exception(server, status, &exception);
7476 } while (exception.retry);
7477 return status;
7478}
7479
Chuck Lever44c99932013-10-17 14:13:30 -04007480/*
7481 * This operation also signals the server that this client is
7482 * performing "lease moved" recovery. The server can stop
7483 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7484 * is appended to this compound to identify the client ID which is
7485 * performing recovery.
7486 */
NeilBrowna52458b2018-12-03 11:30:31 +11007487static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007488{
7489 struct nfs_server *server = NFS_SERVER(inode);
7490 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7491 struct rpc_clnt *clnt = server->client;
7492 struct nfs4_fsid_present_arg args = {
7493 .fh = NFS_FH(inode),
7494 .clientid = clp->cl_clientid,
7495 .renew = 1, /* append RENEW */
7496 };
7497 struct nfs4_fsid_present_res res = {
7498 .renew = 1,
7499 };
7500 struct rpc_message msg = {
7501 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7502 .rpc_argp = &args,
7503 .rpc_resp = &res,
7504 .rpc_cred = cred,
7505 };
7506 unsigned long now = jiffies;
7507 int status;
7508
7509 res.fh = nfs_alloc_fhandle();
7510 if (res.fh == NULL)
7511 return -ENOMEM;
7512
Anna Schumakerfba83f32018-05-04 16:22:50 -04007513 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007514 status = nfs4_call_sync_sequence(clnt, server, &msg,
7515 &args.seq_args, &res.seq_res);
7516 nfs_free_fhandle(res.fh);
7517 if (status)
7518 return status;
7519
7520 do_renew_lease(clp, now);
7521 return 0;
7522}
7523
7524#ifdef CONFIG_NFS_V4_1
7525
7526/*
7527 * This operation also signals the server that this client is
7528 * performing "lease moved" recovery. The server can stop asserting
7529 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7530 * this operation is identified in the SEQUENCE operation in this
7531 * compound.
7532 */
NeilBrowna52458b2018-12-03 11:30:31 +11007533static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007534{
7535 struct nfs_server *server = NFS_SERVER(inode);
7536 struct rpc_clnt *clnt = server->client;
7537 struct nfs4_fsid_present_arg args = {
7538 .fh = NFS_FH(inode),
7539 };
7540 struct nfs4_fsid_present_res res = {
7541 };
7542 struct rpc_message msg = {
7543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7544 .rpc_argp = &args,
7545 .rpc_resp = &res,
7546 .rpc_cred = cred,
7547 };
7548 int status;
7549
7550 res.fh = nfs_alloc_fhandle();
7551 if (res.fh == NULL)
7552 return -ENOMEM;
7553
Anna Schumakerfba83f32018-05-04 16:22:50 -04007554 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007555 status = nfs4_call_sync_sequence(clnt, server, &msg,
7556 &args.seq_args, &res.seq_res);
7557 nfs_free_fhandle(res.fh);
7558 if (status == NFS4_OK &&
7559 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7560 status = -NFS4ERR_LEASE_MOVED;
7561 return status;
7562}
7563
7564#endif /* CONFIG_NFS_V4_1 */
7565
7566/**
7567 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7568 * @inode: inode on FSID to check
7569 * @cred: credential to use for this operation
7570 *
7571 * Server indicates whether the FSID is present, moved, or not
7572 * recognized. This operation is necessary to clear a LEASE_MOVED
7573 * condition for this client ID.
7574 *
7575 * Returns NFS4_OK if the FSID is present on this server,
7576 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7577 * NFS4ERR code if some error occurred on the server, or a
7578 * negative errno if a local failure occurred.
7579 */
NeilBrowna52458b2018-12-03 11:30:31 +11007580int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007581{
7582 struct nfs_server *server = NFS_SERVER(inode);
7583 struct nfs_client *clp = server->nfs_client;
7584 const struct nfs4_mig_recovery_ops *ops =
7585 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007586 struct nfs4_exception exception = {
7587 .interruptible = true,
7588 };
Chuck Lever44c99932013-10-17 14:13:30 -04007589 int status;
7590
7591 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7592 (unsigned long long)server->fsid.major,
7593 (unsigned long long)server->fsid.minor,
7594 clp->cl_hostname);
7595 nfs_display_fhandle(NFS_FH(inode), __func__);
7596
7597 do {
7598 status = ops->fsid_present(inode, cred);
7599 if (status != -NFS4ERR_DELAY)
7600 break;
7601 nfs4_handle_exception(server, status, &exception);
7602 } while (exception.retry);
7603 return status;
7604}
7605
Trond Myklebust302fad72019-02-18 13:32:38 -05007606/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007607 * If 'use_integrity' is true and the state managment nfs_client
7608 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7609 * and the machine credential as per RFC3530bis and RFC5661 Security
7610 * Considerations sections. Otherwise, just use the user cred with the
7611 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007612 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007613static 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 +00007614{
7615 int status;
7616 struct nfs4_secinfo_arg args = {
7617 .dir_fh = NFS_FH(dir),
7618 .name = name,
7619 };
7620 struct nfs4_secinfo_res res = {
7621 .flavors = flavors,
7622 };
7623 struct rpc_message msg = {
7624 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7625 .rpc_argp = &args,
7626 .rpc_resp = &res,
7627 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007628 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
NeilBrowna52458b2018-12-03 11:30:31 +11007629 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007630
7631 if (use_integrity) {
7632 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007633 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7634 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007635 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007636
7637 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007638
7639 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7640 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7641
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007642 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
NeilBrown5a0c2572019-05-30 10:41:28 +10007643 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007644 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007645
NeilBrowna52458b2018-12-03 11:30:31 +11007646 put_cred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007647
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007648 return status;
7649}
7650
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007651int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7652 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007653{
Trond Myklebust0688e642019-04-07 13:59:09 -04007654 struct nfs4_exception exception = {
7655 .interruptible = true,
7656 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007657 int err;
7658 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007659 err = -NFS4ERR_WRONGSEC;
7660
7661 /* try to use integrity protection with machine cred */
7662 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7663 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7664
7665 /*
7666 * if unable to use integrity protection, or SECINFO with
7667 * integrity protection returns NFS4ERR_WRONGSEC (which is
7668 * disallowed by spec, but exists in deployed servers) use
7669 * the current filesystem's rpc_client and the user cred.
7670 */
7671 if (err == -NFS4ERR_WRONGSEC)
7672 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7673
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007674 trace_nfs4_secinfo(dir, name, err);
7675 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007676 &exception);
7677 } while (exception.retry);
7678 return err;
7679}
7680
Andy Adamson557134a2009-04-01 09:21:53 -04007681#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007682/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007683 * Check the exchange flags returned by the server for invalid flags, having
7684 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7685 * DS flags set.
7686 */
7687static int nfs4_check_cl_exchange_flags(u32 flags)
7688{
7689 if (flags & ~EXCHGID4_FLAG_MASK_R)
7690 goto out_inval;
7691 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7692 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7693 goto out_inval;
7694 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7695 goto out_inval;
7696 return NFS_OK;
7697out_inval:
7698 return -NFS4ERR_INVAL;
7699}
7700
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007701static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007702nfs41_same_server_scope(struct nfs41_server_scope *a,
7703 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007704{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007705 if (a->server_scope_sz != b->server_scope_sz)
7706 return false;
7707 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007708}
7709
Andy Adamson02a95de2016-02-05 16:08:37 -05007710static void
7711nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7712{
7713}
7714
7715static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7716 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7717};
7718
Andy Adamson357f54d2010-12-14 10:11:57 -05007719/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007720 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007721 *
7722 * The 4.1 client currently uses the same TCP connection for the
7723 * fore and backchannel.
7724 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007725static
7726int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7727 struct rpc_xprt *xprt,
7728 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007729 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007730{
7731 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007732 struct nfs41_bind_conn_to_session_args args = {
7733 .client = clp,
7734 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7735 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007736 struct nfs41_bind_conn_to_session_res res;
7737 struct rpc_message msg = {
7738 .rpc_proc =
7739 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007740 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007741 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007742 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007743 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007744 struct rpc_task_setup task_setup_data = {
7745 .rpc_client = clnt,
7746 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007747 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007748 .rpc_message = &msg,
7749 .flags = RPC_TASK_TIMEOUT,
7750 };
7751 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007752
Trond Myklebust71a097c2015-02-18 09:27:18 -08007753 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7754 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7755 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007756
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007757 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7758 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7759 args.dir = NFS4_CDFC4_FORE;
7760
7761 task = rpc_run_task(&task_setup_data);
7762 if (!IS_ERR(task)) {
7763 status = task->tk_status;
7764 rpc_put_task(task);
7765 } else
7766 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007767 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007768 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007769 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007770 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7771 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007772 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007773 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007774 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007775 dprintk("NFS: %s: Unexpected direction from server\n",
7776 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007777 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007778 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007779 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007780 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7781 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007782 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007783 }
7784 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007785
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007786 return status;
7787}
7788
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007789struct rpc_bind_conn_calldata {
7790 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007791 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007792};
7793
7794static int
7795nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7796 struct rpc_xprt *xprt,
7797 void *calldata)
7798{
7799 struct rpc_bind_conn_calldata *p = calldata;
7800
7801 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7802}
7803
NeilBrowna52458b2018-12-03 11:30:31 +11007804int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007805{
7806 struct rpc_bind_conn_calldata data = {
7807 .clp = clp,
7808 .cred = cred,
7809 };
7810 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7811 nfs4_proc_bind_conn_to_session_callback, &data);
7812}
7813
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007814/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007815 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7816 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007817 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007818static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7819 .how = SP4_MACH_CRED,
7820 .enforce.u.words = {
7821 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7822 1 << (OP_EXCHANGE_ID - 32) |
7823 1 << (OP_CREATE_SESSION - 32) |
7824 1 << (OP_DESTROY_SESSION - 32) |
7825 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007826 },
7827 .allow.u.words = {
7828 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007829 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007830 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007831 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007832 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007833 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007834 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007835 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007836 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007837 1 << (OP_FREE_STATEID - 32) |
7838 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007839 }
7840};
7841
7842/*
7843 * Select the state protection mode for client `clp' given the server results
7844 * from exchange_id in `sp'.
7845 *
7846 * Returns 0 on success, negative errno otherwise.
7847 */
7848static int nfs4_sp4_select_mode(struct nfs_client *clp,
7849 struct nfs41_state_protection *sp)
7850{
7851 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7852 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7853 1 << (OP_EXCHANGE_ID - 32) |
7854 1 << (OP_CREATE_SESSION - 32) |
7855 1 << (OP_DESTROY_SESSION - 32) |
7856 1 << (OP_DESTROY_CLIENTID - 32)
7857 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007858 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007859 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007860 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007861
7862 if (sp->how == SP4_MACH_CRED) {
7863 /* Print state protect result */
7864 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7865 for (i = 0; i <= LAST_NFS4_OP; i++) {
7866 if (test_bit(i, sp->enforce.u.longs))
7867 dfprintk(MOUNT, " enforce op %d\n", i);
7868 if (test_bit(i, sp->allow.u.longs))
7869 dfprintk(MOUNT, " allow op %d\n", i);
7870 }
7871
7872 /* make sure nothing is on enforce list that isn't supported */
7873 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7874 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7875 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007876 ret = -EINVAL;
7877 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007878 }
7879 }
7880
7881 /*
7882 * Minimal mode - state operations are allowed to use machine
7883 * credential. Note this already happens by default, so the
7884 * client doesn't have to do anything more than the negotiation.
7885 *
7886 * NOTE: we don't care if EXCHANGE_ID is in the list -
7887 * we're already using the machine cred for exchange_id
7888 * and will never use a different cred.
7889 */
7890 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7891 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7892 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7893 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7894 dfprintk(MOUNT, "sp4_mach_cred:\n");
7895 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007896 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007897 } else {
7898 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007899 ret = -EINVAL;
7900 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007901 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007902
7903 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007904 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7905 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007906 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7907 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007908 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007909 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007910
Andrew Elble99ade3c2015-12-02 09:39:51 -05007911 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7912 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007913 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007914 }
7915
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007916 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7917 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7918 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007919 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007920 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007921
7922 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7923 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7924 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007925 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007926 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007927
7928 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7929 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007930 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007931 }
7932
7933 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7934 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007935 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007936 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007937 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007938out:
7939 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007940 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007941}
7942
Andy Adamson8d89bd72016-09-09 09:22:18 -04007943struct nfs41_exchange_id_data {
7944 struct nfs41_exchange_id_res res;
7945 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007946};
7947
Andy Adamson8d89bd72016-09-09 09:22:18 -04007948static void nfs4_exchange_id_release(void *data)
7949{
7950 struct nfs41_exchange_id_data *cdata =
7951 (struct nfs41_exchange_id_data *)data;
7952
Olga Kornievskaia63513232017-03-13 10:36:19 -04007953 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007954 kfree(cdata->res.impl_id);
7955 kfree(cdata->res.server_scope);
7956 kfree(cdata->res.server_owner);
7957 kfree(cdata);
7958}
7959
7960static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007961 .rpc_release = nfs4_exchange_id_release,
7962};
7963
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007964/*
7965 * _nfs4_proc_exchange_id()
7966 *
7967 * Wrapper for EXCHANGE_ID operation.
7968 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007969static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11007970nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007971 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007972{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007973 struct rpc_message msg = {
7974 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007975 .rpc_cred = cred,
7976 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007977 struct rpc_task_setup task_setup_data = {
7978 .rpc_client = clp->cl_rpcclient,
7979 .callback_ops = &nfs4_exchange_id_call_ops,
7980 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10007981 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007982 };
7983 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007984 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007985
Elena Reshetova212bf412017-10-20 12:53:38 +03007986 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007987 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007988
Trond Myklebust9c760d12017-07-31 18:38:50 -04007989 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007990 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007991 if (!calldata)
7992 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007993
Trond Myklebustfd405592017-08-01 16:02:47 -04007994 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007995
7996 status = nfs4_init_uniform_client_string(clp);
7997 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007998 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007999
Andy Adamson8d89bd72016-09-09 09:22:18 -04008000 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8001 GFP_NOFS);
8002 status = -ENOMEM;
8003 if (unlikely(calldata->res.server_owner == NULL))
8004 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008005
Andy Adamson8d89bd72016-09-09 09:22:18 -04008006 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008007 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008008 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008009 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008010
Andy Adamson8d89bd72016-09-09 09:22:18 -04008011 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8012 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008013 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008014
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008015 switch (sp4_how) {
8016 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008017 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008018 break;
8019
8020 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008021 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008022 break;
8023
8024 default:
8025 /* unsupported! */
8026 WARN_ON_ONCE(1);
8027 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008028 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008029 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008030 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008031 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008032 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008033 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8034 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008035 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008036 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008037 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8038 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008039#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008040 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008041#endif
8042 msg.rpc_argp = &calldata->args;
8043 msg.rpc_resp = &calldata->res;
8044 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008045
Trond Myklebust9c760d12017-07-31 18:38:50 -04008046 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008047
8048out_impl_id:
8049 kfree(calldata->res.impl_id);
8050out_server_scope:
8051 kfree(calldata->res.server_scope);
8052out_server_owner:
8053 kfree(calldata->res.server_owner);
8054out_calldata:
8055 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008056out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008057 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008058 return ERR_PTR(status);
8059}
8060
8061/*
8062 * _nfs4_proc_exchange_id()
8063 *
8064 * Wrapper for EXCHANGE_ID operation.
8065 */
NeilBrowna52458b2018-12-03 11:30:31 +11008066static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008067 u32 sp4_how)
8068{
8069 struct rpc_task *task;
8070 struct nfs41_exchange_id_args *argp;
8071 struct nfs41_exchange_id_res *resp;
8072 int status;
8073
8074 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8075 if (IS_ERR(task))
8076 return PTR_ERR(task);
8077
8078 argp = task->tk_msg.rpc_argp;
8079 resp = task->tk_msg.rpc_resp;
8080 status = task->tk_status;
8081 if (status != 0)
8082 goto out;
8083
8084 status = nfs4_check_cl_exchange_flags(resp->flags);
8085 if (status != 0)
8086 goto out;
8087
8088 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8089 if (status != 0)
8090 goto out;
8091
8092 clp->cl_clientid = resp->clientid;
8093 clp->cl_exchange_flags = resp->flags;
8094 clp->cl_seqid = resp->seqid;
8095 /* Client ID is not confirmed */
8096 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8097 clear_bit(NFS4_SESSION_ESTABLISHED,
8098 &clp->cl_session->session_state);
8099
8100 if (clp->cl_serverscope != NULL &&
8101 !nfs41_same_server_scope(clp->cl_serverscope,
8102 resp->server_scope)) {
8103 dprintk("%s: server_scope mismatch detected\n",
8104 __func__);
8105 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8106 }
8107
8108 swap(clp->cl_serverowner, resp->server_owner);
8109 swap(clp->cl_serverscope, resp->server_scope);
8110 swap(clp->cl_implid, resp->impl_id);
8111
8112 /* Save the EXCHANGE_ID verifier session trunk tests */
8113 memcpy(clp->cl_confirm.data, argp->verifier.data,
8114 sizeof(clp->cl_confirm.data));
8115out:
8116 trace_nfs4_exchange_id(clp, status);
8117 rpc_put_task(task);
8118 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008119}
8120
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008121/*
8122 * nfs4_proc_exchange_id()
8123 *
8124 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8125 *
8126 * Since the clientid has expired, all compounds using sessions
8127 * associated with the stale clientid will be returning
8128 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8129 * be in some phase of session reset.
8130 *
8131 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8132 */
NeilBrowna52458b2018-12-03 11:30:31 +11008133int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008134{
8135 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8136 int status;
8137
8138 /* try SP4_MACH_CRED if krb5i/p */
8139 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8140 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008141 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008142 if (!status)
8143 return 0;
8144 }
8145
8146 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008147 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008148}
8149
Andy Adamson04fa2c62016-09-09 09:22:29 -04008150/**
8151 * nfs4_test_session_trunk
8152 *
8153 * This is an add_xprt_test() test function called from
8154 * rpc_clnt_setup_test_and_add_xprt.
8155 *
8156 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8157 * and is dereferrenced in nfs4_exchange_id_release
8158 *
8159 * Upon success, add the new transport to the rpc_clnt
8160 *
8161 * @clnt: struct rpc_clnt to get new transport
8162 * @xprt: the rpc_xprt to test
8163 * @data: call data for _nfs4_proc_exchange_id.
8164 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308165void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008166 void *data)
8167{
8168 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008169 struct rpc_task *task;
8170 int status;
8171
Andy Adamson04fa2c62016-09-09 09:22:29 -04008172 u32 sp4_how;
8173
8174 dprintk("--> %s try %s\n", __func__,
8175 xprt->address_strings[RPC_DISPLAY_ADDR]);
8176
8177 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8178
8179 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008180 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8181 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308182 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008183
8184 status = task->tk_status;
8185 if (status == 0)
8186 status = nfs4_detect_session_trunking(adata->clp,
8187 task->tk_msg.rpc_resp, xprt);
8188
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308189 if (status == 0)
8190 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8191
Trond Myklebust9c760d12017-07-31 18:38:50 -04008192 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008193}
8194EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8195
Trond Myklebust66245532012-05-25 17:18:09 -04008196static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008197 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008198{
8199 struct rpc_message msg = {
8200 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8201 .rpc_argp = clp,
8202 .rpc_cred = cred,
8203 };
8204 int status;
8205
NeilBrown5a0c2572019-05-30 10:41:28 +10008206 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8207 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008208 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008209 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008210 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008211 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8212 return status;
8213}
8214
8215static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008216 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008217{
8218 unsigned int loop;
8219 int ret;
8220
8221 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8222 ret = _nfs4_proc_destroy_clientid(clp, cred);
8223 switch (ret) {
8224 case -NFS4ERR_DELAY:
8225 case -NFS4ERR_CLIENTID_BUSY:
8226 ssleep(1);
8227 break;
8228 default:
8229 return ret;
8230 }
8231 }
8232 return 0;
8233}
8234
8235int nfs4_destroy_clientid(struct nfs_client *clp)
8236{
NeilBrowna52458b2018-12-03 11:30:31 +11008237 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008238 int ret = 0;
8239
8240 if (clp->cl_mvops->minor_version < 1)
8241 goto out;
8242 if (clp->cl_exchange_flags == 0)
8243 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008244 if (clp->cl_preserve_clid)
8245 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008246 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008247 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008248 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008249 switch (ret) {
8250 case 0:
8251 case -NFS4ERR_STALE_CLIENTID:
8252 clp->cl_exchange_flags = 0;
8253 }
8254out:
8255 return ret;
8256}
8257
Andy Adamson2050f0c2009-04-01 09:22:30 -04008258struct nfs4_get_lease_time_data {
8259 struct nfs4_get_lease_time_args *args;
8260 struct nfs4_get_lease_time_res *res;
8261 struct nfs_client *clp;
8262};
8263
8264static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8265 void *calldata)
8266{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008267 struct nfs4_get_lease_time_data *data =
8268 (struct nfs4_get_lease_time_data *)calldata;
8269
8270 dprintk("--> %s\n", __func__);
8271 /* just setup sequence, do not trigger session recovery
8272 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008273 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008274 &data->args->la_seq_args,
8275 &data->res->lr_seq_res,
8276 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008277 dprintk("<-- %s\n", __func__);
8278}
8279
8280/*
8281 * Called from nfs4_state_manager thread for session setup, so don't recover
8282 * from sequence operation or clientid errors.
8283 */
8284static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8285{
8286 struct nfs4_get_lease_time_data *data =
8287 (struct nfs4_get_lease_time_data *)calldata;
8288
8289 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008290 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8291 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008292 switch (task->tk_status) {
8293 case -NFS4ERR_DELAY:
8294 case -NFS4ERR_GRACE:
8295 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8296 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8297 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008298 /* fall through */
8299 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008300 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008301 return;
8302 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008303 dprintk("<-- %s\n", __func__);
8304}
8305
Trond Myklebust17280172012-03-11 13:11:00 -04008306static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008307 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8308 .rpc_call_done = nfs4_get_lease_time_done,
8309};
8310
8311int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8312{
8313 struct rpc_task *task;
8314 struct nfs4_get_lease_time_args args;
8315 struct nfs4_get_lease_time_res res = {
8316 .lr_fsinfo = fsinfo,
8317 };
8318 struct nfs4_get_lease_time_data data = {
8319 .args = &args,
8320 .res = &res,
8321 .clp = clp,
8322 };
8323 struct rpc_message msg = {
8324 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8325 .rpc_argp = &args,
8326 .rpc_resp = &res,
8327 };
8328 struct rpc_task_setup task_setup = {
8329 .rpc_client = clp->cl_rpcclient,
8330 .rpc_message = &msg,
8331 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008332 .callback_data = &data,
8333 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008334 };
8335 int status;
8336
Anna Schumakerfba83f32018-05-04 16:22:50 -04008337 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008338 task = rpc_run_task(&task_setup);
8339
8340 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008341 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008342
Anna Schumakerf6148712017-04-07 14:15:23 -04008343 status = task->tk_status;
8344 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008345 return status;
8346}
8347
Andy Adamsonfc931582009-04-01 09:22:31 -04008348/*
8349 * Initialize the values to be used by the client in CREATE_SESSION
8350 * If nfs4_init_session set the fore channel request and response sizes,
8351 * use them.
8352 *
8353 * Set the back channel max_resp_sz_cached to zero to force the client to
8354 * always set csa_cachethis to FALSE because the current implementation
8355 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8356 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008357static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8358 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008359{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008360 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008361 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008362
Andy Adamson18aad3d2013-06-26 12:21:49 -04008363 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8364 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8365
Andy Adamsonfc931582009-04-01 09:22:31 -04008366 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008367 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8368 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008369 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008370 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008371
8372 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008373 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008374 __func__,
8375 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008376 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008377
8378 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008379 args->bc_attrs.max_rqst_sz = max_bc_payload;
8380 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008381 args->bc_attrs.max_resp_sz_cached = 0;
8382 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008383 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008384
8385 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8386 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8387 __func__,
8388 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8389 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8390 args->bc_attrs.max_reqs);
8391}
8392
Trond Myklebust79969dd2015-02-18 11:30:18 -08008393static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8394 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008395{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008396 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008397 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008398
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008399 if (rcvd->max_resp_sz > sent->max_resp_sz)
8400 return -EINVAL;
8401 /*
8402 * Our requested max_ops is the minimum we need; we're not
8403 * prepared to break up compounds into smaller pieces than that.
8404 * So, no point even trying to continue if the server won't
8405 * cooperate:
8406 */
8407 if (rcvd->max_ops < sent->max_ops)
8408 return -EINVAL;
8409 if (rcvd->max_reqs == 0)
8410 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008411 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8412 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008413 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008414}
8415
Trond Myklebust79969dd2015-02-18 11:30:18 -08008416static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8417 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008418{
8419 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008420 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008421
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008422 if (!(res->flags & SESSION4_BACK_CHAN))
8423 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008424 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8425 return -EINVAL;
8426 if (rcvd->max_resp_sz < sent->max_resp_sz)
8427 return -EINVAL;
8428 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8429 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008430 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008431 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008432 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008433 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008434out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008435 return 0;
8436}
Andy Adamson8d353012009-04-01 09:22:32 -04008437
Andy Adamson8d353012009-04-01 09:22:32 -04008438static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008439 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008440{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008441 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008442
Trond Myklebust79969dd2015-02-18 11:30:18 -08008443 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008444 if (ret)
8445 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008446 return nfs4_verify_back_channel_attrs(args, res);
8447}
8448
8449static void nfs4_update_session(struct nfs4_session *session,
8450 struct nfs41_create_session_res *res)
8451{
8452 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008453 /* Mark client id and session as being confirmed */
8454 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8455 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008456 session->flags = res->flags;
8457 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008458 if (res->flags & SESSION4_BACK_CHAN)
8459 memcpy(&session->bc_attrs, &res->bc_attrs,
8460 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008461}
8462
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008463static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008464 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008465{
8466 struct nfs4_session *session = clp->cl_session;
8467 struct nfs41_create_session_args args = {
8468 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008469 .clientid = clp->cl_clientid,
8470 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008471 .cb_program = NFS4_CALLBACK,
8472 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008473 struct nfs41_create_session_res res;
8474
Andy Adamsonfc931582009-04-01 09:22:31 -04008475 struct rpc_message msg = {
8476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8477 .rpc_argp = &args,
8478 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008479 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008480 };
8481 int status;
8482
Chuck Lever6b26cc82016-05-02 14:40:40 -04008483 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008484 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008485
NeilBrown5a0c2572019-05-30 10:41:28 +10008486 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8487 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008488 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008489
Trond Myklebustb519d402016-09-11 14:50:01 -04008490 switch (status) {
8491 case -NFS4ERR_STALE_CLIENTID:
8492 case -NFS4ERR_DELAY:
8493 case -ETIMEDOUT:
8494 case -EACCES:
8495 case -EAGAIN:
8496 goto out;
8497 };
8498
8499 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008500 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008501 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008502 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008503 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008504 if (status)
8505 goto out;
8506 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008507 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008508out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008509 return status;
8510}
8511
8512/*
8513 * Issues a CREATE_SESSION operation to the server.
8514 * It is the responsibility of the caller to verify the session is
8515 * expired before calling this routine.
8516 */
NeilBrowna52458b2018-12-03 11:30:31 +11008517int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008518{
8519 int status;
8520 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008521 struct nfs4_session *session = clp->cl_session;
8522
8523 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8524
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008525 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008526 if (status)
8527 goto out;
8528
Andy Adamsonaacd5532011-11-09 13:58:21 -05008529 /* Init or reset the session slot tables */
8530 status = nfs4_setup_session_slot_tables(session);
8531 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008532 if (status)
8533 goto out;
8534
8535 ptr = (unsigned *)&session->sess_id.data[0];
8536 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8537 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008538out:
8539 dprintk("<-- %s\n", __func__);
8540 return status;
8541}
8542
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008543/*
8544 * Issue the over-the-wire RPC DESTROY_SESSION.
8545 * The caller must serialize access to this routine.
8546 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008547int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008548 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008549{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008550 struct rpc_message msg = {
8551 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8552 .rpc_argp = session,
8553 .rpc_cred = cred,
8554 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008555 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008556
8557 dprintk("--> nfs4_proc_destroy_session\n");
8558
8559 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008560 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8561 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008562
NeilBrown5a0c2572019-05-30 10:41:28 +10008563 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8564 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008565 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008566
8567 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008568 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008569 "Session has been destroyed regardless...\n", status);
8570
8571 dprintk("<-- nfs4_proc_destroy_session\n");
8572 return status;
8573}
8574
Trond Myklebust7b38c362012-05-23 13:23:31 -04008575/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008576 * Renew the cl_session lease.
8577 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008578struct nfs4_sequence_data {
8579 struct nfs_client *clp;
8580 struct nfs4_sequence_args args;
8581 struct nfs4_sequence_res res;
8582};
8583
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008584static void nfs41_sequence_release(void *data)
8585{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008586 struct nfs4_sequence_data *calldata = data;
8587 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008588
Elena Reshetova212bf412017-10-20 12:53:38 +03008589 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008590 nfs4_schedule_state_renewal(clp);
8591 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008592 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008593}
8594
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008595static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8596{
8597 switch(task->tk_status) {
8598 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008599 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8600 return -EAGAIN;
8601 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008602 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008603 }
8604 return 0;
8605}
8606
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008607static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008608{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008609 struct nfs4_sequence_data *calldata = data;
8610 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008611
Trond Myklebust14516c32010-07-31 14:29:06 -04008612 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8613 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008614
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008615 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008616 if (task->tk_status < 0) {
8617 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008618 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008619 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008620
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008621 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8622 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008623 return;
8624 }
8625 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008626 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008627out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008628 dprintk("<-- %s\n", __func__);
8629}
8630
8631static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8632{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008633 struct nfs4_sequence_data *calldata = data;
8634 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008635 struct nfs4_sequence_args *args;
8636 struct nfs4_sequence_res *res;
8637
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008638 args = task->tk_msg.rpc_argp;
8639 res = task->tk_msg.rpc_resp;
8640
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008641 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008642}
8643
8644static const struct rpc_call_ops nfs41_sequence_ops = {
8645 .rpc_call_done = nfs41_sequence_call_done,
8646 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008647 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008648};
8649
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008650static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008651 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008652 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008653 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008654{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008655 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008656 struct rpc_message msg = {
8657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8658 .rpc_cred = cred,
8659 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008660 struct rpc_task_setup task_setup_data = {
8661 .rpc_client = clp->cl_rpcclient,
8662 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008663 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008664 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008665 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008666 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008667
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008668 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008669 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008670 goto out_err;
8671
8672 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008673 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008674 if (calldata == NULL)
8675 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008676 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008677 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008678 msg.rpc_argp = &calldata->args;
8679 msg.rpc_resp = &calldata->res;
8680 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008681 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008682
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008683 ret = rpc_run_task(&task_setup_data);
8684 if (IS_ERR(ret))
8685 goto out_err;
8686 return ret;
8687out_put_clp:
8688 nfs_put_client(clp);
8689out_err:
8690 nfs41_release_slot(slot);
8691 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008692}
8693
NeilBrowna52458b2018-12-03 11:30:31 +11008694static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008695{
8696 struct rpc_task *task;
8697 int ret = 0;
8698
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008699 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008700 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008701 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008702 if (IS_ERR(task))
8703 ret = PTR_ERR(task);
8704 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008705 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008706 dprintk("<-- %s status=%d\n", __func__, ret);
8707 return ret;
8708}
8709
NeilBrowna52458b2018-12-03 11:30:31 +11008710static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008711{
8712 struct rpc_task *task;
8713 int ret;
8714
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008715 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008716 if (IS_ERR(task)) {
8717 ret = PTR_ERR(task);
8718 goto out;
8719 }
8720 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008721 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008722 ret = task->tk_status;
8723 rpc_put_task(task);
8724out:
8725 dprintk("<-- %s status=%d\n", __func__, ret);
8726 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008727}
8728
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008729struct nfs4_reclaim_complete_data {
8730 struct nfs_client *clp;
8731 struct nfs41_reclaim_complete_args arg;
8732 struct nfs41_reclaim_complete_res res;
8733};
8734
8735static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8736{
8737 struct nfs4_reclaim_complete_data *calldata = data;
8738
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008739 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008740 &calldata->arg.seq_args,
8741 &calldata->res.seq_res,
8742 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008743}
8744
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008745static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8746{
8747 switch(task->tk_status) {
8748 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008749 wake_up_all(&clp->cl_lock_waitq);
8750 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008751 case -NFS4ERR_COMPLETE_ALREADY:
8752 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8753 break;
8754 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008755 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008756 /* fall through */
8757 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008758 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008759 case -NFS4ERR_BADSESSION:
8760 case -NFS4ERR_DEADSESSION:
8761 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8762 nfs4_schedule_session_recovery(clp->cl_session,
8763 task->tk_status);
8764 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008765 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008766 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008767 }
8768 return 0;
8769}
8770
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008771static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8772{
8773 struct nfs4_reclaim_complete_data *calldata = data;
8774 struct nfs_client *clp = calldata->clp;
8775 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8776
8777 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008778 if (!nfs41_sequence_done(task, res))
8779 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008780
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008781 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008782 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8783 rpc_restart_call_prepare(task);
8784 return;
8785 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008786 dprintk("<-- %s\n", __func__);
8787}
8788
8789static void nfs4_free_reclaim_complete_data(void *data)
8790{
8791 struct nfs4_reclaim_complete_data *calldata = data;
8792
8793 kfree(calldata);
8794}
8795
8796static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8797 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8798 .rpc_call_done = nfs4_reclaim_complete_done,
8799 .rpc_release = nfs4_free_reclaim_complete_data,
8800};
8801
8802/*
8803 * Issue a global reclaim complete.
8804 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008805static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008806 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008807{
8808 struct nfs4_reclaim_complete_data *calldata;
8809 struct rpc_task *task;
8810 struct rpc_message msg = {
8811 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008812 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008813 };
8814 struct rpc_task_setup task_setup_data = {
8815 .rpc_client = clp->cl_rpcclient,
8816 .rpc_message = &msg,
8817 .callback_ops = &nfs4_reclaim_complete_call_ops,
NeilBrown5a0c2572019-05-30 10:41:28 +10008818 .flags = RPC_TASK_ASYNC | RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008819 };
8820 int status = -ENOMEM;
8821
8822 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008823 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008824 if (calldata == NULL)
8825 goto out;
8826 calldata->clp = clp;
8827 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008828
Anna Schumakerfba83f32018-05-04 16:22:50 -04008829 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008830 msg.rpc_argp = &calldata->arg;
8831 msg.rpc_resp = &calldata->res;
8832 task_setup_data.callback_data = calldata;
8833 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008834 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008835 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008836 goto out;
8837 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008838 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008839 if (status == 0)
8840 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008841 rpc_put_task(task);
8842out:
8843 dprintk("<-- %s status=%d\n", __func__, status);
8844 return status;
8845}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008846
8847static void
8848nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8849{
8850 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008851 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008852
8853 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008854 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008855 &lgp->res.seq_res, task);
8856 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008857}
8858
8859static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8860{
8861 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008862
8863 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008864 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008865 dprintk("<-- %s\n", __func__);
8866}
8867
8868static int
8869nfs4_layoutget_handle_exception(struct rpc_task *task,
8870 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8871{
Trond Myklebustee314c22012-10-01 17:25:48 -07008872 struct inode *inode = lgp->args.inode;
8873 struct nfs_server *server = NFS_SERVER(inode);
8874 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008875 int nfs4err = task->tk_status;
8876 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008877 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008878
Boaz Harroshed7e5422014-01-22 20:34:54 +02008879 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008880
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008881 nfs4_sequence_free_slot(&lgp->res.seq_res);
8882
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008883 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008884 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008885 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008886
8887 /*
8888 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8889 * on the file. set tk_status to -ENODATA to tell upper layer to
8890 * retry go inband.
8891 */
8892 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008893 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008894 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008895 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008896 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8897 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8898 */
8899 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008900 status = -EOVERFLOW;
8901 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008902 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008903 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008904 * (or clients) writing to the same RAID stripe except when
8905 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008906 *
8907 * Treat it like we would RECALLCONFLICT -- we retry for a little
8908 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008909 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008910 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008911 if (lgp->args.minlength == 0) {
8912 status = -EOVERFLOW;
8913 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008914 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008915 status = -EBUSY;
8916 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008917 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008918 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008919 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008920 case -NFS4ERR_DELEG_REVOKED:
8921 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008922 case -NFS4ERR_EXPIRED:
8923 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008924 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008925 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008926 lo = NFS_I(inode)->layout;
8927 /* If the open stateid was bad, then recover it. */
8928 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008929 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008930 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008931 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008932 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008933 break;
8934 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008935
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008936 /*
8937 * Mark the bad layout state as invalid, then retry
8938 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008939 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008940 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008941 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008942 pnfs_free_lseg_list(&head);
8943 status = -EAGAIN;
8944 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008945 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008946
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008947 err = nfs4_handle_exception(server, nfs4err, exception);
8948 if (!status) {
8949 if (exception->retry)
8950 status = -EAGAIN;
8951 else
8952 status = err;
8953 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008954out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008955 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008956 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008957}
8958
Fred Isamandacb4522016-09-19 17:47:09 -04008959size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008960{
8961 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8962 return nfs_page_array_len(0, max_resp_sz);
8963}
8964
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008965static void nfs4_layoutget_release(void *calldata)
8966{
8967 struct nfs4_layoutget *lgp = calldata;
8968
8969 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008970 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008971 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008972 dprintk("<-- %s\n", __func__);
8973}
8974
8975static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8976 .rpc_call_prepare = nfs4_layoutget_prepare,
8977 .rpc_call_done = nfs4_layoutget_done,
8978 .rpc_release = nfs4_layoutget_release,
8979};
8980
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008981struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008982nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008983{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008984 struct inode *inode = lgp->args.inode;
8985 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008986 struct rpc_task *task;
8987 struct rpc_message msg = {
8988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8989 .rpc_argp = &lgp->args,
8990 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008991 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008992 };
8993 struct rpc_task_setup task_setup_data = {
8994 .rpc_client = server->client,
8995 .rpc_message = &msg,
8996 .callback_ops = &nfs4_layoutget_call_ops,
8997 .callback_data = lgp,
8998 .flags = RPC_TASK_ASYNC,
8999 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009000 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009001 struct nfs4_exception exception = {
9002 .inode = inode,
9003 .timeout = *timeout,
9004 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009005 int status = 0;
9006
9007 dprintk("--> %s\n", __func__);
9008
Peng Tao4bd5a982014-11-17 11:05:17 +08009009 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9010 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9011
Anna Schumakerfba83f32018-05-04 16:22:50 -04009012 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009013
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009014 task = rpc_run_task(&task_setup_data);
9015 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009016 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009017 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009018 if (status != 0)
9019 goto out;
9020
Trond Myklebust18c07782019-02-13 07:55:31 -05009021 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009022 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9023 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009024 } else if (lgp->res.layoutp->len == 0) {
9025 status = -EAGAIN;
9026 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009027 } else
9028 lseg = pnfs_layout_process(lgp);
9029out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009030 trace_nfs4_layoutget(lgp->args.ctx,
9031 &lgp->args.range,
9032 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009033 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009034 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009035
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009036 rpc_put_task(task);
9037 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009038 if (status)
9039 return ERR_PTR(status);
9040 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009041}
9042
Benny Halevycbe82602011-05-22 19:52:37 +03009043static void
9044nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9045{
9046 struct nfs4_layoutreturn *lrp = calldata;
9047
9048 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009049 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009050 &lrp->args.seq_args,
9051 &lrp->res.seq_res,
9052 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009053 if (!pnfs_layout_is_valid(lrp->args.layout))
9054 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009055}
9056
9057static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9058{
9059 struct nfs4_layoutreturn *lrp = calldata;
9060 struct nfs_server *server;
9061
9062 dprintk("--> %s\n", __func__);
9063
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009064 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009065 return;
9066
9067 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009068 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009069 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04009070 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009071 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009072 lrp->args.inode))
9073 goto out_restart;
9074 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009075 default:
9076 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009077 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009078 case 0:
9079 break;
9080 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009081 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009082 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009083 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009084 }
Benny Halevycbe82602011-05-22 19:52:37 +03009085 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009086 return;
9087out_restart:
9088 task->tk_status = 0;
9089 nfs4_sequence_free_slot(&lrp->res.seq_res);
9090 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009091}
9092
9093static void nfs4_layoutreturn_release(void *calldata)
9094{
9095 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009096 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009097
9098 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009099 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009100 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009101 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009102 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9103 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009104 pnfs_put_layout_hdr(lrp->args.layout);
9105 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009106 kfree(calldata);
9107 dprintk("<-- %s\n", __func__);
9108}
9109
9110static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9111 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9112 .rpc_call_done = nfs4_layoutreturn_done,
9113 .rpc_release = nfs4_layoutreturn_release,
9114};
9115
Peng Tao6c166052014-11-17 09:30:40 +08009116int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009117{
9118 struct rpc_task *task;
9119 struct rpc_message msg = {
9120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9121 .rpc_argp = &lrp->args,
9122 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009123 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009124 };
9125 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009126 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009127 .rpc_message = &msg,
9128 .callback_ops = &nfs4_layoutreturn_call_ops,
9129 .callback_data = lrp,
9130 };
Peng Tao6c166052014-11-17 09:30:40 +08009131 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009132
Andrew Elble99ade3c2015-12-02 09:39:51 -05009133 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9134 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9135 &task_setup_data.rpc_client, &msg);
9136
Benny Halevycbe82602011-05-22 19:52:37 +03009137 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009138 if (!sync) {
9139 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9140 if (!lrp->inode) {
9141 nfs4_layoutreturn_release(lrp);
9142 return -EAGAIN;
9143 }
9144 task_setup_data.flags |= RPC_TASK_ASYNC;
9145 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009146 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009147 task = rpc_run_task(&task_setup_data);
9148 if (IS_ERR(task))
9149 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009150 if (sync)
9151 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009152 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009153 dprintk("<-- %s status=%d\n", __func__, status);
9154 rpc_put_task(task);
9155 return status;
9156}
9157
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009158static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009159_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9160 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009161 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009162{
9163 struct nfs4_getdeviceinfo_args args = {
9164 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009165 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9166 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009167 };
9168 struct nfs4_getdeviceinfo_res res = {
9169 .pdev = pdev,
9170 };
9171 struct rpc_message msg = {
9172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9173 .rpc_argp = &args,
9174 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009175 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009176 };
9177 int status;
9178
9179 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009180 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009181 if (res.notification & ~args.notify_types)
9182 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009183 if (res.notification != args.notify_types)
9184 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009185
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009186 dprintk("<-- %s status=%d\n", __func__, status);
9187
9188 return status;
9189}
9190
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009191int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9192 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009193 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009194{
9195 struct nfs4_exception exception = { };
9196 int err;
9197
9198 do {
9199 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009200 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009201 &exception);
9202 } while (exception.retry);
9203 return err;
9204}
9205EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9206
Andy Adamson863a3c62011-03-23 13:27:54 +00009207static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9208{
9209 struct nfs4_layoutcommit_data *data = calldata;
9210 struct nfs_server *server = NFS_SERVER(data->args.inode);
9211
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009212 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009213 &data->args.seq_args,
9214 &data->res.seq_res,
9215 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009216}
9217
9218static void
9219nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9220{
9221 struct nfs4_layoutcommit_data *data = calldata;
9222 struct nfs_server *server = NFS_SERVER(data->args.inode);
9223
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009224 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009225 return;
9226
9227 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009228 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9229 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9230 case -NFS4ERR_BADLAYOUT: /* no layout */
9231 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009232 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009233 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009234 break;
9235 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009236 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009237 rpc_restart_call_prepare(task);
9238 return;
9239 }
9240 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009241}
9242
9243static void nfs4_layoutcommit_release(void *calldata)
9244{
9245 struct nfs4_layoutcommit_data *data = calldata;
9246
Andy Adamsondb29c082011-07-30 20:52:38 -04009247 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009248 nfs_post_op_update_inode_force_wcc(data->args.inode,
9249 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009250 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009251 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009252 kfree(data);
9253}
9254
9255static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9256 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9257 .rpc_call_done = nfs4_layoutcommit_done,
9258 .rpc_release = nfs4_layoutcommit_release,
9259};
9260
9261int
Andy Adamsonef311532011-03-12 02:58:10 -05009262nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009263{
9264 struct rpc_message msg = {
9265 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9266 .rpc_argp = &data->args,
9267 .rpc_resp = &data->res,
9268 .rpc_cred = data->cred,
9269 };
9270 struct rpc_task_setup task_setup_data = {
9271 .task = &data->task,
9272 .rpc_client = NFS_CLIENT(data->args.inode),
9273 .rpc_message = &msg,
9274 .callback_ops = &nfs4_layoutcommit_ops,
9275 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009276 };
9277 struct rpc_task *task;
9278 int status = 0;
9279
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009280 dprintk("NFS: initiating layoutcommit call. sync %d "
9281 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009282 data->args.lastbytewritten,
9283 data->args.inode->i_ino);
9284
Trond Myklebust472e2592015-02-05 16:50:30 -05009285 if (!sync) {
9286 data->inode = nfs_igrab_and_active(data->args.inode);
9287 if (data->inode == NULL) {
9288 nfs4_layoutcommit_release(data);
9289 return -EAGAIN;
9290 }
9291 task_setup_data.flags = RPC_TASK_ASYNC;
9292 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009293 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009294 task = rpc_run_task(&task_setup_data);
9295 if (IS_ERR(task))
9296 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009297 if (sync)
9298 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009299 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009300 dprintk("%s: status %d\n", __func__, status);
9301 rpc_put_task(task);
9302 return status;
9303}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009304
Trond Myklebust302fad72019-02-18 13:32:38 -05009305/*
Andy Adamson97431202013-08-08 10:57:56 -04009306 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9307 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9308 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009309static int
9310_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009311 struct nfs_fsinfo *info,
9312 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009313{
9314 struct nfs41_secinfo_no_name_args args = {
9315 .style = SECINFO_STYLE_CURRENT_FH,
9316 };
9317 struct nfs4_secinfo_res res = {
9318 .flavors = flavors,
9319 };
9320 struct rpc_message msg = {
9321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9322 .rpc_argp = &args,
9323 .rpc_resp = &res,
9324 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009325 struct rpc_clnt *clnt = server->client;
NeilBrowna52458b2018-12-03 11:30:31 +11009326 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009327 int status;
9328
9329 if (use_integrity) {
9330 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009331 cred = nfs4_get_clid_cred(server->nfs_client);
9332 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009333 }
9334
9335 dprintk("--> %s\n", __func__);
9336 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
NeilBrown5a0c2572019-05-30 10:41:28 +10009337 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009338 dprintk("<-- %s status=%d\n", __func__, status);
9339
NeilBrowna52458b2018-12-03 11:30:31 +11009340 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009341
9342 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009343}
9344
9345static int
9346nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9347 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9348{
Trond Myklebust0688e642019-04-07 13:59:09 -04009349 struct nfs4_exception exception = {
9350 .interruptible = true,
9351 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009352 int err;
9353 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009354 /* first try using integrity protection */
9355 err = -NFS4ERR_WRONGSEC;
9356
9357 /* try to use integrity protection with machine cred */
9358 if (_nfs4_is_integrity_protected(server->nfs_client))
9359 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9360 flavors, true);
9361
9362 /*
9363 * if unable to use integrity protection, or SECINFO with
9364 * integrity protection returns NFS4ERR_WRONGSEC (which is
9365 * disallowed by spec, but exists in deployed servers) use
9366 * the current filesystem's rpc_client and the user cred.
9367 */
9368 if (err == -NFS4ERR_WRONGSEC)
9369 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9370 flavors, false);
9371
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009372 switch (err) {
9373 case 0:
9374 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009375 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009376 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009377 default:
9378 err = nfs4_handle_exception(server, err, &exception);
9379 }
9380 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009381out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009382 return err;
9383}
9384
9385static int
9386nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9387 struct nfs_fsinfo *info)
9388{
9389 int err;
9390 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009391 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009392 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009393 struct nfs4_secinfo4 *secinfo;
9394 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009395
9396 page = alloc_page(GFP_KERNEL);
9397 if (!page) {
9398 err = -ENOMEM;
9399 goto out;
9400 }
9401
9402 flavors = page_address(page);
9403 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9404
9405 /*
9406 * Fall back on "guess and check" method if
9407 * the server doesn't support SECINFO_NO_NAME
9408 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009409 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009410 err = nfs4_find_root_sec(server, fhandle, info);
9411 goto out_freepage;
9412 }
9413 if (err)
9414 goto out_freepage;
9415
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009416 for (i = 0; i < flavors->num_flavors; i++) {
9417 secinfo = &flavors->flavors[i];
9418
9419 switch (secinfo->flavor) {
9420 case RPC_AUTH_NULL:
9421 case RPC_AUTH_UNIX:
9422 case RPC_AUTH_GSS:
9423 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9424 &secinfo->flavor_info);
9425 break;
9426 default:
9427 flavor = RPC_AUTH_MAXFLAVOR;
9428 break;
9429 }
9430
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009431 if (!nfs_auth_info_match(&server->auth_info, flavor))
9432 flavor = RPC_AUTH_MAXFLAVOR;
9433
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009434 if (flavor != RPC_AUTH_MAXFLAVOR) {
9435 err = nfs4_lookup_root_sec(server, fhandle,
9436 info, flavor);
9437 if (!err)
9438 break;
9439 }
9440 }
9441
9442 if (flavor == RPC_AUTH_MAXFLAVOR)
9443 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009444
9445out_freepage:
9446 put_page(page);
9447 if (err == -EACCES)
9448 return -EPERM;
9449out:
9450 return err;
9451}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009452
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009453static int _nfs41_test_stateid(struct nfs_server *server,
9454 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009455 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009456{
9457 int status;
9458 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009459 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009460 };
9461 struct nfs41_test_stateid_res res;
9462 struct rpc_message msg = {
9463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9464 .rpc_argp = &args,
9465 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009466 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009467 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009468 struct rpc_clnt *rpc_client = server->client;
9469
9470 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9471 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009472
Chuck Lever38527b12012-07-11 16:30:23 -04009473 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009474 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009475 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009476 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009477 if (status != NFS_OK) {
9478 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009479 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009480 }
9481 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009482 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009483}
9484
Trond Myklebust43912bb2016-09-22 13:38:56 -04009485static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9486 int err, struct nfs4_exception *exception)
9487{
9488 exception->retry = 0;
9489 switch(err) {
9490 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009491 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009492 nfs4_handle_exception(server, err, exception);
9493 break;
9494 case -NFS4ERR_BADSESSION:
9495 case -NFS4ERR_BADSLOT:
9496 case -NFS4ERR_BAD_HIGH_SLOT:
9497 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9498 case -NFS4ERR_DEADSESSION:
9499 nfs4_do_handle_exception(server, err, exception);
9500 }
9501}
9502
Chuck Lever38527b12012-07-11 16:30:23 -04009503/**
9504 * nfs41_test_stateid - perform a TEST_STATEID operation
9505 *
9506 * @server: server / transport on which to perform the operation
9507 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009508 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009509 *
9510 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9511 * Otherwise a negative NFS4ERR value is returned if the operation
9512 * failed or the state ID is not currently valid.
9513 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009514static int nfs41_test_stateid(struct nfs_server *server,
9515 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009516 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009517{
Trond Myklebust0688e642019-04-07 13:59:09 -04009518 struct nfs4_exception exception = {
9519 .interruptible = true,
9520 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009521 int err;
9522 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009523 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009524 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009525 } while (exception.retry);
9526 return err;
9527}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009528
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009529struct nfs_free_stateid_data {
9530 struct nfs_server *server;
9531 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009532 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009533};
9534
9535static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9536{
9537 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009538 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009539 &data->args.seq_args,
9540 &data->res.seq_res,
9541 task);
9542}
9543
9544static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9545{
9546 struct nfs_free_stateid_data *data = calldata;
9547
9548 nfs41_sequence_done(task, &data->res.seq_res);
9549
9550 switch (task->tk_status) {
9551 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009552 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009553 rpc_restart_call_prepare(task);
9554 }
9555}
9556
9557static void nfs41_free_stateid_release(void *calldata)
9558{
9559 kfree(calldata);
9560}
9561
Trond Myklebust17f26b12013-08-21 15:48:42 -04009562static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009563 .rpc_call_prepare = nfs41_free_stateid_prepare,
9564 .rpc_call_done = nfs41_free_stateid_done,
9565 .rpc_release = nfs41_free_stateid_release,
9566};
9567
Anna Schumaker2f261022018-05-15 13:03:39 -04009568/**
9569 * nfs41_free_stateid - perform a FREE_STATEID operation
9570 *
9571 * @server: server / transport on which to perform the operation
9572 * @stateid: state ID to release
9573 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009574 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009575 *
9576 * Note: this function is always asynchronous.
9577 */
9578static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009579 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009580 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009581 bool privileged)
9582{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009583 struct rpc_message msg = {
9584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009585 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009586 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009587 struct rpc_task_setup task_setup = {
9588 .rpc_client = server->client,
9589 .rpc_message = &msg,
9590 .callback_ops = &nfs41_free_stateid_ops,
9591 .flags = RPC_TASK_ASYNC,
9592 };
9593 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009594 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009595
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009596 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9597 &task_setup.rpc_client, &msg);
9598
Chuck Lever38527b12012-07-11 16:30:23 -04009599 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009600 data = kmalloc(sizeof(*data), GFP_NOFS);
9601 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009602 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009603 data->server = server;
9604 nfs4_stateid_copy(&data->args.stateid, stateid);
9605
9606 task_setup.callback_data = data;
9607
9608 msg.rpc_argp = &data->args;
9609 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009610 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009611 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009612 if (IS_ERR(task))
9613 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009614 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009615 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009616}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009617
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009618static void
9619nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009620{
NeilBrowna52458b2018-12-03 11:30:31 +11009621 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009622
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009623 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009624 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009625}
9626
Trond Myklebust36281ca2012-03-04 18:13:56 -05009627static bool nfs41_match_stateid(const nfs4_stateid *s1,
9628 const nfs4_stateid *s2)
9629{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009630 if (s1->type != s2->type)
9631 return false;
9632
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009633 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009634 return false;
9635
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009636 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009637 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009638
Anna Schumaker045c5512017-01-11 16:59:48 -05009639 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009640}
9641
Andy Adamson557134a2009-04-01 09:21:53 -04009642#endif /* CONFIG_NFS_V4_1 */
9643
Trond Myklebust36281ca2012-03-04 18:13:56 -05009644static bool nfs4_match_stateid(const nfs4_stateid *s1,
9645 const nfs4_stateid *s2)
9646{
Trond Myklebustf597c532012-03-04 18:13:56 -05009647 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009648}
9649
9650
Trond Myklebust17280172012-03-11 13:11:00 -04009651static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009652 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009653 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 .recover_open = nfs4_open_reclaim,
9655 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009656 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009657 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658};
9659
Andy Adamson591d71c2009-04-01 09:22:47 -04009660#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009661static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009662 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9663 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9664 .recover_open = nfs4_open_reclaim,
9665 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009666 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009667 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009668 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009669};
9670#endif /* CONFIG_NFS_V4_1 */
9671
Trond Myklebust17280172012-03-11 13:11:00 -04009672static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009673 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009674 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009675 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009677 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678};
9679
Andy Adamson591d71c2009-04-01 09:22:47 -04009680#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009681static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009682 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9683 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009684 .recover_open = nfs41_open_expired,
9685 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009686 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009687};
9688#endif /* CONFIG_NFS_V4_1 */
9689
Trond Myklebust17280172012-03-11 13:11:00 -04009690static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009691 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009692 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009693 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009694};
9695
9696#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009697static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009698 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009699 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009700 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009701};
9702#endif
9703
Chuck Leverec011fe2013-10-17 14:12:39 -04009704static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009705 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009706 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009707};
9708
9709#if defined(CONFIG_NFS_V4_1)
9710static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009711 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009712 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009713};
9714#endif /* CONFIG_NFS_V4_1 */
9715
Trond Myklebust97dc1352010-06-16 09:52:26 -04009716static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9717 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009718 .init_caps = NFS_CAP_READDIRPLUS
9719 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009720 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009721 .init_client = nfs40_init_client,
9722 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009723 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009724 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009725 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009726 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009727 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009728 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009729 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9730 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9731 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009732 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009733};
9734
9735#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009736static struct nfs_seqid *
9737nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9738{
9739 return NULL;
9740}
9741
Trond Myklebust97dc1352010-06-16 09:52:26 -04009742static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9743 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009744 .init_caps = NFS_CAP_READDIRPLUS
9745 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009746 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009747 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009748 | NFS_CAP_ATOMIC_OPEN_V1
9749 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009750 .init_client = nfs41_init_client,
9751 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009752 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009753 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009754 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009755 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009756 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009757 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009758 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009759 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9760 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9761 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009762 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009763};
9764#endif
9765
Steve Dickson42c2c422013-05-22 12:50:38 -04009766#if defined(CONFIG_NFS_V4_2)
9767static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9768 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009769 .init_caps = NFS_CAP_READDIRPLUS
9770 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009771 | NFS_CAP_POSIX_LOCK
9772 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009773 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009774 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009775 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009776 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009777 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009778 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009779 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009780 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009781 | NFS_CAP_CLONE
9782 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009783 .init_client = nfs41_init_client,
9784 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009785 .match_stateid = nfs41_match_stateid,
9786 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009787 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009788 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009789 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009790 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009791 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009792 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9793 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9794 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009795 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009796};
9797#endif
9798
Trond Myklebust97dc1352010-06-16 09:52:26 -04009799const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9800 [0] = &nfs_v4_0_minor_ops,
9801#if defined(CONFIG_NFS_V4_1)
9802 [1] = &nfs_v4_1_minor_ops,
9803#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009804#if defined(CONFIG_NFS_V4_2)
9805 [2] = &nfs_v4_2_minor_ops,
9806#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009807};
9808
Trond Myklebust13997822016-07-24 17:10:52 -04009809static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009810{
9811 ssize_t error, error2;
9812
9813 error = generic_listxattr(dentry, list, size);
9814 if (error < 0)
9815 return error;
9816 if (list) {
9817 list += error;
9818 size -= error;
9819 }
9820
9821 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9822 if (error2 < 0)
9823 return error2;
9824 return error + error2;
9825}
9826
Trond Myklebust17f26b12013-08-21 15:48:42 -04009827static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009828 .create = nfs_create,
9829 .lookup = nfs_lookup,
9830 .atomic_open = nfs_atomic_open,
9831 .link = nfs_link,
9832 .unlink = nfs_unlink,
9833 .symlink = nfs_symlink,
9834 .mkdir = nfs_mkdir,
9835 .rmdir = nfs_rmdir,
9836 .mknod = nfs_mknod,
9837 .rename = nfs_rename,
9838 .permission = nfs_permission,
9839 .getattr = nfs_getattr,
9840 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009841 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009842};
9843
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009844static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009845 .permission = nfs_permission,
9846 .getattr = nfs_getattr,
9847 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009848 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009849};
9850
David Howells509de812006-08-22 20:06:11 -04009851const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009852 .version = 4, /* protocol version */
9853 .dentry_ops = &nfs4_dentry_operations,
9854 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009855 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009856 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009857 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009858 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009859 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009860 .getattr = nfs4_proc_getattr,
9861 .setattr = nfs4_proc_setattr,
9862 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009863 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009864 .access = nfs4_proc_access,
9865 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009866 .create = nfs4_proc_create,
9867 .remove = nfs4_proc_remove,
9868 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009869 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009870 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009871 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009872 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009873 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009874 .link = nfs4_proc_link,
9875 .symlink = nfs4_proc_symlink,
9876 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009877 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009878 .readdir = nfs4_proc_readdir,
9879 .mknod = nfs4_proc_mknod,
9880 .statfs = nfs4_proc_statfs,
9881 .fsinfo = nfs4_proc_fsinfo,
9882 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009883 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009884 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009885 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009886 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009887 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009888 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009889 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009890 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009891 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009892 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009893 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009894 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009895 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009896 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009897 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009898 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009899 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009900 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009901 .create_server = nfs4_create_server,
9902 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009903};
9904
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009905static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009906 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009907 .list = nfs4_xattr_list_nfs4_acl,
9908 .get = nfs4_xattr_get_nfs4_acl,
9909 .set = nfs4_xattr_set_nfs4_acl,
9910};
9911
9912const struct xattr_handler *nfs4_xattr_handlers[] = {
9913 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009914#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9915 &nfs4_xattr_nfs4_label_handler,
9916#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009917 NULL
9918};
9919
Linus Torvalds1da177e2005-04-16 15:20:36 -07009920/*
9921 * Local variables:
9922 * c-basic-offset: 8
9923 * End:
9924 */