blob: de2b3fd806efffb1976655af816d381e029093be [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust30846df2018-04-07 13:44:28 -040074#define NFS4_BITMASK_SZ 3
75
Trond Myklebust2066fe82006-09-15 08:30:46 -040076#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070077#define NFS4_POLL_RETRY_MAX (15*HZ)
78
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020079/* file attributes which can be mapped to nfs attributes */
80#define NFS4_VALID_ATTRS (ATTR_MODE \
81 | ATTR_UID \
82 | ATTR_GID \
83 | ATTR_SIZE \
84 | ATTR_ATIME \
85 | ATTR_MTIME \
86 | ATTR_CTIME \
87 | ATTR_ATIME_SET \
88 | ATTR_MTIME_SET)
89
Trond Myklebustcdd4e682006-01-03 09:55:12 +010090struct nfs4_opendata;
Alexandros Batsakisb2579572009-12-14 21:27:57 -080091static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070092static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050093static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
Trond Myklebusta841b542018-04-07 13:50:59 -040094static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label, struct inode *inode);
95static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label, struct inode *inode);
NeilBrowna52458b2018-12-03 11:30:31 +110096static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust0ab64e02010-04-16 16:22:51 -040097 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110098 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040099 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400100#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400101static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +1100102 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400103 struct nfs4_slot *slot,
104 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400105static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100106 const struct cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
NeilBrowna52458b2018-12-03 11:30:31 +1100108 const struct cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400109#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400110
111#ifdef CONFIG_NFS_V4_SECURITY_LABEL
112static inline struct nfs4_label *
113nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
114 struct iattr *sattr, struct nfs4_label *label)
115{
116 int err;
117
118 if (label == NULL)
119 return NULL;
120
121 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
122 return NULL;
123
David Quigleyaa9c2662013-05-22 12:50:44 -0400124 err = security_dentry_init_security(dentry, sattr->ia_mode,
125 &dentry->d_name, (void **)&label->label, &label->len);
126 if (err == 0)
127 return label;
128
129 return NULL;
130}
131static inline void
132nfs4_label_release_security(struct nfs4_label *label)
133{
134 if (label)
135 security_release_secctx(label->label, label->len);
136}
137static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
138{
139 if (label)
140 return server->attr_bitmask;
141
142 return server->attr_bitmask_nl;
143}
144#else
145static inline struct nfs4_label *
146nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
147 struct iattr *sattr, struct nfs4_label *l)
148{ return NULL; }
149static inline void
150nfs4_label_release_security(struct nfs4_label *label)
151{ return; }
152static inline u32 *
153nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
154{ return server->attr_bitmask; }
155#endif
156
Linus Torvalds1da177e2005-04-16 15:20:36 -0700157/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500158static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159{
Trond Myklebust52567b02009-10-23 14:46:42 -0400160 if (err >= -1000)
161 return err;
162 switch (err) {
163 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500164 case -NFS4ERR_LAYOUTTRYLATER:
165 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400166 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000167 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400168 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000169 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800170 case -NFS4ERR_BADOWNER:
171 case -NFS4ERR_BADNAME:
172 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400173 case -NFS4ERR_SHARE_DENIED:
174 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400175 case -NFS4ERR_MINOR_VERS_MISMATCH:
176 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400177 case -NFS4ERR_FILE_OPEN:
178 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400179 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700180 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700181 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400182 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400184 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185}
186
187/*
188 * This is our standard bitmap for GETATTR requests.
189 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400190const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 FATTR4_WORD0_TYPE
192 | FATTR4_WORD0_CHANGE
193 | FATTR4_WORD0_SIZE
194 | FATTR4_WORD0_FSID
195 | FATTR4_WORD0_FILEID,
196 FATTR4_WORD1_MODE
197 | FATTR4_WORD1_NUMLINKS
198 | FATTR4_WORD1_OWNER
199 | FATTR4_WORD1_OWNER_GROUP
200 | FATTR4_WORD1_RAWDEV
201 | FATTR4_WORD1_SPACE_USED
202 | FATTR4_WORD1_TIME_ACCESS
203 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400204 | FATTR4_WORD1_TIME_MODIFY
205 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400206#ifdef CONFIG_NFS_V4_SECURITY_LABEL
207 FATTR4_WORD2_SECURITY_LABEL
208#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209};
210
Trond Myklebust1549210f2012-06-05 09:16:47 -0400211static const u32 nfs4_pnfs_open_bitmap[3] = {
212 FATTR4_WORD0_TYPE
213 | FATTR4_WORD0_CHANGE
214 | FATTR4_WORD0_SIZE
215 | FATTR4_WORD0_FSID
216 | FATTR4_WORD0_FILEID,
217 FATTR4_WORD1_MODE
218 | FATTR4_WORD1_NUMLINKS
219 | FATTR4_WORD1_OWNER
220 | FATTR4_WORD1_OWNER_GROUP
221 | FATTR4_WORD1_RAWDEV
222 | FATTR4_WORD1_SPACE_USED
223 | FATTR4_WORD1_TIME_ACCESS
224 | FATTR4_WORD1_TIME_METADATA
225 | FATTR4_WORD1_TIME_MODIFY,
226 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500227#ifdef CONFIG_NFS_V4_SECURITY_LABEL
228 | FATTR4_WORD2_SECURITY_LABEL
229#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400230};
231
Andy Adamsone23008e2012-10-02 21:07:32 -0400232static const u32 nfs4_open_noattr_bitmap[3] = {
233 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400234 | FATTR4_WORD0_FILEID,
235};
236
David Quigleya09df2c2013-05-22 12:50:41 -0400237const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700238 FATTR4_WORD0_FILES_AVAIL
239 | FATTR4_WORD0_FILES_FREE
240 | FATTR4_WORD0_FILES_TOTAL,
241 FATTR4_WORD1_SPACE_AVAIL
242 | FATTR4_WORD1_SPACE_FREE
243 | FATTR4_WORD1_SPACE_TOTAL
244};
245
David Quigleya09df2c2013-05-22 12:50:41 -0400246const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700247 FATTR4_WORD0_MAXLINK
248 | FATTR4_WORD0_MAXNAME,
249 0
250};
251
Fred Isamandae100c2011-07-30 20:52:37 -0400252const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700253 | FATTR4_WORD0_MAXREAD
254 | FATTR4_WORD0_MAXWRITE
255 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700256 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400257 | FATTR4_WORD1_FS_LAYOUT_TYPES,
258 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800259 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260};
261
David Quigleya09df2c2013-05-22 12:50:41 -0400262const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500263 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400264 | FATTR4_WORD0_SIZE
265 | FATTR4_WORD0_FSID
266 | FATTR4_WORD0_FILEID
267 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500268 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400269 | FATTR4_WORD1_OWNER_GROUP
270 | FATTR4_WORD1_RAWDEV
271 | FATTR4_WORD1_SPACE_USED
272 | FATTR4_WORD1_TIME_ACCESS
273 | FATTR4_WORD1_TIME_METADATA
274 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400275 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400276};
277
Trond Myklebust30846df2018-04-07 13:44:28 -0400278static void nfs4_bitmap_copy_adjust(__u32 *dst, const __u32 *src,
279 struct inode *inode)
280{
281 unsigned long cache_validity;
282
283 memcpy(dst, src, NFS4_BITMASK_SZ*sizeof(*dst));
284 if (!inode || !nfs4_have_delegation(inode, FMODE_READ))
285 return;
286
287 cache_validity = READ_ONCE(NFS_I(inode)->cache_validity);
288 if (!(cache_validity & NFS_INO_REVAL_FORCED))
289 cache_validity &= ~(NFS_INO_INVALID_CHANGE
290 | NFS_INO_INVALID_SIZE);
291
292 if (!(cache_validity & NFS_INO_INVALID_SIZE))
293 dst[0] &= ~FATTR4_WORD0_SIZE;
294
295 if (!(cache_validity & NFS_INO_INVALID_CHANGE))
296 dst[0] &= ~FATTR4_WORD0_CHANGE;
297}
298
299static void nfs4_bitmap_copy_adjust_setattr(__u32 *dst,
300 const __u32 *src, struct inode *inode)
301{
302 nfs4_bitmap_copy_adjust(dst, src, inode);
303}
304
Al Virobc4785c2006-10-19 23:28:51 -0700305static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700306 struct nfs4_readdir_arg *readdir)
307{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400308 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700309 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700310
Linus Torvalds1da177e2005-04-16 15:20:36 -0700311 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000312 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
314 return;
315 }
316
317 readdir->cookie = 0;
318 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
319 if (cookie == 2)
320 return;
321
322 /*
323 * NFSv4 servers do not return entries for '.' and '..'
324 * Therefore, we fake these entries here. We let '.'
325 * have cookie 0 and '..' have cookie 1. Note that
326 * when talking to the server, we always send cookie 0
327 * instead of 1 or 2.
328 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800329 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700330
331 if (cookie == 0) {
332 *p++ = xdr_one; /* next */
333 *p++ = xdr_zero; /* cookie, first word */
334 *p++ = xdr_one; /* cookie, second word */
335 *p++ = xdr_one; /* entry len */
336 memcpy(p, ".\0\0\0", 4); /* entry */
337 p++;
338 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400339 *p++ = htonl(attrs); /* bitmap */
340 *p++ = htonl(12); /* attribute buffer length */
341 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000342 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343 }
344
345 *p++ = xdr_one; /* next */
346 *p++ = xdr_zero; /* cookie, first word */
347 *p++ = xdr_two; /* cookie, second word */
348 *p++ = xdr_two; /* entry len */
349 memcpy(p, "..\0\0", 4); /* entry */
350 p++;
351 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400352 *p++ = htonl(attrs); /* bitmap */
353 *p++ = htonl(12); /* attribute buffer length */
354 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000355 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700356
357 readdir->pgbase = (char *)p - (char *)start;
358 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800359 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360}
361
Trond Myklebust26d36302016-09-22 13:39:05 -0400362static void nfs4_test_and_free_stateid(struct nfs_server *server,
363 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100364 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400365{
366 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
367
368 ops->test_and_free_expired(server, stateid, cred);
369}
370
371static void __nfs4_free_revoked_stateid(struct nfs_server *server,
372 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100373 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400374{
375 stateid->type = NFS4_REVOKED_STATEID_TYPE;
376 nfs4_test_and_free_stateid(server, stateid, cred);
377}
378
379static void nfs4_free_revoked_stateid(struct nfs_server *server,
380 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +1100381 const struct cred *cred)
Trond Myklebust26d36302016-09-22 13:39:05 -0400382{
383 nfs4_stateid tmp;
384
385 nfs4_stateid_copy(&tmp, stateid);
386 __nfs4_free_revoked_stateid(server, &tmp, cred);
387}
388
NeilBrown8478eaa2014-09-18 16:09:27 +1000389static long nfs4_update_delay(long *timeout)
390{
391 long ret;
392 if (!timeout)
393 return NFS4_POLL_RETRY_MAX;
394 if (*timeout <= 0)
395 *timeout = NFS4_POLL_RETRY_MIN;
396 if (*timeout > NFS4_POLL_RETRY_MAX)
397 *timeout = NFS4_POLL_RETRY_MAX;
398 ret = *timeout;
399 *timeout <<= 1;
400 return ret;
401}
402
Trond Myklebust0688e642019-04-07 13:59:09 -0400403static int nfs4_delay_killable(long *timeout)
Trond Myklebust65de8722008-12-23 15:21:44 -0500404{
Trond Myklebust65de8722008-12-23 15:21:44 -0500405 might_sleep();
406
NeilBrown8478eaa2014-09-18 16:09:27 +1000407 freezable_schedule_timeout_killable_unsafe(
408 nfs4_update_delay(timeout));
Trond Myklebust0688e642019-04-07 13:59:09 -0400409 if (!__fatal_signal_pending(current))
410 return 0;
411 return -EINTR;
412}
413
414static int nfs4_delay_interruptible(long *timeout)
415{
416 might_sleep();
417
418 freezable_schedule_timeout_interruptible(nfs4_update_delay(timeout));
419 if (!signal_pending(current))
420 return 0;
421 return __fatal_signal_pending(current) ? -EINTR :-ERESTARTSYS;
422}
423
424static int nfs4_delay(long *timeout, bool interruptible)
425{
426 if (interruptible)
427 return nfs4_delay_interruptible(timeout);
428 return nfs4_delay_killable(timeout);
Trond Myklebust65de8722008-12-23 15:21:44 -0500429}
430
Trond Myklebust50c80002019-07-11 19:02:18 -0400431static const nfs4_stateid *
432nfs4_recoverable_stateid(const nfs4_stateid *stateid)
433{
434 if (!stateid)
435 return NULL;
436 switch (stateid->type) {
437 case NFS4_OPEN_STATEID_TYPE:
438 case NFS4_LOCK_STATEID_TYPE:
439 case NFS4_DELEGATION_STATEID_TYPE:
440 return stateid;
441 default:
442 break;
443 }
444 return NULL;
445}
446
Trond Myklebust65de8722008-12-23 15:21:44 -0500447/* This is the error handling routine for processes that are allowed
448 * to sleep.
449 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400450static int nfs4_do_handle_exception(struct nfs_server *server,
451 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500452{
453 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500454 struct nfs4_state *state = exception->state;
Trond Myklebust50c80002019-07-11 19:02:18 -0400455 const nfs4_stateid *stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500456 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500457 int ret = errorcode;
458
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400459 exception->delay = 0;
460 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500461 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400462
Trond Myklebust50c80002019-07-11 19:02:18 -0400463 stateid = nfs4_recoverable_stateid(exception->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400464 if (stateid == NULL && state != NULL)
Trond Myklebust50c80002019-07-11 19:02:18 -0400465 stateid = nfs4_recoverable_stateid(&state->stateid);
Trond Myklebust272289a2016-09-22 13:39:15 -0400466
Trond Myklebust65de8722008-12-23 15:21:44 -0500467 switch(errorcode) {
468 case 0:
469 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400470 case -NFS4ERR_BADHANDLE:
471 case -ESTALE:
472 if (inode != NULL && S_ISREG(inode->i_mode))
473 pnfs_destroy_layout(NFS_I(inode));
474 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400475 case -NFS4ERR_DELEG_REVOKED:
476 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400477 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400478 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400479 if (inode != NULL && stateid != NULL) {
480 nfs_inode_find_state_and_recover(inode,
481 stateid);
482 goto wait_on_recovery;
483 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500484 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400485 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400486 if (inode) {
487 int err;
488
489 err = nfs_async_inode_return_delegation(inode,
490 stateid);
491 if (err == 0)
492 goto wait_on_recovery;
493 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
494 exception->retry = 1;
495 break;
496 }
497 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500498 if (state == NULL)
499 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400500 ret = nfs4_schedule_stateid_recovery(server, state);
501 if (ret < 0)
502 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500503 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500504 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500505 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500506 nfs4_schedule_lease_recovery(clp);
507 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400508 case -NFS4ERR_MOVED:
509 ret = nfs4_schedule_migration_recovery(server);
510 if (ret < 0)
511 break;
512 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400513 case -NFS4ERR_LEASE_MOVED:
514 nfs4_schedule_lease_moved_recovery(clp);
515 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500516#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400517 case -NFS4ERR_BADSESSION:
518 case -NFS4ERR_BADSLOT:
519 case -NFS4ERR_BAD_HIGH_SLOT:
520 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
521 case -NFS4ERR_DEADSESSION:
522 case -NFS4ERR_SEQ_FALSE_RETRY:
523 case -NFS4ERR_SEQ_MISORDERED:
524 dprintk("%s ERROR: %d Reset session\n", __func__,
525 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400526 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400527 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500528#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500529 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500530 if (exception->timeout > HZ) {
531 /* We have retried a decent amount, time to
532 * fail
533 */
534 ret = -EBUSY;
535 break;
536 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500537 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500538 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400539 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500540 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400541 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400542 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400543 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400544 exception->delay = 1;
545 return 0;
546
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400547 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500548 case -NFS4ERR_OLD_STATEID:
549 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800550 break;
551 case -NFS4ERR_BADOWNER:
552 /* The following works around a Linux server bug! */
553 case -NFS4ERR_BADNAME:
554 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
555 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
556 exception->retry = 1;
557 printk(KERN_WARNING "NFS: v4 server %s "
558 "does not accept raw "
559 "uid/gids. "
560 "Reenabling the idmapper.\n",
561 server->nfs_client->cl_hostname);
562 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500563 }
564 /* We failed to handle the error */
565 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500566wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400567 exception->recovering = 1;
568 return 0;
569}
570
571/* This is the error handling routine for processes that are allowed
572 * to sleep.
573 */
574int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
575{
576 struct nfs_client *clp = server->nfs_client;
577 int ret;
578
579 ret = nfs4_do_handle_exception(server, errorcode, exception);
580 if (exception->delay) {
Trond Myklebust0688e642019-04-07 13:59:09 -0400581 ret = nfs4_delay(&exception->timeout,
582 exception->interruptible);
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400583 goto out_retry;
584 }
585 if (exception->recovering) {
586 ret = nfs4_wait_clnt_recover(clp);
587 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
588 return -EIO;
589 goto out_retry;
590 }
591 return ret;
592out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500593 if (ret == 0)
594 exception->retry = 1;
595 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500596}
597
Trond Myklebust037fc982015-09-20 15:51:00 -0400598static int
599nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
600 int errorcode, struct nfs4_exception *exception)
601{
602 struct nfs_client *clp = server->nfs_client;
603 int ret;
604
605 ret = nfs4_do_handle_exception(server, errorcode, exception);
606 if (exception->delay) {
607 rpc_delay(task, nfs4_update_delay(&exception->timeout));
608 goto out_retry;
609 }
610 if (exception->recovering) {
611 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
612 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
613 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
614 goto out_retry;
615 }
616 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
617 ret = -EIO;
618 return ret;
619out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500620 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400621 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500622 /*
623 * For NFS4ERR_MOVED, the client transport will need to
624 * be recomputed after migration recovery has completed.
625 */
626 if (errorcode == -NFS4ERR_MOVED)
627 rpc_task_release_transport(task);
628 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400629 return ret;
630}
631
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400632int
Trond Myklebust037fc982015-09-20 15:51:00 -0400633nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
634 struct nfs4_state *state, long *timeout)
635{
636 struct nfs4_exception exception = {
637 .state = state,
638 };
639
640 if (task->tk_status >= 0)
641 return 0;
642 if (timeout)
643 exception.timeout = *timeout;
644 task->tk_status = nfs4_async_handle_exception(task, server,
645 task->tk_status,
646 &exception);
647 if (exception.delay && timeout)
648 *timeout = exception.timeout;
649 if (exception.retry)
650 return -EAGAIN;
651 return 0;
652}
653
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400654/*
655 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
656 * or 'false' otherwise.
657 */
658static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
659{
660 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500661 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400662}
Trond Myklebust65de8722008-12-23 15:21:44 -0500663
Trond Myklebust452e9352010-07-31 14:29:06 -0400664static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 spin_lock(&clp->cl_lock);
667 if (time_before(clp->cl_last_renewal,timestamp))
668 clp->cl_last_renewal = timestamp;
669 spin_unlock(&clp->cl_lock);
670}
671
Trond Myklebust452e9352010-07-31 14:29:06 -0400672static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
673{
Trond Myklebustbe824162015-07-05 14:50:46 -0400674 struct nfs_client *clp = server->nfs_client;
675
676 if (!nfs4_has_session(clp))
677 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400678}
679
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400680struct nfs4_call_sync_data {
681 const struct nfs_server *seq_server;
682 struct nfs4_sequence_args *seq_args;
683 struct nfs4_sequence_res *seq_res;
684};
685
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800686void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400687 struct nfs4_sequence_res *res, int cache_reply,
688 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400689{
690 args->sa_slot = NULL;
691 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400692 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400693
694 res->sr_slot = NULL;
695}
696
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400697static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400698{
699 struct nfs4_slot *slot = res->sr_slot;
700 struct nfs4_slot_table *tbl;
701
Chuck Lever3bd23842013-08-09 12:49:19 -0400702 tbl = slot->table;
703 spin_lock(&tbl->slot_tbl_lock);
704 if (!nfs41_wake_and_assign_slot(tbl, slot))
705 nfs4_free_slot(tbl, slot);
706 spin_unlock(&tbl->slot_tbl_lock);
707
708 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400709}
710
711static int nfs40_sequence_done(struct rpc_task *task,
712 struct nfs4_sequence_res *res)
713{
714 if (res->sr_slot != NULL)
715 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400716 return 1;
717}
718
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400719#if defined(CONFIG_NFS_V4_1)
720
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400721static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400722{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400724 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500725 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727 if (!slot)
728 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500729 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500730 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500731
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400732 /* Bump the slot sequence number */
733 if (slot->seq_done)
734 slot->seq_nr++;
735 slot->seq_done = 0;
736
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500737 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500738 /* Be nice to the server: try to ensure that the last transmitted
739 * value for highest_user_slotid <= target_highest_slotid
740 */
741 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
742 send_new_highest_used_slotid = true;
743
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500744 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500745 send_new_highest_used_slotid = false;
746 goto out_unlock;
747 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500748 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500749
750 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
751 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500752out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500753 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500754 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400755 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400756 if (waitqueue_active(&tbl->slot_waitq))
757 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400758}
759
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400760static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
761{
762 nfs41_release_slot(res->sr_slot);
763 res->sr_slot = NULL;
764}
765
Trond Myklebust3453d572018-06-20 17:53:34 -0400766static void nfs4_slot_sequence_record_sent(struct nfs4_slot *slot,
767 u32 seqnr)
768{
769 if ((s32)(seqnr - slot->seq_nr_highest_sent) > 0)
770 slot->seq_nr_highest_sent = seqnr;
771}
772static void nfs4_slot_sequence_acked(struct nfs4_slot *slot,
773 u32 seqnr)
774{
775 slot->seq_nr_highest_sent = seqnr;
776 slot->seq_nr_last_acked = seqnr;
777}
778
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400779static int nfs41_sequence_process(struct rpc_task *task,
780 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400781{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500782 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500783 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400784 struct nfs_client *clp;
Trond Myklebust85563072012-12-11 10:31:12 -0500785 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400786
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500787 if (slot == NULL)
788 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400789 /* don't increment the sequence number if the task wasn't sent */
Trond Myklebustc71c46f2019-03-01 11:40:05 -0500790 if (!RPC_WAS_SENT(task) || slot->seq_done)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400791 goto out;
792
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500793 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500794
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400795 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500796 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400797 switch (res->sr_status) {
798 case 0:
Trond Myklebust3453d572018-06-20 17:53:34 -0400799 /* Mark this sequence number as having been acked */
800 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400801 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400802 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500803 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500804 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500805 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400806 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
807 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500808 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400809 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500810 case 1:
811 /*
812 * sr_status remains 1 if an RPC level error occurred.
813 * The server may or may not have processed the sequence
814 * operation..
Trond Myklebustac20d162012-12-15 15:36:07 -0500815 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400816 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
817 slot->seq_done = 1;
Trond Myklebustac20d162012-12-15 15:36:07 -0500818 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 case -NFS4ERR_DELAY:
820 /* The server detected a resend of the RPC call and
821 * returned NFS4ERR_DELAY as per Section 2.10.6.2
822 * of RFC5661.
823 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500824 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400825 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500826 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500827 slot->seq_nr);
Trond Myklebust3453d572018-06-20 17:53:34 -0400828 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400829 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400830 case -NFS4ERR_RETRY_UNCACHED_REP:
831 case -NFS4ERR_SEQ_FALSE_RETRY:
832 /*
833 * The server thinks we tried to replay a request.
834 * Retry the call after bumping the sequence ID.
835 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400836 nfs4_slot_sequence_acked(slot, slot->seq_nr);
Trond Myklebustf9312a52018-06-09 19:10:31 -0400837 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500838 case -NFS4ERR_BADSLOT:
839 /*
840 * The slot id we used was probably retired. Try again
841 * using a different slot id.
842 */
Trond Myklebust99589102018-06-09 12:50:50 -0400843 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400844 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500845 goto retry_nowait;
846 case -NFS4ERR_SEQ_MISORDERED:
Trond Myklebust3453d572018-06-20 17:53:34 -0400847 nfs4_slot_sequence_record_sent(slot, slot->seq_nr);
Trond Myklebuste8794442012-12-15 13:56:18 -0500848 /*
Trond Myklebust3453d572018-06-20 17:53:34 -0400849 * Were one or more calls using this slot interrupted?
850 * If the server never received the request, then our
851 * transmitted slot sequence number may be too high.
Trond Myklebustac20d162012-12-15 15:36:07 -0500852 */
Trond Myklebust3453d572018-06-20 17:53:34 -0400853 if ((s32)(slot->seq_nr - slot->seq_nr_last_acked) > 1) {
854 slot->seq_nr--;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500855 goto retry_nowait;
856 }
Trond Myklebust3453d572018-06-20 17:53:34 -0400857 /*
858 * RFC5661:
859 * A retry might be sent while the original request is
860 * still in progress on the replier. The replier SHOULD
861 * deal with the issue by returning NFS4ERR_DELAY as the
862 * reply to SEQUENCE or CB_SEQUENCE operation, but
863 * implementations MAY return NFS4ERR_SEQ_MISORDERED.
864 *
865 * Restart the search after a delay.
866 */
867 slot->seq_nr = slot->seq_nr_highest_sent;
868 goto out_retry;
Trond Myklebust14516c32010-07-31 14:29:06 -0400869 default:
870 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400871 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400872 }
873out:
874 /* The session may be reset by one of the error handlers. */
875 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500876out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500877 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400878session_recover:
879 nfs4_schedule_session_recovery(session, res->sr_status);
880 goto retry_nowait;
881retry_new_seq:
882 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500883retry_nowait:
884 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400885 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500886 task->tk_status = 0;
887 ret = 0;
888 }
889 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400890out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400891 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400892 goto out;
893 rpc_delay(task, NFS4_POLL_RETRY_MAX);
894 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400895}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400896
897int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
898{
899 if (!nfs41_sequence_process(task, res))
900 return 0;
901 if (res->sr_slot != NULL)
902 nfs41_sequence_free_slot(res);
903 return 1;
904
905}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500906EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400907
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400908static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
909{
910 if (res->sr_slot == NULL)
911 return 1;
912 if (res->sr_slot->table->session != NULL)
913 return nfs41_sequence_process(task, res);
914 return nfs40_sequence_done(task, res);
915}
916
917static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
918{
919 if (res->sr_slot != NULL) {
920 if (res->sr_slot->table->session != NULL)
921 nfs41_sequence_free_slot(res);
922 else
923 nfs40_sequence_free_slot(res);
924 }
925}
926
Peng Tao2c4b1312014-06-11 05:24:16 +0800927int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400928{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500929 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400930 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400931 if (!res->sr_slot->table->session)
932 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400933 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400934}
Peng Tao2c4b1312014-06-11 05:24:16 +0800935EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400936
Andy Adamsonce5039c2009-04-01 09:22:13 -0400937static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
938{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400939 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400940
Trond Myklebust035168ab2010-06-16 09:52:26 -0400941 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
942
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500943 nfs4_setup_sequence(data->seq_server->nfs_client,
944 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400945}
946
Andy Adamson69ab40c2009-04-01 09:22:19 -0400947static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
948{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400949 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400950
Trond Myklebust14516c32010-07-31 14:29:06 -0400951 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400952}
953
Trond Myklebust17280172012-03-11 13:11:00 -0400954static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400955 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400956 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400957};
958
Chuck Lever3bd23842013-08-09 12:49:19 -0400959#else /* !CONFIG_NFS_V4_1 */
960
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400961static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
962{
963 return nfs40_sequence_done(task, res);
964}
965
966static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
967{
968 if (res->sr_slot != NULL)
969 nfs40_sequence_free_slot(res);
970}
971
Peng Tao2c4b1312014-06-11 05:24:16 +0800972int nfs4_sequence_done(struct rpc_task *task,
973 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400974{
Chuck Lever3bd23842013-08-09 12:49:19 -0400975 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400976}
Peng Tao2c4b1312014-06-11 05:24:16 +0800977EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400978
979#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400980
Trond Myklebustc1dffe02019-03-01 12:13:34 -0500981static void nfs41_sequence_res_init(struct nfs4_sequence_res *res)
982{
983 res->sr_timestamp = jiffies;
984 res->sr_status_flags = 0;
985 res->sr_status = 1;
986}
987
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400988static
989void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
990 struct nfs4_sequence_res *res,
991 struct nfs4_slot *slot)
992{
993 if (!slot)
994 return;
995 slot->privileged = args->sa_privileged ? 1 : 0;
996 args->sa_slot = slot;
997
998 res->sr_slot = slot;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400999}
1000
1001int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001002 struct nfs4_sequence_args *args,
1003 struct nfs4_sequence_res *res,
1004 struct rpc_task *task)
1005{
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001006 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -05001007 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -05001008 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001009
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001010 /* slot already allocated? */
1011 if (res->sr_slot != NULL)
1012 goto out_start;
1013
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001014 if (session)
Anna Schumaker76ee0352017-01-10 16:49:31 -05001015 tbl = &session->fc_slot_table;
Anna Schumaker76ee0352017-01-10 16:49:31 -05001016
Trond Myklebust3453d572018-06-20 17:53:34 -04001017 spin_lock(&tbl->slot_tbl_lock);
1018 /* The state manager will wait until the slot table is empty */
1019 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
1020 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -05001021
Trond Myklebust3453d572018-06-20 17:53:34 -04001022 slot = nfs4_alloc_slot(tbl);
1023 if (IS_ERR(slot)) {
Trond Myklebust3453d572018-06-20 17:53:34 -04001024 if (slot == ERR_PTR(-ENOMEM))
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001025 goto out_sleep_timeout;
Trond Myklebust3453d572018-06-20 17:53:34 -04001026 goto out_sleep;
Anna Schumaker3d358082017-01-11 10:54:04 -05001027 }
Trond Myklebust3453d572018-06-20 17:53:34 -04001028 spin_unlock(&tbl->slot_tbl_lock);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001029
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001030 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001031
Anna Schumakerad05cc02017-01-11 13:37:06 -05001032 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001033out_start:
Trond Myklebustc1dffe02019-03-01 12:13:34 -05001034 nfs41_sequence_res_init(res);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001035 rpc_call_start(task);
1036 return 0;
Trond Myklebust6b2e6852019-04-07 13:58:49 -04001037out_sleep_timeout:
1038 /* Try again in 1/4 second */
1039 if (args->sa_privileged)
1040 rpc_sleep_on_priority_timeout(&tbl->slot_tbl_waitq, task,
1041 jiffies + (HZ >> 2), RPC_PRIORITY_PRIVILEGED);
1042 else
1043 rpc_sleep_on_timeout(&tbl->slot_tbl_waitq, task,
1044 NULL, jiffies + (HZ >> 2));
1045 spin_unlock(&tbl->slot_tbl_lock);
1046 return -EAGAIN;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001047out_sleep:
1048 if (args->sa_privileged)
1049 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
Trond Myklebust8357a9b2019-04-07 13:58:48 -04001050 RPC_PRIORITY_PRIVILEGED);
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001051 else
1052 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1053 spin_unlock(&tbl->slot_tbl_lock);
1054 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001055}
1056EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1057
Chuck Lever9915ea72013-08-09 12:48:27 -04001058static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1059{
1060 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001061 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001062 data->seq_args, data->seq_res, task);
1063}
1064
1065static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1066{
1067 struct nfs4_call_sync_data *data = calldata;
1068 nfs4_sequence_done(task, data->seq_res);
1069}
1070
1071static const struct rpc_call_ops nfs40_call_sync_ops = {
1072 .rpc_call_prepare = nfs40_call_sync_prepare,
1073 .rpc_call_done = nfs40_call_sync_done,
1074};
1075
Anna Schumaker48c05852019-08-14 15:27:00 -04001076static int nfs4_call_sync_custom(struct rpc_task_setup *task_setup)
1077{
1078 int ret;
1079 struct rpc_task *task;
1080
1081 task = rpc_run_task(task_setup);
1082 if (IS_ERR(task))
1083 return PTR_ERR(task);
1084
1085 ret = task->tk_status;
1086 rpc_put_task(task);
1087 return ret;
1088}
1089
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001090static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001091 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001092 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001093 struct nfs4_sequence_args *args,
1094 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001095{
Chuck Lever9915ea72013-08-09 12:48:27 -04001096 struct nfs_client *clp = server->nfs_client;
1097 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001098 .seq_server = server,
1099 .seq_args = args,
1100 .seq_res = res,
1101 };
1102 struct rpc_task_setup task_setup = {
1103 .rpc_client = clnt,
1104 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001105 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001106 .callback_data = &data
1107 };
1108
Anna Schumaker48c05852019-08-14 15:27:00 -04001109 return nfs4_call_sync_custom(&task_setup);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001110}
1111
Bryan Schumaker7c513052011-03-24 17:12:24 +00001112int nfs4_call_sync(struct rpc_clnt *clnt,
1113 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001114 struct rpc_message *msg,
1115 struct nfs4_sequence_args *args,
1116 struct nfs4_sequence_res *res,
1117 int cache_reply)
1118{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001119 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001120 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001121}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001122
Trond Myklebust3c591172018-07-31 15:54:10 -04001123static void
1124nfs4_inc_nlink_locked(struct inode *inode)
1125{
1126 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1127 inc_nlink(inode);
1128}
1129
1130static void
1131nfs4_dec_nlink_locked(struct inode *inode)
1132{
1133 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1134 drop_nlink(inode);
1135}
1136
1137static void
1138update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001139 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140{
1141 struct nfs_inode *nfsi = NFS_I(dir);
1142
Trond Myklebust16e14372018-03-20 16:53:31 -04001143 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1144 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001145 | NFS_INO_INVALID_DATA
1146 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001147 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001148 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1149 nfsi->attrtimeo_timestamp = jiffies;
1150 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001152 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001153 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1154 NFS_INO_INVALID_ACL;
1155 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001156 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001157 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001158 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001159 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001160 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001161}
1162
1163static void
1164update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001165 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001166{
1167 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001168 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 spin_unlock(&dir->i_lock);
1170}
1171
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001172struct nfs4_open_createattrs {
1173 struct nfs4_label *label;
1174 struct iattr *sattr;
1175 const __u32 verf[2];
1176};
1177
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001178static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1179 int err, struct nfs4_exception *exception)
1180{
1181 if (err != -EINVAL)
1182 return false;
1183 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1184 return false;
1185 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1186 exception->retry = 1;
1187 return true;
1188}
1189
Trond Myklebust1bf85d82019-06-27 06:30:48 -04001190static fmode_t _nfs4_ctx_to_accessmode(const struct nfs_open_context *ctx)
1191{
1192 return ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
1193}
1194
1195static fmode_t _nfs4_ctx_to_openmode(const struct nfs_open_context *ctx)
1196{
1197 fmode_t ret = ctx->mode & (FMODE_READ|FMODE_WRITE);
1198
1199 return (ctx->mode & FMODE_EXEC) ? FMODE_READ | ret : ret;
1200}
1201
Trond Myklebust6ae37332015-01-30 14:21:14 -05001202static u32
1203nfs4_map_atomic_open_share(struct nfs_server *server,
1204 fmode_t fmode, int openflags)
1205{
1206 u32 res = 0;
1207
1208 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1209 case FMODE_READ:
1210 res = NFS4_SHARE_ACCESS_READ;
1211 break;
1212 case FMODE_WRITE:
1213 res = NFS4_SHARE_ACCESS_WRITE;
1214 break;
1215 case FMODE_READ|FMODE_WRITE:
1216 res = NFS4_SHARE_ACCESS_BOTH;
1217 }
1218 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1219 goto out;
1220 /* Want no delegation if we're using O_DIRECT */
1221 if (openflags & O_DIRECT)
1222 res |= NFS4_SHARE_WANT_NO_DELEG;
1223out:
1224 return res;
1225}
1226
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001227static enum open_claim_type4
1228nfs4_map_atomic_open_claim(struct nfs_server *server,
1229 enum open_claim_type4 claim)
1230{
1231 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1232 return claim;
1233 switch (claim) {
1234 default:
1235 return claim;
1236 case NFS4_OPEN_CLAIM_FH:
1237 return NFS4_OPEN_CLAIM_NULL;
1238 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1239 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1240 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1241 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1242 }
1243}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244
1245static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246{
1247 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001248 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001249 p->o_res.seqid = p->o_arg.seqid;
1250 p->c_res.seqid = p->c_arg.seqid;
1251 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001252 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001253 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001254 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001255}
1256
Al Viro82a2c1b2011-06-22 18:30:55 -04001257static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001258 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001259 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001260 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001261 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001262{
Al Viro82a2c1b2011-06-22 18:30:55 -04001263 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001264 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001265 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001266 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001267 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001268 struct nfs4_opendata *p;
1269
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001270 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001271 if (p == NULL)
1272 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001273
1274 p->f_label = nfs4_label_alloc(server, gfp_mask);
1275 if (IS_ERR(p->f_label))
1276 goto err_free_p;
1277
Kinglong Meea49c2692015-07-27 15:31:38 +08001278 p->a_label = nfs4_label_alloc(server, gfp_mask);
1279 if (IS_ERR(p->a_label))
1280 goto err_free_f;
1281
Trond Myklebust63f5f792015-01-23 19:19:25 -05001282 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1283 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001284 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001285 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001286 nfs_sb_active(dentry->d_sb);
1287 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 p->dir = parent;
1289 p->owner = sp;
1290 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001291 p->o_arg.open_flags = flags;
1292 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Trond Myklebust536585c2016-11-10 15:40:34 -05001293 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001294 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1295 fmode, flags);
Benjamin Coddington90910512019-06-07 06:37:30 -04001296 if (flags & O_CREAT) {
1297 p->o_arg.umask = current_umask();
1298 p->o_arg.label = nfs4_label_copy(p->a_label, label);
1299 if (c->sattr != NULL && c->sattr->ia_valid != 0) {
1300 p->o_arg.u.attrs = &p->attrs;
1301 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
1302
1303 memcpy(p->o_arg.u.verifier.data, c->verf,
1304 sizeof(p->o_arg.u.verifier.data));
1305 }
1306 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001307 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1308 * will return permission denied for all bits until close */
1309 if (!(flags & O_EXCL)) {
1310 /* ask server to check for all possible rights as results
1311 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001312 switch (p->o_arg.claim) {
1313 default:
1314 break;
1315 case NFS4_OPEN_CLAIM_NULL:
1316 case NFS4_OPEN_CLAIM_FH:
1317 p->o_arg.access = NFS4_ACCESS_READ |
1318 NFS4_ACCESS_MODIFY |
1319 NFS4_ACCESS_EXTEND |
1320 NFS4_ACCESS_EXECUTE;
1321 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001322 }
David Howells7539bba2006-08-22 20:06:09 -04001323 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001324 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1325 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001326 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001327 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001328 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001329 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001330 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001331 case NFS4_OPEN_CLAIM_NULL:
1332 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1333 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1334 p->o_arg.fh = NFS_FH(dir);
1335 break;
1336 case NFS4_OPEN_CLAIM_PREVIOUS:
1337 case NFS4_OPEN_CLAIM_FH:
1338 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1339 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001340 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001341 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001342 p->c_arg.fh = &p->o_res.fh;
1343 p->c_arg.stateid = &p->o_res.stateid;
1344 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001345 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001346 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001347 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001348
1349err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001350 nfs4_label_free(p->a_label);
1351err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001352 nfs4_label_free(p->f_label);
1353err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001354 kfree(p);
1355err:
1356 dput(parent);
1357 return NULL;
1358}
1359
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001360static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001361{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001362 struct nfs4_opendata *p = container_of(kref,
1363 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001364 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001365
Fred Isaman30ae2412016-10-18 13:39:51 -04001366 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001367 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001368 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001369 if (p->state != NULL)
1370 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001371 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001372
Kinglong Meea49c2692015-07-27 15:31:38 +08001373 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001374 nfs4_label_free(p->f_label);
1375
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001376 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001377 dput(p->dentry);
1378 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001379 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001380 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001381 kfree(p);
1382}
1383
1384static void nfs4_opendata_put(struct nfs4_opendata *p)
1385{
1386 if (p != NULL)
1387 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001388}
1389
Trond Myklebust24311f82015-09-20 10:50:17 -04001390static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1391 fmode_t fmode)
1392{
1393 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1394 case FMODE_READ|FMODE_WRITE:
1395 return state->n_rdwr != 0;
1396 case FMODE_WRITE:
1397 return state->n_wronly != 0;
1398 case FMODE_READ:
1399 return state->n_rdonly != 0;
1400 }
1401 WARN_ON_ONCE(1);
1402 return false;
1403}
1404
Trond Myklebustbe189f72018-09-27 17:12:33 -04001405static int can_open_cached(struct nfs4_state *state, fmode_t mode,
1406 int open_mode, enum open_claim_type4 claim)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001407{
1408 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001409
Trond Myklebust536e43d2012-01-17 22:04:26 -05001410 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001411 goto out;
Trond Myklebustbe189f72018-09-27 17:12:33 -04001412 switch (claim) {
1413 case NFS4_OPEN_CLAIM_NULL:
1414 case NFS4_OPEN_CLAIM_FH:
1415 goto out;
1416 default:
1417 break;
1418 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001419 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001420 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001421 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1422 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001423 break;
1424 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001425 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1426 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001427 break;
1428 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001429 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1430 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001431 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001432out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001433 return ret;
1434}
1435
Trond Myklebust2a606182015-08-19 22:30:00 -05001436static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1437 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001438{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001439 if (delegation == NULL)
1440 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001441 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001442 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001443 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1444 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001445 switch (claim) {
1446 case NFS4_OPEN_CLAIM_NULL:
1447 case NFS4_OPEN_CLAIM_FH:
1448 break;
1449 case NFS4_OPEN_CLAIM_PREVIOUS:
1450 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1451 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001452 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001453 default:
1454 return 0;
1455 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001456 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001457 return 1;
1458}
1459
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001460static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001461{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001462 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001463 case FMODE_WRITE:
1464 state->n_wronly++;
1465 break;
1466 case FMODE_READ:
1467 state->n_rdonly++;
1468 break;
1469 case FMODE_READ|FMODE_WRITE:
1470 state->n_rdwr++;
1471 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001472 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001473}
1474
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001475#ifdef CONFIG_NFS_V4_1
1476static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1477{
1478 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1479 return true;
1480 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1481 return true;
1482 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1483 return true;
1484 return false;
1485}
1486#endif /* CONFIG_NFS_V4_1 */
1487
Trond Myklebustc9399f22017-11-06 15:28:01 -05001488static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1489{
1490 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1491 wake_up_all(&state->waitq);
1492}
1493
1494static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1495 const nfs4_stateid *stateid)
1496{
1497 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1498 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1499
1500 if (stateid_seqid == state_seqid + 1U ||
1501 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1502 nfs_state_log_update_open_stateid(state);
1503 else
1504 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1505}
1506
Trond Myklebust4f14c192014-02-12 19:15:06 -05001507static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001508{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001509 struct nfs_client *clp = state->owner->so_server->nfs_client;
1510 bool need_recover = false;
1511
1512 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1513 need_recover = true;
1514 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1515 need_recover = true;
1516 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1517 need_recover = true;
1518 if (need_recover)
1519 nfs4_state_mark_reclaim_nograce(clp, state);
1520}
1521
Trond Myklebustc9399f22017-11-06 15:28:01 -05001522/*
1523 * Check for whether or not the caller may update the open stateid
1524 * to the value passed in by stateid.
1525 *
1526 * Note: This function relies heavily on the server implementing
1527 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1528 * correctly.
1529 * i.e. The stateid seqids have to be initialised to 1, and
1530 * are then incremented on every state transition.
1531 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001532static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001533 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001534{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001535 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1536 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1537 if (stateid->seqid == cpu_to_be32(1))
1538 nfs_state_log_update_open_stateid(state);
1539 else
1540 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001541 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001542 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543
1544 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1545 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001546 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001547 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001548 return false;
1549}
1550
Trond Myklebustf95549c2015-01-23 18:06:09 -05001551static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1552{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001553 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1554 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001555 if (state->n_wronly)
1556 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1557 if (state->n_rdonly)
1558 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1559 if (state->n_rdwr)
1560 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001561 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001562}
1563
Trond Myklebust226056c2014-02-11 10:41:07 -05001564static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1565 nfs4_stateid *stateid, fmode_t fmode)
1566{
1567 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1568 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1569 case FMODE_WRITE:
1570 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_READ:
1573 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1574 break;
1575 case 0:
1576 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1577 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1578 clear_bit(NFS_OPEN_STATE, &state->flags);
1579 }
1580 if (stateid == NULL)
1581 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001582 /* Handle OPEN+OPEN_DOWNGRADE races */
1583 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1584 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001585 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001586 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001587 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001588 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001589 nfs4_stateid_copy(&state->stateid, stateid);
1590 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001591 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001592out:
1593 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001594}
1595
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001596static void nfs_clear_open_stateid(struct nfs4_state *state,
1597 nfs4_stateid *arg_stateid,
1598 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001599{
1600 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001601 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1602 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1603 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001604 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001605 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1606 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001607}
1608
Trond Myklebust1393d962016-09-22 13:39:13 -04001609static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001610 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebuste9acf212019-01-22 14:01:16 -05001611 __must_hold(&state->owner->so_lock)
1612 __must_hold(&state->seqlock)
1613 __must_hold(RCU)
1614
Trond Myklebust003707c2007-07-05 18:07:55 -04001615{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001616 DEFINE_WAIT(wait);
1617 int status = 0;
1618 for (;;) {
1619
1620 if (!nfs_need_update_open_stateid(state, stateid))
1621 return;
1622 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001623 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001624 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001625 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001626 /* Rely on seqids for serialisation with NFSv4.0 */
1627 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1628 break;
1629
1630 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1631 /*
1632 * Ensure we process the state changes in the same order
1633 * in which the server processed them by delaying the
1634 * update of the stateid until we are in sequence.
1635 */
1636 write_sequnlock(&state->seqlock);
1637 spin_unlock(&state->owner->so_lock);
1638 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001639 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001640 if (!signal_pending(current)) {
1641 if (schedule_timeout(5*HZ) == 0)
1642 status = -EAGAIN;
1643 else
1644 status = 0;
1645 } else
1646 status = -EINTR;
1647 finish_wait(&state->waitq, &wait);
1648 rcu_read_lock();
1649 spin_lock(&state->owner->so_lock);
1650 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001651 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001652
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001653 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1654 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001655 nfs4_stateid_copy(freeme, &state->open_stateid);
1656 nfs_test_and_clear_all_open_stateid(state);
1657 }
1658
Trond Myklebuste999e802014-02-10 18:20:47 -05001659 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1660 nfs4_stateid_copy(&state->stateid, stateid);
1661 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001662 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001663 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001664}
1665
Trond Myklebustc9399f22017-11-06 15:28:01 -05001666static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001667 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001668 fmode_t fmode,
1669 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001671 /*
1672 * Protect the call to nfs4_state_set_mode_locked and
1673 * serialise the stateid update
1674 */
1675 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001676 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1677 switch (fmode) {
1678 case FMODE_READ:
1679 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1680 break;
1681 case FMODE_WRITE:
1682 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1683 break;
1684 case FMODE_READ|FMODE_WRITE:
1685 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001686 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001687 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001688 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001689}
1690
Trond Myklebust27a30cf2019-07-22 18:32:59 +01001691static void nfs_state_clear_open_state_flags(struct nfs4_state *state)
1692{
1693 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1694 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1695 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1696 clear_bit(NFS_OPEN_STATE, &state->flags);
1697}
1698
Trond Myklebustc9399f22017-11-06 15:28:01 -05001699static void nfs_state_set_delegation(struct nfs4_state *state,
1700 const nfs4_stateid *deleg_stateid,
1701 fmode_t fmode)
1702{
1703 /*
1704 * Protect the call to nfs4_state_set_mode_locked and
1705 * serialise the stateid update
1706 */
1707 write_seqlock(&state->seqlock);
1708 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1709 set_bit(NFS_DELEGATED_STATE, &state->flags);
1710 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001711}
1712
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001713static void nfs_state_clear_delegation(struct nfs4_state *state)
1714{
1715 write_seqlock(&state->seqlock);
1716 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1717 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1718 write_sequnlock(&state->seqlock);
1719}
1720
Trond Myklebust1393d962016-09-22 13:39:13 -04001721static int update_open_stateid(struct nfs4_state *state,
1722 const nfs4_stateid *open_stateid,
1723 const nfs4_stateid *delegation,
1724 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001725{
Trond Myklebust1393d962016-09-22 13:39:13 -04001726 struct nfs_server *server = NFS_SERVER(state->inode);
1727 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001728 struct nfs_inode *nfsi = NFS_I(state->inode);
1729 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001730 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001731 int ret = 0;
1732
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001733 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001734
1735 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001736 spin_lock(&state->owner->so_lock);
1737 if (open_stateid != NULL) {
1738 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1739 ret = 1;
1740 }
1741
Trond Myklebust34310432008-12-23 15:21:38 -05001742 deleg_cur = rcu_dereference(nfsi->delegation);
1743 if (deleg_cur == NULL)
1744 goto no_delegation;
1745
1746 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001747 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001748 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001749 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001750 goto no_delegation_unlock;
1751
1752 if (delegation == NULL)
1753 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001754 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001755 goto no_delegation_unlock;
1756
Trond Myklebustb7391f42008-12-23 15:21:52 -05001757 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001758 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001759 ret = 1;
1760no_delegation_unlock:
1761 spin_unlock(&deleg_cur->lock);
1762no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001763 if (ret)
1764 update_open_stateflags(state, fmode);
1765 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001766 rcu_read_unlock();
1767
Trond Myklebust4f14c192014-02-12 19:15:06 -05001768 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001769 nfs4_schedule_state_manager(clp);
1770 if (freeme.type != 0)
1771 nfs4_test_and_free_stateid(server, &freeme,
1772 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001773
1774 return ret;
1775}
1776
Trond Myklebust39071e62015-01-24 15:07:56 -05001777static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1778 const nfs4_stateid *stateid)
1779{
1780 struct nfs4_state *state = lsp->ls_state;
1781 bool ret = false;
1782
1783 spin_lock(&state->state_lock);
1784 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1785 goto out_noupdate;
1786 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1787 goto out_noupdate;
1788 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1789 ret = true;
1790out_noupdate:
1791 spin_unlock(&state->state_lock);
1792 return ret;
1793}
Trond Myklebust34310432008-12-23 15:21:38 -05001794
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001795static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001796{
1797 struct nfs_delegation *delegation;
1798
Trond Myklebustf5086242018-03-20 16:43:13 -04001799 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001800 rcu_read_lock();
1801 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001802 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001803 rcu_read_unlock();
1804 return;
1805 }
1806 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001807 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001808}
1809
Trond Myklebust6ee41262007-07-08 14:11:36 -04001810static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001811{
1812 struct nfs4_state *state = opendata->state;
1813 struct nfs_inode *nfsi = NFS_I(state->inode);
1814 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001815 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001816 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001817 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001818 nfs4_stateid stateid;
1819 int ret = -EAGAIN;
1820
Trond Myklebustaac00a82007-07-05 19:02:21 -04001821 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001822 spin_lock(&state->owner->so_lock);
Trond Myklebustbe189f72018-09-27 17:12:33 -04001823 if (can_open_cached(state, fmode, open_mode, claim)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001824 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001825 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001826 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001827 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001828 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001829 rcu_read_lock();
1830 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001831 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001832 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001833 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001834 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001835 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001836 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001837 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001838 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001839 if (!opendata->is_recover) {
1840 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1841 if (ret != 0)
1842 goto out;
1843 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001844 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001845
1846 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001847 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001848 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001849 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001850out:
1851 return ERR_PTR(ret);
1852out_return_state:
Trond Myklebustace9fad2018-09-02 19:19:07 -04001853 refcount_inc(&state->count);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001854 return state;
1855}
1856
Andy Adamsone23008e2012-10-02 21:07:32 -04001857static void
1858nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1859{
1860 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1861 struct nfs_delegation *delegation;
1862 int delegation_flags = 0;
1863
1864 rcu_read_lock();
1865 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1866 if (delegation)
1867 delegation_flags = delegation->flags;
1868 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001869 switch (data->o_arg.claim) {
1870 default:
1871 break;
1872 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1873 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001874 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1875 "returning a delegation for "
1876 "OPEN(CLAIM_DELEGATE_CUR)\n",
1877 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001878 return;
1879 }
1880 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001881 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001882 data->owner->so_cred,
1883 data->o_res.delegation_type,
1884 &data->o_res.delegation,
1885 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001886 else
1887 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001888 data->owner->so_cred,
1889 data->o_res.delegation_type,
1890 &data->o_res.delegation,
1891 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001892
1893 if (data->o_res.do_recall)
1894 nfs_async_inode_return_delegation(state->inode,
1895 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001896}
1897
1898/*
1899 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1900 * and update the nfs4_state.
1901 */
1902static struct nfs4_state *
1903_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1904{
1905 struct inode *inode = data->state->inode;
1906 struct nfs4_state *state = data->state;
1907 int ret;
1908
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001909 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001910 if (data->rpc_status)
1911 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001912 /* cached opens have already been processed */
1913 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001914 }
1915
Andy Adamsone23008e2012-10-02 21:07:32 -04001916 ret = nfs_refresh_inode(inode, &data->f_attr);
1917 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001918 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001919
1920 if (data->o_res.delegation_type != 0)
1921 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001922update:
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001923 if (!update_open_stateid(state, &data->o_res.stateid,
1924 NULL, data->o_arg.fmode))
1925 return ERR_PTR(-EAGAIN);
Trond Myklebustace9fad2018-09-02 19:19:07 -04001926 refcount_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001927
1928 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001929}
1930
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001931static struct inode *
1932nfs4_opendata_get_inode(struct nfs4_opendata *data)
1933{
1934 struct inode *inode;
1935
1936 switch (data->o_arg.claim) {
1937 case NFS4_OPEN_CLAIM_NULL:
1938 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1939 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1940 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1941 return ERR_PTR(-EAGAIN);
1942 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1943 &data->f_attr, data->f_label);
1944 break;
1945 default:
1946 inode = d_inode(data->dentry);
1947 ihold(inode);
1948 nfs_refresh_inode(inode, &data->f_attr);
1949 }
1950 return inode;
1951}
1952
Andy Adamsone23008e2012-10-02 21:07:32 -04001953static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001954nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1955{
1956 struct nfs4_state *state;
1957 struct inode *inode;
1958
1959 inode = nfs4_opendata_get_inode(data);
1960 if (IS_ERR(inode))
1961 return ERR_CAST(inode);
1962 if (data->state != NULL && data->state->inode == inode) {
1963 state = data->state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04001964 refcount_inc(&state->count);
Trond Myklebust75e8c482017-08-08 10:38:07 -04001965 } else
1966 state = nfs4_get_open_state(inode, data->owner);
1967 iput(inode);
1968 if (state == NULL)
1969 state = ERR_PTR(-ENOMEM);
1970 return state;
1971}
1972
Andy Adamsone23008e2012-10-02 21:07:32 -04001973static struct nfs4_state *
1974_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001975{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001976 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001977
Trond Myklebustaac00a82007-07-05 19:02:21 -04001978 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001979 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001980 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001981 goto out;
1982 }
1983
Trond Myklebust75e8c482017-08-08 10:38:07 -04001984 state = nfs4_opendata_find_nfs4_state(data);
1985 if (IS_ERR(state))
1986 goto out;
1987
Andy Adamsone23008e2012-10-02 21:07:32 -04001988 if (data->o_res.delegation_type != 0)
1989 nfs4_opendata_check_deleg(data, state);
Trond Myklebuste3c8dc72019-07-29 18:25:00 +01001990 if (!update_open_stateid(state, &data->o_res.stateid,
1991 NULL, data->o_arg.fmode)) {
1992 nfs4_put_open_state(state);
1993 state = ERR_PTR(-EAGAIN);
1994 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001995out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001996 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001997 return state;
1998}
1999
Andy Adamsone23008e2012-10-02 21:07:32 -04002000static struct nfs4_state *
2001nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
2002{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002003 struct nfs4_state *ret;
2004
Andy Adamsone23008e2012-10-02 21:07:32 -04002005 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002006 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
2007 else
2008 ret = _nfs4_opendata_to_nfs4_state(data);
2009 nfs4_sequence_free_slot(&data->o_res.seq_res);
2010 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04002011}
2012
Trond Myklebust0de43972018-09-02 15:57:01 -04002013static struct nfs_open_context *
2014nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002015{
2016 struct nfs_inode *nfsi = NFS_I(state->inode);
2017 struct nfs_open_context *ctx;
2018
Trond Myklebust0de43972018-09-02 15:57:01 -04002019 rcu_read_lock();
2020 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01002021 if (ctx->state != state)
2022 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04002023 if ((ctx->mode & mode) != mode)
2024 continue;
2025 if (!get_nfs_open_context(ctx))
2026 continue;
2027 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002028 return ctx;
2029 }
Trond Myklebust0de43972018-09-02 15:57:01 -04002030 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002031 return ERR_PTR(-ENOENT);
2032}
2033
Trond Myklebust0de43972018-09-02 15:57:01 -04002034static struct nfs_open_context *
2035nfs4_state_find_open_context(struct nfs4_state *state)
2036{
2037 struct nfs_open_context *ctx;
2038
2039 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
2040 if (!IS_ERR(ctx))
2041 return ctx;
2042 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
2043 if (!IS_ERR(ctx))
2044 return ctx;
2045 return nfs4_state_find_open_context_mode(state, FMODE_READ);
2046}
2047
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002048static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
2049 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002050{
2051 struct nfs4_opendata *opendata;
2052
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002053 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002054 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002055 if (opendata == NULL)
2056 return ERR_PTR(-ENOMEM);
2057 opendata->state = state;
Trond Myklebustace9fad2018-09-02 19:19:07 -04002058 refcount_inc(&state->count);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002059 return opendata;
2060}
2061
Trond Myklebust24311f82015-09-20 10:50:17 -04002062static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
2063 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01002064{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002065 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01002066 int ret;
2067
Trond Myklebust24311f82015-09-20 10:50:17 -04002068 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10002069 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002070 opendata->o_arg.open_flags = 0;
2071 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05002072 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
2073 NFS_SB(opendata->dentry->d_sb),
2074 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002075 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
2076 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2077 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002078 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002079 if (ret != 0)
2080 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002081 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002082 if (IS_ERR(newstate))
2083 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002084 if (newstate != opendata->state)
2085 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002086 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002087 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002088}
2089
2090static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2091{
Trond Myklebust864472e2006-01-03 09:55:15 +01002092 int ret;
2093
2094 /* memory barrier prior to reading state->n_* */
2095 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002096 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2097 if (ret != 0)
2098 return ret;
2099 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2100 if (ret != 0)
2101 return ret;
2102 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2103 if (ret != 0)
2104 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002105 /*
2106 * We may have performed cached opens for all three recoveries.
2107 * Check if we need to update the current stateid.
2108 */
2109 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002110 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002111 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002112 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002113 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002114 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002115 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002116 return 0;
2117}
2118
Linus Torvalds1da177e2005-04-16 15:20:36 -07002119/*
2120 * OPEN_RECLAIM:
2121 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002122 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002123static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002124{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002125 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002126 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002127 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002128 int status;
2129
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002130 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2131 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002132 if (IS_ERR(opendata))
2133 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002134 rcu_read_lock();
2135 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002136 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002137 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002138 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002139 opendata->o_arg.u.delegation_type = delegation_type;
2140 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002141 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002142 return status;
2143}
2144
Trond Myklebust539cd032007-06-05 11:46:42 -04002145static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146{
2147 struct nfs_server *server = NFS_SERVER(state->inode);
2148 struct nfs4_exception exception = { };
2149 int err;
2150 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002151 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002152 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002153 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2154 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002155 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002156 break;
2157 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 } while (exception.retry);
2159 return err;
2160}
2161
Trond Myklebust864472e2006-01-03 09:55:15 +01002162static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2163{
2164 struct nfs_open_context *ctx;
2165 int ret;
2166
2167 ctx = nfs4_state_find_open_context(state);
2168 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002169 return -EAGAIN;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002170 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2171 nfs_state_clear_open_state_flags(state);
Trond Myklebust539cd032007-06-05 11:46:42 -04002172 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002173 put_nfs_open_context(ctx);
2174 return ret;
2175}
2176
NeilBrowndce26302017-12-13 09:57:09 +11002177static 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 -07002178{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002179 switch (err) {
2180 default:
2181 printk(KERN_ERR "NFS: %s: unhandled error "
2182 "%d.\n", __func__, err);
2183 case 0:
2184 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002185 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002186 case -ESTALE:
Trond Myklebust67e7b522019-08-07 07:31:27 -04002187 case -ETIMEDOUT:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002188 break;
2189 case -NFS4ERR_BADSESSION:
2190 case -NFS4ERR_BADSLOT:
2191 case -NFS4ERR_BAD_HIGH_SLOT:
2192 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2193 case -NFS4ERR_DEADSESSION:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002194 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2195 return -EAGAIN;
2196 case -NFS4ERR_STALE_CLIENTID:
2197 case -NFS4ERR_STALE_STATEID:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002198 /* Don't recall a delegation if it was lost */
2199 nfs4_schedule_lease_recovery(server->nfs_client);
2200 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002201 case -NFS4ERR_MOVED:
2202 nfs4_schedule_migration_recovery(server);
2203 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002204 case -NFS4ERR_LEASE_MOVED:
2205 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2206 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002207 case -NFS4ERR_DELEG_REVOKED:
2208 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002209 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002210 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002211 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002212 nfs_inode_find_state_and_recover(state->inode,
2213 stateid);
2214 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002215 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002216 case -NFS4ERR_DELAY:
2217 case -NFS4ERR_GRACE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002218 ssleep(1);
2219 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002220 case -ENOMEM:
2221 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002222 if (fl) {
2223 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2224 if (lsp)
2225 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2226 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002227 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002228 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002229 return err;
2230}
2231
Trond Myklebust24311f82015-09-20 10:50:17 -04002232int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002233 struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002234{
2235 struct nfs_server *server = NFS_SERVER(state->inode);
2236 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002237 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002238
2239 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2240 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2241 if (IS_ERR(opendata))
2242 return PTR_ERR(opendata);
2243 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002244 if (!test_bit(NFS_O_RDWR_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002245 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2246 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002247 goto out;
2248 }
2249 if (!test_bit(NFS_O_WRONLY_STATE, &state->flags)) {
Trond Myklebust24311f82015-09-20 10:50:17 -04002250 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2251 if (err)
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002252 goto out;
Trond Myklebust24311f82015-09-20 10:50:17 -04002253 }
Trond Myklebust5eb8d182019-07-19 14:08:37 -04002254 if (!test_bit(NFS_O_RDONLY_STATE, &state->flags)) {
2255 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2256 if (err)
2257 goto out;
2258 }
2259 nfs_state_clear_delegation(state);
2260out:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002261 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002262 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002263}
2264
Chuck Leverbe05c862013-08-09 12:49:47 -04002265static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2266{
2267 struct nfs4_opendata *data = calldata;
2268
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002269 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2270 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002271}
2272
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002273static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2274{
2275 struct nfs4_opendata *data = calldata;
2276
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002277 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002278
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002279 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002280 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002281 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002282 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002283 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002284 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002285 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002286}
2287
2288static void nfs4_open_confirm_release(void *calldata)
2289{
2290 struct nfs4_opendata *data = calldata;
2291 struct nfs4_state *state = NULL;
2292
2293 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002294 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002295 goto out_free;
2296 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002297 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002298 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002299 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002300 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002301 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002302out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002303 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002304}
2305
2306static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002307 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002308 .rpc_call_done = nfs4_open_confirm_done,
2309 .rpc_release = nfs4_open_confirm_release,
2310};
2311
2312/*
2313 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2314 */
2315static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2316{
David Howells2b0143b2015-03-17 22:25:59 +00002317 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002318 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002319 struct rpc_message msg = {
2320 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2321 .rpc_argp = &data->c_arg,
2322 .rpc_resp = &data->c_res,
2323 .rpc_cred = data->owner->so_cred,
2324 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002325 struct rpc_task_setup task_setup_data = {
2326 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002327 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002328 .callback_ops = &nfs4_open_confirm_ops,
2329 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002330 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002331 .flags = RPC_TASK_ASYNC,
2332 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002333 int status;
2334
Anna Schumakerfba83f32018-05-04 16:22:50 -04002335 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2336 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002337 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002338 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002339 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002340 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002341 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002342 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002343 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002344 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002345 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002346 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002347 smp_wmb();
2348 } else
2349 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002350 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002351 return status;
2352}
2353
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002354static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002355{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002356 struct nfs4_opendata *data = calldata;
2357 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002358 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002359 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002360
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002361 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002362 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002363 /*
2364 * Check if we still need to send an OPEN call, or if we can use
2365 * a delegation instead.
2366 */
2367 if (data->state != NULL) {
2368 struct nfs_delegation *delegation;
2369
Trond Myklebustbe189f72018-09-27 17:12:33 -04002370 if (can_open_cached(data->state, data->o_arg.fmode,
2371 data->o_arg.open_flags, claim))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002372 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002373 rcu_read_lock();
2374 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002375 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002376 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002377 rcu_read_unlock();
2378 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002379 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002380 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002381 switch (claim) {
2382 default:
2383 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002384 case NFS4_OPEN_CLAIM_PREVIOUS:
2385 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2386 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002387 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002388 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002389 case NFS4_OPEN_CLAIM_FH:
2390 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002391 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002392 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002393 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002394 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002395 &data->o_res.seq_res,
2396 task) != 0)
2397 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002398
2399 /* Set the create mode (note dependency on the session type) */
2400 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2401 if (data->o_arg.open_flags & O_EXCL) {
2402 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2403 if (nfs4_has_persistent_session(clp))
2404 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2405 else if (clp->cl_mvops->minor_version > 0)
2406 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2407 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002408 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002409unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002410 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002411 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002412out_no_action:
2413 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002414out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002415 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002416}
2417
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002418static void nfs4_open_done(struct rpc_task *task, void *calldata)
2419{
2420 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002422 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002423
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002424 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002425 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002426
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002427 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002428 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2429 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002430 case S_IFREG:
2431 break;
2432 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002433 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002434 break;
2435 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002436 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002437 break;
2438 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002439 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002440 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002441 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002442 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002443 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2444 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002445 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002446 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002447}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002448
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002449static void nfs4_open_release(void *calldata)
2450{
2451 struct nfs4_opendata *data = calldata;
2452 struct nfs4_state *state = NULL;
2453
2454 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002455 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002456 goto out_free;
2457 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002458 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002459 goto out_free;
2460 /* In case we need an open_confirm, no cleanup! */
2461 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2462 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002463 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002464 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002465 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002466out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002467 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002468}
2469
2470static const struct rpc_call_ops nfs4_open_ops = {
2471 .rpc_call_prepare = nfs4_open_prepare,
2472 .rpc_call_done = nfs4_open_done,
2473 .rpc_release = nfs4_open_release,
2474};
2475
Fred Isaman3b65a302016-09-19 10:06:49 -04002476static int nfs4_run_open_task(struct nfs4_opendata *data,
2477 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002478{
David Howells2b0143b2015-03-17 22:25:59 +00002479 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002480 struct nfs_server *server = NFS_SERVER(dir);
2481 struct nfs_openargs *o_arg = &data->o_arg;
2482 struct nfs_openres *o_res = &data->o_res;
2483 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002484 struct rpc_message msg = {
2485 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2486 .rpc_argp = o_arg,
2487 .rpc_resp = o_res,
2488 .rpc_cred = data->owner->so_cred,
2489 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002490 struct rpc_task_setup task_setup_data = {
2491 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002492 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002493 .callback_ops = &nfs4_open_ops,
2494 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002495 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002496 .flags = RPC_TASK_ASYNC,
2497 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002498 int status;
2499
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002500 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002501 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002502 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002503 data->cancelled = false;
2504 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002505 if (!ctx) {
2506 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002507 data->is_recover = true;
Trond Myklebust67e7b522019-08-07 07:31:27 -04002508 task_setup_data.flags |= RPC_TASK_TIMEOUT;
Fred Isaman2409a972016-10-06 12:11:21 -04002509 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002510 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002511 pnfs_lgopen_prepare(data, ctx);
2512 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002513 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002514 if (IS_ERR(task))
2515 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002516 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002517 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002518 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002519 smp_wmb();
2520 } else
2521 status = data->rpc_status;
2522 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002523
2524 return status;
2525}
2526
2527static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2528{
David Howells2b0143b2015-03-17 22:25:59 +00002529 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002530 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002531 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002532
Fred Isaman3b65a302016-09-19 10:06:49 -04002533 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002534 if (status != 0 || !data->rpc_done)
2535 return status;
2536
Trond Myklebust6926afd12012-01-07 13:22:46 -05002537 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2538
Anna Schumakerd7e98252017-01-11 16:13:29 -05002539 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002540 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002541
2542 return status;
2543}
2544
Trond Myklebustf3792d62014-07-10 08:54:32 -04002545/*
2546 * Additional permission checks in order to distinguish between an
2547 * open for read, and an open for execute. This works around the
2548 * fact that NFSv4 OPEN treats read and execute permissions as being
2549 * the same.
2550 * Note that in the non-execute case, we want to turn off permission
2551 * checking if we just created a new file (POSIX open() semantics).
2552 */
NeilBrowna52458b2018-12-03 11:30:31 +11002553static int nfs4_opendata_access(const struct cred *cred,
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002554 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002555 struct nfs4_state *state, fmode_t fmode,
2556 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002557{
2558 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002559 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002560
2561 /* access call failed or for some reason the server doesn't
2562 * support any access modes -- defer access call until later */
2563 if (opendata->o_res.access_supported == 0)
2564 return 0;
2565
2566 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002567 /*
2568 * Use openflags to check for exec, because fmode won't
2569 * always have FMODE_EXEC set when file open for exec.
2570 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002571 if (openflags & __FMODE_EXEC) {
2572 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002573 if (S_ISDIR(state->inode->i_mode))
2574 mask = NFS4_ACCESS_LOOKUP;
2575 else
2576 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002577 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002578 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002579
2580 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002581 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2582 nfs_access_add_cache(state->inode, &cache);
2583
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002584 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2585 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002586 return 0;
2587
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002588 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002589}
2590
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002591/*
2592 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2593 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002594static int _nfs4_proc_open(struct nfs4_opendata *data,
2595 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002596{
David Howells2b0143b2015-03-17 22:25:59 +00002597 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002598 struct nfs_server *server = NFS_SERVER(dir);
2599 struct nfs_openargs *o_arg = &data->o_arg;
2600 struct nfs_openres *o_res = &data->o_res;
2601 int status;
2602
Fred Isaman3b65a302016-09-19 10:06:49 -04002603 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002604 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002605 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002606 if (status != 0) {
2607 if (status == -NFS4ERR_BADNAME &&
2608 !(o_arg->open_flags & O_CREAT))
2609 return -ENOENT;
2610 return status;
2611 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002612
Trond Myklebust6926afd12012-01-07 13:22:46 -05002613 nfs_fattr_map_and_free_names(server, &data->f_attr);
2614
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002615 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002616 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002617 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002618 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002619 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002620 if (data->file_created ||
2621 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002622 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002623 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002624 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002625 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2626 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002627 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002628 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002629 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002630 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002631 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002632 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2633 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002634 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2635 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002636 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002637 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002638}
2639
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640/*
2641 * OPEN_EXPIRED:
2642 * reclaim state on the server after a network partition.
2643 * Assumes caller holds the appropriate lock
2644 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002645static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002646{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002647 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002648 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002649
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002650 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002651 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002652 if (IS_ERR(opendata))
2653 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002654 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002655 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002656 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002657 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002658 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659}
2660
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002661static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002662{
Trond Myklebust539cd032007-06-05 11:46:42 -04002663 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002664 struct nfs4_exception exception = { };
2665 int err;
2666
2667 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002668 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002669 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002670 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2671 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002672 switch (err) {
2673 default:
2674 goto out;
2675 case -NFS4ERR_GRACE:
2676 case -NFS4ERR_DELAY:
2677 nfs4_handle_exception(server, err, &exception);
2678 err = 0;
2679 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002680 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002681out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002682 return err;
2683}
2684
Linus Torvalds1da177e2005-04-16 15:20:36 -07002685static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2686{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002688 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689
Trond Myklebust864472e2006-01-03 09:55:15 +01002690 ctx = nfs4_state_find_open_context(state);
2691 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002692 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002693 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002694 put_nfs_open_context(ctx);
2695 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002696}
2697
Trond Myklebust41020b62016-09-22 13:38:58 -04002698static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2699 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002700{
Trond Myklebust41020b62016-09-22 13:38:58 -04002701 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002702 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002703}
2704
2705static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2706{
2707 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002708 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002709}
2710
2711static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2712{
2713 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2714 nfs40_clear_delegation_stateid(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002715 nfs_state_clear_open_state_flags(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002716 return nfs4_open_expired(sp, state);
2717}
2718
Trond Myklebust45870d62016-09-22 13:38:59 -04002719static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2720 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002721 const struct cred *cred)
Trond Myklebust45870d62016-09-22 13:38:59 -04002722{
2723 return -NFS4ERR_BAD_STATEID;
2724}
2725
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002726#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002727static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2728 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11002729 const struct cred *cred)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002730{
2731 int status;
2732
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002733 switch (stateid->type) {
2734 default:
2735 break;
2736 case NFS4_INVALID_STATEID_TYPE:
2737 case NFS4_SPECIAL_STATEID_TYPE:
2738 return -NFS4ERR_BAD_STATEID;
2739 case NFS4_REVOKED_STATEID_TYPE:
2740 goto out_free;
2741 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002742
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002743 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002744 switch (status) {
2745 case -NFS4ERR_EXPIRED:
2746 case -NFS4ERR_ADMIN_REVOKED:
2747 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002748 break;
2749 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002750 return status;
2751 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002752out_free:
2753 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002754 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002755 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002756}
2757
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002758static int nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002759{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002760 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002761 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002762 struct nfs_delegation *delegation;
NeilBrowna52458b2018-12-03 11:30:31 +11002763 const struct cred *cred = NULL;
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002764 int status, ret = NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002765
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002766 /* Get the delegation credential for use by test/free_stateid */
2767 rcu_read_lock();
2768 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002769 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002770 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002771 nfs_state_clear_delegation(state);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002772 return NFS_OK;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002773 }
2774
2775 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002776
Trond Myklebust994b15b2018-09-05 14:07:14 -04002777 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2778 &delegation->flags)) {
2779 rcu_read_unlock();
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002780 return NFS_OK;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002781 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002782
NeilBrowna52458b2018-12-03 11:30:31 +11002783 if (delegation->cred)
2784 cred = get_cred(delegation->cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002785 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002786 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002787 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002788 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002789 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002790 else
2791 ret = status;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002792
Trond Myklebust8c39a392019-07-19 13:48:44 -04002793 put_cred(cred);
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002794 return ret;
2795}
2796
2797static void nfs41_delegation_recover_stateid(struct nfs4_state *state)
2798{
2799 nfs4_stateid tmp;
2800
2801 if (test_bit(NFS_DELEGATED_STATE, &state->flags) &&
2802 nfs4_copy_delegation_stateid(state->inode, state->state,
2803 &tmp, NULL) &&
2804 nfs4_stateid_match_other(&state->stateid, &tmp))
2805 nfs_state_set_delegation(state, &tmp, state->state);
2806 else
2807 nfs_state_clear_delegation(state);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002808}
2809
2810/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002811 * nfs41_check_expired_locks - possibly free a lock stateid
2812 *
2813 * @state: NFSv4 state for an inode
2814 *
2815 * Returns NFS_OK if recovery for this stateid is now finished.
2816 * Otherwise a negative NFS4ERR value is returned.
2817 */
2818static int nfs41_check_expired_locks(struct nfs4_state *state)
2819{
2820 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002821 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002822 struct nfs_server *server = NFS_SERVER(state->inode);
2823
2824 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2825 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002826
2827 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002828 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2829 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
NeilBrowna52458b2018-12-03 11:30:31 +11002830 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002831
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002832 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002833 spin_unlock(&state->state_lock);
2834
2835 nfs4_put_lock_state(prev);
2836 prev = lsp;
2837
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002838 status = nfs41_test_and_free_expired_stateid(server,
2839 &lsp->ls_stateid,
2840 cred);
2841 trace_nfs4_test_lock_stateid(state, lsp, status);
2842 if (status == -NFS4ERR_EXPIRED ||
2843 status == -NFS4ERR_BAD_STATEID) {
2844 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002845 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002846 if (!recover_lost_locks)
2847 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2848 } else if (status != NFS_OK) {
2849 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002850 nfs4_put_lock_state(prev);
2851 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002852 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002853 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002854 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002855 }
2856 spin_unlock(&state->state_lock);
2857 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002858out:
2859 return ret;
2860}
2861
2862/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002863 * nfs41_check_open_stateid - possibly free an open stateid
2864 *
2865 * @state: NFSv4 state for an inode
2866 *
2867 * Returns NFS_OK if recovery for this stateid is now finished.
2868 * Otherwise a negative NFS4ERR value is returned.
2869 */
2870static int nfs41_check_open_stateid(struct nfs4_state *state)
2871{
2872 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002873 nfs4_stateid *stateid = &state->open_stateid;
NeilBrowna52458b2018-12-03 11:30:31 +11002874 const struct cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002875 int status;
2876
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002877 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0)
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002878 return -NFS4ERR_BAD_STATEID;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002879 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002880 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002881 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002882 nfs_state_clear_open_state_flags(state);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002883 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002884 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002885 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002886 if (nfs_open_stateid_recover_openmode(state))
2887 return -NFS4ERR_OPENMODE;
2888 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002889}
2890
2891static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2892{
Chuck Levereb64cf92012-07-11 16:30:05 -04002893 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002894
Trond Myklebust27a30cf2019-07-22 18:32:59 +01002895 status = nfs41_check_delegation_stateid(state);
2896 if (status != NFS_OK)
2897 return status;
2898 nfs41_delegation_recover_stateid(state);
2899
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002900 status = nfs41_check_expired_locks(state);
2901 if (status != NFS_OK)
2902 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002903 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002904 if (status != NFS_OK)
2905 status = nfs4_open_expired(sp, state);
2906 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002907}
2908#endif
2909
Linus Torvalds1da177e2005-04-16 15:20:36 -07002910/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002911 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2912 * fields corresponding to attributes that were used to store the verifier.
2913 * Make sure we clobber those fields in the later setattr call
2914 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002915static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002916 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002917{
Trond Myklebust609339c2018-03-28 16:18:17 -04002918 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2919 __u32 attrset[3];
2920 unsigned ret;
2921 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002922
Trond Myklebust609339c2018-03-28 16:18:17 -04002923 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2924 attrset[i] = opendata->o_res.attrset[i];
2925 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2926 attrset[i] &= ~bitmask[i];
2927 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002928
Trond Myklebust609339c2018-03-28 16:18:17 -04002929 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2930 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002931
Trond Myklebust609339c2018-03-28 16:18:17 -04002932 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2933 if (sattr->ia_valid & ATTR_ATIME_SET)
2934 ret |= ATTR_ATIME_SET;
2935 else
2936 ret |= ATTR_ATIME;
2937 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002938
Trond Myklebust609339c2018-03-28 16:18:17 -04002939 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2940 if (sattr->ia_valid & ATTR_MTIME_SET)
2941 ret |= ATTR_MTIME_SET;
2942 else
2943 ret |= ATTR_MTIME;
2944 }
2945
2946 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002947 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002948 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002949}
2950
Trond Myklebustc21443c2013-02-07 14:26:21 -05002951static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002952 int flags, struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002953{
2954 struct nfs4_state_owner *sp = opendata->owner;
2955 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002956 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002957 struct nfs4_state *state;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002958 fmode_t acc_mode = _nfs4_ctx_to_accessmode(ctx);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002959 unsigned int seq;
2960 int ret;
2961
2962 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2963
Fred Isaman3b65a302016-09-19 10:06:49 -04002964 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002965 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002966 goto out;
2967
Trond Myklebustae55e592018-05-22 11:17:16 -04002968 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002969 ret = PTR_ERR(state);
2970 if (IS_ERR(state))
2971 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002972 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002973 if (server->caps & NFS_CAP_POSIX_LOCK)
2974 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002975 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2976 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002977
Trond Myklebust275bb302013-05-29 13:11:28 -04002978 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002979 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002980 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002981 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002982 alias = d_exact_alias(dentry, state->inode);
2983 if (!alias)
2984 alias = d_splice_alias(igrab(state->inode), dentry);
2985 /* d_splice_alias() can't fail here - it's a non-directory */
2986 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002987 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002988 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002989 }
2990 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002991 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002992 }
2993
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002994 /* Parse layoutget results before we check for access */
2995 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2996
Trond Myklebust1bf85d82019-06-27 06:30:48 -04002997 ret = nfs4_opendata_access(sp->so_cred, opendata, state,
2998 acc_mode, flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002999 if (ret != 0)
3000 goto out;
3001
David Howells2b0143b2015-03-17 22:25:59 +00003002 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04003003 nfs_inode_attach_open_context(ctx);
3004 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
3005 nfs4_schedule_stateid_recovery(server, state);
3006 }
Fred Isaman2409a972016-10-06 12:11:21 -04003007
Trond Myklebustc21443c2013-02-07 14:26:21 -05003008out:
Olga Kornievskaia0cb98ab2019-03-19 12:12:13 -04003009 if (!opendata->cancelled)
3010 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05003011 return ret;
3012}
3013
Jeff Laytonaa53ed52007-06-05 14:49:03 -04003014/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01003015 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07003016 */
Andy Adamson82be4172012-05-23 05:02:35 -04003017static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003018 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003019 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003020 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003021 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022{
3023 struct nfs4_state_owner *sp;
3024 struct nfs4_state *state = NULL;
3025 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003026 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04003027 struct dentry *dentry = ctx->dentry;
NeilBrowna52458b2018-12-03 11:30:31 +11003028 const struct cred *cred = ctx->cred;
Trond Myklebust4197a052013-05-29 12:37:49 -04003029 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003030 fmode_t fmode = _nfs4_ctx_to_openmode(ctx);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003031 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003032 struct iattr *sattr = c->sattr;
3033 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04003034 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04003035 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003036
3037 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003038 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05003039 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
3040 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003041 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
3042 goto out_err;
3043 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05003044 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01003045 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01003046 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00003047 if (d_really_is_positive(dentry))
3048 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01003049 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00003050 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003051 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003052 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
3053 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003054 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05003055 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003056
David Quigley14c43f72013-05-22 12:50:43 -04003057 if (label) {
3058 olabel = nfs4_label_alloc(server, GFP_KERNEL);
3059 if (IS_ERR(olabel)) {
3060 status = PTR_ERR(olabel);
3061 goto err_opendata_put;
3062 }
3063 }
3064
Trond Myklebuste911b812014-03-26 13:24:37 -07003065 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
3066 if (!opendata->f_attr.mdsthreshold) {
3067 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
3068 if (!opendata->f_attr.mdsthreshold)
3069 goto err_free_label;
3070 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04003071 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04003072 }
David Howells2b0143b2015-03-17 22:25:59 +00003073 if (d_really_is_positive(dentry))
3074 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04003075
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003076 status = _nfs4_open_and_get_state(opendata, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003077 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04003078 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04003079 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04003080
NeilBrownefcbc042015-07-30 13:00:56 +10003081 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04003082 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04003083 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003084 /*
3085 * send create attributes which was not set by open
3086 * with an extra setattr.
3087 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003088 if (attrs || label) {
3089 unsigned ia_old = sattr->ia_valid;
3090
3091 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003092 nfs_fattr_init(opendata->o_res.f_attr);
3093 status = nfs4_do_setattr(state->inode, cred,
3094 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003095 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003096 if (status == 0) {
3097 nfs_setattr_update_inode(state->inode, sattr,
3098 opendata->o_res.f_attr);
3099 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3100 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003101 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003102 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003103 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003104 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003105 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003106
Trond Myklebuste911b812014-03-26 13:24:37 -07003107 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003108 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003109 opendata->f_attr.mdsthreshold = NULL;
3110 }
Andy Adamson82be4172012-05-23 05:02:35 -04003111
David Quigley14c43f72013-05-22 12:50:43 -04003112 nfs4_label_free(olabel);
3113
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003114 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003115 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003116 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003117err_free_label:
3118 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003119err_opendata_put:
3120 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003121err_put_state_owner:
3122 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003123out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return status;
3125}
3126
3127
Andy Adamson82be4172012-05-23 05:02:35 -04003128static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003129 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003130 int flags,
3131 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003132 struct nfs4_label *label,
3133 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003135 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04003136 struct nfs4_exception exception = {
3137 .interruptible = true,
3138 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003139 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003140 struct nfs4_open_createattrs c = {
3141 .label = label,
3142 .sattr = sattr,
3143 .verf = {
3144 [0] = (__u32)jiffies,
3145 [1] = (__u32)current->pid,
3146 },
3147 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148 int status;
3149
3150 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003151 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003152 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003153 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003154 if (status == 0)
3155 break;
3156 /* NOTE: BAD_SEQID means the server and client disagree about the
3157 * book-keeping w.r.t. state-changing operations
3158 * (OPEN/CLOSE/LOCK/LOCKU...)
3159 * It is actually a sign of a bug on the client or on the server.
3160 *
3161 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003162 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163 * have unhashed the old state_owner for us, and that we can
3164 * therefore safely retry using a new one. We should still warn
3165 * the user though...
3166 */
3167 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003168 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003169 " returned a bad sequence-id error!\n",
3170 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 exception.retry = 1;
3172 continue;
3173 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003174 /*
3175 * BAD_STATEID on OPEN means that the server cancelled our
3176 * state before it received the OPEN_CONFIRM.
3177 * Recover by retrying the request as per the discussion
3178 * on Page 181 of RFC3530.
3179 */
3180 if (status == -NFS4ERR_BAD_STATEID) {
3181 exception.retry = 1;
3182 continue;
3183 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003184 if (status == -EAGAIN) {
3185 /* We must have found a delegation */
3186 exception.retry = 1;
3187 continue;
3188 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003189 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3190 continue;
3191 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192 status, &exception));
3193 } while (exception.retry);
3194 return res;
3195}
3196
Trond Myklebust8487c472016-06-26 08:44:35 -04003197static int _nfs4_do_setattr(struct inode *inode,
3198 struct nfs_setattrargs *arg,
3199 struct nfs_setattrres *res,
NeilBrowna52458b2018-12-03 11:30:31 +11003200 const struct cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003201 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003202{
3203 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003204 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3206 .rpc_argp = arg,
3207 .rpc_resp = res,
3208 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003209 };
NeilBrowna52458b2018-12-03 11:30:31 +11003210 const struct cred *delegation_cred = NULL;
Trond Myklebust8487c472016-06-26 08:44:35 -04003211 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003212 bool truncate;
3213 int status;
3214
3215 nfs_fattr_init(res->fattr);
3216
3217 /* Servers should only apply open mode checks for file size changes */
3218 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003219 if (!truncate)
3220 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003221
Trond Myklebust991eedb2018-04-09 11:15:30 -04003222 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003223 /* Use that stateid */
Trond Myklebust09a54f02019-08-03 10:28:18 -04003224 } else if (ctx != NULL && ctx->state) {
NeilBrown17393472016-10-13 15:26:47 +11003225 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003226 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003227 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003228 l_ctx = nfs_get_lock_context(ctx);
3229 if (IS_ERR(l_ctx))
3230 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003231 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3232 &arg->stateid, &delegation_cred);
3233 nfs_put_lock_context(l_ctx);
3234 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003235 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003236 } else {
3237zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003238 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003239 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003240 if (delegation_cred)
3241 msg.rpc_cred = delegation_cred;
3242
3243 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3244
NeilBrowna52458b2018-12-03 11:30:31 +11003245 put_cred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003246 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003247 renew_lease(server, timestamp);
3248 trace_nfs4_setattr(inode, &arg->stateid, status);
3249 return status;
3250}
3251
NeilBrowna52458b2018-12-03 11:30:31 +11003252static int nfs4_do_setattr(struct inode *inode, const struct cred *cred,
Trond Myklebust8487c472016-06-26 08:44:35 -04003253 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003254 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003255 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003256{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003257 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003258 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003259 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003260 struct nfs_setattrargs arg = {
3261 .fh = NFS_FH(inode),
3262 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003264 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003265 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003266 };
3267 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003269 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003271 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003272 struct nfs4_exception exception = {
3273 .state = state,
3274 .inode = inode,
3275 .stateid = &arg.stateid,
3276 };
3277 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003278
Linus Torvalds1da177e2005-04-16 15:20:36 -07003279 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003280 nfs4_bitmap_copy_adjust_setattr(bitmask,
3281 nfs4_bitmask(server, olabel),
3282 inode);
3283
NeilBrown29b59f92016-10-13 15:26:47 +11003284 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003285 switch (err) {
3286 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003287 if (!(sattr->ia_valid & ATTR_SIZE)) {
3288 pr_warn_once("NFSv4: server %s is incorrectly "
3289 "applying open mode checks to "
3290 "a SETATTR that is not "
3291 "changing file size.\n",
3292 server->nfs_client->cl_hostname);
3293 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003294 if (state && !(state->state & FMODE_WRITE)) {
3295 err = -EBADF;
3296 if (sattr->ia_valid & ATTR_OPEN)
3297 err = -EACCES;
3298 goto out;
3299 }
3300 }
3301 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003302 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003303out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003304 return err;
3305}
3306
Peng Tao500d7012015-09-22 11:35:22 +08003307static bool
3308nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3309{
3310 if (inode == NULL || !nfs_have_layout(inode))
3311 return false;
3312
3313 return pnfs_wait_on_layoutreturn(inode, task);
3314}
3315
Linus Torvalds1da177e2005-04-16 15:20:36 -07003316struct nfs4_closedata {
3317 struct inode *inode;
3318 struct nfs4_state *state;
3319 struct nfs_closeargs arg;
3320 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003321 struct {
3322 struct nfs4_layoutreturn_args arg;
3323 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003324 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003325 u32 roc_barrier;
3326 bool roc;
3327 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003328 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003329 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330};
3331
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003332static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003333{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003334 struct nfs4_closedata *calldata = data;
3335 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003336 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003337
Trond Myklebustcf805162016-11-15 14:56:07 -05003338 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003339 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3340 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003341 nfs4_put_open_state(calldata->state);
3342 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003343 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003344 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003345 kfree(calldata);
3346}
3347
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003348static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003349{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003350 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003353 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003354 struct nfs4_exception exception = {
3355 .state = state,
3356 .inode = calldata->inode,
3357 .stateid = &calldata->arg.stateid,
3358 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359
Chuck Levera3ca5652012-03-01 17:00:40 -05003360 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003361 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3362 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003363 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003364
3365 /* Handle Layoutreturn errors */
3366 if (calldata->arg.lr_args && task->tk_status != 0) {
3367 switch (calldata->res.lr_ret) {
3368 default:
3369 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3370 break;
3371 case 0:
3372 calldata->arg.lr_args = NULL;
3373 calldata->res.lr_res = NULL;
3374 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003375 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003376 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04003377 &calldata->arg.lr_args->range,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003378 calldata->inode))
3379 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003380 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003381 case -NFS4ERR_ADMIN_REVOKED:
3382 case -NFS4ERR_DELEG_REVOKED:
3383 case -NFS4ERR_EXPIRED:
3384 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003385 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3386 case -NFS4ERR_WRONG_CRED:
3387 calldata->arg.lr_args = NULL;
3388 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003389 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003390 }
3391 }
3392
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003393 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 * the state_owner. we keep this around to process errors
3395 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003396 switch (task->tk_status) {
3397 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003398 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003399 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003400 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003401 case -NFS4ERR_ACCESS:
3402 if (calldata->arg.bitmask != NULL) {
3403 calldata->arg.bitmask = NULL;
3404 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003405 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003406
3407 }
3408 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003409 case -NFS4ERR_OLD_STATEID:
3410 /* Did we race with OPEN? */
3411 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003412 state))
3413 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003414 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003415 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003417 case -NFS4ERR_EXPIRED:
3418 nfs4_free_revoked_stateid(server,
3419 &calldata->arg.stateid,
3420 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003421 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003422 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003423 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003425 task->tk_status = nfs4_async_handle_exception(task,
3426 server, task->tk_status, &exception);
3427 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003428 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003429 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003430 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3431 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003432out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003433 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003434 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003435 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003436 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003437 return;
3438lr_restart:
3439 calldata->res.lr_ret = 0;
3440out_restart:
3441 task->tk_status = 0;
3442 rpc_restart_call_prepare(task);
3443 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444}
3445
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003446static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003447{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003448 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003449 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003450 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003451 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003452 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003453 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003454
Chuck Levera3ca5652012-03-01 17:00:40 -05003455 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003456 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003457 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003458
Trond Myklebust88069f72009-12-08 08:33:16 -05003459 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003460 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003461 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3462 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3463 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003464 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003465 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003466 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003467 if (state->n_rdonly == 0)
3468 call_close |= is_rdonly;
3469 else if (is_rdonly)
3470 calldata->arg.fmode |= FMODE_READ;
3471 if (state->n_wronly == 0)
3472 call_close |= is_wronly;
3473 else if (is_wronly)
3474 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003475 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3476 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003477 } else if (is_rdwr)
3478 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3479
Trond Myklebust5cc78612016-11-14 11:19:56 -05003480 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003481 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003482 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003483 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003484
3485 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003486 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003487 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003488 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003489
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003490 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003491 nfs_release_seqid(calldata->arg.seqid);
3492 goto out_wait;
3493 }
3494
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003495 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3496 if (lo && !pnfs_layout_is_valid(lo)) {
3497 calldata->arg.lr_args = NULL;
3498 calldata->res.lr_res = NULL;
3499 }
3500
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003501 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003502 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003503
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003504 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003505 /* Close-to-open cache consistency revalidation */
3506 if (!nfs4_have_delegation(inode, FMODE_READ))
3507 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3508 else
3509 calldata->arg.bitmask = NULL;
3510 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003511
Trond Myklebust6ae37332015-01-30 14:21:14 -05003512 calldata->arg.share_access =
3513 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3514 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003515
Trond Myklebustd8d84982016-12-19 12:14:44 -05003516 if (calldata->res.fattr == NULL)
3517 calldata->arg.bitmask = NULL;
3518 else if (calldata->arg.bitmask == NULL)
3519 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003520 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003521 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003522 &calldata->arg.seq_args,
3523 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003524 task) != 0)
3525 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003526 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003527 return;
3528out_no_action:
3529 task->tk_action = NULL;
3530out_wait:
3531 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003532}
3533
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003534static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003535 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003536 .rpc_call_done = nfs4_close_done,
3537 .rpc_release = nfs4_free_closedata,
3538};
3539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540/*
3541 * It is possible for data to be read/written from a mem-mapped file
3542 * after the sys_close call (which hits the vfs layer as a flush).
3543 * This means that we can't safely call nfsv4 close on a file until
3544 * the inode is cleared. This in turn means that we are not good
3545 * NFSv4 citizens - we do not indicate to the server to update the file's
3546 * share state even when we are done with one of the three share
3547 * stateid's in the inode.
3548 *
3549 * NOTE: Caller must be holding the sp->so_owner semaphore!
3550 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003551int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003553 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003554 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003555 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003556 struct nfs4_state_owner *sp = state->owner;
3557 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003558 struct rpc_message msg = {
3559 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3560 .rpc_cred = state->owner->so_cred,
3561 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003562 struct rpc_task_setup task_setup_data = {
3563 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003564 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003565 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003566 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003567 .flags = RPC_TASK_ASYNC,
3568 };
Trond Myklebust95121352005-10-18 14:20:12 -07003569 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003570
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003571 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3572 &task_setup_data.rpc_client, &msg);
3573
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003574 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003576 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003577 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003578 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003580 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003581 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3582 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003584 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3585 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003586 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003587 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003588 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003589 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003590 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003591 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003592 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003593 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003594 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003595 calldata->lr.roc = pnfs_roc(state->inode,
3596 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3597 if (calldata->lr.roc) {
3598 calldata->arg.lr_args = &calldata->lr.arg;
3599 calldata->res.lr_res = &calldata->lr.res;
3600 }
Al Viro643168c2011-06-22 18:20:23 -04003601 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003602
Trond Myklebust1174dd12010-12-21 10:52:24 -05003603 msg.rpc_argp = &calldata->arg;
3604 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003605 task_setup_data.callback_data = calldata;
3606 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003607 if (IS_ERR(task))
3608 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003609 status = 0;
3610 if (wait)
3611 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003612 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003613 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003614out_free_calldata:
3615 kfree(calldata);
3616out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003617 nfs4_put_open_state(state);
3618 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003619 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003620}
3621
Trond Myklebust2b484292010-09-17 10:56:51 -04003622static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003623nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3624 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003625{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003627 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3628
3629 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003630
Trond Myklebust565277f2007-10-15 18:17:53 -04003631 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003632 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003633
3634 nfs4_label_release_security(label);
3635
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003636 if (IS_ERR(state))
3637 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003638 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003639}
3640
Trond Myklebust1185a552009-12-03 15:54:02 -05003641static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003642{
3643 if (ctx->state == NULL)
3644 return;
3645 if (is_sync)
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003646 nfs4_close_sync(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003647 else
Trond Myklebust1bf85d82019-06-27 06:30:48 -04003648 nfs4_close_state(ctx->state, _nfs4_ctx_to_openmode(ctx));
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003649}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003650
Trond Myklebustb944dba2013-11-04 15:20:20 -05003651#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3652#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003653#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003654
Linus Torvalds1da177e2005-04-16 15:20:36 -07003655static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3656{
Kinglong Mee8c612822015-08-26 21:12:58 +08003657 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003658 struct nfs4_server_caps_arg args = {
3659 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003660 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003661 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003662 struct nfs4_server_caps_res res = {};
3663 struct rpc_message msg = {
3664 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003665 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003666 .rpc_resp = &res,
3667 };
3668 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003669 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003670
Kinglong Mee8c612822015-08-26 21:12:58 +08003671 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3672 FATTR4_WORD0_FH_EXPIRE_TYPE |
3673 FATTR4_WORD0_LINK_SUPPORT |
3674 FATTR4_WORD0_SYMLINK_SUPPORT |
3675 FATTR4_WORD0_ACLSUPPORT;
3676 if (minorversion)
3677 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3678
Bryan Schumaker7c513052011-03-24 17:12:24 +00003679 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003680 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003681 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003682 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003683 case 0:
3684 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3685 res.attr_bitmask[2] = 0;
3686 break;
3687 case 1:
3688 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3689 break;
3690 case 2:
3691 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3692 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003693 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003694 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3695 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3696 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3697 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003698 NFS_CAP_CTIME|NFS_CAP_MTIME|
3699 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003700 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3701 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702 server->caps |= NFS_CAP_ACLS;
3703 if (res.has_links != 0)
3704 server->caps |= NFS_CAP_HARDLINKS;
3705 if (res.has_symlinks != 0)
3706 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003707 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3708 server->caps |= NFS_CAP_FILEID;
3709 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3710 server->caps |= NFS_CAP_MODE;
3711 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3712 server->caps |= NFS_CAP_NLINK;
3713 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3714 server->caps |= NFS_CAP_OWNER;
3715 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3716 server->caps |= NFS_CAP_OWNER_GROUP;
3717 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3718 server->caps |= NFS_CAP_ATIME;
3719 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3720 server->caps |= NFS_CAP_CTIME;
3721 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3722 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003723#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3724 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3725 server->caps |= NFS_CAP_SECURITY_LABEL;
3726#endif
3727 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3728 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003729 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003730
Trond Myklebusta65318b2009-03-11 14:10:28 -04003731 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3732 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3733 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003734 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003735
3736 /* Avoid a regression due to buggy server */
3737 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3738 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003739 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3740 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003741
Linus Torvalds1da177e2005-04-16 15:20:36 -07003742 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003743 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003744 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003745
Linus Torvalds1da177e2005-04-16 15:20:36 -07003746 return status;
3747}
3748
Trond Myklebust55a97592006-06-09 09:34:19 -04003749int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003750{
Trond Myklebust0688e642019-04-07 13:59:09 -04003751 struct nfs4_exception exception = {
3752 .interruptible = true,
3753 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003754 int err;
3755 do {
3756 err = nfs4_handle_exception(server,
3757 _nfs4_server_capabilities(server, fhandle),
3758 &exception);
3759 } while (exception.retry);
3760 return err;
3761}
3762
3763static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3764 struct nfs_fsinfo *info)
3765{
David Quigleyaa9c2662013-05-22 12:50:44 -04003766 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003767 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003768 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003769 };
3770 struct nfs4_lookup_res res = {
3771 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003772 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003773 .fh = fhandle,
3774 };
3775 struct rpc_message msg = {
3776 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3777 .rpc_argp = &args,
3778 .rpc_resp = &res,
3779 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003780
David Quigleyaa9c2662013-05-22 12:50:44 -04003781 bitmask[0] = nfs4_fattr_bitmap[0];
3782 bitmask[1] = nfs4_fattr_bitmap[1];
3783 /*
3784 * Process the label in the upcoming getfattr
3785 */
3786 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3787
Trond Myklebust0e574af2005-10-27 22:12:38 -04003788 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003789 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790}
3791
3792static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3793 struct nfs_fsinfo *info)
3794{
Trond Myklebust0688e642019-04-07 13:59:09 -04003795 struct nfs4_exception exception = {
3796 .interruptible = true,
3797 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 int err;
3799 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003800 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003801 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003802 switch (err) {
3803 case 0:
3804 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003805 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003806 default:
3807 err = nfs4_handle_exception(server, err, &exception);
3808 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003809 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003810out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811 return err;
3812}
3813
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003814static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3815 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3816{
Trond Myklebustc2190662013-08-26 19:23:04 -04003817 struct rpc_auth_create_args auth_args = {
3818 .pseudoflavor = flavor,
3819 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003820 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003821
Trond Myklebustc2190662013-08-26 19:23:04 -04003822 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003823 if (IS_ERR(auth))
3824 return -EACCES;
3825 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003826}
3827
Chuck Lever9a744ba2013-03-16 15:56:02 -04003828/*
3829 * Retry pseudoroot lookup with various security flavors. We do this when:
3830 *
3831 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3832 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3833 *
3834 * Returns zero on success, or a negative NFS4ERR value, or a
3835 * negative errno value.
3836 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003837static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003838 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003839{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003840 /* Per 3530bis 15.33.5 */
3841 static const rpc_authflavor_t flav_array[] = {
3842 RPC_AUTH_GSS_KRB5P,
3843 RPC_AUTH_GSS_KRB5I,
3844 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003845 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003846 RPC_AUTH_NULL,
3847 };
3848 int status = -EPERM;
3849 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003850
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003851 if (server->auth_info.flavor_len > 0) {
3852 /* try each flavor specified by user */
3853 for (i = 0; i < server->auth_info.flavor_len; i++) {
3854 status = nfs4_lookup_root_sec(server, fhandle, info,
3855 server->auth_info.flavors[i]);
3856 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3857 continue;
3858 break;
3859 }
3860 } else {
3861 /* no flavors specified by user, try default list */
3862 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3863 status = nfs4_lookup_root_sec(server, fhandle, info,
3864 flav_array[i]);
3865 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3866 continue;
3867 break;
3868 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003869 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003870
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003871 /*
Colin Ian Kingd3787af2018-10-26 19:10:31 +01003872 * -EACCES could mean that the user doesn't have correct permissions
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003873 * to access the mount. It could also mean that we tried to mount
3874 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3875 * existing mount programs don't handle -EACCES very well so it should
3876 * be mapped to -EPERM instead.
3877 */
3878 if (status == -EACCES)
3879 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003880 return status;
3881}
3882
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003883/**
3884 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3885 * @server: initialized nfs_server handle
3886 * @fhandle: we fill in the pseudo-fs root file handle
3887 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003888 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003889 *
3890 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003891 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003892int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003893 struct nfs_fsinfo *info,
3894 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003895{
Andre Przywarac7757072015-04-23 17:17:40 +01003896 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003897
Andre Przywarac7757072015-04-23 17:17:40 +01003898 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003899 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003900
3901 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003902 status = server->nfs_client->cl_mvops->find_root_sec(server,
3903 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003904
Linus Torvalds1da177e2005-04-16 15:20:36 -07003905 if (status == 0)
3906 status = nfs4_server_capabilities(server, fhandle);
3907 if (status == 0)
3908 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003909
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003910 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911}
3912
Bryan Schumakerbae36242012-05-10 15:07:31 -04003913static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3914 struct nfs_fsinfo *info)
3915{
3916 int error;
3917 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003918 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003919
3920 error = nfs4_server_capabilities(server, mntfh);
3921 if (error < 0) {
3922 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3923 return error;
3924 }
3925
David Quigley14c43f72013-05-22 12:50:43 -04003926 label = nfs4_label_alloc(server, GFP_KERNEL);
3927 if (IS_ERR(label))
3928 return PTR_ERR(label);
3929
Trond Myklebusta841b542018-04-07 13:50:59 -04003930 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003931 if (error < 0) {
3932 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003933 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003934 }
3935
3936 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3937 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3938 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3939
David Quigley14c43f72013-05-22 12:50:43 -04003940err_free_label:
3941 nfs4_label_free(label);
3942
Bryan Schumakerbae36242012-05-10 15:07:31 -04003943 return error;
3944}
3945
Manoj Naik6b97fd32006-06-09 09:34:29 -04003946/*
3947 * Get locations and (maybe) other attributes of a referral.
3948 * Note that we'll actually follow the referral later when
3949 * we detect fsid mismatch in inode revalidation
3950 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003951static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3952 const struct qstr *name, struct nfs_fattr *fattr,
3953 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003954{
3955 int status = -ENOMEM;
3956 struct page *page = NULL;
3957 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003958
3959 page = alloc_page(GFP_KERNEL);
3960 if (page == NULL)
3961 goto out;
3962 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3963 if (locations == NULL)
3964 goto out;
3965
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003966 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003967 if (status != 0)
3968 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003969
3970 /*
3971 * If the fsid didn't change, this is a migration event, not a
3972 * referral. Cause us to drop into the exception handler, which
3973 * will kick off migration recovery.
3974 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003975 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003976 dprintk("%s: server did not return a different fsid for"
3977 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003978 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003979 goto out;
3980 }
Andy Adamson533eb462011-06-13 18:25:56 -04003981 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3982 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003983
Andy Adamson533eb462011-06-13 18:25:56 -04003984 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003985 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003986 memset(fhandle, 0, sizeof(struct nfs_fh));
3987out:
3988 if (page)
3989 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003990 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003991 return status;
3992}
3993
David Quigley1775fd32013-05-22 12:50:42 -04003994static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003995 struct nfs_fattr *fattr, struct nfs4_label *label,
3996 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003997{
Trond Myklebust771734f2018-04-07 13:54:23 -04003998 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003999 struct nfs4_getattr_arg args = {
4000 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04004001 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002 };
4003 struct nfs4_getattr_res res = {
4004 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04004005 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006 .server = server,
4007 };
4008 struct rpc_message msg = {
4009 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
4010 .rpc_argp = &args,
4011 .rpc_resp = &res,
4012 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004013
Trond Myklebust771734f2018-04-07 13:54:23 -04004014 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04004015
Trond Myklebust0e574af2005-10-27 22:12:38 -04004016 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004017 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004018}
4019
David Quigley1775fd32013-05-22 12:50:42 -04004020static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04004021 struct nfs_fattr *fattr, struct nfs4_label *label,
4022 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004023{
Trond Myklebust0688e642019-04-07 13:59:09 -04004024 struct nfs4_exception exception = {
4025 .interruptible = true,
4026 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004027 int err;
4028 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04004029 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004030 trace_nfs4_getattr(server, fhandle, fattr, err);
4031 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004032 &exception);
4033 } while (exception.retry);
4034 return err;
4035}
4036
4037/*
4038 * The file is not closed if it is opened due to the a request to change
4039 * the size of the file. The open call will not be needed once the
4040 * VFS layer lookup-intents are implemented.
4041 *
4042 * Close is called when the inode is destroyed.
4043 * If we haven't opened the file for O_WRONLY, we
4044 * need to in the size_change case to obtain a stateid.
4045 *
4046 * Got race?
4047 * Because OPEN is always done by name in nfsv4, it is
4048 * possible that we opened a different file by the same
4049 * name. We can recognize this race condition, but we
4050 * can't do anything about it besides returning an error.
4051 *
4052 * This will be fixed with VFS changes (lookup-intent).
4053 */
4054static int
4055nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
4056 struct iattr *sattr)
4057{
David Howells2b0143b2015-03-17 22:25:59 +00004058 struct inode *inode = d_inode(dentry);
NeilBrowna52458b2018-12-03 11:30:31 +11004059 const struct cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11004060 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04004061 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 int status;
4063
Peng Tao88ac8152014-09-12 11:04:10 +08004064 if (pnfs_ld_layoutret_on_setattr(inode) &&
4065 sattr->ia_valid & ATTR_SIZE &&
4066 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04004067 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04004068
Trond Myklebust0e574af2005-10-27 22:12:38 -04004069 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070
Andy Adamson26699402012-05-30 16:12:24 -04004071 /* Deal with open(O_TRUNC) */
4072 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03004073 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04004074
4075 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03004076 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04004077 return 0;
4078
Trond Myklebustd5308382005-11-04 15:33:38 -05004079 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004080 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00004081
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004082 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11004083 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11004084 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04004085 }
4086
David Quigley14c43f72013-05-22 12:50:43 -04004087 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
4088 if (IS_ERR(label))
4089 return PTR_ERR(label);
4090
Trond Myklebust199366f2018-03-20 16:43:18 -04004091 /* Return any delegations if we're going to change ACLs */
4092 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004093 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004094
NeilBrown29b59f92016-10-13 15:26:47 +11004095 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004096 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004097 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004098 nfs_setsecurity(inode, fattr, label);
4099 }
David Quigley14c43f72013-05-22 12:50:43 -04004100 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return status;
4102}
4103
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004104static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4105 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004106 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004107{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004108 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004109 int status;
4110 struct nfs4_lookup_arg args = {
4111 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004112 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004113 .name = name,
4114 };
4115 struct nfs4_lookup_res res = {
4116 .server = server,
4117 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004118 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004119 .fh = fhandle,
4120 };
4121 struct rpc_message msg = {
4122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4123 .rpc_argp = &args,
4124 .rpc_resp = &res,
4125 };
4126
David Quigleyaa9c2662013-05-22 12:50:44 -04004127 args.bitmask = nfs4_bitmask(server, label);
4128
David Howells2b3de442006-08-22 20:06:09 -04004129 nfs_fattr_init(fattr);
4130
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004132 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 dprintk("NFS reply lookup: %d\n", status);
4134 return status;
4135}
4136
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004137static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004138{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004139 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004140 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004141 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4142 fattr->nlink = 2;
4143}
4144
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004145static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004146 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004147 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004148{
Trond Myklebust0688e642019-04-07 13:59:09 -04004149 struct nfs4_exception exception = {
4150 .interruptible = true,
4151 };
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004152 struct rpc_clnt *client = *clnt;
4153 int err;
4154 do {
David Quigley1775fd32013-05-22 12:50:42 -04004155 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004156 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004157 switch (err) {
4158 case -NFS4ERR_BADNAME:
4159 err = -ENOENT;
4160 goto out;
4161 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004162 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004163 if (err == -NFS4ERR_MOVED)
4164 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004165 goto out;
4166 case -NFS4ERR_WRONGSEC:
4167 err = -EPERM;
4168 if (client != *clnt)
4169 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004170 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004171 if (IS_ERR(client))
4172 return PTR_ERR(client);
4173
4174 exception.retry = 1;
4175 break;
4176 default:
4177 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4178 }
4179 } while (exception.retry);
4180
4181out:
4182 if (err == 0)
4183 *clnt = client;
4184 else if (client != *clnt)
4185 rpc_shutdown_client(client);
4186
4187 return err;
4188}
4189
Al Virobeffb8f2016-07-20 16:34:42 -04004190static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004191 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4192 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004194 int status;
4195 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004196
David Quigley1775fd32013-05-22 12:50:42 -04004197 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004198 if (client != NFS_CLIENT(dir)) {
4199 rpc_shutdown_client(client);
4200 nfs_fixup_secinfo_attributes(fattr);
4201 }
4202 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203}
4204
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004205struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004206nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004207 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4208{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004209 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004210 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004211
David Quigley1775fd32013-05-22 12:50:42 -04004212 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004213 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004214 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004215 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004216}
4217
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004218static int _nfs4_proc_lookupp(struct inode *inode,
4219 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4220 struct nfs4_label *label)
4221{
4222 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4223 struct nfs_server *server = NFS_SERVER(inode);
4224 int status;
4225 struct nfs4_lookupp_arg args = {
4226 .bitmask = server->attr_bitmask,
4227 .fh = NFS_FH(inode),
4228 };
4229 struct nfs4_lookupp_res res = {
4230 .server = server,
4231 .fattr = fattr,
4232 .label = label,
4233 .fh = fhandle,
4234 };
4235 struct rpc_message msg = {
4236 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4237 .rpc_argp = &args,
4238 .rpc_resp = &res,
4239 };
4240
4241 args.bitmask = nfs4_bitmask(server, label);
4242
4243 nfs_fattr_init(fattr);
4244
4245 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4246 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4247 &res.seq_res, 0);
4248 dprintk("NFS reply lookupp: %d\n", status);
4249 return status;
4250}
4251
4252static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4253 struct nfs_fattr *fattr, struct nfs4_label *label)
4254{
Trond Myklebust0688e642019-04-07 13:59:09 -04004255 struct nfs4_exception exception = {
4256 .interruptible = true,
4257 };
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004258 int err;
4259 do {
4260 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4261 trace_nfs4_lookupp(inode, err);
4262 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4263 &exception);
4264 } while (exception.retry);
4265 return err;
4266}
4267
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4269{
Trond Myklebust76b32992007-08-10 17:45:11 -04004270 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271 struct nfs4_accessargs args = {
4272 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004273 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004275 struct nfs4_accessres res = {
4276 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004277 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278 struct rpc_message msg = {
4279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4280 .rpc_argp = &args,
4281 .rpc_resp = &res,
4282 .rpc_cred = entry->cred,
4283 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004284 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285
Trond Myklebust7c672652018-06-04 15:00:53 -04004286 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004287 res.fattr = nfs_alloc_fattr();
4288 if (res.fattr == NULL)
4289 return -ENOMEM;
4290 args.bitmask = server->cache_consistency_bitmask;
4291 }
Bryan Schumaker7c513052011-03-24 17:12:24 +00004292 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004294 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004295 if (res.fattr)
4296 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004298 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004299 return status;
4300}
4301
4302static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4303{
Trond Myklebust0688e642019-04-07 13:59:09 -04004304 struct nfs4_exception exception = {
4305 .interruptible = true,
4306 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 int err;
4308 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004309 err = _nfs4_proc_access(inode, entry);
4310 trace_nfs4_access(inode, err);
4311 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 &exception);
4313 } while (exception.retry);
4314 return err;
4315}
4316
4317/*
4318 * TODO: For the time being, we don't try to get any attributes
4319 * along with any of the zero-copy operations READ, READDIR,
4320 * READLINK, WRITE.
4321 *
4322 * In the case of the first three, we want to put the GETATTR
4323 * after the read-type operation -- this is because it is hard
4324 * to predict the length of a GETATTR response in v4, and thus
4325 * align the READ data correctly. This means that the GETATTR
4326 * may end up partially falling into the page cache, and we should
4327 * shift it into the 'tail' of the xdr_buf before processing.
4328 * To do this efficiently, we need to know the total length
4329 * of data received, which doesn't seem to be available outside
4330 * of the RPC layer.
4331 *
4332 * In the case of WRITE, we also want to put the GETATTR after
4333 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004334 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 *
4336 * Both of these changes to the XDR layer would in fact be quite
4337 * minor, but I decided to leave them for a subsequent patch.
4338 */
4339static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4340 unsigned int pgbase, unsigned int pglen)
4341{
4342 struct nfs4_readlink args = {
4343 .fh = NFS_FH(inode),
4344 .pgbase = pgbase,
4345 .pglen = pglen,
4346 .pages = &page,
4347 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004348 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004349 struct rpc_message msg = {
4350 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4351 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004352 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 };
4354
Bryan Schumaker7c513052011-03-24 17:12:24 +00004355 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 -07004356}
4357
4358static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4359 unsigned int pgbase, unsigned int pglen)
4360{
Trond Myklebust0688e642019-04-07 13:59:09 -04004361 struct nfs4_exception exception = {
4362 .interruptible = true,
4363 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 int err;
4365 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004366 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4367 trace_nfs4_readlink(inode, err);
4368 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 &exception);
4370 } while (exception.retry);
4371 return err;
4372}
4373
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004375 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004377static int
4378nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004379 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004380{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004381 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004382 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004383 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004384 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004385 int status = 0;
4386
NeilBrown532d4de2016-10-13 15:26:47 +11004387 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004388 if (IS_ERR(ctx))
4389 return PTR_ERR(ctx);
4390
David Quigleyaa9c2662013-05-22 12:50:44 -04004391 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4392
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004393 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4394 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004395 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396 if (IS_ERR(state)) {
4397 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004398 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004400out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004401 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004402 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004403 return status;
4404}
4405
Trond Myklebust3c591172018-07-31 15:54:10 -04004406static int
4407_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004408{
Trond Myklebust16e42952005-10-27 22:12:44 -04004409 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004410 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004411 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004412 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004413 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004414 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004415 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004416 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004418 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4419 .rpc_argp = &args,
4420 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004421 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004422 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004423 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004424
Bryan Schumaker7c513052011-03-24 17:12:24 +00004425 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004426 if (status == 0) {
4427 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004428 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004429 /* Removing a directory decrements nlink in the parent */
4430 if (ftype == NF4DIR && dir->i_nlink > 2)
4431 nfs4_dec_nlink_locked(dir);
4432 spin_unlock(&dir->i_lock);
4433 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004434 return status;
4435}
4436
Trond Myklebust912678d2018-03-20 16:43:15 -04004437static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4438{
Trond Myklebust0688e642019-04-07 13:59:09 -04004439 struct nfs4_exception exception = {
4440 .interruptible = true,
4441 };
Trond Myklebust912678d2018-03-20 16:43:15 -04004442 struct inode *inode = d_inode(dentry);
4443 int err;
4444
Trond Myklebustc01d3642018-03-20 16:43:20 -04004445 if (inode) {
4446 if (inode->i_nlink == 1)
4447 nfs4_inode_return_delegation(inode);
4448 else
4449 nfs4_inode_make_writeable(inode);
4450 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004451 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004452 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004453 trace_nfs4_remove(dir, &dentry->d_name, err);
4454 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4455 &exception);
4456 } while (exception.retry);
4457 return err;
4458}
4459
4460static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004461{
Trond Myklebust0688e642019-04-07 13:59:09 -04004462 struct nfs4_exception exception = {
4463 .interruptible = true,
4464 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004466
Linus Torvalds1da177e2005-04-16 15:20:36 -07004467 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004468 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004469 trace_nfs4_remove(dir, name, err);
4470 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471 &exception);
4472 } while (exception.retry);
4473 return err;
4474}
4475
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004476static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4477 struct dentry *dentry,
4478 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004479{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004480 struct nfs_removeargs *args = msg->rpc_argp;
4481 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482
Trond Myklebust977fcc22018-03-20 16:43:17 -04004483 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004484 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004485 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004486
4487 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004488
4489 if (inode)
4490 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491}
4492
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004493static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4494{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004495 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004496 &data->args.seq_args,
4497 &data->res.seq_res,
4498 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499}
4500
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004501static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004503 struct nfs_unlinkdata *data = task->tk_calldata;
4504 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004505
Trond Myklebust14516c32010-07-31 14:29:06 -04004506 if (!nfs4_sequence_done(task, &res->seq_res))
4507 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004508 if (nfs4_async_handle_error(task, res->server, NULL,
4509 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004510 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004511 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004512 update_changeattr(dir, &res->cinfo,
4513 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004514 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515}
4516
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004517static void nfs4_proc_rename_setup(struct rpc_message *msg,
4518 struct dentry *old_dentry,
4519 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004520{
Jeff Laytond3d41522010-09-17 17:31:57 -04004521 struct nfs_renameargs *arg = msg->rpc_argp;
4522 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004523 struct inode *old_inode = d_inode(old_dentry);
4524 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004525
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004526 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004527 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004528 if (new_inode)
4529 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004530 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004531 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004532 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004533}
4534
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004535static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4536{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004537 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004538 &data->args.seq_args,
4539 &data->res.seq_res,
4540 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004541}
4542
4543static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4544 struct inode *new_dir)
4545{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004546 struct nfs_renamedata *data = task->tk_calldata;
4547 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004548
4549 if (!nfs4_sequence_done(task, &res->seq_res))
4550 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004551 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004552 return 0;
4553
Trond Myklebustc733c492017-01-11 12:32:26 -05004554 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004555 if (new_dir != old_dir) {
4556 /* Note: If we moved a directory, nlink will change */
4557 update_changeattr(old_dir, &res->old_cinfo,
4558 res->old_fattr->time_start,
4559 NFS_INO_INVALID_OTHER);
4560 update_changeattr(new_dir, &res->new_cinfo,
4561 res->new_fattr->time_start,
4562 NFS_INO_INVALID_OTHER);
4563 } else
4564 update_changeattr(old_dir, &res->old_cinfo,
4565 res->old_fattr->time_start,
4566 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004567 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004568 return 1;
4569}
4570
Al Virobeffb8f2016-07-20 16:34:42 -04004571static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004572{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004573 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004574 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004575 struct nfs4_link_arg arg = {
4576 .fh = NFS_FH(inode),
4577 .dir_fh = NFS_FH(dir),
4578 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004579 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004581 struct nfs4_link_res res = {
4582 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004583 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004584 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004585 struct rpc_message msg = {
4586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4587 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004588 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004589 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004590 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591
Trond Myklebust136f2622010-04-16 16:22:49 -04004592 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004593 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004594 goto out;
4595
David Quigley14c43f72013-05-22 12:50:43 -04004596 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4597 if (IS_ERR(res.label)) {
4598 status = PTR_ERR(res.label);
4599 goto out;
4600 }
4601
Trond Myklebustc01d3642018-03-20 16:43:20 -04004602 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004603 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004604
Bryan Schumaker7c513052011-03-24 17:12:24 +00004605 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004606 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004607 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004608 status = nfs_post_op_update_inode(inode, res.fattr);
4609 if (!status)
4610 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004611 }
David Quigley14c43f72013-05-22 12:50:43 -04004612
4613
4614 nfs4_label_free(res.label);
4615
Trond Myklebust136f2622010-04-16 16:22:49 -04004616out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004617 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 return status;
4619}
4620
Al Virobeffb8f2016-07-20 16:34:42 -04004621static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622{
Trond Myklebust0688e642019-04-07 13:59:09 -04004623 struct nfs4_exception exception = {
4624 .interruptible = true,
4625 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 int err;
4627 do {
4628 err = nfs4_handle_exception(NFS_SERVER(inode),
4629 _nfs4_proc_link(inode, dir, name),
4630 &exception);
4631 } while (exception.retry);
4632 return err;
4633}
4634
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004635struct nfs4_createdata {
4636 struct rpc_message msg;
4637 struct nfs4_create_arg arg;
4638 struct nfs4_create_res res;
4639 struct nfs_fh fh;
4640 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004641 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004642};
4643
4644static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004645 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004646{
4647 struct nfs4_createdata *data;
4648
4649 data = kzalloc(sizeof(*data), GFP_KERNEL);
4650 if (data != NULL) {
4651 struct nfs_server *server = NFS_SERVER(dir);
4652
David Quigley14c43f72013-05-22 12:50:43 -04004653 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4654 if (IS_ERR(data->label))
4655 goto out_free;
4656
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004657 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4658 data->msg.rpc_argp = &data->arg;
4659 data->msg.rpc_resp = &data->res;
4660 data->arg.dir_fh = NFS_FH(dir);
4661 data->arg.server = server;
4662 data->arg.name = name;
4663 data->arg.attrs = sattr;
4664 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004665 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004666 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004667 data->res.server = server;
4668 data->res.fh = &data->fh;
4669 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004670 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004671 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004672 }
4673 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004674out_free:
4675 kfree(data);
4676 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004677}
4678
4679static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4680{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004681 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004682 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004683 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004684 spin_lock(&dir->i_lock);
4685 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004686 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004687 /* Creating a directory bumps nlink in the parent */
4688 if (data->arg.ftype == NF4DIR)
4689 nfs4_inc_nlink_locked(dir);
4690 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004691 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004692 }
4693 return status;
4694}
4695
4696static void nfs4_free_createdata(struct nfs4_createdata *data)
4697{
David Quigley14c43f72013-05-22 12:50:43 -04004698 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004699 kfree(data);
4700}
4701
Chuck Lever4f390c12006-08-22 20:06:22 -04004702static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004703 struct page *page, unsigned int len, struct iattr *sattr,
4704 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004705{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004706 struct nfs4_createdata *data;
4707 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708
Chuck Lever94a6d752006-08-22 20:06:23 -04004709 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004710 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004711
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004712 status = -ENOMEM;
4713 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4714 if (data == NULL)
4715 goto out;
4716
4717 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4718 data->arg.u.symlink.pages = &page;
4719 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004720 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004722 status = nfs4_do_create(dir, dentry, data);
4723
4724 nfs4_free_createdata(data);
4725out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004726 return status;
4727}
4728
Chuck Lever4f390c12006-08-22 20:06:22 -04004729static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004730 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004731{
Trond Myklebust0688e642019-04-07 13:59:09 -04004732 struct nfs4_exception exception = {
4733 .interruptible = true,
4734 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004735 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004736 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004737
4738 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4739
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004741 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4742 trace_nfs4_symlink(dir, &dentry->d_name, err);
4743 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744 &exception);
4745 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004746
4747 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004748 return err;
4749}
4750
4751static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004752 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004753{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004754 struct nfs4_createdata *data;
4755 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004756
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004757 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4758 if (data == NULL)
4759 goto out;
4760
David Quigley1775fd32013-05-22 12:50:42 -04004761 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004762 status = nfs4_do_create(dir, dentry, data);
4763
4764 nfs4_free_createdata(data);
4765out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 return status;
4767}
4768
4769static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4770 struct iattr *sattr)
4771{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004772 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004773 struct nfs4_exception exception = {
4774 .interruptible = true,
4775 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004776 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004778
David Quigleyaa9c2662013-05-22 12:50:44 -04004779 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4780
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004781 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4782 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004783 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004784 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4785 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4786 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004787 &exception);
4788 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004789 nfs4_label_release_security(label);
4790
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 return err;
4792}
4793
NeilBrown684f39b2018-12-03 11:30:30 +11004794static int _nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004795 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796{
David Howells2b0143b2015-03-17 22:25:59 +00004797 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004798 struct nfs4_readdir_arg args = {
4799 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004800 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004801 .pgbase = 0,
4802 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004803 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004804 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805 };
4806 struct nfs4_readdir_res res;
4807 struct rpc_message msg = {
4808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4809 .rpc_argp = &args,
4810 .rpc_resp = &res,
4811 .rpc_cred = cred,
4812 };
4813 int status;
4814
Al Viro6de14722013-09-16 10:53:17 -04004815 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4816 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004817 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004818 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004819 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004820 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 -05004821 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004822 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004823 status += args.pgbase;
4824 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004825
4826 nfs_invalidate_atime(dir);
4827
Harvey Harrison3110ff82008-05-02 13:42:44 -07004828 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829 return status;
4830}
4831
NeilBrown684f39b2018-12-03 11:30:30 +11004832static int nfs4_proc_readdir(struct dentry *dentry, const struct cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004833 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
Trond Myklebust0688e642019-04-07 13:59:09 -04004835 struct nfs4_exception exception = {
4836 .interruptible = true,
4837 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004838 int err;
4839 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004840 err = _nfs4_proc_readdir(dentry, cred, cookie,
4841 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004842 trace_nfs4_readdir(d_inode(dentry), err);
4843 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004844 &exception);
4845 } while (exception.retry);
4846 return err;
4847}
4848
4849static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004850 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004851{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004852 struct nfs4_createdata *data;
4853 int mode = sattr->ia_mode;
4854 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004856 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4857 if (data == NULL)
4858 goto out;
4859
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004861 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004862 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004863 data->arg.ftype = NF4BLK;
4864 data->arg.u.device.specdata1 = MAJOR(rdev);
4865 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004866 }
4867 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004868 data->arg.ftype = NF4CHR;
4869 data->arg.u.device.specdata1 = MAJOR(rdev);
4870 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004871 } else if (!S_ISSOCK(mode)) {
4872 status = -EINVAL;
4873 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004874 }
David Quigley1775fd32013-05-22 12:50:42 -04004875
David Quigleyaa9c2662013-05-22 12:50:44 -04004876 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004877 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004878out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004879 nfs4_free_createdata(data);
4880out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881 return status;
4882}
4883
4884static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4885 struct iattr *sattr, dev_t rdev)
4886{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004887 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust0688e642019-04-07 13:59:09 -04004888 struct nfs4_exception exception = {
4889 .interruptible = true,
4890 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004891 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004893
David Quigleyaa9c2662013-05-22 12:50:44 -04004894 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4895
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004896 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4897 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004898 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004899 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4900 trace_nfs4_mknod(dir, &dentry->d_name, err);
4901 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004902 &exception);
4903 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004904
4905 nfs4_label_release_security(label);
4906
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 return err;
4908}
4909
4910static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4911 struct nfs_fsstat *fsstat)
4912{
4913 struct nfs4_statfs_arg args = {
4914 .fh = fhandle,
4915 .bitmask = server->attr_bitmask,
4916 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004917 struct nfs4_statfs_res res = {
4918 .fsstat = fsstat,
4919 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920 struct rpc_message msg = {
4921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4922 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004923 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004924 };
4925
Trond Myklebust0e574af2005-10-27 22:12:38 -04004926 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004927 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004928}
4929
4930static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4931{
Trond Myklebust0688e642019-04-07 13:59:09 -04004932 struct nfs4_exception exception = {
4933 .interruptible = true,
4934 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935 int err;
4936 do {
4937 err = nfs4_handle_exception(server,
4938 _nfs4_proc_statfs(server, fhandle, fsstat),
4939 &exception);
4940 } while (exception.retry);
4941 return err;
4942}
4943
4944static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4945 struct nfs_fsinfo *fsinfo)
4946{
4947 struct nfs4_fsinfo_arg args = {
4948 .fh = fhandle,
4949 .bitmask = server->attr_bitmask,
4950 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004951 struct nfs4_fsinfo_res res = {
4952 .fsinfo = fsinfo,
4953 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 struct rpc_message msg = {
4955 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4956 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004957 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004958 };
4959
Bryan Schumaker7c513052011-03-24 17:12:24 +00004960 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961}
4962
4963static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4964{
Trond Myklebust0688e642019-04-07 13:59:09 -04004965 struct nfs4_exception exception = {
4966 .interruptible = true,
4967 };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004968 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969 int err;
4970
4971 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004972 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004973 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004974 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004975 nfs4_set_lease_period(server->nfs_client,
4976 fsinfo->lease_time * HZ,
4977 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004978 break;
4979 }
4980 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 } while (exception.retry);
4982 return err;
4983}
4984
4985static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4986{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004987 int error;
4988
Trond Myklebust0e574af2005-10-27 22:12:38 -04004989 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004990 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004991 if (error == 0) {
4992 /* block layout checks this! */
4993 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004994 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004995 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004996
4997 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004998}
4999
5000static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5001 struct nfs_pathconf *pathconf)
5002{
5003 struct nfs4_pathconf_arg args = {
5004 .fh = fhandle,
5005 .bitmask = server->attr_bitmask,
5006 };
Benny Halevyd45b2982009-04-01 09:21:58 -04005007 struct nfs4_pathconf_res res = {
5008 .pathconf = pathconf,
5009 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005010 struct rpc_message msg = {
5011 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
5012 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04005013 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014 };
5015
5016 /* None of the pathconf attributes are mandatory to implement */
5017 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
5018 memset(pathconf, 0, sizeof(*pathconf));
5019 return 0;
5020 }
5021
Trond Myklebust0e574af2005-10-27 22:12:38 -04005022 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005023 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005024}
5025
5026static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
5027 struct nfs_pathconf *pathconf)
5028{
Trond Myklebust0688e642019-04-07 13:59:09 -04005029 struct nfs4_exception exception = {
5030 .interruptible = true,
5031 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032 int err;
5033
5034 do {
5035 err = nfs4_handle_exception(server,
5036 _nfs4_proc_pathconf(server, fhandle, pathconf),
5037 &exception);
5038 } while (exception.retry);
5039 return err;
5040}
5041
Trond Myklebust5521abf2013-03-16 20:54:34 -04005042int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04005043 const struct nfs_open_context *ctx,
5044 const struct nfs_lock_context *l_ctx,
5045 fmode_t fmode)
5046{
NeilBrown17393472016-10-13 15:26:47 +11005047 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04005048}
5049EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
5050
Trond Myklebust5521abf2013-03-16 20:54:34 -04005051static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
5052 const struct nfs_open_context *ctx,
5053 const struct nfs_lock_context *l_ctx,
5054 fmode_t fmode)
5055{
5056 nfs4_stateid current_stateid;
5057
Trond Myklebuste1253be2014-03-05 08:44:23 -05005058 /* If the current stateid represents a lost lock, then exit */
5059 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
5060 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04005061 return nfs4_stateid_match(stateid, &current_stateid);
5062}
5063
5064static bool nfs4_error_stateid_expired(int err)
5065{
5066 switch (err) {
5067 case -NFS4ERR_DELEG_REVOKED:
5068 case -NFS4ERR_ADMIN_REVOKED:
5069 case -NFS4ERR_BAD_STATEID:
5070 case -NFS4ERR_STALE_STATEID:
5071 case -NFS4ERR_OLD_STATEID:
5072 case -NFS4ERR_OPENMODE:
5073 case -NFS4ERR_EXPIRED:
5074 return true;
5075 }
5076 return false;
5077}
5078
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005079static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005081 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005083 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005084 if (task->tk_status < 0) {
5085 struct nfs4_exception exception = {
5086 .inode = hdr->inode,
5087 .state = hdr->args.context->state,
5088 .stateid = &hdr->args.stateid,
5089 };
5090 task->tk_status = nfs4_async_handle_exception(task,
5091 server, task->tk_status, &exception);
5092 if (exception.retry) {
5093 rpc_restart_call_prepare(task);
5094 return -EAGAIN;
5095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 }
Trond Myklebust8850df92007-09-28 17:20:07 -04005097
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005099 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05005100 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101}
5102
Trond Myklebust5521abf2013-03-16 20:54:34 -04005103static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005104 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005105{
5106
5107 if (!nfs4_error_stateid_expired(task->tk_status) ||
5108 nfs4_stateid_is_current(&args->stateid,
5109 args->context,
5110 args->lock_context,
5111 FMODE_READ))
5112 return false;
5113 rpc_restart_call_prepare(task);
5114 return true;
5115}
5116
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005117static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005118{
5119
5120 dprintk("--> %s\n", __func__);
5121
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005122 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005123 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005124 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005125 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005126 if (task->tk_status > 0)
5127 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005128 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5129 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005130}
5131
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005132static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5133 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005135 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005136 if (!hdr->pgio_done_cb)
5137 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005138 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005139 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005140}
5141
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005142static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5143 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005144{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005145 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005146 &hdr->args.seq_args,
5147 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005148 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005149 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005150 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5151 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005152 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005153 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005154 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005155 return -EIO;
5156 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005157}
5158
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005159static int nfs4_write_done_cb(struct rpc_task *task,
5160 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005161{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005162 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005163
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005164 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005165 if (task->tk_status < 0) {
5166 struct nfs4_exception exception = {
5167 .inode = hdr->inode,
5168 .state = hdr->args.context->state,
5169 .stateid = &hdr->args.stateid,
5170 };
5171 task->tk_status = nfs4_async_handle_exception(task,
5172 NFS_SERVER(inode), task->tk_status,
5173 &exception);
5174 if (exception.retry) {
5175 rpc_restart_call_prepare(task);
5176 return -EAGAIN;
5177 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005178 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005179 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005180 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005181 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005182 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005183 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005184}
5185
Trond Myklebust5521abf2013-03-16 20:54:34 -04005186static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005187 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005188{
5189
5190 if (!nfs4_error_stateid_expired(task->tk_status) ||
5191 nfs4_stateid_is_current(&args->stateid,
5192 args->context,
5193 args->lock_context,
5194 FMODE_WRITE))
5195 return false;
5196 rpc_restart_call_prepare(task);
5197 return true;
5198}
5199
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005200static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005201{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005202 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005203 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005204 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005205 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005206 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5207 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005208}
5209
Trond Myklebust5a37f852012-04-28 14:55:16 -04005210static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005211bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005212{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005213 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005214 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005215 return false;
5216 /* Otherwise, request attributes if and only if we don't hold
5217 * a delegation
5218 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005219 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005220}
Fred Isamana69aef12011-03-03 15:13:47 +00005221
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005222static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005223 struct rpc_message *msg,
5224 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005226 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005227
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005228 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5229 hdr->args.bitmask = NULL;
5230 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005231 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005232 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005233
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005234 if (!hdr->pgio_done_cb)
5235 hdr->pgio_done_cb = nfs4_write_done_cb;
5236 hdr->res.server = server;
5237 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005238
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005239 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005240 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005241 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242}
5243
Fred Isaman0b7c0152012-04-20 14:47:39 -04005244static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5245{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005246 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005247 &data->args.seq_args,
5248 &data->res.seq_res,
5249 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005250}
5251
Fred Isaman0b7c0152012-04-20 14:47:39 -04005252static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005253{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005254 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005255
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005256 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005257 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5258 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005259 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005260 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005261 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005262 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005263}
5264
Fred Isaman0b7c0152012-04-20 14:47:39 -04005265static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005266{
5267 if (!nfs4_sequence_done(task, &data->res.seq_res))
5268 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005269 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005270}
5271
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005272static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5273 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005274{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005275 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005276
Fred Isaman0b7c0152012-04-20 14:47:39 -04005277 if (data->commit_done_cb == NULL)
5278 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005279 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005280 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005281 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005282 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005283}
5284
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005285static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5286 struct nfs_commitres *res)
5287{
5288 struct inode *dst_inode = file_inode(dst);
5289 struct nfs_server *server = NFS_SERVER(dst_inode);
5290 struct rpc_message msg = {
5291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5292 .rpc_argp = args,
5293 .rpc_resp = res,
5294 };
5295
5296 args->fh = NFS_FH(dst_inode);
5297 return nfs4_call_sync(server->client, server, &msg,
5298 &args->seq_args, &res->seq_res, 1);
5299}
5300
5301int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5302{
5303 struct nfs_commitargs args = {
5304 .offset = offset,
5305 .count = count,
5306 };
5307 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5308 struct nfs4_exception exception = { };
5309 int status;
5310
5311 do {
5312 status = _nfs4_proc_commit(dst, &args, res);
5313 status = nfs4_handle_exception(dst_server, status, &exception);
5314 } while (exception.retry);
5315
5316 return status;
5317}
5318
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005319struct nfs4_renewdata {
5320 struct nfs_client *client;
5321 unsigned long timestamp;
5322};
5323
Linus Torvalds1da177e2005-04-16 15:20:36 -07005324/*
5325 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5326 * standalone procedure for queueing an asynchronous RENEW.
5327 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005328static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005329{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005330 struct nfs4_renewdata *data = calldata;
5331 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005332
Elena Reshetova212bf412017-10-20 12:53:38 +03005333 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005334 nfs4_schedule_state_renewal(clp);
5335 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005336 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005337}
5338
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005339static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005340{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005341 struct nfs4_renewdata *data = calldata;
5342 struct nfs_client *clp = data->client;
5343 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005344
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005345 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005346 switch (task->tk_status) {
5347 case 0:
5348 break;
5349 case -NFS4ERR_LEASE_MOVED:
5350 nfs4_schedule_lease_moved_recovery(clp);
5351 break;
5352 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005353 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005354 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5355 return;
5356 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005357 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005358 return;
5359 }
5360 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005361 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005362 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005363}
5364
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005365static const struct rpc_call_ops nfs4_renew_ops = {
5366 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005367 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005368};
5369
NeilBrowna52458b2018-12-03 11:30:31 +11005370static int nfs4_proc_async_renew(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005371{
5372 struct rpc_message msg = {
5373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5374 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005375 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005376 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005377 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005378
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005379 if (renew_flags == 0)
5380 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005381 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005382 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005383 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005384 if (data == NULL) {
5385 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005386 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005387 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005388 data->client = clp;
5389 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005390 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005391 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005392}
5393
NeilBrowna52458b2018-12-03 11:30:31 +11005394static int nfs4_proc_renew(struct nfs_client *clp, const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005395{
5396 struct rpc_message msg = {
5397 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5398 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005399 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005400 };
5401 unsigned long now = jiffies;
5402 int status;
5403
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005404 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005405 if (status < 0)
5406 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005407 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005408 return 0;
5409}
5410
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005411static inline int nfs4_server_supports_acls(struct nfs_server *server)
5412{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005413 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005414}
5415
Trond Myklebust21f498c2012-08-24 10:59:25 -04005416/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5417 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005418 * the stack.
5419 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005420#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005421
Neil Hormane9e3d722011-03-04 19:26:03 -05005422static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005423 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005424{
5425 struct page *newpage, **spages;
5426 int rc = 0;
5427 size_t len;
5428 spages = pages;
5429
5430 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005431 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005432 newpage = alloc_page(GFP_KERNEL);
5433
5434 if (newpage == NULL)
5435 goto unwind;
5436 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005437 buf += len;
5438 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005439 *pages++ = newpage;
5440 rc++;
5441 } while (buflen != 0);
5442
5443 return rc;
5444
5445unwind:
5446 for(; rc > 0; rc--)
5447 __free_page(spages[rc-1]);
5448 return -ENOMEM;
5449}
5450
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005451struct nfs4_cached_acl {
5452 int cached;
5453 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005454 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005455};
5456
5457static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005458{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005459 struct nfs_inode *nfsi = NFS_I(inode);
5460
5461 spin_lock(&inode->i_lock);
5462 kfree(nfsi->nfs4_acl);
5463 nfsi->nfs4_acl = acl;
5464 spin_unlock(&inode->i_lock);
5465}
5466
5467static void nfs4_zap_acl_attr(struct inode *inode)
5468{
5469 nfs4_set_cached_acl(inode, NULL);
5470}
5471
5472static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5473{
5474 struct nfs_inode *nfsi = NFS_I(inode);
5475 struct nfs4_cached_acl *acl;
5476 int ret = -ENOENT;
5477
5478 spin_lock(&inode->i_lock);
5479 acl = nfsi->nfs4_acl;
5480 if (acl == NULL)
5481 goto out;
5482 if (buf == NULL) /* user is just asking for length */
5483 goto out_len;
5484 if (acl->cached == 0)
5485 goto out;
5486 ret = -ERANGE; /* see getxattr(2) man page */
5487 if (acl->len > buflen)
5488 goto out;
5489 memcpy(buf, acl->data, acl->len);
5490out_len:
5491 ret = acl->len;
5492out:
5493 spin_unlock(&inode->i_lock);
5494 return ret;
5495}
5496
Sachin Prabhu5794d212012-04-17 14:36:40 +01005497static 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 +00005498{
5499 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005500 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005501
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005502 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005503 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005504 if (acl == NULL)
5505 goto out;
5506 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005507 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005508 } else {
5509 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5510 if (acl == NULL)
5511 goto out;
5512 acl->cached = 0;
5513 }
5514 acl->len = acl_len;
5515out:
5516 nfs4_set_cached_acl(inode, acl);
5517}
5518
Andy Adamsonbf118a32011-12-07 11:55:27 -05005519/*
5520 * The getxattr API returns the required buffer length when called with a
5521 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5522 * the required buf. On a NULL buf, we send a page of data to the server
5523 * guessing that the ACL request can be serviced by a page. If so, we cache
5524 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5525 * the cache. If not so, we throw away the page, and cache the required
5526 * length. The next getxattr call will then produce another round trip to
5527 * the server, this time with the input buf of the required size.
5528 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005529static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005530{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005531 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005532 struct nfs_getaclargs args = {
5533 .fh = NFS_FH(inode),
5534 .acl_pages = pages,
5535 .acl_len = buflen,
5536 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005537 struct nfs_getaclres res = {
5538 .acl_len = buflen,
5539 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005540 struct rpc_message msg = {
5541 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5542 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005543 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005544 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005545 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005546 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005547
Trond Myklebust21f498c2012-08-24 10:59:25 -04005548 if (npages > ARRAY_SIZE(pages))
5549 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005550
Andy Adamsonbf118a32011-12-07 11:55:27 -05005551 for (i = 0; i < npages; i++) {
5552 pages[i] = alloc_page(GFP_KERNEL);
5553 if (!pages[i])
5554 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005555 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005556
5557 /* for decoding across pages */
5558 res.acl_scratch = alloc_page(GFP_KERNEL);
5559 if (!res.acl_scratch)
5560 goto out_free;
5561
Andy Adamsonbf118a32011-12-07 11:55:27 -05005562 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005563
Peng Taode040be2012-01-10 22:42:47 +08005564 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005565 __func__, buf, buflen, npages, args.acl_len);
5566 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5567 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005568 if (ret)
5569 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005570
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005571 /* Handle the case where the passed-in buffer is too short */
5572 if (res.acl_flags & NFS4_ACL_TRUNC) {
5573 /* Did the user only issue a request for the acl length? */
5574 if (buf == NULL)
5575 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005576 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005577 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005578 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005579 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005580 if (buf) {
5581 if (res.acl_len > buflen) {
5582 ret = -ERANGE;
5583 goto out_free;
5584 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005585 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005586 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005587out_ok:
5588 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005589out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005590 for (i = 0; i < npages; i++)
5591 if (pages[i])
5592 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005593 if (res.acl_scratch)
5594 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005595 return ret;
5596}
5597
Trond Myklebust16b42892006-08-24 12:27:15 -04005598static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5599{
Trond Myklebust0688e642019-04-07 13:59:09 -04005600 struct nfs4_exception exception = {
5601 .interruptible = true,
5602 };
Trond Myklebust16b42892006-08-24 12:27:15 -04005603 ssize_t ret;
5604 do {
5605 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005606 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005607 if (ret >= 0)
5608 break;
5609 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5610 } while (exception.retry);
5611 return ret;
5612}
5613
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005614static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5615{
5616 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005617 int ret;
5618
5619 if (!nfs4_server_supports_acls(server))
5620 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005621 ret = nfs_revalidate_inode(server, inode);
5622 if (ret < 0)
5623 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005624 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5625 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005626 ret = nfs4_read_cached_acl(inode, buf, buflen);
5627 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005628 /* -ENOENT is returned if there is no ACL or if there is an ACL
5629 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005630 return ret;
5631 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005632}
5633
Trond Myklebust16b42892006-08-24 12:27:15 -04005634static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005635{
5636 struct nfs_server *server = NFS_SERVER(inode);
5637 struct page *pages[NFS4ACL_MAXPAGES];
5638 struct nfs_setaclargs arg = {
5639 .fh = NFS_FH(inode),
5640 .acl_pages = pages,
5641 .acl_len = buflen,
5642 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005643 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005644 struct rpc_message msg = {
5645 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5646 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005647 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005648 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005649 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005650 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005651
5652 if (!nfs4_server_supports_acls(server))
5653 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005654 if (npages > ARRAY_SIZE(pages))
5655 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005656 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005657 if (i < 0)
5658 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005659 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005660 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005661
5662 /*
5663 * Free each page after tx, so the only ref left is
5664 * held by the network stack
5665 */
5666 for (; i > 0; i--)
5667 put_page(pages[i-1]);
5668
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005669 /*
5670 * Acl update can result in inode attribute update.
5671 * so mark the attribute cache invalid.
5672 */
5673 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005674 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005675 | NFS_INO_INVALID_CTIME
5676 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005677 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005678 nfs_access_zap_cache(inode);
5679 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005680 return ret;
5681}
5682
Trond Myklebust16b42892006-08-24 12:27:15 -04005683static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5684{
5685 struct nfs4_exception exception = { };
5686 int err;
5687 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005688 err = __nfs4_proc_set_acl(inode, buf, buflen);
5689 trace_nfs4_set_acl(inode, err);
5690 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005691 &exception);
5692 } while (exception.retry);
5693 return err;
5694}
5695
David Quigleyaa9c2662013-05-22 12:50:44 -04005696#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5697static int _nfs4_get_security_label(struct inode *inode, void *buf,
5698 size_t buflen)
5699{
5700 struct nfs_server *server = NFS_SERVER(inode);
5701 struct nfs_fattr fattr;
5702 struct nfs4_label label = {0, 0, buflen, buf};
5703
5704 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005705 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005706 .fh = NFS_FH(inode),
5707 .bitmask = bitmask,
5708 };
5709 struct nfs4_getattr_res res = {
5710 .fattr = &fattr,
5711 .label = &label,
5712 .server = server,
5713 };
5714 struct rpc_message msg = {
5715 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005716 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005717 .rpc_resp = &res,
5718 };
5719 int ret;
5720
5721 nfs_fattr_init(&fattr);
5722
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005723 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005724 if (ret)
5725 return ret;
5726 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5727 return -ENOENT;
5728 if (buflen < label.len)
5729 return -ERANGE;
5730 return 0;
5731}
5732
5733static int nfs4_get_security_label(struct inode *inode, void *buf,
5734 size_t buflen)
5735{
Trond Myklebust0688e642019-04-07 13:59:09 -04005736 struct nfs4_exception exception = {
5737 .interruptible = true,
5738 };
David Quigleyaa9c2662013-05-22 12:50:44 -04005739 int err;
5740
5741 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5742 return -EOPNOTSUPP;
5743
5744 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005745 err = _nfs4_get_security_label(inode, buf, buflen);
5746 trace_nfs4_get_security_label(inode, err);
5747 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005748 &exception);
5749 } while (exception.retry);
5750 return err;
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
5759 struct iattr sattr = {0};
5760 struct nfs_server *server = NFS_SERVER(inode);
5761 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005762 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005763 .fh = NFS_FH(inode),
5764 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005765 .server = server,
5766 .bitmask = bitmask,
5767 .label = ilabel,
5768 };
5769 struct nfs_setattrres res = {
5770 .fattr = fattr,
5771 .label = olabel,
5772 .server = server,
5773 };
5774 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005775 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5776 .rpc_argp = &arg,
5777 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005778 };
5779 int status;
5780
Jeff Layton12207f62013-11-01 10:49:32 -04005781 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005782
Jeff Layton12207f62013-11-01 10:49:32 -04005783 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005784 if (status)
5785 dprintk("%s failed: %d\n", __func__, status);
5786
5787 return status;
5788}
5789
5790static int nfs4_do_set_security_label(struct inode *inode,
5791 struct nfs4_label *ilabel,
5792 struct nfs_fattr *fattr,
5793 struct nfs4_label *olabel)
5794{
5795 struct nfs4_exception exception = { };
5796 int err;
5797
5798 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005799 err = _nfs4_do_set_security_label(inode, ilabel,
5800 fattr, olabel);
5801 trace_nfs4_set_security_label(inode, err);
5802 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005803 &exception);
5804 } while (exception.retry);
5805 return err;
5806}
5807
5808static int
Al Viro59301222016-05-27 10:19:30 -04005809nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005810{
5811 struct nfs4_label ilabel, *olabel = NULL;
5812 struct nfs_fattr fattr;
David Quigleyaa9c2662013-05-22 12:50:44 -04005813 int status;
5814
5815 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5816 return -EOPNOTSUPP;
5817
5818 nfs_fattr_init(&fattr);
5819
5820 ilabel.pi = 0;
5821 ilabel.lfs = 0;
5822 ilabel.label = (char *)buf;
5823 ilabel.len = buflen;
5824
David Quigleyaa9c2662013-05-22 12:50:44 -04005825 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5826 if (IS_ERR(olabel)) {
5827 status = -PTR_ERR(olabel);
5828 goto out;
5829 }
5830
5831 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5832 if (status == 0)
5833 nfs_setsecurity(inode, &fattr, olabel);
5834
5835 nfs4_label_free(olabel);
5836out:
David Quigleyaa9c2662013-05-22 12:50:44 -04005837 return status;
5838}
5839#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5840
5841
Chuck Leverf0920752012-05-21 22:45:41 -04005842static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5843 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005844{
5845 __be32 verf[2];
5846
Chuck Lever2c820d92012-05-21 22:45:33 -04005847 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5848 /* An impossible timestamp guarantees this value
5849 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005850 verf[0] = cpu_to_be32(U32_MAX);
5851 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005852 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005853 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005854 u64 ns = ktime_to_ns(nn->boot_time);
5855
5856 verf[0] = cpu_to_be32(ns >> 32);
5857 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005858 }
Chuck Levercd937102012-03-02 17:14:31 -05005859 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5860}
5861
Jeff Laytona3192682015-06-09 19:43:59 -04005862static int
5863nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005864{
Jeff Laytona3192682015-06-09 19:43:59 -04005865 size_t len;
5866 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005867
Trond Myklebustceb3a162015-01-03 15:16:04 -05005868 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005869 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005870
Jeff Laytona3192682015-06-09 19:43:59 -04005871 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005872 len = 14 +
5873 strlen(clp->cl_rpcclient->cl_nodename) +
5874 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005875 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005876 1;
5877 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005878 if (nfs4_client_id_uniquifier[0] != '\0')
5879 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005880 if (len > NFS4_OPAQUE_LIMIT + 1)
5881 return -EINVAL;
5882
5883 /*
5884 * Since this string is allocated at mount time, and held until the
5885 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5886 * about a memory-reclaim deadlock.
5887 */
5888 str = kmalloc(len, GFP_KERNEL);
5889 if (!str)
5890 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005891
Chuck Levere984a552012-09-14 17:24:21 -04005892 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005893 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005894 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005895 clp->cl_rpcclient->cl_nodename,
5896 nfs4_client_id_uniquifier,
5897 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005898 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005899 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005900 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005901 clp->cl_rpcclient->cl_nodename,
5902 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005903 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005904 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005905
Jeff Laytona3192682015-06-09 19:43:59 -04005906 clp->cl_owner_id = str;
5907 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005908}
5909
Jeff Layton873e3852015-06-09 19:44:00 -04005910static int
5911nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005912{
Jeff Layton873e3852015-06-09 19:44:00 -04005913 size_t len;
5914 char *str;
5915
5916 len = 10 + 10 + 1 + 10 + 1 +
5917 strlen(nfs4_client_id_uniquifier) + 1 +
5918 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5919
5920 if (len > NFS4_OPAQUE_LIMIT + 1)
5921 return -EINVAL;
5922
5923 /*
5924 * Since this string is allocated at mount time, and held until the
5925 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5926 * about a memory-reclaim deadlock.
5927 */
5928 str = kmalloc(len, GFP_KERNEL);
5929 if (!str)
5930 return -ENOMEM;
5931
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005932 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005933 clp->rpc_ops->version, clp->cl_minorversion,
5934 nfs4_client_id_uniquifier,
5935 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005936 clp->cl_owner_id = str;
5937 return 0;
5938}
5939
5940static int
5941nfs4_init_uniform_client_string(struct nfs_client *clp)
5942{
Jeff Layton873e3852015-06-09 19:44:00 -04005943 size_t len;
5944 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005945
5946 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005947 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005948
5949 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005950 return nfs4_init_uniquifier_client_string(clp);
5951
5952 len = 10 + 10 + 1 + 10 + 1 +
5953 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5954
5955 if (len > NFS4_OPAQUE_LIMIT + 1)
5956 return -EINVAL;
5957
5958 /*
5959 * Since this string is allocated at mount time, and held until the
5960 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5961 * about a memory-reclaim deadlock.
5962 */
5963 str = kmalloc(len, GFP_KERNEL);
5964 if (!str)
5965 return -ENOMEM;
5966
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005967 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005968 clp->rpc_ops->version, clp->cl_minorversion,
5969 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005970 clp->cl_owner_id = str;
5971 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005972}
5973
Chuck Lever706cb8d2014-03-12 12:51:47 -04005974/*
5975 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5976 * services. Advertise one based on the address family of the
5977 * clientaddr.
5978 */
5979static unsigned int
5980nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5981{
5982 if (strchr(clp->cl_ipaddr, ':') != NULL)
5983 return scnprintf(buf, len, "tcp6");
5984 else
5985 return scnprintf(buf, len, "tcp");
5986}
5987
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005988static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5989{
5990 struct nfs4_setclientid *sc = calldata;
5991
5992 if (task->tk_status == 0)
5993 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5994}
5995
5996static const struct rpc_call_ops nfs4_setclientid_ops = {
5997 .rpc_call_done = nfs4_setclientid_done,
5998};
5999
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006000/**
6001 * nfs4_proc_setclientid - Negotiate client ID
6002 * @clp: state data structure
6003 * @program: RPC program for NFSv4 callback service
6004 * @port: IP port number for NFS4 callback service
NeilBrowna52458b2018-12-03 11:30:31 +11006005 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006006 * @res: where to place the result
6007 *
6008 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6009 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006010int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
NeilBrowna52458b2018-12-03 11:30:31 +11006011 unsigned short port, const struct cred *cred,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006012 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006013{
6014 nfs4_verifier sc_verifier;
6015 struct nfs4_setclientid setclientid = {
6016 .sc_verifier = &sc_verifier,
6017 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006018 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019 };
6020 struct rpc_message msg = {
6021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
6022 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006023 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006024 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006026 struct rpc_task_setup task_setup_data = {
6027 .rpc_client = clp->cl_rpcclient,
6028 .rpc_message = &msg,
6029 .callback_ops = &nfs4_setclientid_ops,
6030 .callback_data = &setclientid,
NeilBrown5a0c2572019-05-30 10:41:28 +10006031 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006032 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006033 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006034
Chuck Leverde734832012-07-11 16:30:50 -04006035 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04006036 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04006037
6038 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
6039 status = nfs4_init_uniform_client_string(clp);
6040 else
Jeff Laytona3192682015-06-09 19:43:59 -04006041 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04006042
6043 if (status)
6044 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04006045
Chuck Leverde734832012-07-11 16:30:50 -04006046 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04006047 setclientid.sc_netid_len =
6048 nfs4_init_callback_netid(clp,
6049 setclientid.sc_netid,
6050 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04006051 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05006052 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07006053 clp->cl_ipaddr, port >> 8, port & 255);
6054
Jeff Layton3a6bb732015-06-09 19:43:57 -04006055 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006056 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04006057 clp->cl_owner_id);
Anna Schumakerdae40962019-08-14 15:28:28 -04006058
6059 status = nfs4_call_sync_custom(&task_setup_data);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006060 if (setclientid.sc_cred) {
6061 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
6062 put_rpccred(setclientid.sc_cred);
6063 }
Jeff Laytonf11b2a12014-06-21 20:52:17 -04006064out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006065 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006066 dprintk("NFS reply setclientid: %d\n", status);
6067 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068}
6069
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006070/**
6071 * nfs4_proc_setclientid_confirm - Confirm client ID
6072 * @clp: state data structure
Trond Myklebust302fad72019-02-18 13:32:38 -05006073 * @arg: result of a previous SETCLIENTID
NeilBrowna52458b2018-12-03 11:30:31 +11006074 * @cred: credential to use for this call
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006075 *
6076 * Returns zero, a negative errno, or a negative NFS4ERR status code.
6077 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04006078int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006079 struct nfs4_setclientid_res *arg,
NeilBrowna52458b2018-12-03 11:30:31 +11006080 const struct cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006081{
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 struct rpc_message msg = {
6083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04006084 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01006085 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006086 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006087 int status;
6088
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006089 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
6090 clp->cl_rpcclient->cl_auth->au_ops->au_name,
6091 clp->cl_clientid);
NeilBrown5a0c2572019-05-30 10:41:28 +10006092 status = rpc_call_sync(clp->cl_rpcclient, &msg,
6093 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04006094 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04006095 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006096 return status;
6097}
6098
Trond Myklebustfe650402006-01-03 09:55:18 +01006099struct nfs4_delegreturndata {
6100 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006101 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01006102 struct nfs_fh fh;
6103 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006104 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006105 struct {
6106 struct nfs4_layoutreturn_args arg;
6107 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006108 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006109 u32 roc_barrier;
6110 bool roc;
6111 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006112 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006113 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006114 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006115};
6116
Trond Myklebustfe650402006-01-03 09:55:18 +01006117static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6118{
6119 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006120 struct nfs4_exception exception = {
6121 .inode = data->inode,
6122 .stateid = &data->stateid,
6123 };
Andy Adamson938e1012009-04-01 09:22:28 -04006124
Trond Myklebust14516c32010-07-31 14:29:06 -04006125 if (!nfs4_sequence_done(task, &data->res.seq_res))
6126 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006127
Trond Myklebustca8acf82013-08-13 10:36:56 -04006128 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006129
6130 /* Handle Layoutreturn errors */
6131 if (data->args.lr_args && task->tk_status != 0) {
6132 switch(data->res.lr_ret) {
6133 default:
6134 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6135 break;
6136 case 0:
6137 data->args.lr_args = NULL;
6138 data->res.lr_res = NULL;
6139 break;
Trond Myklebust73800202017-11-06 15:28:07 -05006140 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04006141 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04006142 &data->args.lr_args->range,
Trond Myklebust140087fd2017-11-06 15:28:10 -05006143 data->inode))
6144 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05006145 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05006146 case -NFS4ERR_ADMIN_REVOKED:
6147 case -NFS4ERR_DELEG_REVOKED:
6148 case -NFS4ERR_EXPIRED:
6149 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05006150 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6151 case -NFS4ERR_WRONG_CRED:
6152 data->args.lr_args = NULL;
6153 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006154 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006155 }
6156 }
6157
Ricardo Labiaga79708862009-12-07 09:23:21 -05006158 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006159 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006160 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006161 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006162 case -NFS4ERR_ADMIN_REVOKED:
6163 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006164 case -NFS4ERR_EXPIRED:
6165 nfs4_free_revoked_stateid(data->res.server,
6166 data->args.stateid,
6167 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006168 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006169 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006170 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006171 task->tk_status = 0;
6172 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006173 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006174 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6175 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006176 task->tk_status = 0;
6177 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006178 case -NFS4ERR_ACCESS:
6179 if (data->args.bitmask) {
6180 data->args.bitmask = NULL;
6181 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006182 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006183 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006184 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006185 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006186 task->tk_status = nfs4_async_handle_exception(task,
6187 data->res.server, task->tk_status,
6188 &exception);
6189 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006190 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006191 }
6192 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006193 return;
6194lr_restart:
6195 data->res.lr_ret = 0;
6196out_restart:
6197 task->tk_status = 0;
6198 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006199}
6200
6201static void nfs4_delegreturn_release(void *calldata)
6202{
Peng Tao039b7562014-07-03 13:05:02 +08006203 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006204 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006205
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006206 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006207 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006208 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6209 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006210 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006211 nfs_iput_and_deactive(inode);
6212 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006213 kfree(calldata);
6214}
6215
Andy Adamson938e1012009-04-01 09:22:28 -04006216static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6217{
6218 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006219 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006220
6221 d_data = (struct nfs4_delegreturndata *)data;
6222
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006223 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006224 return;
6225
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006226 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6227 if (lo && !pnfs_layout_is_valid(lo)) {
6228 d_data->args.lr_args = NULL;
6229 d_data->res.lr_res = NULL;
6230 }
6231
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006232 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006233 &d_data->args.seq_args,
6234 &d_data->res.seq_res,
6235 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006236}
Andy Adamson938e1012009-04-01 09:22:28 -04006237
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006238static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006239 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006240 .rpc_call_done = nfs4_delegreturn_done,
6241 .rpc_release = nfs4_delegreturn_release,
6242};
6243
NeilBrowna52458b2018-12-03 11:30:31 +11006244static int _nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006245{
6246 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006247 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006248 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006249 struct rpc_message msg = {
6250 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6251 .rpc_cred = cred,
6252 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006253 struct rpc_task_setup task_setup_data = {
6254 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006255 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006256 .callback_ops = &nfs4_delegreturn_ops,
6257 .flags = RPC_TASK_ASYNC,
6258 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006259 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006260
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006261 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006262 if (data == NULL)
6263 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006264 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006265
6266 nfs4_state_protect(server->nfs_client,
6267 NFS_SP4_MACH_CRED_CLEANUP,
6268 &task_setup_data.rpc_client, &msg);
6269
Trond Myklebustfe650402006-01-03 09:55:18 +01006270 data->args.fhandle = &data->fh;
6271 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006272 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006273 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006274 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006275 data->res.fattr = &data->fattr;
6276 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006277 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006278 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006279 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006280 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006281 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006282 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006283 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006284 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006285 if (data->lr.roc) {
6286 data->args.lr_args = &data->lr.arg;
6287 data->res.lr_res = &data->lr.res;
6288 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006289 } else if (data->lr.roc) {
6290 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6291 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006292 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006293
Trond Myklebustc970aa82007-07-14 15:39:59 -04006294 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006295 msg.rpc_argp = &data->args;
6296 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006297 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006298 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006299 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006300 if (!issync)
6301 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006302 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006303 if (status != 0)
6304 goto out;
6305 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006306out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006307 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006308 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006309}
6310
NeilBrowna52458b2018-12-03 11:30:31 +11006311int nfs4_proc_delegreturn(struct inode *inode, const struct cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006312{
6313 struct nfs_server *server = NFS_SERVER(inode);
6314 struct nfs4_exception exception = { };
6315 int err;
6316 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006317 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006318 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006319 switch (err) {
6320 case -NFS4ERR_STALE_STATEID:
6321 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006322 case 0:
6323 return 0;
6324 }
6325 err = nfs4_handle_exception(server, err, &exception);
6326 } while (exception.retry);
6327 return err;
6328}
6329
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6331{
6332 struct inode *inode = state->inode;
6333 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006334 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006335 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006336 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006337 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006338 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006339 struct nfs_lockt_res res = {
6340 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006341 };
6342 struct rpc_message msg = {
6343 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006344 .rpc_argp = &arg,
6345 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006346 .rpc_cred = state->owner->so_cred,
6347 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006348 struct nfs4_lock_state *lsp;
6349 int status;
6350
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006351 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006352 status = nfs4_set_lock_state(state, request);
6353 if (status != 0)
6354 goto out;
6355 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006356 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006357 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006358 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006359 switch (status) {
6360 case 0:
6361 request->fl_type = F_UNLCK;
6362 break;
6363 case -NFS4ERR_DENIED:
6364 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006365 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006366 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006367 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006368out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006369 return status;
6370}
6371
6372static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6373{
Trond Myklebust0688e642019-04-07 13:59:09 -04006374 struct nfs4_exception exception = {
6375 .interruptible = true,
6376 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006377 int err;
6378
6379 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006380 err = _nfs4_proc_getlk(state, cmd, request);
6381 trace_nfs4_get_lock(request, state, cmd, err);
6382 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006383 &exception);
6384 } while (exception.retry);
6385 return err;
6386}
6387
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006388struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006389 struct nfs_locku_args arg;
6390 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006391 struct nfs4_lock_state *lsp;
6392 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006393 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006394 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006395 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006396 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006397};
6398
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006399static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6400 struct nfs_open_context *ctx,
6401 struct nfs4_lock_state *lsp,
6402 struct nfs_seqid *seqid)
6403{
6404 struct nfs4_unlockdata *p;
6405 struct inode *inode = lsp->ls_state->inode;
6406
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006407 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006408 if (p == NULL)
6409 return NULL;
6410 p->arg.fh = NFS_FH(inode);
6411 p->arg.fl = &p->fl;
6412 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006413 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006414 p->lsp = lsp;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006415 /* Ensure we don't close file until we're done freeing locks! */
6416 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006417 p->l_ctx = nfs_get_lock_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006418 locks_init_lock(&p->fl);
6419 locks_copy_lock(&p->fl, fl);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006420 p->server = NFS_SERVER(inode);
6421 return p;
6422}
6423
Trond Myklebust06f814a2006-01-03 09:55:07 +01006424static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006425{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006426 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006427 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006428 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006429 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006430 put_nfs_open_context(calldata->ctx);
6431 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006432}
6433
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006434static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006435{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006436 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006437 struct nfs4_exception exception = {
6438 .inode = calldata->lsp->ls_state->inode,
6439 .stateid = &calldata->arg.stateid,
6440 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006441
Trond Myklebust14516c32010-07-31 14:29:06 -04006442 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6443 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006444 switch (task->tk_status) {
6445 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006446 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006447 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006448 if (nfs4_update_lock_stateid(calldata->lsp,
6449 &calldata->res.stateid))
6450 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006451 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006452 case -NFS4ERR_ADMIN_REVOKED:
6453 case -NFS4ERR_EXPIRED:
6454 nfs4_free_revoked_stateid(calldata->server,
6455 &calldata->arg.stateid,
6456 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006457 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006458 case -NFS4ERR_BAD_STATEID:
6459 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006460 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006461 if (!nfs4_stateid_match(&calldata->arg.stateid,
6462 &calldata->lsp->ls_stateid))
6463 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006464 break;
6465 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006466 task->tk_status = nfs4_async_handle_exception(task,
6467 calldata->server, task->tk_status,
6468 &exception);
6469 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006470 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006471 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006472 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006473}
6474
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006475static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006476{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006477 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006478
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006479 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6480 nfs_async_iocounter_wait(task, calldata->l_ctx))
6481 return;
6482
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006483 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006484 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006485 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006486 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006487 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006488 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006489 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006490 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006491 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006492 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006493 &calldata->res.seq_res,
6494 task) != 0)
6495 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006496 return;
6497out_no_action:
6498 task->tk_action = NULL;
6499out_wait:
6500 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006501}
6502
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006503static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006504 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006505 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006506 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006507};
6508
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006509static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6510 struct nfs_open_context *ctx,
6511 struct nfs4_lock_state *lsp,
6512 struct nfs_seqid *seqid)
6513{
6514 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006515 struct rpc_message msg = {
6516 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6517 .rpc_cred = ctx->cred,
6518 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006519 struct rpc_task_setup task_setup_data = {
6520 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006521 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006522 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006523 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006524 .flags = RPC_TASK_ASYNC,
6525 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006526
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006527 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6528 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6529
Frank Filz137d6ac2007-07-09 15:32:29 -07006530 /* Ensure this is an unlock - when canceling a lock, the
6531 * canceled lock is passed in, and it won't be an unlock.
6532 */
6533 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006534 if (fl->fl_flags & FL_CLOSE)
6535 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006536
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006537 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6538 if (data == NULL) {
6539 nfs_free_seqid(seqid);
6540 return ERR_PTR(-ENOMEM);
6541 }
6542
Anna Schumakerfba83f32018-05-04 16:22:50 -04006543 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006544 msg.rpc_argp = &data->arg;
6545 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006546 task_setup_data.callback_data = data;
6547 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006548}
6549
Linus Torvalds1da177e2005-04-16 15:20:36 -07006550static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6551{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006552 struct inode *inode = state->inode;
6553 struct nfs4_state_owner *sp = state->owner;
6554 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006555 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006556 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006557 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006558 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006559 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006560 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006561
Trond Myklebust9b073572006-06-29 16:38:34 -04006562 status = nfs4_set_lock_state(state, request);
6563 /* Unlock _before_ we do the RPC call */
6564 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006565 /* Exclude nfs_delegation_claim_locks() */
6566 mutex_lock(&sp->so_delegreturn_mutex);
6567 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006568 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006569 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006570 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006571 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006572 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006573 }
6574 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006575 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006576 if (status != 0)
6577 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006578 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006579 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006580 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6581 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006582 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6583 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006584 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006585 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006586 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006587 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006588 status = PTR_ERR(task);
6589 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006590 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006591 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006592 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006593out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006594 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006595 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006596 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006597}
6598
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006599struct nfs4_lockdata {
6600 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006601 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006602 struct nfs4_lock_state *lsp;
6603 struct nfs_open_context *ctx;
6604 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006605 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006606 int rpc_status;
6607 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006608 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006609};
6610
6611static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006612 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6613 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006614{
6615 struct nfs4_lockdata *p;
6616 struct inode *inode = lsp->ls_state->inode;
6617 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006618 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006619
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006620 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006621 if (p == NULL)
6622 return NULL;
6623
6624 p->arg.fh = NFS_FH(inode);
6625 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006626 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006627 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006628 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006629 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6630 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006631 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006632 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006633 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006634 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006635 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006636 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006637 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006638 p->server = server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006639 p->ctx = get_nfs_open_context(ctx);
NeilBrown7b587e12018-11-30 10:04:08 +11006640 locks_init_lock(&p->fl);
6641 locks_copy_lock(&p->fl, fl);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006642 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006643out_free_seqid:
6644 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006645out_free:
6646 kfree(p);
6647 return NULL;
6648}
6649
6650static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6651{
6652 struct nfs4_lockdata *data = calldata;
6653 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654
Harvey Harrison3110ff82008-05-02 13:42:44 -07006655 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006656 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006657 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006658 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006659 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006660 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006661 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006662 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006663 nfs4_stateid_copy(&data->arg.open_stateid,
6664 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006665 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006666 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006667 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006668 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006669 nfs4_stateid_copy(&data->arg.lock_stateid,
6670 &data->lsp->ls_stateid);
6671 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006672 if (!nfs4_valid_open_stateid(state)) {
6673 data->rpc_status = -EBADF;
6674 task->tk_action = NULL;
6675 goto out_release_open_seqid;
6676 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006677 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006678 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006679 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006680 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006681 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006682 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006683out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006684 nfs_release_seqid(data->arg.open_seqid);
6685out_release_lock_seqid:
6686 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006687out_wait:
6688 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006689 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006690}
6691
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006692static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6693{
6694 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006695 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006696
Harvey Harrison3110ff82008-05-02 13:42:44 -07006697 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006698
Trond Myklebust14516c32010-07-31 14:29:06 -04006699 if (!nfs4_sequence_done(task, &data->res.seq_res))
6700 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006701
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006702 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006703 switch (task->tk_status) {
6704 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006705 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006706 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006707 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006708 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006709 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006710 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006711 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006712 if (data->arg.new_lock_owner != 0) {
6713 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6714 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6715 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006716 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6717 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006718 break;
6719 case -NFS4ERR_BAD_STATEID:
6720 case -NFS4ERR_OLD_STATEID:
6721 case -NFS4ERR_STALE_STATEID:
6722 case -NFS4ERR_EXPIRED:
6723 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006724 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006725 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006726 goto out_restart;
6727 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006728 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006729 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006730 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006731out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006732 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006733 return;
6734out_restart:
6735 if (!data->cancelled)
6736 rpc_restart_call_prepare(task);
6737 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006738}
6739
6740static void nfs4_lock_release(void *calldata)
6741{
6742 struct nfs4_lockdata *data = calldata;
6743
Harvey Harrison3110ff82008-05-02 13:42:44 -07006744 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006745 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006746 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006747 struct rpc_task *task;
6748 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6749 data->arg.lock_seqid);
6750 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006751 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006752 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006753 } else
6754 nfs_free_seqid(data->arg.lock_seqid);
6755 nfs4_put_lock_state(data->lsp);
6756 put_nfs_open_context(data->ctx);
6757 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006758 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006759}
6760
6761static const struct rpc_call_ops nfs4_lock_ops = {
6762 .rpc_call_prepare = nfs4_lock_prepare,
6763 .rpc_call_done = nfs4_lock_done,
6764 .rpc_release = nfs4_lock_release,
6765};
6766
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006767static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6768{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006769 switch (error) {
6770 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006771 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006772 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006773 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006774 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006775 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006776 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006777 break;
6778 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006779 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006780 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006781 };
6782}
6783
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006784static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006785{
6786 struct nfs4_lockdata *data;
6787 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006788 struct rpc_message msg = {
6789 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6790 .rpc_cred = state->owner->so_cred,
6791 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006792 struct rpc_task_setup task_setup_data = {
6793 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006794 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006795 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006796 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006797 .flags = RPC_TASK_ASYNC,
6798 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006799 int ret;
6800
Harvey Harrison3110ff82008-05-02 13:42:44 -07006801 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006802 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006803 fl->fl_u.nfs4_fl.owner,
6804 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006805 if (data == NULL)
6806 return -ENOMEM;
6807 if (IS_SETLKW(cmd))
6808 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006809 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6810 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006811 msg.rpc_argp = &data->arg;
6812 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006813 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006814 if (recovery_type > NFS_LOCK_NEW) {
6815 if (recovery_type == NFS_LOCK_RECLAIM)
6816 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006817 } else
6818 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006819 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006820 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006821 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006822 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006823 if (ret == 0) {
6824 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006825 if (ret)
6826 nfs4_handle_setlk_error(data->server, data->lsp,
6827 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006828 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006829 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006830 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006831 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006832 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006833 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006834}
6835
6836static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6837{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006838 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006839 struct nfs4_exception exception = {
6840 .inode = state->inode,
6841 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006842 int err;
6843
6844 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006845 /* Cache the lock if possible... */
6846 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6847 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006848 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006849 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006850 break;
6851 nfs4_handle_exception(server, err, &exception);
6852 } while (exception.retry);
6853 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006854}
6855
6856static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6857{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006858 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006859 struct nfs4_exception exception = {
6860 .inode = state->inode,
6861 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006862 int err;
6863
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006864 err = nfs4_set_lock_state(state, request);
6865 if (err != 0)
6866 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006867 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006868 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6869 return 0;
6870 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006871 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006872 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6873 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006874 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006875 switch (err) {
6876 default:
6877 goto out;
6878 case -NFS4ERR_GRACE:
6879 case -NFS4ERR_DELAY:
6880 nfs4_handle_exception(server, err, &exception);
6881 err = 0;
6882 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006883 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006884out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006885 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006886}
6887
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006888#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006889static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6890{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006891 struct nfs4_lock_state *lsp;
6892 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006893
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006894 status = nfs4_set_lock_state(state, request);
6895 if (status != 0)
6896 return status;
6897 lsp = request->fl_u.nfs4_fl.owner;
6898 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6899 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6900 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006901 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006902}
6903#endif
6904
Linus Torvalds1da177e2005-04-16 15:20:36 -07006905static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6906{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006907 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006908 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006909 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006910 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006911
Trond Myklebust01c3b862006-06-29 16:38:39 -04006912 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006913 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006914 if (status < 0)
6915 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006916 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006917 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006918 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006919 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006920 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006921 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006922 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006923 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006924 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006925 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006926 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006927 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006928 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006929 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006930out:
6931 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006932 return status;
6933}
6934
6935static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6936{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006937 struct nfs4_exception exception = {
6938 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006939 .inode = state->inode,
Trond Myklebust0688e642019-04-07 13:59:09 -04006940 .interruptible = true,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006941 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006942 int err;
6943
6944 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006945 err = _nfs4_proc_setlk(state, cmd, request);
6946 if (err == -NFS4ERR_DENIED)
6947 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006948 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006949 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006950 } while (exception.retry);
6951 return err;
6952}
6953
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006954#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6955#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6956
6957static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006958nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6959 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006960{
6961 int status = -ERESTARTSYS;
6962 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6963
6964 while(!signalled()) {
6965 status = nfs4_proc_setlk(state, cmd, request);
6966 if ((status != -EAGAIN) || IS_SETLK(cmd))
6967 break;
6968 freezable_schedule_timeout_interruptible(timeout);
6969 timeout *= 2;
6970 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6971 status = -ERESTARTSYS;
6972 }
6973 return status;
6974}
6975
Jeff Laytona1d617d82016-09-17 18:17:39 -04006976#ifdef CONFIG_NFS_V4_1
6977struct nfs4_lock_waiter {
6978 struct task_struct *task;
6979 struct inode *inode;
6980 struct nfs_lowner *owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006981};
6982
6983static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006984nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006985{
6986 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006987 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006988
Jeff Layton57174592018-03-18 08:37:03 -04006989 /* NULL key means to wake up everyone */
6990 if (key) {
6991 struct cb_notify_lock_args *cbnl = key;
6992 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6993 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006994
Jeff Layton57174592018-03-18 08:37:03 -04006995 /* Only wake if the callback was for the same owner. */
6996 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6997 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006998
Jeff Layton57174592018-03-18 08:37:03 -04006999 /* Make sure it's for the right inode */
7000 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
7001 return 0;
Jeff Layton57174592018-03-18 08:37:03 -04007002 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007003
7004 /* override "private" so we can use default_wake_function */
7005 wait->private = waiter->task;
Yihao Wu52b042a2019-05-22 01:57:10 +08007006 ret = woken_wake_function(wait, mode, flags, key);
7007 if (ret)
7008 list_del_init(&wait->entry);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007009 wait->private = waiter;
7010 return ret;
7011}
7012
7013static int
7014nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7015{
7016 int status = -ERESTARTSYS;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007017 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
7018 struct nfs_server *server = NFS_SERVER(state->inode);
7019 struct nfs_client *clp = server->nfs_client;
7020 wait_queue_head_t *q = &clp->cl_lock_waitq;
7021 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
7022 .id = lsp->ls_seqid.owner_id,
7023 .s_dev = server->s_dev };
7024 struct nfs4_lock_waiter waiter = { .task = current,
7025 .inode = state->inode,
Yihao Wu52b042a2019-05-22 01:57:10 +08007026 .owner = &owner};
Ingo Molnarac6424b2017-06-20 12:06:13 +02007027 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007028
7029 /* Don't bother with waitqueue if we don't expect a callback */
7030 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
7031 return nfs4_retry_setlk_simple(state, cmd, request);
7032
7033 init_wait(&wait);
7034 wait.private = &waiter;
7035 wait.func = nfs4_wake_lock_waiter;
Jeff Laytona1d617d82016-09-17 18:17:39 -04007036
7037 while(!signalled()) {
Yihao Wuba851a32019-05-13 14:58:22 +08007038 add_wait_queue(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007039 status = nfs4_proc_setlk(state, cmd, request);
Yihao Wuba851a32019-05-13 14:58:22 +08007040 if ((status != -EAGAIN) || IS_SETLK(cmd)) {
7041 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007042 break;
Yihao Wuba851a32019-05-13 14:58:22 +08007043 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04007044
7045 status = -ERESTARTSYS;
Yihao Wu52b042a2019-05-22 01:57:10 +08007046 freezer_do_not_count();
7047 wait_woken(&wait, TASK_INTERRUPTIBLE, NFS4_LOCK_MAXTIMEOUT);
7048 freezer_count();
Yihao Wuba851a32019-05-13 14:58:22 +08007049 finish_wait(q, &wait);
Jeff Laytona1d617d82016-09-17 18:17:39 -04007050 }
7051
Jeff Laytona1d617d82016-09-17 18:17:39 -04007052 return status;
7053}
7054#else /* !CONFIG_NFS_V4_1 */
7055static inline int
7056nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
7057{
7058 return nfs4_retry_setlk_simple(state, cmd, request);
7059}
7060#endif
7061
Linus Torvalds1da177e2005-04-16 15:20:36 -07007062static int
7063nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
7064{
7065 struct nfs_open_context *ctx;
7066 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07007067 int status;
7068
7069 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04007070 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007071 state = ctx->state;
7072
Trond Myklebustd9531262009-07-21 19:22:38 -04007073 if (IS_GETLK(cmd)) {
7074 if (state != NULL)
7075 return nfs4_proc_getlk(state, F_GETLK, request);
7076 return 0;
7077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007078
7079 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
7080 return -EINVAL;
7081
Trond Myklebustd9531262009-07-21 19:22:38 -04007082 if (request->fl_type == F_UNLCK) {
7083 if (state != NULL)
7084 return nfs4_proc_unlck(state, cmd, request);
7085 return 0;
7086 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07007087
Trond Myklebustd9531262009-07-21 19:22:38 -04007088 if (state == NULL)
7089 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04007090
7091 if ((request->fl_flags & FL_POSIX) &&
7092 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
7093 return -ENOLCK;
7094
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05007095 /*
7096 * Don't rely on the VFS having checked the file open mode,
7097 * since it won't do this for flock() locks.
7098 */
7099 switch (request->fl_type) {
7100 case F_RDLCK:
7101 if (!(filp->f_mode & FMODE_READ))
7102 return -EBADF;
7103 break;
7104 case F_WRLCK:
7105 if (!(filp->f_mode & FMODE_WRITE))
7106 return -EBADF;
7107 }
7108
Jeff Layton1ea67db2016-09-17 18:17:37 -04007109 status = nfs4_set_lock_state(state, request);
7110 if (status != 0)
7111 return status;
7112
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007113 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007114}
7115
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007116int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007117{
7118 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007119 int err;
7120
7121 err = nfs4_set_lock_state(state, fl);
7122 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007123 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007124 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007125 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007126}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007127
Trond Myklebustcf470c32012-03-07 13:49:12 -05007128struct nfs_release_lockowner_data {
7129 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007130 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007131 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007132 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007133 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007134};
7135
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007136static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7137{
7138 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007139 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007140 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7141 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007142 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007143 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007144}
7145
7146static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7147{
7148 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007149 struct nfs_server *server = data->server;
7150
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007151 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007152
7153 switch (task->tk_status) {
7154 case 0:
7155 renew_lease(server, data->timestamp);
7156 break;
7157 case -NFS4ERR_STALE_CLIENTID:
7158 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007159 nfs4_schedule_lease_recovery(server->nfs_client);
7160 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007161 case -NFS4ERR_LEASE_MOVED:
7162 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007163 if (nfs4_async_handle_error(task, server,
7164 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007165 rpc_restart_call_prepare(task);
7166 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007167}
7168
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007169static void nfs4_release_lockowner_release(void *calldata)
7170{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007171 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007172 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007173 kfree(calldata);
7174}
7175
Trond Myklebust17280172012-03-11 13:11:00 -04007176static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007177 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7178 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007179 .rpc_release = nfs4_release_lockowner_release,
7180};
7181
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007182static void
7183nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007184{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007185 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007186 struct rpc_message msg = {
7187 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7188 };
7189
7190 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007191 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007192
Trond Myklebustcf470c32012-03-07 13:49:12 -05007193 data = kmalloc(sizeof(*data), GFP_NOFS);
7194 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007195 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007196 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007197 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007198 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7199 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7200 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007201
Trond Myklebustcf470c32012-03-07 13:49:12 -05007202 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007203 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007204 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007205 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007206}
7207
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007208#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7209
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007210static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007211 struct dentry *unused, struct inode *inode,
7212 const char *key, const void *buf,
7213 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007214{
Al Viro59301222016-05-27 10:19:30 -04007215 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007216}
7217
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007218static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007219 struct dentry *unused, struct inode *inode,
7220 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007221{
Al Virob2968212016-04-10 20:48:24 -04007222 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007223}
7224
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007225static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007226{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007227 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007228}
7229
David Quigleyc9bccef2013-05-22 12:50:45 -04007230#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007231
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007232static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007233 struct dentry *unused, struct inode *inode,
7234 const char *key, const void *buf,
7235 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007236{
7237 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007238 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007239
7240 return -EOPNOTSUPP;
7241}
7242
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007243static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007244 struct dentry *unused, struct inode *inode,
7245 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007246{
7247 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007248 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007249 return -EOPNOTSUPP;
7250}
7251
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007252static ssize_t
7253nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007254{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007255 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007256
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007257 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7258 len = security_inode_listsecurity(inode, list, list_len);
7259 if (list_len && len > list_len)
7260 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007261 }
7262 return len;
7263}
7264
7265static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7266 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007267 .get = nfs4_xattr_get_nfs4_label,
7268 .set = nfs4_xattr_set_nfs4_label,
7269};
David Quigleyc9bccef2013-05-22 12:50:45 -04007270
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007271#else
7272
7273static ssize_t
7274nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7275{
7276 return 0;
7277}
7278
7279#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007280
Andy Adamson533eb462011-06-13 18:25:56 -04007281/*
7282 * nfs_fhget will use either the mounted_on_fileid or the fileid
7283 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007284static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7285{
Andy Adamson533eb462011-06-13 18:25:56 -04007286 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7287 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7288 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007289 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007290 return;
7291
7292 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007293 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007294 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7295 fattr->nlink = 2;
7296}
7297
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007298static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7299 const struct qstr *name,
7300 struct nfs4_fs_locations *fs_locations,
7301 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007302{
7303 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007304 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007305 struct nfs4_fs_locations_arg args = {
7306 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007307 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007308 .page = page,
7309 .bitmask = bitmask,
7310 };
Benny Halevy22958462009-04-01 09:22:02 -04007311 struct nfs4_fs_locations_res res = {
7312 .fs_locations = fs_locations,
7313 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007314 struct rpc_message msg = {
7315 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7316 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007317 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007318 };
7319 int status;
7320
Harvey Harrison3110ff82008-05-02 13:42:44 -07007321 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007322
Chuck Leverc05cefc2017-11-05 15:45:22 -05007323 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7324 bitmask[1] = nfs4_fattr_bitmap[1];
7325
Andy Adamson533eb462011-06-13 18:25:56 -04007326 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7327 * is not supported */
7328 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007329 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007330 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007331 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007332
Trond Myklebustc228fd32007-01-13 02:28:11 -05007333 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007334 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007335 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007336 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007337 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007338 return status;
7339}
7340
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007341int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7342 const struct qstr *name,
7343 struct nfs4_fs_locations *fs_locations,
7344 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007345{
Trond Myklebust0688e642019-04-07 13:59:09 -04007346 struct nfs4_exception exception = {
7347 .interruptible = true,
7348 };
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007349 int err;
7350 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007351 err = _nfs4_proc_fs_locations(client, dir, name,
7352 fs_locations, page);
7353 trace_nfs4_get_fs_locations(dir, name, err);
7354 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007355 &exception);
7356 } while (exception.retry);
7357 return err;
7358}
7359
Chuck Leverb03d7352013-10-17 14:12:50 -04007360/*
7361 * This operation also signals the server that this client is
7362 * performing migration recovery. The server can stop returning
7363 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7364 * appended to this compound to identify the client ID which is
7365 * performing recovery.
7366 */
7367static int _nfs40_proc_get_locations(struct inode *inode,
7368 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007369 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007370{
7371 struct nfs_server *server = NFS_SERVER(inode);
7372 struct rpc_clnt *clnt = server->client;
7373 u32 bitmask[2] = {
7374 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7375 };
7376 struct nfs4_fs_locations_arg args = {
7377 .clientid = server->nfs_client->cl_clientid,
7378 .fh = NFS_FH(inode),
7379 .page = page,
7380 .bitmask = bitmask,
7381 .migration = 1, /* skip LOOKUP */
7382 .renew = 1, /* append RENEW */
7383 };
7384 struct nfs4_fs_locations_res res = {
7385 .fs_locations = locations,
7386 .migration = 1,
7387 .renew = 1,
7388 };
7389 struct rpc_message msg = {
7390 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7391 .rpc_argp = &args,
7392 .rpc_resp = &res,
7393 .rpc_cred = cred,
7394 };
7395 unsigned long now = jiffies;
7396 int status;
7397
7398 nfs_fattr_init(&locations->fattr);
7399 locations->server = server;
7400 locations->nlocations = 0;
7401
Anna Schumakerfba83f32018-05-04 16:22:50 -04007402 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007403 status = nfs4_call_sync_sequence(clnt, server, &msg,
7404 &args.seq_args, &res.seq_res);
7405 if (status)
7406 return status;
7407
7408 renew_lease(server, now);
7409 return 0;
7410}
7411
7412#ifdef CONFIG_NFS_V4_1
7413
7414/*
7415 * This operation also signals the server that this client is
7416 * performing migration recovery. The server can stop asserting
7417 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7418 * performing this operation is identified in the SEQUENCE
7419 * operation in this compound.
7420 *
7421 * When the client supports GETATTR(fs_locations_info), it can
7422 * be plumbed in here.
7423 */
7424static int _nfs41_proc_get_locations(struct inode *inode,
7425 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007426 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007427{
7428 struct nfs_server *server = NFS_SERVER(inode);
7429 struct rpc_clnt *clnt = server->client;
7430 u32 bitmask[2] = {
7431 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7432 };
7433 struct nfs4_fs_locations_arg args = {
7434 .fh = NFS_FH(inode),
7435 .page = page,
7436 .bitmask = bitmask,
7437 .migration = 1, /* skip LOOKUP */
7438 };
7439 struct nfs4_fs_locations_res res = {
7440 .fs_locations = locations,
7441 .migration = 1,
7442 };
7443 struct rpc_message msg = {
7444 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7445 .rpc_argp = &args,
7446 .rpc_resp = &res,
7447 .rpc_cred = cred,
7448 };
7449 int status;
7450
7451 nfs_fattr_init(&locations->fattr);
7452 locations->server = server;
7453 locations->nlocations = 0;
7454
Anna Schumakerfba83f32018-05-04 16:22:50 -04007455 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007456 status = nfs4_call_sync_sequence(clnt, server, &msg,
7457 &args.seq_args, &res.seq_res);
7458 if (status == NFS4_OK &&
7459 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7460 status = -NFS4ERR_LEASE_MOVED;
7461 return status;
7462}
7463
7464#endif /* CONFIG_NFS_V4_1 */
7465
7466/**
7467 * nfs4_proc_get_locations - discover locations for a migrated FSID
7468 * @inode: inode on FSID that is migrating
7469 * @locations: result of query
7470 * @page: buffer
7471 * @cred: credential to use for this operation
7472 *
7473 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7474 * operation failed, or a negative errno if a local error occurred.
7475 *
7476 * On success, "locations" is filled in, but if the server has
7477 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7478 * asserted.
7479 *
7480 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7481 * from this client that require migration recovery.
7482 */
7483int nfs4_proc_get_locations(struct inode *inode,
7484 struct nfs4_fs_locations *locations,
NeilBrowna52458b2018-12-03 11:30:31 +11007485 struct page *page, const struct cred *cred)
Chuck Leverb03d7352013-10-17 14:12:50 -04007486{
7487 struct nfs_server *server = NFS_SERVER(inode);
7488 struct nfs_client *clp = server->nfs_client;
7489 const struct nfs4_mig_recovery_ops *ops =
7490 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007491 struct nfs4_exception exception = {
7492 .interruptible = true,
7493 };
Chuck Leverb03d7352013-10-17 14:12:50 -04007494 int status;
7495
7496 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7497 (unsigned long long)server->fsid.major,
7498 (unsigned long long)server->fsid.minor,
7499 clp->cl_hostname);
7500 nfs_display_fhandle(NFS_FH(inode), __func__);
7501
7502 do {
7503 status = ops->get_locations(inode, locations, page, cred);
7504 if (status != -NFS4ERR_DELAY)
7505 break;
7506 nfs4_handle_exception(server, status, &exception);
7507 } while (exception.retry);
7508 return status;
7509}
7510
Chuck Lever44c99932013-10-17 14:13:30 -04007511/*
7512 * This operation also signals the server that this client is
7513 * performing "lease moved" recovery. The server can stop
7514 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7515 * is appended to this compound to identify the client ID which is
7516 * performing recovery.
7517 */
NeilBrowna52458b2018-12-03 11:30:31 +11007518static int _nfs40_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007519{
7520 struct nfs_server *server = NFS_SERVER(inode);
7521 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7522 struct rpc_clnt *clnt = server->client;
7523 struct nfs4_fsid_present_arg args = {
7524 .fh = NFS_FH(inode),
7525 .clientid = clp->cl_clientid,
7526 .renew = 1, /* append RENEW */
7527 };
7528 struct nfs4_fsid_present_res res = {
7529 .renew = 1,
7530 };
7531 struct rpc_message msg = {
7532 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7533 .rpc_argp = &args,
7534 .rpc_resp = &res,
7535 .rpc_cred = cred,
7536 };
7537 unsigned long now = jiffies;
7538 int status;
7539
7540 res.fh = nfs_alloc_fhandle();
7541 if (res.fh == NULL)
7542 return -ENOMEM;
7543
Anna Schumakerfba83f32018-05-04 16:22:50 -04007544 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007545 status = nfs4_call_sync_sequence(clnt, server, &msg,
7546 &args.seq_args, &res.seq_res);
7547 nfs_free_fhandle(res.fh);
7548 if (status)
7549 return status;
7550
7551 do_renew_lease(clp, now);
7552 return 0;
7553}
7554
7555#ifdef CONFIG_NFS_V4_1
7556
7557/*
7558 * This operation also signals the server that this client is
7559 * performing "lease moved" recovery. The server can stop asserting
7560 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7561 * this operation is identified in the SEQUENCE operation in this
7562 * compound.
7563 */
NeilBrowna52458b2018-12-03 11:30:31 +11007564static int _nfs41_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007565{
7566 struct nfs_server *server = NFS_SERVER(inode);
7567 struct rpc_clnt *clnt = server->client;
7568 struct nfs4_fsid_present_arg args = {
7569 .fh = NFS_FH(inode),
7570 };
7571 struct nfs4_fsid_present_res res = {
7572 };
7573 struct rpc_message msg = {
7574 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7575 .rpc_argp = &args,
7576 .rpc_resp = &res,
7577 .rpc_cred = cred,
7578 };
7579 int status;
7580
7581 res.fh = nfs_alloc_fhandle();
7582 if (res.fh == NULL)
7583 return -ENOMEM;
7584
Anna Schumakerfba83f32018-05-04 16:22:50 -04007585 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007586 status = nfs4_call_sync_sequence(clnt, server, &msg,
7587 &args.seq_args, &res.seq_res);
7588 nfs_free_fhandle(res.fh);
7589 if (status == NFS4_OK &&
7590 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7591 status = -NFS4ERR_LEASE_MOVED;
7592 return status;
7593}
7594
7595#endif /* CONFIG_NFS_V4_1 */
7596
7597/**
7598 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7599 * @inode: inode on FSID to check
7600 * @cred: credential to use for this operation
7601 *
7602 * Server indicates whether the FSID is present, moved, or not
7603 * recognized. This operation is necessary to clear a LEASE_MOVED
7604 * condition for this client ID.
7605 *
7606 * Returns NFS4_OK if the FSID is present on this server,
7607 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7608 * NFS4ERR code if some error occurred on the server, or a
7609 * negative errno if a local failure occurred.
7610 */
NeilBrowna52458b2018-12-03 11:30:31 +11007611int nfs4_proc_fsid_present(struct inode *inode, const struct cred *cred)
Chuck Lever44c99932013-10-17 14:13:30 -04007612{
7613 struct nfs_server *server = NFS_SERVER(inode);
7614 struct nfs_client *clp = server->nfs_client;
7615 const struct nfs4_mig_recovery_ops *ops =
7616 clp->cl_mvops->mig_recovery_ops;
Trond Myklebust0688e642019-04-07 13:59:09 -04007617 struct nfs4_exception exception = {
7618 .interruptible = true,
7619 };
Chuck Lever44c99932013-10-17 14:13:30 -04007620 int status;
7621
7622 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7623 (unsigned long long)server->fsid.major,
7624 (unsigned long long)server->fsid.minor,
7625 clp->cl_hostname);
7626 nfs_display_fhandle(NFS_FH(inode), __func__);
7627
7628 do {
7629 status = ops->fsid_present(inode, cred);
7630 if (status != -NFS4ERR_DELAY)
7631 break;
7632 nfs4_handle_exception(server, status, &exception);
7633 } while (exception.retry);
7634 return status;
7635}
7636
Trond Myklebust302fad72019-02-18 13:32:38 -05007637/*
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007638 * If 'use_integrity' is true and the state managment nfs_client
7639 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7640 * and the machine credential as per RFC3530bis and RFC5661 Security
7641 * Considerations sections. Otherwise, just use the user cred with the
7642 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007643 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007644static 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 +00007645{
7646 int status;
Anna Schumaker50493362019-08-14 15:30:16 -04007647 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
7648 struct nfs_client *clp = NFS_SERVER(dir)->nfs_client;
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007649 struct nfs4_secinfo_arg args = {
7650 .dir_fh = NFS_FH(dir),
7651 .name = name,
7652 };
7653 struct nfs4_secinfo_res res = {
7654 .flavors = flavors,
7655 };
7656 struct rpc_message msg = {
7657 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7658 .rpc_argp = &args,
7659 .rpc_resp = &res,
7660 };
Anna Schumaker50493362019-08-14 15:30:16 -04007661 struct nfs4_call_sync_data data = {
7662 .seq_server = NFS_SERVER(dir),
7663 .seq_args = &args.seq_args,
7664 .seq_res = &res.seq_res,
7665 };
7666 struct rpc_task_setup task_setup = {
7667 .rpc_client = clnt,
7668 .rpc_message = &msg,
7669 .callback_ops = clp->cl_mvops->call_sync_ops,
7670 .callback_data = &data,
7671 .flags = RPC_TASK_NO_ROUND_ROBIN,
7672 };
NeilBrowna52458b2018-12-03 11:30:31 +11007673 const struct cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007674
7675 if (use_integrity) {
Anna Schumaker50493362019-08-14 15:30:16 -04007676 clnt = clp->cl_rpcclient;
7677 task_setup.rpc_client = clnt;
7678
7679 cred = nfs4_get_clid_cred(clp);
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007680 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007681 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007682
7683 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007684
Anna Schumaker50493362019-08-14 15:30:16 -04007685 nfs4_state_protect(clp, NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7686 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 0);
7687 status = nfs4_call_sync_custom(&task_setup);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007688
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007689 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007690
NeilBrowna52458b2018-12-03 11:30:31 +11007691 put_cred(cred);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007692 return status;
7693}
7694
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007695int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7696 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007697{
Trond Myklebust0688e642019-04-07 13:59:09 -04007698 struct nfs4_exception exception = {
7699 .interruptible = true,
7700 };
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007701 int err;
7702 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007703 err = -NFS4ERR_WRONGSEC;
7704
7705 /* try to use integrity protection with machine cred */
7706 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7707 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7708
7709 /*
7710 * if unable to use integrity protection, or SECINFO with
7711 * integrity protection returns NFS4ERR_WRONGSEC (which is
7712 * disallowed by spec, but exists in deployed servers) use
7713 * the current filesystem's rpc_client and the user cred.
7714 */
7715 if (err == -NFS4ERR_WRONGSEC)
7716 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7717
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007718 trace_nfs4_secinfo(dir, name, err);
7719 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007720 &exception);
7721 } while (exception.retry);
7722 return err;
7723}
7724
Andy Adamson557134a2009-04-01 09:21:53 -04007725#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007726/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007727 * Check the exchange flags returned by the server for invalid flags, having
7728 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7729 * DS flags set.
7730 */
7731static int nfs4_check_cl_exchange_flags(u32 flags)
7732{
7733 if (flags & ~EXCHGID4_FLAG_MASK_R)
7734 goto out_inval;
7735 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7736 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7737 goto out_inval;
7738 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7739 goto out_inval;
7740 return NFS_OK;
7741out_inval:
7742 return -NFS4ERR_INVAL;
7743}
7744
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007745static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007746nfs41_same_server_scope(struct nfs41_server_scope *a,
7747 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007748{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007749 if (a->server_scope_sz != b->server_scope_sz)
7750 return false;
7751 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007752}
7753
Andy Adamson02a95de2016-02-05 16:08:37 -05007754static void
7755nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7756{
7757}
7758
7759static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7760 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7761};
7762
Andy Adamson357f54d2010-12-14 10:11:57 -05007763/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007764 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007765 *
7766 * The 4.1 client currently uses the same TCP connection for the
7767 * fore and backchannel.
7768 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007769static
7770int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7771 struct rpc_xprt *xprt,
7772 struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11007773 const struct cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007774{
7775 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007776 struct nfs41_bind_conn_to_session_args args = {
7777 .client = clp,
7778 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7779 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007780 struct nfs41_bind_conn_to_session_res res;
7781 struct rpc_message msg = {
7782 .rpc_proc =
7783 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007784 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007785 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007786 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007787 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007788 struct rpc_task_setup task_setup_data = {
7789 .rpc_client = clnt,
7790 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007791 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007792 .rpc_message = &msg,
7793 .flags = RPC_TASK_TIMEOUT,
7794 };
7795 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007796
Trond Myklebust71a097c2015-02-18 09:27:18 -08007797 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7798 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7799 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007800
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007801 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7802 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7803 args.dir = NFS4_CDFC4_FORE;
7804
7805 task = rpc_run_task(&task_setup_data);
7806 if (!IS_ERR(task)) {
7807 status = task->tk_status;
7808 rpc_put_task(task);
7809 } else
7810 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007811 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007812 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007813 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007814 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7815 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007816 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007817 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007818 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007819 dprintk("NFS: %s: Unexpected direction from server\n",
7820 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007821 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007822 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007823 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007824 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7825 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007826 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007827 }
7828 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007829
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007830 return status;
7831}
7832
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007833struct rpc_bind_conn_calldata {
7834 struct nfs_client *clp;
NeilBrowna52458b2018-12-03 11:30:31 +11007835 const struct cred *cred;
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007836};
7837
7838static int
7839nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7840 struct rpc_xprt *xprt,
7841 void *calldata)
7842{
7843 struct rpc_bind_conn_calldata *p = calldata;
7844
7845 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7846}
7847
NeilBrowna52458b2018-12-03 11:30:31 +11007848int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, const struct cred *cred)
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007849{
7850 struct rpc_bind_conn_calldata data = {
7851 .clp = clp,
7852 .cred = cred,
7853 };
7854 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7855 nfs4_proc_bind_conn_to_session_callback, &data);
7856}
7857
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007858/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007859 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7860 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007861 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007862static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7863 .how = SP4_MACH_CRED,
7864 .enforce.u.words = {
7865 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7866 1 << (OP_EXCHANGE_ID - 32) |
7867 1 << (OP_CREATE_SESSION - 32) |
7868 1 << (OP_DESTROY_SESSION - 32) |
7869 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007870 },
7871 .allow.u.words = {
7872 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007873 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007874 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007875 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007876 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007877 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007878 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007879 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007880 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007881 1 << (OP_FREE_STATEID - 32) |
7882 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007883 }
7884};
7885
7886/*
7887 * Select the state protection mode for client `clp' given the server results
7888 * from exchange_id in `sp'.
7889 *
7890 * Returns 0 on success, negative errno otherwise.
7891 */
7892static int nfs4_sp4_select_mode(struct nfs_client *clp,
7893 struct nfs41_state_protection *sp)
7894{
7895 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7896 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7897 1 << (OP_EXCHANGE_ID - 32) |
7898 1 << (OP_CREATE_SESSION - 32) |
7899 1 << (OP_DESTROY_SESSION - 32) |
7900 1 << (OP_DESTROY_CLIENTID - 32)
7901 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007902 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007903 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007904 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007905
7906 if (sp->how == SP4_MACH_CRED) {
7907 /* Print state protect result */
7908 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7909 for (i = 0; i <= LAST_NFS4_OP; i++) {
7910 if (test_bit(i, sp->enforce.u.longs))
7911 dfprintk(MOUNT, " enforce op %d\n", i);
7912 if (test_bit(i, sp->allow.u.longs))
7913 dfprintk(MOUNT, " allow op %d\n", i);
7914 }
7915
7916 /* make sure nothing is on enforce list that isn't supported */
7917 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7918 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7919 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007920 ret = -EINVAL;
7921 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007922 }
7923 }
7924
7925 /*
7926 * Minimal mode - state operations are allowed to use machine
7927 * credential. Note this already happens by default, so the
7928 * client doesn't have to do anything more than the negotiation.
7929 *
7930 * NOTE: we don't care if EXCHANGE_ID is in the list -
7931 * we're already using the machine cred for exchange_id
7932 * and will never use a different cred.
7933 */
7934 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7935 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7936 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7937 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7938 dfprintk(MOUNT, "sp4_mach_cred:\n");
7939 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007940 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007941 } else {
7942 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007943 ret = -EINVAL;
7944 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007945 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007946
7947 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007948 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7949 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007950 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7951 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007952 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007953 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007954
Andrew Elble99ade3c2015-12-02 09:39:51 -05007955 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7956 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007957 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007958 }
7959
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007960 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7961 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7962 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007963 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007964 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007965
7966 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7967 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7968 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007969 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007970 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007971
7972 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7973 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007974 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007975 }
7976
7977 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7978 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007979 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007980 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007981 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007982out:
7983 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007984 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007985}
7986
Andy Adamson8d89bd72016-09-09 09:22:18 -04007987struct nfs41_exchange_id_data {
7988 struct nfs41_exchange_id_res res;
7989 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007990};
7991
Andy Adamson8d89bd72016-09-09 09:22:18 -04007992static void nfs4_exchange_id_release(void *data)
7993{
7994 struct nfs41_exchange_id_data *cdata =
7995 (struct nfs41_exchange_id_data *)data;
7996
Olga Kornievskaia63513232017-03-13 10:36:19 -04007997 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007998 kfree(cdata->res.impl_id);
7999 kfree(cdata->res.server_scope);
8000 kfree(cdata->res.server_owner);
8001 kfree(cdata);
8002}
8003
8004static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04008005 .rpc_release = nfs4_exchange_id_release,
8006};
8007
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008008/*
8009 * _nfs4_proc_exchange_id()
8010 *
8011 * Wrapper for EXCHANGE_ID operation.
8012 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008013static struct rpc_task *
NeilBrowna52458b2018-12-03 11:30:31 +11008014nfs4_run_exchange_id(struct nfs_client *clp, const struct cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04008015 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04008016{
Benny Halevy99fe60d2009-04-01 09:22:29 -04008017 struct rpc_message msg = {
8018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04008019 .rpc_cred = cred,
8020 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04008021 struct rpc_task_setup task_setup_data = {
8022 .rpc_client = clp->cl_rpcclient,
8023 .callback_ops = &nfs4_exchange_id_call_ops,
8024 .rpc_message = &msg,
NeilBrown5a0c2572019-05-30 10:41:28 +10008025 .flags = RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN,
Andy Adamson8d89bd72016-09-09 09:22:18 -04008026 };
8027 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04008028 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008029
Elena Reshetova212bf412017-10-20 12:53:38 +03008030 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04008031 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008032
Trond Myklebust9c760d12017-07-31 18:38:50 -04008033 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008034 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008035 if (!calldata)
8036 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008037
Trond Myklebustfd405592017-08-01 16:02:47 -04008038 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04008039
8040 status = nfs4_init_uniform_client_string(clp);
8041 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04008042 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04008043
Andy Adamson8d89bd72016-09-09 09:22:18 -04008044 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
8045 GFP_NOFS);
8046 status = -ENOMEM;
8047 if (unlikely(calldata->res.server_owner == NULL))
8048 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008049
Andy Adamson8d89bd72016-09-09 09:22:18 -04008050 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04008051 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008052 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04008053 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008054
Andy Adamson8d89bd72016-09-09 09:22:18 -04008055 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
8056 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008057 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05008058
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008059 switch (sp4_how) {
8060 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008061 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008062 break;
8063
8064 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04008065 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008066 break;
8067
8068 default:
8069 /* unsupported! */
8070 WARN_ON_ONCE(1);
8071 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08008072 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008073 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04008074 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04008075 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04008076 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04008077 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
8078 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04008079 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04008080 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04008081 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
8082 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008083#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04008084 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04008085#endif
8086 msg.rpc_argp = &calldata->args;
8087 msg.rpc_resp = &calldata->res;
8088 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04008089
Trond Myklebust9c760d12017-07-31 18:38:50 -04008090 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04008091
8092out_impl_id:
8093 kfree(calldata->res.impl_id);
8094out_server_scope:
8095 kfree(calldata->res.server_scope);
8096out_server_owner:
8097 kfree(calldata->res.server_owner);
8098out_calldata:
8099 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008100out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04008101 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04008102 return ERR_PTR(status);
8103}
8104
8105/*
8106 * _nfs4_proc_exchange_id()
8107 *
8108 * Wrapper for EXCHANGE_ID operation.
8109 */
NeilBrowna52458b2018-12-03 11:30:31 +11008110static int _nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred,
Trond Myklebust9c760d12017-07-31 18:38:50 -04008111 u32 sp4_how)
8112{
8113 struct rpc_task *task;
8114 struct nfs41_exchange_id_args *argp;
8115 struct nfs41_exchange_id_res *resp;
8116 int status;
8117
8118 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
8119 if (IS_ERR(task))
8120 return PTR_ERR(task);
8121
8122 argp = task->tk_msg.rpc_argp;
8123 resp = task->tk_msg.rpc_resp;
8124 status = task->tk_status;
8125 if (status != 0)
8126 goto out;
8127
8128 status = nfs4_check_cl_exchange_flags(resp->flags);
8129 if (status != 0)
8130 goto out;
8131
8132 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8133 if (status != 0)
8134 goto out;
8135
8136 clp->cl_clientid = resp->clientid;
8137 clp->cl_exchange_flags = resp->flags;
8138 clp->cl_seqid = resp->seqid;
8139 /* Client ID is not confirmed */
8140 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8141 clear_bit(NFS4_SESSION_ESTABLISHED,
8142 &clp->cl_session->session_state);
8143
8144 if (clp->cl_serverscope != NULL &&
8145 !nfs41_same_server_scope(clp->cl_serverscope,
8146 resp->server_scope)) {
8147 dprintk("%s: server_scope mismatch detected\n",
8148 __func__);
8149 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8150 }
8151
8152 swap(clp->cl_serverowner, resp->server_owner);
8153 swap(clp->cl_serverscope, resp->server_scope);
8154 swap(clp->cl_implid, resp->impl_id);
8155
8156 /* Save the EXCHANGE_ID verifier session trunk tests */
8157 memcpy(clp->cl_confirm.data, argp->verifier.data,
8158 sizeof(clp->cl_confirm.data));
8159out:
8160 trace_nfs4_exchange_id(clp, status);
8161 rpc_put_task(task);
8162 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008163}
8164
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008165/*
8166 * nfs4_proc_exchange_id()
8167 *
8168 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8169 *
8170 * Since the clientid has expired, all compounds using sessions
8171 * associated with the stale clientid will be returning
8172 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8173 * be in some phase of session reset.
8174 *
8175 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8176 */
NeilBrowna52458b2018-12-03 11:30:31 +11008177int nfs4_proc_exchange_id(struct nfs_client *clp, const struct cred *cred)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008178{
8179 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8180 int status;
8181
8182 /* try SP4_MACH_CRED if krb5i/p */
8183 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8184 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008185 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008186 if (!status)
8187 return 0;
8188 }
8189
8190 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008191 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008192}
8193
Andy Adamson04fa2c62016-09-09 09:22:29 -04008194/**
8195 * nfs4_test_session_trunk
8196 *
8197 * This is an add_xprt_test() test function called from
8198 * rpc_clnt_setup_test_and_add_xprt.
8199 *
8200 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8201 * and is dereferrenced in nfs4_exchange_id_release
8202 *
8203 * Upon success, add the new transport to the rpc_clnt
8204 *
8205 * @clnt: struct rpc_clnt to get new transport
8206 * @xprt: the rpc_xprt to test
8207 * @data: call data for _nfs4_proc_exchange_id.
8208 */
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308209void nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
Andy Adamson04fa2c62016-09-09 09:22:29 -04008210 void *data)
8211{
8212 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008213 struct rpc_task *task;
8214 int status;
8215
Andy Adamson04fa2c62016-09-09 09:22:29 -04008216 u32 sp4_how;
8217
8218 dprintk("--> %s try %s\n", __func__,
8219 xprt->address_strings[RPC_DISPLAY_ADDR]);
8220
8221 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8222
8223 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008224 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8225 if (IS_ERR(task))
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308226 return;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008227
8228 status = task->tk_status;
8229 if (status == 0)
8230 status = nfs4_detect_session_trunking(adata->clp,
8231 task->tk_msg.rpc_resp, xprt);
8232
Santosh kumar pradhan10e037d2018-12-19 12:29:57 +05308233 if (status == 0)
8234 rpc_clnt_xprt_switch_add_xprt(clnt, xprt);
8235
Trond Myklebust9c760d12017-07-31 18:38:50 -04008236 rpc_put_task(task);
Andy Adamson04fa2c62016-09-09 09:22:29 -04008237}
8238EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8239
Trond Myklebust66245532012-05-25 17:18:09 -04008240static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008241 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008242{
8243 struct rpc_message msg = {
8244 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8245 .rpc_argp = clp,
8246 .rpc_cred = cred,
8247 };
8248 int status;
8249
NeilBrown5a0c2572019-05-30 10:41:28 +10008250 status = rpc_call_sync(clp->cl_rpcclient, &msg,
8251 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008252 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008253 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008254 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008255 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8256 return status;
8257}
8258
8259static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008260 const struct cred *cred)
Trond Myklebust66245532012-05-25 17:18:09 -04008261{
8262 unsigned int loop;
8263 int ret;
8264
8265 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8266 ret = _nfs4_proc_destroy_clientid(clp, cred);
8267 switch (ret) {
8268 case -NFS4ERR_DELAY:
8269 case -NFS4ERR_CLIENTID_BUSY:
8270 ssleep(1);
8271 break;
8272 default:
8273 return ret;
8274 }
8275 }
8276 return 0;
8277}
8278
8279int nfs4_destroy_clientid(struct nfs_client *clp)
8280{
NeilBrowna52458b2018-12-03 11:30:31 +11008281 const struct cred *cred;
Trond Myklebust66245532012-05-25 17:18:09 -04008282 int ret = 0;
8283
8284 if (clp->cl_mvops->minor_version < 1)
8285 goto out;
8286 if (clp->cl_exchange_flags == 0)
8287 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008288 if (clp->cl_preserve_clid)
8289 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008290 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008291 ret = nfs4_proc_destroy_clientid(clp, cred);
NeilBrowna52458b2018-12-03 11:30:31 +11008292 put_cred(cred);
Trond Myklebust66245532012-05-25 17:18:09 -04008293 switch (ret) {
8294 case 0:
8295 case -NFS4ERR_STALE_CLIENTID:
8296 clp->cl_exchange_flags = 0;
8297 }
8298out:
8299 return ret;
8300}
8301
Donald Buczek0efb01b2019-07-07 21:26:08 +02008302#endif /* CONFIG_NFS_V4_1 */
8303
Andy Adamson2050f0c2009-04-01 09:22:30 -04008304struct nfs4_get_lease_time_data {
8305 struct nfs4_get_lease_time_args *args;
8306 struct nfs4_get_lease_time_res *res;
8307 struct nfs_client *clp;
8308};
8309
8310static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8311 void *calldata)
8312{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008313 struct nfs4_get_lease_time_data *data =
8314 (struct nfs4_get_lease_time_data *)calldata;
8315
8316 dprintk("--> %s\n", __func__);
8317 /* just setup sequence, do not trigger session recovery
8318 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008319 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008320 &data->args->la_seq_args,
8321 &data->res->lr_seq_res,
8322 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008323 dprintk("<-- %s\n", __func__);
8324}
8325
8326/*
8327 * Called from nfs4_state_manager thread for session setup, so don't recover
8328 * from sequence operation or clientid errors.
8329 */
8330static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8331{
8332 struct nfs4_get_lease_time_data *data =
8333 (struct nfs4_get_lease_time_data *)calldata;
8334
8335 dprintk("--> %s\n", __func__);
Donald Buczek0efb01b2019-07-07 21:26:08 +02008336 if (!nfs4_sequence_done(task, &data->res->lr_seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04008337 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008338 switch (task->tk_status) {
8339 case -NFS4ERR_DELAY:
8340 case -NFS4ERR_GRACE:
8341 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8342 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8343 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008344 /* fall through */
8345 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008346 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008347 return;
8348 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008349 dprintk("<-- %s\n", __func__);
8350}
8351
Trond Myklebust17280172012-03-11 13:11:00 -04008352static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008353 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8354 .rpc_call_done = nfs4_get_lease_time_done,
8355};
8356
8357int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8358{
8359 struct rpc_task *task;
8360 struct nfs4_get_lease_time_args args;
8361 struct nfs4_get_lease_time_res res = {
8362 .lr_fsinfo = fsinfo,
8363 };
8364 struct nfs4_get_lease_time_data data = {
8365 .args = &args,
8366 .res = &res,
8367 .clp = clp,
8368 };
8369 struct rpc_message msg = {
8370 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8371 .rpc_argp = &args,
8372 .rpc_resp = &res,
8373 };
8374 struct rpc_task_setup task_setup = {
8375 .rpc_client = clp->cl_rpcclient,
8376 .rpc_message = &msg,
8377 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008378 .callback_data = &data,
8379 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008380 };
8381 int status;
8382
Anna Schumakerfba83f32018-05-04 16:22:50 -04008383 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008384 task = rpc_run_task(&task_setup);
8385
8386 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008387 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008388
Anna Schumakerf6148712017-04-07 14:15:23 -04008389 status = task->tk_status;
8390 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008391 return status;
8392}
8393
Donald Buczek0efb01b2019-07-07 21:26:08 +02008394#ifdef CONFIG_NFS_V4_1
8395
Andy Adamsonfc931582009-04-01 09:22:31 -04008396/*
8397 * Initialize the values to be used by the client in CREATE_SESSION
8398 * If nfs4_init_session set the fore channel request and response sizes,
8399 * use them.
8400 *
8401 * Set the back channel max_resp_sz_cached to zero to force the client to
8402 * always set csa_cachethis to FALSE because the current implementation
8403 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8404 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008405static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8406 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008407{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008408 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008409 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008410 unsigned int max_bc_slots = rpc_num_bc_slots(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008411
Andy Adamson18aad3d2013-06-26 12:21:49 -04008412 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8413 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8414
Andy Adamsonfc931582009-04-01 09:22:31 -04008415 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008416 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8417 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008418 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008419 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008420
8421 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008422 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008423 __func__,
8424 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008425 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008426
8427 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008428 args->bc_attrs.max_rqst_sz = max_bc_payload;
8429 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008430 args->bc_attrs.max_resp_sz_cached = 0;
8431 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008432 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Trond Myklebust7402a4f2019-07-16 13:51:29 -04008433 if (args->bc_attrs.max_reqs > max_bc_slots)
8434 args->bc_attrs.max_reqs = max_bc_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008435
8436 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8437 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8438 __func__,
8439 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8440 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8441 args->bc_attrs.max_reqs);
8442}
8443
Trond Myklebust79969dd2015-02-18 11:30:18 -08008444static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8445 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008446{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008447 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008448 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008449
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008450 if (rcvd->max_resp_sz > sent->max_resp_sz)
8451 return -EINVAL;
8452 /*
8453 * Our requested max_ops is the minimum we need; we're not
8454 * prepared to break up compounds into smaller pieces than that.
8455 * So, no point even trying to continue if the server won't
8456 * cooperate:
8457 */
8458 if (rcvd->max_ops < sent->max_ops)
8459 return -EINVAL;
8460 if (rcvd->max_reqs == 0)
8461 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008462 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8463 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008464 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008465}
8466
Trond Myklebust79969dd2015-02-18 11:30:18 -08008467static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8468 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008469{
8470 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008471 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008472
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008473 if (!(res->flags & SESSION4_BACK_CHAN))
8474 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008475 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8476 return -EINVAL;
8477 if (rcvd->max_resp_sz < sent->max_resp_sz)
8478 return -EINVAL;
8479 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8480 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008481 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008482 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008483 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008484 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008485out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008486 return 0;
8487}
Andy Adamson8d353012009-04-01 09:22:32 -04008488
Andy Adamson8d353012009-04-01 09:22:32 -04008489static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008490 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008491{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008492 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008493
Trond Myklebust79969dd2015-02-18 11:30:18 -08008494 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008495 if (ret)
8496 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008497 return nfs4_verify_back_channel_attrs(args, res);
8498}
8499
8500static void nfs4_update_session(struct nfs4_session *session,
8501 struct nfs41_create_session_res *res)
8502{
8503 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008504 /* Mark client id and session as being confirmed */
8505 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8506 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008507 session->flags = res->flags;
8508 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008509 if (res->flags & SESSION4_BACK_CHAN)
8510 memcpy(&session->bc_attrs, &res->bc_attrs,
8511 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008512}
8513
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008514static int _nfs4_proc_create_session(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008515 const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008516{
8517 struct nfs4_session *session = clp->cl_session;
8518 struct nfs41_create_session_args args = {
8519 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008520 .clientid = clp->cl_clientid,
8521 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008522 .cb_program = NFS4_CALLBACK,
8523 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008524 struct nfs41_create_session_res res;
8525
Andy Adamsonfc931582009-04-01 09:22:31 -04008526 struct rpc_message msg = {
8527 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8528 .rpc_argp = &args,
8529 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008530 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008531 };
8532 int status;
8533
Chuck Lever6b26cc82016-05-02 14:40:40 -04008534 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008535 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008536
NeilBrown5a0c2572019-05-30 10:41:28 +10008537 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8538 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008539 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008540
Trond Myklebustb519d402016-09-11 14:50:01 -04008541 switch (status) {
8542 case -NFS4ERR_STALE_CLIENTID:
8543 case -NFS4ERR_DELAY:
8544 case -ETIMEDOUT:
8545 case -EACCES:
8546 case -EAGAIN:
8547 goto out;
8548 };
8549
8550 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008551 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008552 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008553 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008554 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008555 if (status)
8556 goto out;
8557 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008558 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008559out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008560 return status;
8561}
8562
8563/*
8564 * Issues a CREATE_SESSION operation to the server.
8565 * It is the responsibility of the caller to verify the session is
8566 * expired before calling this routine.
8567 */
NeilBrowna52458b2018-12-03 11:30:31 +11008568int nfs4_proc_create_session(struct nfs_client *clp, const struct cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008569{
8570 int status;
8571 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008572 struct nfs4_session *session = clp->cl_session;
8573
8574 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8575
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008576 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008577 if (status)
8578 goto out;
8579
Andy Adamsonaacd5532011-11-09 13:58:21 -05008580 /* Init or reset the session slot tables */
8581 status = nfs4_setup_session_slot_tables(session);
8582 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008583 if (status)
8584 goto out;
8585
8586 ptr = (unsigned *)&session->sess_id.data[0];
8587 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8588 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008589out:
8590 dprintk("<-- %s\n", __func__);
8591 return status;
8592}
8593
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008594/*
8595 * Issue the over-the-wire RPC DESTROY_SESSION.
8596 * The caller must serialize access to this routine.
8597 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008598int nfs4_proc_destroy_session(struct nfs4_session *session,
NeilBrowna52458b2018-12-03 11:30:31 +11008599 const struct cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008600{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008601 struct rpc_message msg = {
8602 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8603 .rpc_argp = session,
8604 .rpc_cred = cred,
8605 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008606 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008607
8608 dprintk("--> nfs4_proc_destroy_session\n");
8609
8610 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008611 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8612 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008613
NeilBrown5a0c2572019-05-30 10:41:28 +10008614 status = rpc_call_sync(session->clp->cl_rpcclient, &msg,
8615 RPC_TASK_TIMEOUT | RPC_TASK_NO_ROUND_ROBIN);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008616 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008617
8618 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008619 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008620 "Session has been destroyed regardless...\n", status);
8621
8622 dprintk("<-- nfs4_proc_destroy_session\n");
8623 return status;
8624}
8625
Trond Myklebust7b38c362012-05-23 13:23:31 -04008626/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008627 * Renew the cl_session lease.
8628 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008629struct nfs4_sequence_data {
8630 struct nfs_client *clp;
8631 struct nfs4_sequence_args args;
8632 struct nfs4_sequence_res res;
8633};
8634
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008635static void nfs41_sequence_release(void *data)
8636{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008637 struct nfs4_sequence_data *calldata = data;
8638 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008639
Elena Reshetova212bf412017-10-20 12:53:38 +03008640 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008641 nfs4_schedule_state_renewal(clp);
8642 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008643 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008644}
8645
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008646static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8647{
8648 switch(task->tk_status) {
8649 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008650 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8651 return -EAGAIN;
8652 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008653 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008654 }
8655 return 0;
8656}
8657
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008658static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008659{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008660 struct nfs4_sequence_data *calldata = data;
8661 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008662
Trond Myklebust14516c32010-07-31 14:29:06 -04008663 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8664 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008665
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008666 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008667 if (task->tk_status < 0) {
8668 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008669 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008670 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008671
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008672 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8673 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008674 return;
8675 }
8676 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008677 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008678out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008679 dprintk("<-- %s\n", __func__);
8680}
8681
8682static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8683{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008684 struct nfs4_sequence_data *calldata = data;
8685 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008686 struct nfs4_sequence_args *args;
8687 struct nfs4_sequence_res *res;
8688
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008689 args = task->tk_msg.rpc_argp;
8690 res = task->tk_msg.rpc_resp;
8691
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008692 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008693}
8694
8695static const struct rpc_call_ops nfs41_sequence_ops = {
8696 .rpc_call_done = nfs41_sequence_call_done,
8697 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008698 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008699};
8700
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008701static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008702 const struct cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008703 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008704 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008705{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008706 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008707 struct rpc_message msg = {
8708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8709 .rpc_cred = cred,
8710 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008711 struct rpc_task_setup task_setup_data = {
8712 .rpc_client = clp->cl_rpcclient,
8713 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008714 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008715 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008716 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008717 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008718
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008719 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008720 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008721 goto out_err;
8722
8723 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008724 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008725 if (calldata == NULL)
8726 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008727 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008728 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008729 msg.rpc_argp = &calldata->args;
8730 msg.rpc_resp = &calldata->res;
8731 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008732 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008733
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008734 ret = rpc_run_task(&task_setup_data);
8735 if (IS_ERR(ret))
8736 goto out_err;
8737 return ret;
8738out_put_clp:
8739 nfs_put_client(clp);
8740out_err:
8741 nfs41_release_slot(slot);
8742 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008743}
8744
NeilBrowna52458b2018-12-03 11:30:31 +11008745static int nfs41_proc_async_sequence(struct nfs_client *clp, const struct cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008746{
8747 struct rpc_task *task;
8748 int ret = 0;
8749
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008750 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008751 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008752 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008753 if (IS_ERR(task))
8754 ret = PTR_ERR(task);
8755 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008756 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008757 dprintk("<-- %s status=%d\n", __func__, ret);
8758 return ret;
8759}
8760
NeilBrowna52458b2018-12-03 11:30:31 +11008761static int nfs4_proc_sequence(struct nfs_client *clp, const struct cred *cred)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008762{
8763 struct rpc_task *task;
8764 int ret;
8765
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008766 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008767 if (IS_ERR(task)) {
8768 ret = PTR_ERR(task);
8769 goto out;
8770 }
8771 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008772 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008773 ret = task->tk_status;
8774 rpc_put_task(task);
8775out:
8776 dprintk("<-- %s status=%d\n", __func__, ret);
8777 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008778}
8779
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008780struct nfs4_reclaim_complete_data {
8781 struct nfs_client *clp;
8782 struct nfs41_reclaim_complete_args arg;
8783 struct nfs41_reclaim_complete_res res;
8784};
8785
8786static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8787{
8788 struct nfs4_reclaim_complete_data *calldata = data;
8789
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008790 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008791 &calldata->arg.seq_args,
8792 &calldata->res.seq_res,
8793 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008794}
8795
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008796static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8797{
8798 switch(task->tk_status) {
8799 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008800 wake_up_all(&clp->cl_lock_waitq);
8801 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008802 case -NFS4ERR_COMPLETE_ALREADY:
8803 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8804 break;
8805 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008806 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008807 /* fall through */
8808 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008809 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008810 case -NFS4ERR_BADSESSION:
8811 case -NFS4ERR_DEADSESSION:
8812 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8813 nfs4_schedule_session_recovery(clp->cl_session,
8814 task->tk_status);
8815 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008816 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008817 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008818 }
8819 return 0;
8820}
8821
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008822static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8823{
8824 struct nfs4_reclaim_complete_data *calldata = data;
8825 struct nfs_client *clp = calldata->clp;
8826 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8827
8828 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008829 if (!nfs41_sequence_done(task, res))
8830 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008831
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008832 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008833 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8834 rpc_restart_call_prepare(task);
8835 return;
8836 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008837 dprintk("<-- %s\n", __func__);
8838}
8839
8840static void nfs4_free_reclaim_complete_data(void *data)
8841{
8842 struct nfs4_reclaim_complete_data *calldata = data;
8843
8844 kfree(calldata);
8845}
8846
8847static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8848 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8849 .rpc_call_done = nfs4_reclaim_complete_done,
8850 .rpc_release = nfs4_free_reclaim_complete_data,
8851};
8852
8853/*
8854 * Issue a global reclaim complete.
8855 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008856static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
NeilBrowna52458b2018-12-03 11:30:31 +11008857 const struct cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008858{
8859 struct nfs4_reclaim_complete_data *calldata;
8860 struct rpc_task *task;
8861 struct rpc_message msg = {
8862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008863 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008864 };
8865 struct rpc_task_setup task_setup_data = {
8866 .rpc_client = clp->cl_rpcclient,
8867 .rpc_message = &msg,
8868 .callback_ops = &nfs4_reclaim_complete_call_ops,
NeilBrown5a0c2572019-05-30 10:41:28 +10008869 .flags = RPC_TASK_ASYNC | RPC_TASK_NO_ROUND_ROBIN,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008870 };
8871 int status = -ENOMEM;
8872
8873 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008874 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008875 if (calldata == NULL)
8876 goto out;
8877 calldata->clp = clp;
8878 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008879
Anna Schumakerfba83f32018-05-04 16:22:50 -04008880 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008881 msg.rpc_argp = &calldata->arg;
8882 msg.rpc_resp = &calldata->res;
8883 task_setup_data.callback_data = calldata;
8884 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008885 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008886 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008887 goto out;
8888 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008889 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008890 if (status == 0)
8891 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008892 rpc_put_task(task);
8893out:
8894 dprintk("<-- %s status=%d\n", __func__, status);
8895 return status;
8896}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008897
8898static void
8899nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8900{
8901 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008902 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008903
8904 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008905 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008906 &lgp->res.seq_res, task);
8907 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008908}
8909
8910static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8911{
8912 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008913
8914 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008915 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008916 dprintk("<-- %s\n", __func__);
8917}
8918
8919static int
8920nfs4_layoutget_handle_exception(struct rpc_task *task,
8921 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8922{
Trond Myklebustee314c22012-10-01 17:25:48 -07008923 struct inode *inode = lgp->args.inode;
8924 struct nfs_server *server = NFS_SERVER(inode);
8925 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008926 int nfs4err = task->tk_status;
8927 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008928 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008929
Boaz Harroshed7e5422014-01-22 20:34:54 +02008930 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008931
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008932 nfs4_sequence_free_slot(&lgp->res.seq_res);
8933
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008934 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008935 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008936 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008937
8938 /*
8939 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8940 * on the file. set tk_status to -ENODATA to tell upper layer to
8941 * retry go inband.
8942 */
8943 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008944 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008945 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008946 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008947 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8948 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8949 */
8950 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008951 status = -EOVERFLOW;
8952 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008953 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008954 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008955 * (or clients) writing to the same RAID stripe except when
8956 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008957 *
8958 * Treat it like we would RECALLCONFLICT -- we retry for a little
8959 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008960 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008961 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008962 if (lgp->args.minlength == 0) {
8963 status = -EOVERFLOW;
8964 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008965 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008966 status = -EBUSY;
8967 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008968 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008969 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008970 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008971 case -NFS4ERR_DELEG_REVOKED:
8972 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008973 case -NFS4ERR_EXPIRED:
8974 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008975 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008976 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008977 lo = NFS_I(inode)->layout;
8978 /* If the open stateid was bad, then recover it. */
8979 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008980 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008981 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008982 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008983 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008984 break;
8985 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008986
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008987 /*
8988 * Mark the bad layout state as invalid, then retry
8989 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008990 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008991 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008992 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008993 pnfs_free_lseg_list(&head);
8994 status = -EAGAIN;
8995 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008996 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008997
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008998 err = nfs4_handle_exception(server, nfs4err, exception);
8999 if (!status) {
9000 if (exception->retry)
9001 status = -EAGAIN;
9002 else
9003 status = err;
9004 }
Trond Myklebustee314c22012-10-01 17:25:48 -07009005out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009006 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04009007 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009008}
9009
Fred Isamandacb4522016-09-19 17:47:09 -04009010size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03009011{
9012 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
9013 return nfs_page_array_len(0, max_resp_sz);
9014}
9015
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009016static void nfs4_layoutget_release(void *calldata)
9017{
9018 struct nfs4_layoutget *lgp = calldata;
9019
9020 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04009021 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04009022 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009023 dprintk("<-- %s\n", __func__);
9024}
9025
9026static const struct rpc_call_ops nfs4_layoutget_call_ops = {
9027 .rpc_call_prepare = nfs4_layoutget_prepare,
9028 .rpc_call_done = nfs4_layoutget_done,
9029 .rpc_release = nfs4_layoutget_release,
9030};
9031
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009032struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04009033nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009034{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009035 struct inode *inode = lgp->args.inode;
9036 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009037 struct rpc_task *task;
9038 struct rpc_message msg = {
9039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
9040 .rpc_argp = &lgp->args,
9041 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04009042 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009043 };
9044 struct rpc_task_setup task_setup_data = {
9045 .rpc_client = server->client,
9046 .rpc_message = &msg,
9047 .callback_ops = &nfs4_layoutget_call_ops,
9048 .callback_data = lgp,
9049 .flags = RPC_TASK_ASYNC,
9050 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009051 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04009052 struct nfs4_exception exception = {
9053 .inode = inode,
9054 .timeout = *timeout,
9055 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009056 int status = 0;
9057
9058 dprintk("--> %s\n", __func__);
9059
Peng Tao4bd5a982014-11-17 11:05:17 +08009060 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
9061 pnfs_get_layout_hdr(NFS_I(inode)->layout);
9062
Anna Schumakerfba83f32018-05-04 16:22:50 -04009063 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05009064
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009065 task = rpc_run_task(&task_setup_data);
9066 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009067 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05009068 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009069 if (status != 0)
9070 goto out;
9071
Trond Myklebust18c07782019-02-13 07:55:31 -05009072 if (task->tk_status < 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04009073 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
9074 *timeout = exception.timeout;
Trond Myklebust18c07782019-02-13 07:55:31 -05009075 } else if (lgp->res.layoutp->len == 0) {
9076 status = -EAGAIN;
9077 *timeout = nfs4_update_delay(&exception.timeout);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04009078 } else
9079 lseg = pnfs_layout_process(lgp);
9080out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009081 trace_nfs4_layoutget(lgp->args.ctx,
9082 &lgp->args.range,
9083 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009084 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04009085 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04009086
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009087 rpc_put_task(task);
9088 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04009089 if (status)
9090 return ERR_PTR(status);
9091 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009092}
9093
Benny Halevycbe82602011-05-22 19:52:37 +03009094static void
9095nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
9096{
9097 struct nfs4_layoutreturn *lrp = calldata;
9098
9099 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009100 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009101 &lrp->args.seq_args,
9102 &lrp->res.seq_res,
9103 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04009104 if (!pnfs_layout_is_valid(lrp->args.layout))
9105 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009106}
9107
9108static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
9109{
9110 struct nfs4_layoutreturn *lrp = calldata;
9111 struct nfs_server *server;
9112
9113 dprintk("--> %s\n", __func__);
9114
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009115 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03009116 return;
9117
9118 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009119 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05009120 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04009121 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04009122 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05009123 lrp->args.inode))
9124 goto out_restart;
9125 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009126 default:
9127 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05009128 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009129 case 0:
9130 break;
9131 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009132 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05009133 break;
Trond Myklebustff905142017-11-06 15:28:08 -05009134 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03009135 }
Benny Halevycbe82602011-05-22 19:52:37 +03009136 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05009137 return;
9138out_restart:
9139 task->tk_status = 0;
9140 nfs4_sequence_free_slot(&lrp->res.seq_res);
9141 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009142}
9143
9144static void nfs4_layoutreturn_release(void *calldata)
9145{
9146 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009147 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009148
9149 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009150 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009151 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009152 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009153 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9154 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009155 pnfs_put_layout_hdr(lrp->args.layout);
9156 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009157 kfree(calldata);
9158 dprintk("<-- %s\n", __func__);
9159}
9160
9161static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9162 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9163 .rpc_call_done = nfs4_layoutreturn_done,
9164 .rpc_release = nfs4_layoutreturn_release,
9165};
9166
Peng Tao6c166052014-11-17 09:30:40 +08009167int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009168{
9169 struct rpc_task *task;
9170 struct rpc_message msg = {
9171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9172 .rpc_argp = &lrp->args,
9173 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009174 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009175 };
9176 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009177 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009178 .rpc_message = &msg,
9179 .callback_ops = &nfs4_layoutreturn_call_ops,
9180 .callback_data = lrp,
9181 };
Peng Tao6c166052014-11-17 09:30:40 +08009182 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009183
Andrew Elble99ade3c2015-12-02 09:39:51 -05009184 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9185 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9186 &task_setup_data.rpc_client, &msg);
9187
Benny Halevycbe82602011-05-22 19:52:37 +03009188 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009189 if (!sync) {
9190 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9191 if (!lrp->inode) {
9192 nfs4_layoutreturn_release(lrp);
9193 return -EAGAIN;
9194 }
9195 task_setup_data.flags |= RPC_TASK_ASYNC;
9196 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009197 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009198 task = rpc_run_task(&task_setup_data);
9199 if (IS_ERR(task))
9200 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009201 if (sync)
9202 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009203 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009204 dprintk("<-- %s status=%d\n", __func__, status);
9205 rpc_put_task(task);
9206 return status;
9207}
9208
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009209static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009210_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9211 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009212 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009213{
9214 struct nfs4_getdeviceinfo_args args = {
9215 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009216 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9217 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009218 };
9219 struct nfs4_getdeviceinfo_res res = {
9220 .pdev = pdev,
9221 };
9222 struct rpc_message msg = {
9223 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9224 .rpc_argp = &args,
9225 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009226 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009227 };
9228 int status;
9229
9230 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009231 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009232 if (res.notification & ~args.notify_types)
9233 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009234 if (res.notification != args.notify_types)
9235 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009236
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009237 dprintk("<-- %s status=%d\n", __func__, status);
9238
9239 return status;
9240}
9241
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009242int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9243 struct pnfs_device *pdev,
NeilBrowna52458b2018-12-03 11:30:31 +11009244 const struct cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009245{
9246 struct nfs4_exception exception = { };
9247 int err;
9248
9249 do {
9250 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009251 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009252 &exception);
9253 } while (exception.retry);
9254 return err;
9255}
9256EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9257
Andy Adamson863a3c62011-03-23 13:27:54 +00009258static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9259{
9260 struct nfs4_layoutcommit_data *data = calldata;
9261 struct nfs_server *server = NFS_SERVER(data->args.inode);
9262
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009263 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009264 &data->args.seq_args,
9265 &data->res.seq_res,
9266 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009267}
9268
9269static void
9270nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9271{
9272 struct nfs4_layoutcommit_data *data = calldata;
9273 struct nfs_server *server = NFS_SERVER(data->args.inode);
9274
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009275 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009276 return;
9277
9278 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009279 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9280 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9281 case -NFS4ERR_BADLAYOUT: /* no layout */
9282 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009283 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009284 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009285 break;
9286 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009287 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009288 rpc_restart_call_prepare(task);
9289 return;
9290 }
9291 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009292}
9293
9294static void nfs4_layoutcommit_release(void *calldata)
9295{
9296 struct nfs4_layoutcommit_data *data = calldata;
9297
Andy Adamsondb29c082011-07-30 20:52:38 -04009298 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009299 nfs_post_op_update_inode_force_wcc(data->args.inode,
9300 data->res.fattr);
NeilBrowna52458b2018-12-03 11:30:31 +11009301 put_cred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009302 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009303 kfree(data);
9304}
9305
9306static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9307 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9308 .rpc_call_done = nfs4_layoutcommit_done,
9309 .rpc_release = nfs4_layoutcommit_release,
9310};
9311
9312int
Andy Adamsonef311532011-03-12 02:58:10 -05009313nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009314{
9315 struct rpc_message msg = {
9316 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9317 .rpc_argp = &data->args,
9318 .rpc_resp = &data->res,
9319 .rpc_cred = data->cred,
9320 };
9321 struct rpc_task_setup task_setup_data = {
9322 .task = &data->task,
9323 .rpc_client = NFS_CLIENT(data->args.inode),
9324 .rpc_message = &msg,
9325 .callback_ops = &nfs4_layoutcommit_ops,
9326 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009327 };
9328 struct rpc_task *task;
9329 int status = 0;
9330
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009331 dprintk("NFS: initiating layoutcommit call. sync %d "
9332 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009333 data->args.lastbytewritten,
9334 data->args.inode->i_ino);
9335
Trond Myklebust472e2592015-02-05 16:50:30 -05009336 if (!sync) {
9337 data->inode = nfs_igrab_and_active(data->args.inode);
9338 if (data->inode == NULL) {
9339 nfs4_layoutcommit_release(data);
9340 return -EAGAIN;
9341 }
9342 task_setup_data.flags = RPC_TASK_ASYNC;
9343 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009344 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009345 task = rpc_run_task(&task_setup_data);
9346 if (IS_ERR(task))
9347 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009348 if (sync)
9349 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009350 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009351 dprintk("%s: status %d\n", __func__, status);
9352 rpc_put_task(task);
9353 return status;
9354}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009355
Trond Myklebust302fad72019-02-18 13:32:38 -05009356/*
Andy Adamson97431202013-08-08 10:57:56 -04009357 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9358 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9359 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009360static int
9361_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009362 struct nfs_fsinfo *info,
9363 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009364{
9365 struct nfs41_secinfo_no_name_args args = {
9366 .style = SECINFO_STYLE_CURRENT_FH,
9367 };
9368 struct nfs4_secinfo_res res = {
9369 .flavors = flavors,
9370 };
9371 struct rpc_message msg = {
9372 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9373 .rpc_argp = &args,
9374 .rpc_resp = &res,
9375 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009376 struct rpc_clnt *clnt = server->client;
NeilBrowna52458b2018-12-03 11:30:31 +11009377 const struct cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009378 int status;
9379
9380 if (use_integrity) {
9381 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009382 cred = nfs4_get_clid_cred(server->nfs_client);
9383 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009384 }
9385
9386 dprintk("--> %s\n", __func__);
9387 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
NeilBrown5a0c2572019-05-30 10:41:28 +10009388 &res.seq_res, RPC_TASK_NO_ROUND_ROBIN);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009389 dprintk("<-- %s status=%d\n", __func__, status);
9390
NeilBrowna52458b2018-12-03 11:30:31 +11009391 put_cred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009392
9393 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009394}
9395
9396static int
9397nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9398 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9399{
Trond Myklebust0688e642019-04-07 13:59:09 -04009400 struct nfs4_exception exception = {
9401 .interruptible = true,
9402 };
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009403 int err;
9404 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009405 /* first try using integrity protection */
9406 err = -NFS4ERR_WRONGSEC;
9407
9408 /* try to use integrity protection with machine cred */
9409 if (_nfs4_is_integrity_protected(server->nfs_client))
9410 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9411 flavors, true);
9412
9413 /*
9414 * if unable to use integrity protection, or SECINFO with
9415 * integrity protection returns NFS4ERR_WRONGSEC (which is
9416 * disallowed by spec, but exists in deployed servers) use
9417 * the current filesystem's rpc_client and the user cred.
9418 */
9419 if (err == -NFS4ERR_WRONGSEC)
9420 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9421 flavors, false);
9422
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009423 switch (err) {
9424 case 0:
9425 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009426 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009427 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009428 default:
9429 err = nfs4_handle_exception(server, err, &exception);
9430 }
9431 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009432out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009433 return err;
9434}
9435
9436static int
9437nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9438 struct nfs_fsinfo *info)
9439{
9440 int err;
9441 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009442 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009443 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009444 struct nfs4_secinfo4 *secinfo;
9445 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009446
9447 page = alloc_page(GFP_KERNEL);
9448 if (!page) {
9449 err = -ENOMEM;
9450 goto out;
9451 }
9452
9453 flavors = page_address(page);
9454 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9455
9456 /*
9457 * Fall back on "guess and check" method if
9458 * the server doesn't support SECINFO_NO_NAME
9459 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009460 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009461 err = nfs4_find_root_sec(server, fhandle, info);
9462 goto out_freepage;
9463 }
9464 if (err)
9465 goto out_freepage;
9466
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009467 for (i = 0; i < flavors->num_flavors; i++) {
9468 secinfo = &flavors->flavors[i];
9469
9470 switch (secinfo->flavor) {
9471 case RPC_AUTH_NULL:
9472 case RPC_AUTH_UNIX:
9473 case RPC_AUTH_GSS:
9474 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9475 &secinfo->flavor_info);
9476 break;
9477 default:
9478 flavor = RPC_AUTH_MAXFLAVOR;
9479 break;
9480 }
9481
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009482 if (!nfs_auth_info_match(&server->auth_info, flavor))
9483 flavor = RPC_AUTH_MAXFLAVOR;
9484
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009485 if (flavor != RPC_AUTH_MAXFLAVOR) {
9486 err = nfs4_lookup_root_sec(server, fhandle,
9487 info, flavor);
9488 if (!err)
9489 break;
9490 }
9491 }
9492
9493 if (flavor == RPC_AUTH_MAXFLAVOR)
9494 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009495
9496out_freepage:
9497 put_page(page);
9498 if (err == -EACCES)
9499 return -EPERM;
9500out:
9501 return err;
9502}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009503
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009504static int _nfs41_test_stateid(struct nfs_server *server,
9505 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009506 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009507{
9508 int status;
9509 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009510 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009511 };
9512 struct nfs41_test_stateid_res res;
9513 struct rpc_message msg = {
9514 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9515 .rpc_argp = &args,
9516 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009517 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009518 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009519 struct rpc_clnt *rpc_client = server->client;
9520
9521 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9522 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009523
Chuck Lever38527b12012-07-11 16:30:23 -04009524 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009525 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009526 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009527 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009528 if (status != NFS_OK) {
9529 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009530 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009531 }
9532 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009533 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009534}
9535
Trond Myklebust43912bb2016-09-22 13:38:56 -04009536static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9537 int err, struct nfs4_exception *exception)
9538{
9539 exception->retry = 0;
9540 switch(err) {
9541 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009542 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009543 nfs4_handle_exception(server, err, exception);
9544 break;
9545 case -NFS4ERR_BADSESSION:
9546 case -NFS4ERR_BADSLOT:
9547 case -NFS4ERR_BAD_HIGH_SLOT:
9548 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9549 case -NFS4ERR_DEADSESSION:
9550 nfs4_do_handle_exception(server, err, exception);
9551 }
9552}
9553
Chuck Lever38527b12012-07-11 16:30:23 -04009554/**
9555 * nfs41_test_stateid - perform a TEST_STATEID operation
9556 *
9557 * @server: server / transport on which to perform the operation
9558 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009559 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009560 *
9561 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9562 * Otherwise a negative NFS4ERR value is returned if the operation
9563 * failed or the state ID is not currently valid.
9564 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009565static int nfs41_test_stateid(struct nfs_server *server,
9566 nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009567 const struct cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009568{
Trond Myklebust0688e642019-04-07 13:59:09 -04009569 struct nfs4_exception exception = {
9570 .interruptible = true,
9571 };
Bryan Schumaker7d974792011-06-02 14:59:08 -04009572 int err;
9573 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009574 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009575 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009576 } while (exception.retry);
9577 return err;
9578}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009579
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009580struct nfs_free_stateid_data {
9581 struct nfs_server *server;
9582 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009583 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009584};
9585
9586static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9587{
9588 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009589 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009590 &data->args.seq_args,
9591 &data->res.seq_res,
9592 task);
9593}
9594
9595static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9596{
9597 struct nfs_free_stateid_data *data = calldata;
9598
9599 nfs41_sequence_done(task, &data->res.seq_res);
9600
9601 switch (task->tk_status) {
9602 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009603 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009604 rpc_restart_call_prepare(task);
9605 }
9606}
9607
9608static void nfs41_free_stateid_release(void *calldata)
9609{
9610 kfree(calldata);
9611}
9612
Trond Myklebust17f26b12013-08-21 15:48:42 -04009613static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009614 .rpc_call_prepare = nfs41_free_stateid_prepare,
9615 .rpc_call_done = nfs41_free_stateid_done,
9616 .rpc_release = nfs41_free_stateid_release,
9617};
9618
Anna Schumaker2f261022018-05-15 13:03:39 -04009619/**
9620 * nfs41_free_stateid - perform a FREE_STATEID operation
9621 *
9622 * @server: server / transport on which to perform the operation
9623 * @stateid: state ID to release
9624 * @cred: credential
Trond Myklebust302fad72019-02-18 13:32:38 -05009625 * @privileged: set to true if this call needs to be privileged
Anna Schumaker2f261022018-05-15 13:03:39 -04009626 *
9627 * Note: this function is always asynchronous.
9628 */
9629static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009630 const nfs4_stateid *stateid,
NeilBrowna52458b2018-12-03 11:30:31 +11009631 const struct cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009632 bool privileged)
9633{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009634 struct rpc_message msg = {
9635 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009636 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009637 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009638 struct rpc_task_setup task_setup = {
9639 .rpc_client = server->client,
9640 .rpc_message = &msg,
9641 .callback_ops = &nfs41_free_stateid_ops,
9642 .flags = RPC_TASK_ASYNC,
9643 };
9644 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009645 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009646
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009647 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9648 &task_setup.rpc_client, &msg);
9649
Chuck Lever38527b12012-07-11 16:30:23 -04009650 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009651 data = kmalloc(sizeof(*data), GFP_NOFS);
9652 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009653 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009654 data->server = server;
9655 nfs4_stateid_copy(&data->args.stateid, stateid);
9656
9657 task_setup.callback_data = data;
9658
9659 msg.rpc_argp = &data->args;
9660 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009661 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009662 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009663 if (IS_ERR(task))
9664 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009665 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009666 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009667}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009668
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009669static void
9670nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009671{
NeilBrowna52458b2018-12-03 11:30:31 +11009672 const struct cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009673
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009674 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009675 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009676}
9677
Trond Myklebust36281ca2012-03-04 18:13:56 -05009678static bool nfs41_match_stateid(const nfs4_stateid *s1,
9679 const nfs4_stateid *s2)
9680{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009681 if (s1->type != s2->type)
9682 return false;
9683
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009684 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009685 return false;
9686
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009687 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009688 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009689
Anna Schumaker045c5512017-01-11 16:59:48 -05009690 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009691}
9692
Andy Adamson557134a2009-04-01 09:21:53 -04009693#endif /* CONFIG_NFS_V4_1 */
9694
Trond Myklebust36281ca2012-03-04 18:13:56 -05009695static bool nfs4_match_stateid(const nfs4_stateid *s1,
9696 const nfs4_stateid *s2)
9697{
Trond Myklebustf597c532012-03-04 18:13:56 -05009698 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009699}
9700
9701
Trond Myklebust17280172012-03-11 13:11:00 -04009702static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009703 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009704 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009705 .recover_open = nfs4_open_reclaim,
9706 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009707 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009708 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009709};
9710
Andy Adamson591d71c2009-04-01 09:22:47 -04009711#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009712static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009713 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9714 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9715 .recover_open = nfs4_open_reclaim,
9716 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009717 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009718 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009719 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009720};
9721#endif /* CONFIG_NFS_V4_1 */
9722
Trond Myklebust17280172012-03-11 13:11:00 -04009723static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009724 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009725 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009726 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009727 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009728 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009729};
9730
Andy Adamson591d71c2009-04-01 09:22:47 -04009731#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009732static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009733 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9734 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009735 .recover_open = nfs41_open_expired,
9736 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009737 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009738};
9739#endif /* CONFIG_NFS_V4_1 */
9740
Trond Myklebust17280172012-03-11 13:11:00 -04009741static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009742 .sched_state_renewal = nfs4_proc_async_renew,
NeilBrownf15e1e82018-12-03 11:30:30 +11009743 .get_state_renewal_cred = nfs4_get_renew_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009744 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009745};
9746
9747#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009748static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009749 .sched_state_renewal = nfs41_proc_async_sequence,
NeilBrownf15e1e82018-12-03 11:30:30 +11009750 .get_state_renewal_cred = nfs4_get_machine_cred,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009751 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009752};
9753#endif
9754
Chuck Leverec011fe2013-10-17 14:12:39 -04009755static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009756 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009757 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009758};
9759
9760#if defined(CONFIG_NFS_V4_1)
9761static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009762 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009763 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009764};
9765#endif /* CONFIG_NFS_V4_1 */
9766
Trond Myklebust97dc1352010-06-16 09:52:26 -04009767static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9768 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009769 .init_caps = NFS_CAP_READDIRPLUS
9770 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009771 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009772 .init_client = nfs40_init_client,
9773 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009774 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009775 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009776 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009777 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009778 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009779 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009780 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9781 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9782 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009783 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009784};
9785
9786#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009787static struct nfs_seqid *
9788nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9789{
9790 return NULL;
9791}
9792
Trond Myklebust97dc1352010-06-16 09:52:26 -04009793static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9794 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009795 .init_caps = NFS_CAP_READDIRPLUS
9796 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009797 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009798 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009799 | NFS_CAP_ATOMIC_OPEN_V1
9800 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009801 .init_client = nfs41_init_client,
9802 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009803 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009804 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009805 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009806 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009807 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009808 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009809 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009810 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9811 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9812 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009813 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009814};
9815#endif
9816
Steve Dickson42c2c422013-05-22 12:50:38 -04009817#if defined(CONFIG_NFS_V4_2)
9818static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9819 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009820 .init_caps = NFS_CAP_READDIRPLUS
9821 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009822 | NFS_CAP_POSIX_LOCK
9823 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009824 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009825 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009826 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009827 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009828 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009829 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009830 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009831 | NFS_CAP_LAYOUTSTATS
Trond Myklebust3eb86092019-02-08 10:31:05 -05009832 | NFS_CAP_CLONE
9833 | NFS_CAP_LAYOUTERROR,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009834 .init_client = nfs41_init_client,
9835 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009836 .match_stateid = nfs41_match_stateid,
9837 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009838 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009839 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009840 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009841 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009842 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009843 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9844 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9845 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009846 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009847};
9848#endif
9849
Trond Myklebust97dc1352010-06-16 09:52:26 -04009850const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9851 [0] = &nfs_v4_0_minor_ops,
9852#if defined(CONFIG_NFS_V4_1)
9853 [1] = &nfs_v4_1_minor_ops,
9854#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009855#if defined(CONFIG_NFS_V4_2)
9856 [2] = &nfs_v4_2_minor_ops,
9857#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009858};
9859
Trond Myklebust13997822016-07-24 17:10:52 -04009860static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009861{
9862 ssize_t error, error2;
9863
9864 error = generic_listxattr(dentry, list, size);
9865 if (error < 0)
9866 return error;
9867 if (list) {
9868 list += error;
9869 size -= error;
9870 }
9871
9872 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9873 if (error2 < 0)
9874 return error2;
9875 return error + error2;
9876}
9877
Trond Myklebust17f26b12013-08-21 15:48:42 -04009878static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009879 .create = nfs_create,
9880 .lookup = nfs_lookup,
9881 .atomic_open = nfs_atomic_open,
9882 .link = nfs_link,
9883 .unlink = nfs_unlink,
9884 .symlink = nfs_symlink,
9885 .mkdir = nfs_mkdir,
9886 .rmdir = nfs_rmdir,
9887 .mknod = nfs_mknod,
9888 .rename = nfs_rename,
9889 .permission = nfs_permission,
9890 .getattr = nfs_getattr,
9891 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009892 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009893};
9894
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009895static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009896 .permission = nfs_permission,
9897 .getattr = nfs_getattr,
9898 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009899 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009900};
9901
David Howells509de812006-08-22 20:06:11 -04009902const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009903 .version = 4, /* protocol version */
9904 .dentry_ops = &nfs4_dentry_operations,
9905 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009906 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009907 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009908 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009909 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009910 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009911 .getattr = nfs4_proc_getattr,
9912 .setattr = nfs4_proc_setattr,
9913 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009914 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009915 .access = nfs4_proc_access,
9916 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009917 .create = nfs4_proc_create,
9918 .remove = nfs4_proc_remove,
9919 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009920 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009921 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009922 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009923 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009924 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009925 .link = nfs4_proc_link,
9926 .symlink = nfs4_proc_symlink,
9927 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009928 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009929 .readdir = nfs4_proc_readdir,
9930 .mknod = nfs4_proc_mknod,
9931 .statfs = nfs4_proc_statfs,
9932 .fsinfo = nfs4_proc_fsinfo,
9933 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009934 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009935 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009936 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009937 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009938 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009939 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009940 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009941 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009942 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009943 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009944 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009945 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009946 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009947 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009948 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009949 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009950 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009951 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009952 .create_server = nfs4_create_server,
9953 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009954};
9955
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009956static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009957 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009958 .list = nfs4_xattr_list_nfs4_acl,
9959 .get = nfs4_xattr_get_nfs4_acl,
9960 .set = nfs4_xattr_set_nfs4_acl,
9961};
9962
9963const struct xattr_handler *nfs4_xattr_handlers[] = {
9964 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009965#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9966 &nfs4_xattr_nfs4_label_handler,
9967#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009968 NULL
9969};
9970
Linus Torvalds1da177e2005-04-16 15:20:36 -07009971/*
9972 * Local variables:
9973 * c-basic-offset: 8
9974 * End:
9975 */