blob: 10c20a5b075d9d3314673f520fe5b2dfcd04fb96 [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);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040096static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
97 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,
102 struct rpc_cred *cred,
103 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 *,
106 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400107static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
108 struct rpc_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,
364 struct rpc_cred *cred)
365{
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,
373 struct rpc_cred *cred)
374{
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,
381 struct rpc_cred *cred)
382{
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 Myklebust65de8722008-12-23 15:21:44 -0500403static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
404{
405 int res = 0;
406
407 might_sleep();
408
NeilBrown8478eaa2014-09-18 16:09:27 +1000409 freezable_schedule_timeout_killable_unsafe(
410 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500411 if (fatal_signal_pending(current))
412 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500413 return res;
414}
415
416/* This is the error handling routine for processes that are allowed
417 * to sleep.
418 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400419static int nfs4_do_handle_exception(struct nfs_server *server,
420 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500421{
422 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500423 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400424 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500425 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500426 int ret = errorcode;
427
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400428 exception->delay = 0;
429 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500430 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400431
432 if (stateid == NULL && state != NULL)
433 stateid = &state->stateid;
434
Trond Myklebust65de8722008-12-23 15:21:44 -0500435 switch(errorcode) {
436 case 0:
437 return 0;
Trond Myklebustcf61eb22018-05-29 22:06:08 -0400438 case -NFS4ERR_BADHANDLE:
439 case -ESTALE:
440 if (inode != NULL && S_ISREG(inode->i_mode))
441 pnfs_destroy_layout(NFS_I(inode));
442 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400443 case -NFS4ERR_DELEG_REVOKED:
444 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400445 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400446 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400447 if (inode != NULL && stateid != NULL) {
448 nfs_inode_find_state_and_recover(inode,
449 stateid);
450 goto wait_on_recovery;
451 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500452 /* Fall through */
Trond Myklebust272289a2016-09-22 13:39:15 -0400453 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400454 if (inode) {
455 int err;
456
457 err = nfs_async_inode_return_delegation(inode,
458 stateid);
459 if (err == 0)
460 goto wait_on_recovery;
461 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
462 exception->retry = 1;
463 break;
464 }
465 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500466 if (state == NULL)
467 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400468 ret = nfs4_schedule_stateid_recovery(server, state);
469 if (ret < 0)
470 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500471 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500472 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500473 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500474 nfs4_schedule_lease_recovery(clp);
475 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400476 case -NFS4ERR_MOVED:
477 ret = nfs4_schedule_migration_recovery(server);
478 if (ret < 0)
479 break;
480 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400481 case -NFS4ERR_LEASE_MOVED:
482 nfs4_schedule_lease_moved_recovery(clp);
483 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500484#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400485 case -NFS4ERR_BADSESSION:
486 case -NFS4ERR_BADSLOT:
487 case -NFS4ERR_BAD_HIGH_SLOT:
488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
489 case -NFS4ERR_DEADSESSION:
490 case -NFS4ERR_SEQ_FALSE_RETRY:
491 case -NFS4ERR_SEQ_MISORDERED:
492 dprintk("%s ERROR: %d Reset session\n", __func__,
493 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400494 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400495 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500496#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500497 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500498 if (exception->timeout > HZ) {
499 /* We have retried a decent amount, time to
500 * fail
501 */
502 ret = -EBUSY;
503 break;
504 }
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500505 /* Fall through */
Trond Myklebust65de8722008-12-23 15:21:44 -0500506 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400507 nfs_inc_server_stats(server, NFSIOS_DELAY);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -0500508 /* Fall through */
Trond Myklebust2598ed32015-09-20 16:10:18 -0400509 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400510 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400511 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400512 exception->delay = 1;
513 return 0;
514
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400515 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500516 case -NFS4ERR_OLD_STATEID:
517 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800518 break;
519 case -NFS4ERR_BADOWNER:
520 /* The following works around a Linux server bug! */
521 case -NFS4ERR_BADNAME:
522 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
523 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
524 exception->retry = 1;
525 printk(KERN_WARNING "NFS: v4 server %s "
526 "does not accept raw "
527 "uid/gids. "
528 "Reenabling the idmapper.\n",
529 server->nfs_client->cl_hostname);
530 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500531 }
532 /* We failed to handle the error */
533 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500534wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400535 exception->recovering = 1;
536 return 0;
537}
538
539/* This is the error handling routine for processes that are allowed
540 * to sleep.
541 */
542int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
543{
544 struct nfs_client *clp = server->nfs_client;
545 int ret;
546
547 ret = nfs4_do_handle_exception(server, errorcode, exception);
548 if (exception->delay) {
549 ret = nfs4_delay(server->client, &exception->timeout);
550 goto out_retry;
551 }
552 if (exception->recovering) {
553 ret = nfs4_wait_clnt_recover(clp);
554 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
555 return -EIO;
556 goto out_retry;
557 }
558 return ret;
559out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500560 if (ret == 0)
561 exception->retry = 1;
562 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500563}
564
Trond Myklebust037fc982015-09-20 15:51:00 -0400565static int
566nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
567 int errorcode, struct nfs4_exception *exception)
568{
569 struct nfs_client *clp = server->nfs_client;
570 int ret;
571
572 ret = nfs4_do_handle_exception(server, errorcode, exception);
573 if (exception->delay) {
574 rpc_delay(task, nfs4_update_delay(&exception->timeout));
575 goto out_retry;
576 }
577 if (exception->recovering) {
578 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
579 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
580 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
581 goto out_retry;
582 }
583 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
584 ret = -EIO;
585 return ret;
586out_retry:
Bill Baker0f90be12018-06-19 16:24:58 -0500587 if (ret == 0) {
Trond Myklebust037fc982015-09-20 15:51:00 -0400588 exception->retry = 1;
Bill Baker0f90be12018-06-19 16:24:58 -0500589 /*
590 * For NFS4ERR_MOVED, the client transport will need to
591 * be recomputed after migration recovery has completed.
592 */
593 if (errorcode == -NFS4ERR_MOVED)
594 rpc_task_release_transport(task);
595 }
Trond Myklebust037fc982015-09-20 15:51:00 -0400596 return ret;
597}
598
Olga Kornievskaia0f913a52018-07-09 15:13:33 -0400599int
Trond Myklebust037fc982015-09-20 15:51:00 -0400600nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
601 struct nfs4_state *state, long *timeout)
602{
603 struct nfs4_exception exception = {
604 .state = state,
605 };
606
607 if (task->tk_status >= 0)
608 return 0;
609 if (timeout)
610 exception.timeout = *timeout;
611 task->tk_status = nfs4_async_handle_exception(task, server,
612 task->tk_status,
613 &exception);
614 if (exception.delay && timeout)
615 *timeout = exception.timeout;
616 if (exception.retry)
617 return -EAGAIN;
618 return 0;
619}
620
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400621/*
622 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
623 * or 'false' otherwise.
624 */
625static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
626{
627 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500628 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400629}
Trond Myklebust65de8722008-12-23 15:21:44 -0500630
Trond Myklebust452e9352010-07-31 14:29:06 -0400631static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700633 spin_lock(&clp->cl_lock);
634 if (time_before(clp->cl_last_renewal,timestamp))
635 clp->cl_last_renewal = timestamp;
636 spin_unlock(&clp->cl_lock);
637}
638
Trond Myklebust452e9352010-07-31 14:29:06 -0400639static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
640{
Trond Myklebustbe824162015-07-05 14:50:46 -0400641 struct nfs_client *clp = server->nfs_client;
642
643 if (!nfs4_has_session(clp))
644 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400645}
646
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400647struct nfs4_call_sync_data {
648 const struct nfs_server *seq_server;
649 struct nfs4_sequence_args *seq_args;
650 struct nfs4_sequence_res *seq_res;
651};
652
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800653void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400654 struct nfs4_sequence_res *res, int cache_reply,
655 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400656{
657 args->sa_slot = NULL;
658 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400659 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400660
661 res->sr_slot = NULL;
662}
663
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400664static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400665{
666 struct nfs4_slot *slot = res->sr_slot;
667 struct nfs4_slot_table *tbl;
668
Chuck Lever3bd23842013-08-09 12:49:19 -0400669 tbl = slot->table;
670 spin_lock(&tbl->slot_tbl_lock);
671 if (!nfs41_wake_and_assign_slot(tbl, slot))
672 nfs4_free_slot(tbl, slot);
673 spin_unlock(&tbl->slot_tbl_lock);
674
675 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400676}
677
678static int nfs40_sequence_done(struct rpc_task *task,
679 struct nfs4_sequence_res *res)
680{
681 if (res->sr_slot != NULL)
682 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400683 return 1;
684}
685
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400686#if defined(CONFIG_NFS_V4_1)
687
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400688static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400689{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500690 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400691 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500692 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400693
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400694 if (!slot)
695 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500696 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500698
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400699 /* Bump the slot sequence number */
700 if (slot->seq_done)
701 slot->seq_nr++;
702 slot->seq_done = 0;
703
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500704 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500705 /* Be nice to the server: try to ensure that the last transmitted
706 * value for highest_user_slotid <= target_highest_slotid
707 */
708 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
709 send_new_highest_used_slotid = true;
710
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500711 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500712 send_new_highest_used_slotid = false;
713 goto out_unlock;
714 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500715 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500716
717 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
718 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500719out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500720 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500721 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400722 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400723 if (waitqueue_active(&tbl->slot_waitq))
724 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400725}
726
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400727static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
728{
729 nfs41_release_slot(res->sr_slot);
730 res->sr_slot = NULL;
731}
732
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400733static int nfs41_sequence_process(struct rpc_task *task,
734 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400735{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500736 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500737 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400738 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500739 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500740 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400741
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500742 if (slot == NULL)
743 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400744 /* don't increment the sequence number if the task wasn't sent */
745 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400746 goto out;
747
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500748 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500749
Trond Myklebustac20d162012-12-15 15:36:07 -0500750 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400751 if (res->sr_status != -NFS4ERR_DELAY)
752 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500753 interrupted = true;
754 }
755
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400756 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500757 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400758 switch (res->sr_status) {
759 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400760 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400761 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500762 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500763 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500764 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400765 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
766 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500767 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400768 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500769 case 1:
770 /*
771 * sr_status remains 1 if an RPC level error occurred.
772 * The server may or may not have processed the sequence
773 * operation..
774 * Mark the slot as having hosted an interrupted RPC call.
775 */
776 slot->interrupted = 1;
777 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400778 case -NFS4ERR_DELAY:
779 /* The server detected a resend of the RPC call and
780 * returned NFS4ERR_DELAY as per Section 2.10.6.2
781 * of RFC5661.
782 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500783 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400784 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500785 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500786 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400787 goto out_retry;
Trond Myklebustf9312a52018-06-09 19:10:31 -0400788 case -NFS4ERR_RETRY_UNCACHED_REP:
789 case -NFS4ERR_SEQ_FALSE_RETRY:
790 /*
791 * The server thinks we tried to replay a request.
792 * Retry the call after bumping the sequence ID.
793 */
794 goto retry_new_seq;
Trond Myklebust85563072012-12-11 10:31:12 -0500795 case -NFS4ERR_BADSLOT:
796 /*
797 * The slot id we used was probably retired. Try again
798 * using a different slot id.
799 */
Trond Myklebust99589102018-06-09 12:50:50 -0400800 if (slot->slot_nr < slot->table->target_highest_slotid)
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400801 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500802 goto retry_nowait;
803 case -NFS4ERR_SEQ_MISORDERED:
804 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500805 * Was the last operation on this sequence interrupted?
806 * If so, retry after bumping the sequence number.
807 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400808 if (interrupted)
809 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500810 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500811 * Could this slot have been previously retired?
812 * If so, then the server may be expecting seq_nr = 1!
813 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500814 if (slot->seq_nr != 1) {
815 slot->seq_nr = 1;
816 goto retry_nowait;
817 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400818 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400819 default:
820 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400821 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400822 }
823out:
824 /* The session may be reset by one of the error handlers. */
825 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500826out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500827 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400828session_recover:
829 nfs4_schedule_session_recovery(session, res->sr_status);
830 goto retry_nowait;
831retry_new_seq:
832 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500833retry_nowait:
834 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400835 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500836 task->tk_status = 0;
837 ret = 0;
838 }
839 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400840out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400841 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400842 goto out;
843 rpc_delay(task, NFS4_POLL_RETRY_MAX);
844 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400845}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400846
847int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
848{
849 if (!nfs41_sequence_process(task, res))
850 return 0;
851 if (res->sr_slot != NULL)
852 nfs41_sequence_free_slot(res);
853 return 1;
854
855}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500856EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400857
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400858static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
859{
860 if (res->sr_slot == NULL)
861 return 1;
862 if (res->sr_slot->table->session != NULL)
863 return nfs41_sequence_process(task, res);
864 return nfs40_sequence_done(task, res);
865}
866
867static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
868{
869 if (res->sr_slot != NULL) {
870 if (res->sr_slot->table->session != NULL)
871 nfs41_sequence_free_slot(res);
872 else
873 nfs40_sequence_free_slot(res);
874 }
875}
876
Peng Tao2c4b1312014-06-11 05:24:16 +0800877int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400878{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500879 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400880 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400881 if (!res->sr_slot->table->session)
882 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400883 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400884}
Peng Tao2c4b1312014-06-11 05:24:16 +0800885EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400886
Andy Adamsonce5039c2009-04-01 09:22:13 -0400887static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
888{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400889 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400890
Trond Myklebust035168ab2010-06-16 09:52:26 -0400891 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
892
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500893 nfs4_setup_sequence(data->seq_server->nfs_client,
894 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400895}
896
Andy Adamson69ab40c2009-04-01 09:22:19 -0400897static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
898{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400899 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400900
Trond Myklebust14516c32010-07-31 14:29:06 -0400901 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400902}
903
Trond Myklebust17280172012-03-11 13:11:00 -0400904static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400905 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400906 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400907};
908
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400909static void
910nfs4_sequence_process_interrupted(struct nfs_client *client,
911 struct nfs4_slot *slot, struct rpc_cred *cred)
912{
913 struct rpc_task *task;
914
915 task = _nfs41_proc_sequence(client, cred, slot, true);
916 if (!IS_ERR(task))
917 rpc_put_task_async(task);
918}
919
Chuck Lever3bd23842013-08-09 12:49:19 -0400920#else /* !CONFIG_NFS_V4_1 */
921
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400922static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
923{
924 return nfs40_sequence_done(task, res);
925}
926
927static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
928{
929 if (res->sr_slot != NULL)
930 nfs40_sequence_free_slot(res);
931}
932
Peng Tao2c4b1312014-06-11 05:24:16 +0800933int nfs4_sequence_done(struct rpc_task *task,
934 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400935{
Chuck Lever3bd23842013-08-09 12:49:19 -0400936 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400937}
Peng Tao2c4b1312014-06-11 05:24:16 +0800938EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400939
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400940static void
941nfs4_sequence_process_interrupted(struct nfs_client *client,
942 struct nfs4_slot *slot, struct rpc_cred *cred)
943{
944 WARN_ON_ONCE(1);
945 slot->interrupted = 0;
946}
947
Chuck Lever3bd23842013-08-09 12:49:19 -0400948#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400949
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400950static
951void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
952 struct nfs4_sequence_res *res,
953 struct nfs4_slot *slot)
954{
955 if (!slot)
956 return;
957 slot->privileged = args->sa_privileged ? 1 : 0;
958 args->sa_slot = slot;
959
960 res->sr_slot = slot;
961 res->sr_timestamp = jiffies;
962 res->sr_status_flags = 0;
963 res->sr_status = 1;
964
965}
966
967int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500968 struct nfs4_sequence_args *args,
969 struct nfs4_sequence_res *res,
970 struct rpc_task *task)
971{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500972 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500973 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500974 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500975
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500976 /* slot already allocated? */
977 if (res->sr_slot != NULL)
978 goto out_start;
979
Anna Schumaker76ee0352017-01-10 16:49:31 -0500980 if (session) {
981 tbl = &session->fc_slot_table;
982 task->tk_timeout = 0;
983 }
984
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400985 for (;;) {
986 spin_lock(&tbl->slot_tbl_lock);
987 /* The state manager will wait until the slot table is empty */
988 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
989 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500990
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400991 slot = nfs4_alloc_slot(tbl);
992 if (IS_ERR(slot)) {
993 /* Try again in 1/4 second */
994 if (slot == ERR_PTR(-ENOMEM))
995 task->tk_timeout = HZ >> 2;
996 goto out_sleep;
997 }
998 spin_unlock(&tbl->slot_tbl_lock);
999
1000 if (likely(!slot->interrupted))
1001 break;
1002 nfs4_sequence_process_interrupted(client,
1003 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -05001004 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001005
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04001006 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -05001007
Anna Schumakerad05cc02017-01-11 13:37:06 -05001008 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -05001009out_start:
1010 rpc_call_start(task);
1011 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -05001012
1013out_sleep:
1014 if (args->sa_privileged)
1015 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
1016 NULL, RPC_PRIORITY_PRIVILEGED);
1017 else
1018 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
1019 spin_unlock(&tbl->slot_tbl_lock);
1020 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05001021}
1022EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
1023
Chuck Lever9915ea72013-08-09 12:48:27 -04001024static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
1025{
1026 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05001027 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -04001028 data->seq_args, data->seq_res, task);
1029}
1030
1031static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
1032{
1033 struct nfs4_call_sync_data *data = calldata;
1034 nfs4_sequence_done(task, data->seq_res);
1035}
1036
1037static const struct rpc_call_ops nfs40_call_sync_ops = {
1038 .rpc_call_prepare = nfs40_call_sync_prepare,
1039 .rpc_call_done = nfs40_call_sync_done,
1040};
1041
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001042static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001043 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001044 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001045 struct nfs4_sequence_args *args,
1046 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001047{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001048 int ret;
1049 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001050 struct nfs_client *clp = server->nfs_client;
1051 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001052 .seq_server = server,
1053 .seq_args = args,
1054 .seq_res = res,
1055 };
1056 struct rpc_task_setup task_setup = {
1057 .rpc_client = clnt,
1058 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001059 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001060 .callback_data = &data
1061 };
1062
1063 task = rpc_run_task(&task_setup);
1064 if (IS_ERR(task))
1065 ret = PTR_ERR(task);
1066 else {
1067 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001068 rpc_put_task(task);
1069 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001070 return ret;
1071}
1072
Bryan Schumaker7c513052011-03-24 17:12:24 +00001073int nfs4_call_sync(struct rpc_clnt *clnt,
1074 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001075 struct rpc_message *msg,
1076 struct nfs4_sequence_args *args,
1077 struct nfs4_sequence_res *res,
1078 int cache_reply)
1079{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001080 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001081 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001082}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001083
Trond Myklebust3c591172018-07-31 15:54:10 -04001084static void
1085nfs4_inc_nlink_locked(struct inode *inode)
1086{
1087 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1088 inc_nlink(inode);
1089}
1090
1091static void
1092nfs4_dec_nlink_locked(struct inode *inode)
1093{
1094 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_OTHER;
1095 drop_nlink(inode);
1096}
1097
1098static void
1099update_changeattr_locked(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001100 unsigned long timestamp, unsigned long cache_validity)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101{
1102 struct nfs_inode *nfsi = NFS_I(dir);
1103
Trond Myklebust16e14372018-03-20 16:53:31 -04001104 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1105 | NFS_INO_INVALID_MTIME
Trond Myklebust5636ec42018-07-31 15:54:11 -04001106 | NFS_INO_INVALID_DATA
1107 | cache_validity;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001108 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001109 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1110 nfsi->attrtimeo_timestamp = jiffies;
1111 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001113 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001114 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1115 NFS_INO_INVALID_ACL;
1116 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001117 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001118 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001119 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
Trond Myklebustc8d07152018-07-31 15:54:12 -04001120 nfsi->cache_validity &= ~NFS_INO_INVALID_CHANGE;
David Howellsde242c02012-12-20 21:52:38 +00001121 nfs_fscache_invalidate(dir);
Trond Myklebust3c591172018-07-31 15:54:10 -04001122}
1123
1124static void
1125update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04001126 unsigned long timestamp, unsigned long cache_validity)
Trond Myklebust3c591172018-07-31 15:54:10 -04001127{
1128 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04001129 update_changeattr_locked(dir, cinfo, timestamp, cache_validity);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 spin_unlock(&dir->i_lock);
1131}
1132
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001133struct nfs4_open_createattrs {
1134 struct nfs4_label *label;
1135 struct iattr *sattr;
1136 const __u32 verf[2];
1137};
1138
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001139static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1140 int err, struct nfs4_exception *exception)
1141{
1142 if (err != -EINVAL)
1143 return false;
1144 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1145 return false;
1146 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1147 exception->retry = 1;
1148 return true;
1149}
1150
Trond Myklebust6ae37332015-01-30 14:21:14 -05001151static u32
1152nfs4_map_atomic_open_share(struct nfs_server *server,
1153 fmode_t fmode, int openflags)
1154{
1155 u32 res = 0;
1156
1157 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1158 case FMODE_READ:
1159 res = NFS4_SHARE_ACCESS_READ;
1160 break;
1161 case FMODE_WRITE:
1162 res = NFS4_SHARE_ACCESS_WRITE;
1163 break;
1164 case FMODE_READ|FMODE_WRITE:
1165 res = NFS4_SHARE_ACCESS_BOTH;
1166 }
1167 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1168 goto out;
1169 /* Want no delegation if we're using O_DIRECT */
1170 if (openflags & O_DIRECT)
1171 res |= NFS4_SHARE_WANT_NO_DELEG;
1172out:
1173 return res;
1174}
1175
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001176static enum open_claim_type4
1177nfs4_map_atomic_open_claim(struct nfs_server *server,
1178 enum open_claim_type4 claim)
1179{
1180 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1181 return claim;
1182 switch (claim) {
1183 default:
1184 return claim;
1185 case NFS4_OPEN_CLAIM_FH:
1186 return NFS4_OPEN_CLAIM_NULL;
1187 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1188 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1189 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1190 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1191 }
1192}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001193
1194static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195{
1196 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001197 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001198 p->o_res.seqid = p->o_arg.seqid;
1199 p->c_res.seqid = p->c_arg.seqid;
1200 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001201 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001202 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001203 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001204}
1205
Al Viro82a2c1b2011-06-22 18:30:55 -04001206static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001207 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001208 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001209 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001210 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001211{
Al Viro82a2c1b2011-06-22 18:30:55 -04001212 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001213 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001214 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001215 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001216 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001217 struct nfs4_opendata *p;
1218
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001219 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 if (p == NULL)
1221 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001222
1223 p->f_label = nfs4_label_alloc(server, gfp_mask);
1224 if (IS_ERR(p->f_label))
1225 goto err_free_p;
1226
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->a_label = nfs4_label_alloc(server, gfp_mask);
1228 if (IS_ERR(p->a_label))
1229 goto err_free_f;
1230
Trond Myklebust63f5f792015-01-23 19:19:25 -05001231 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1232 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001233 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001234 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001235 nfs_sb_active(dentry->d_sb);
1236 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 p->dir = parent;
1238 p->owner = sp;
1239 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001240 p->o_arg.open_flags = flags;
1241 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001242 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001243 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001244 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1245 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001246 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1247 * will return permission denied for all bits until close */
1248 if (!(flags & O_EXCL)) {
1249 /* ask server to check for all possible rights as results
1250 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001251 switch (p->o_arg.claim) {
1252 default:
1253 break;
1254 case NFS4_OPEN_CLAIM_NULL:
1255 case NFS4_OPEN_CLAIM_FH:
1256 p->o_arg.access = NFS4_ACCESS_READ |
1257 NFS4_ACCESS_MODIFY |
1258 NFS4_ACCESS_EXTEND |
1259 NFS4_ACCESS_EXECUTE;
1260 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001261 }
David Howells7539bba2006-08-22 20:06:09 -04001262 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001263 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1264 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001265 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001267 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001268 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001269 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001270 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001271 case NFS4_OPEN_CLAIM_NULL:
1272 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1273 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1274 p->o_arg.fh = NFS_FH(dir);
1275 break;
1276 case NFS4_OPEN_CLAIM_PREVIOUS:
1277 case NFS4_OPEN_CLAIM_FH:
1278 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1279 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001280 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001281 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001282 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001283 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001284 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001285
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001286 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001287 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001289 p->c_arg.fh = &p->o_res.fh;
1290 p->c_arg.stateid = &p->o_res.stateid;
1291 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001292 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001293 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001294 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001295
1296err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001297 nfs4_label_free(p->a_label);
1298err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001299 nfs4_label_free(p->f_label);
1300err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001301 kfree(p);
1302err:
1303 dput(parent);
1304 return NULL;
1305}
1306
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001307static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001308{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001309 struct nfs4_opendata *p = container_of(kref,
1310 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001311 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001312
Fred Isaman30ae2412016-10-18 13:39:51 -04001313 nfs4_lgopen_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001314 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001315 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001316 if (p->state != NULL)
1317 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001318 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001319
Kinglong Meea49c2692015-07-27 15:31:38 +08001320 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001321 nfs4_label_free(p->f_label);
1322
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001323 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001324 dput(p->dentry);
1325 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001326 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001327 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001328 kfree(p);
1329}
1330
1331static void nfs4_opendata_put(struct nfs4_opendata *p)
1332{
1333 if (p != NULL)
1334 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001335}
1336
Trond Myklebust24311f82015-09-20 10:50:17 -04001337static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1338 fmode_t fmode)
1339{
1340 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1341 case FMODE_READ|FMODE_WRITE:
1342 return state->n_rdwr != 0;
1343 case FMODE_WRITE:
1344 return state->n_wronly != 0;
1345 case FMODE_READ:
1346 return state->n_rdonly != 0;
1347 }
1348 WARN_ON_ONCE(1);
1349 return false;
1350}
1351
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001352static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001353{
1354 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355
Trond Myklebust536e43d2012-01-17 22:04:26 -05001356 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 goto out;
1358 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001359 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001360 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1361 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001362 break;
1363 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001364 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1365 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001366 break;
1367 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001368 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1369 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001370 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001371out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001372 return ret;
1373}
1374
Trond Myklebust2a606182015-08-19 22:30:00 -05001375static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1376 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001377{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001378 if (delegation == NULL)
1379 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001380 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001381 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001382 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1383 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001384 switch (claim) {
1385 case NFS4_OPEN_CLAIM_NULL:
1386 case NFS4_OPEN_CLAIM_FH:
1387 break;
1388 case NFS4_OPEN_CLAIM_PREVIOUS:
1389 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1390 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05001391 /* Fall through */
Trond Myklebust2a606182015-08-19 22:30:00 -05001392 default:
1393 return 0;
1394 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001395 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001396 return 1;
1397}
1398
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001399static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001400{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001401 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001402 case FMODE_WRITE:
1403 state->n_wronly++;
1404 break;
1405 case FMODE_READ:
1406 state->n_rdonly++;
1407 break;
1408 case FMODE_READ|FMODE_WRITE:
1409 state->n_rdwr++;
1410 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001411 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001412}
1413
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001414#ifdef CONFIG_NFS_V4_1
1415static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1416{
1417 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1418 return true;
1419 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1420 return true;
1421 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1422 return true;
1423 return false;
1424}
1425#endif /* CONFIG_NFS_V4_1 */
1426
Trond Myklebustc9399f22017-11-06 15:28:01 -05001427static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1428{
1429 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1430 wake_up_all(&state->waitq);
1431}
1432
1433static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1434 const nfs4_stateid *stateid)
1435{
1436 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1437 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1438
1439 if (stateid_seqid == state_seqid + 1U ||
1440 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1441 nfs_state_log_update_open_stateid(state);
1442 else
1443 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1444}
1445
Trond Myklebust4f14c192014-02-12 19:15:06 -05001446static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001447{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001448 struct nfs_client *clp = state->owner->so_server->nfs_client;
1449 bool need_recover = false;
1450
1451 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1452 need_recover = true;
1453 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1454 need_recover = true;
1455 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1456 need_recover = true;
1457 if (need_recover)
1458 nfs4_state_mark_reclaim_nograce(clp, state);
1459}
1460
Trond Myklebustc9399f22017-11-06 15:28:01 -05001461/*
1462 * Check for whether or not the caller may update the open stateid
1463 * to the value passed in by stateid.
1464 *
1465 * Note: This function relies heavily on the server implementing
1466 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1467 * correctly.
1468 * i.e. The stateid seqids have to be initialised to 1, and
1469 * are then incremented on every state transition.
1470 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001471static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001472 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001473{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001474 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1475 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1476 if (stateid->seqid == cpu_to_be32(1))
1477 nfs_state_log_update_open_stateid(state);
1478 else
1479 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001480 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001481 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001482
1483 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1484 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001485 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001486 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001487 return false;
1488}
1489
Trond Myklebustf95549c2015-01-23 18:06:09 -05001490static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1491{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001492 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1493 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001494 if (state->n_wronly)
1495 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1496 if (state->n_rdonly)
1497 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1498 if (state->n_rdwr)
1499 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001500 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001501}
1502
Trond Myklebust226056c2014-02-11 10:41:07 -05001503static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1504 nfs4_stateid *stateid, fmode_t fmode)
1505{
1506 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1507 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1508 case FMODE_WRITE:
1509 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1510 break;
1511 case FMODE_READ:
1512 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1513 break;
1514 case 0:
1515 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1516 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1517 clear_bit(NFS_OPEN_STATE, &state->flags);
1518 }
1519 if (stateid == NULL)
1520 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001521 /* Handle OPEN+OPEN_DOWNGRADE races */
1522 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1523 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001524 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001525 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001526 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001527 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001528 nfs4_stateid_copy(&state->stateid, stateid);
1529 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001530 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001531out:
1532 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001533}
1534
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001535static void nfs_clear_open_stateid(struct nfs4_state *state,
1536 nfs4_stateid *arg_stateid,
1537 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001538{
1539 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001540 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1541 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1542 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001543 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001544 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1545 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001546}
1547
Trond Myklebust1393d962016-09-22 13:39:13 -04001548static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001549 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001550{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001551 DEFINE_WAIT(wait);
1552 int status = 0;
1553 for (;;) {
1554
1555 if (!nfs_need_update_open_stateid(state, stateid))
1556 return;
1557 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001558 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001559 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001560 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001561 /* Rely on seqids for serialisation with NFSv4.0 */
1562 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1563 break;
1564
1565 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1566 /*
1567 * Ensure we process the state changes in the same order
1568 * in which the server processed them by delaying the
1569 * update of the stateid until we are in sequence.
1570 */
1571 write_sequnlock(&state->seqlock);
1572 spin_unlock(&state->owner->so_lock);
1573 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001574 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001575 if (!signal_pending(current)) {
1576 if (schedule_timeout(5*HZ) == 0)
1577 status = -EAGAIN;
1578 else
1579 status = 0;
1580 } else
1581 status = -EINTR;
1582 finish_wait(&state->waitq, &wait);
1583 rcu_read_lock();
1584 spin_lock(&state->owner->so_lock);
1585 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001586 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001587
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001588 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1589 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001590 nfs4_stateid_copy(freeme, &state->open_stateid);
1591 nfs_test_and_clear_all_open_stateid(state);
1592 }
1593
Trond Myklebuste999e802014-02-10 18:20:47 -05001594 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1595 nfs4_stateid_copy(&state->stateid, stateid);
1596 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001597 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001598 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001599}
1600
Trond Myklebustc9399f22017-11-06 15:28:01 -05001601static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001602 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001603 fmode_t fmode,
1604 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001606 /*
1607 * Protect the call to nfs4_state_set_mode_locked and
1608 * serialise the stateid update
1609 */
1610 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001611 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1612 switch (fmode) {
1613 case FMODE_READ:
1614 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1615 break;
1616 case FMODE_WRITE:
1617 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1618 break;
1619 case FMODE_READ|FMODE_WRITE:
1620 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001621 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001622 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001623 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001624}
1625
1626static void nfs_state_set_delegation(struct nfs4_state *state,
1627 const nfs4_stateid *deleg_stateid,
1628 fmode_t fmode)
1629{
1630 /*
1631 * Protect the call to nfs4_state_set_mode_locked and
1632 * serialise the stateid update
1633 */
1634 write_seqlock(&state->seqlock);
1635 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1636 set_bit(NFS_DELEGATED_STATE, &state->flags);
1637 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638}
1639
Trond Myklebust9f0c5122018-09-05 14:07:15 -04001640static void nfs_state_clear_delegation(struct nfs4_state *state)
1641{
1642 write_seqlock(&state->seqlock);
1643 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
1644 clear_bit(NFS_DELEGATED_STATE, &state->flags);
1645 write_sequnlock(&state->seqlock);
1646}
1647
Trond Myklebust1393d962016-09-22 13:39:13 -04001648static int update_open_stateid(struct nfs4_state *state,
1649 const nfs4_stateid *open_stateid,
1650 const nfs4_stateid *delegation,
1651 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001652{
Trond Myklebust1393d962016-09-22 13:39:13 -04001653 struct nfs_server *server = NFS_SERVER(state->inode);
1654 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001655 struct nfs_inode *nfsi = NFS_I(state->inode);
1656 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001657 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001658 int ret = 0;
1659
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001660 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001661
1662 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001663 spin_lock(&state->owner->so_lock);
1664 if (open_stateid != NULL) {
1665 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1666 ret = 1;
1667 }
1668
Trond Myklebust34310432008-12-23 15:21:38 -05001669 deleg_cur = rcu_dereference(nfsi->delegation);
1670 if (deleg_cur == NULL)
1671 goto no_delegation;
1672
1673 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001674 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001675 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001676 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001677 goto no_delegation_unlock;
1678
1679 if (delegation == NULL)
1680 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001681 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001682 goto no_delegation_unlock;
1683
Trond Myklebustb7391f42008-12-23 15:21:52 -05001684 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001685 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001686 ret = 1;
1687no_delegation_unlock:
1688 spin_unlock(&deleg_cur->lock);
1689no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001690 if (ret)
1691 update_open_stateflags(state, fmode);
1692 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001693 rcu_read_unlock();
1694
Trond Myklebust4f14c192014-02-12 19:15:06 -05001695 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001696 nfs4_schedule_state_manager(clp);
1697 if (freeme.type != 0)
1698 nfs4_test_and_free_stateid(server, &freeme,
1699 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001700
1701 return ret;
1702}
1703
Trond Myklebust39071e62015-01-24 15:07:56 -05001704static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1705 const nfs4_stateid *stateid)
1706{
1707 struct nfs4_state *state = lsp->ls_state;
1708 bool ret = false;
1709
1710 spin_lock(&state->state_lock);
1711 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1712 goto out_noupdate;
1713 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1714 goto out_noupdate;
1715 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1716 ret = true;
1717out_noupdate:
1718 spin_unlock(&state->state_lock);
1719 return ret;
1720}
Trond Myklebust34310432008-12-23 15:21:38 -05001721
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001722static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723{
1724 struct nfs_delegation *delegation;
1725
Trond Myklebustf5086242018-03-20 16:43:13 -04001726 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001727 rcu_read_lock();
1728 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001729 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001730 rcu_read_unlock();
1731 return;
1732 }
1733 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001734 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001735}
1736
Trond Myklebust6ee41262007-07-08 14:11:36 -04001737static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001738{
1739 struct nfs4_state *state = opendata->state;
1740 struct nfs_inode *nfsi = NFS_I(state->inode);
1741 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001742 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001743 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001744 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001745 nfs4_stateid stateid;
1746 int ret = -EAGAIN;
1747
Trond Myklebustaac00a82007-07-05 19:02:21 -04001748 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001749 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001750 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001751 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001752 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001753 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001754 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001755 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001756 rcu_read_lock();
1757 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001758 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001759 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001760 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001761 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001762 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001763 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001764 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001765 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001766 if (!opendata->is_recover) {
1767 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1768 if (ret != 0)
1769 goto out;
1770 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001771 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001772
1773 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001774 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001775 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001776 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001777out:
1778 return ERR_PTR(ret);
1779out_return_state:
1780 atomic_inc(&state->count);
1781 return state;
1782}
1783
Andy Adamsone23008e2012-10-02 21:07:32 -04001784static void
1785nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1786{
1787 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1788 struct nfs_delegation *delegation;
1789 int delegation_flags = 0;
1790
1791 rcu_read_lock();
1792 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1793 if (delegation)
1794 delegation_flags = delegation->flags;
1795 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001796 switch (data->o_arg.claim) {
1797 default:
1798 break;
1799 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1800 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001801 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1802 "returning a delegation for "
1803 "OPEN(CLAIM_DELEGATE_CUR)\n",
1804 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001805 return;
1806 }
1807 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001808 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001809 data->owner->so_cred,
1810 data->o_res.delegation_type,
1811 &data->o_res.delegation,
1812 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001813 else
1814 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001815 data->owner->so_cred,
1816 data->o_res.delegation_type,
1817 &data->o_res.delegation,
1818 data->o_res.pagemod_limit);
Jeff Layton8b199e52018-07-05 05:48:14 -04001819
1820 if (data->o_res.do_recall)
1821 nfs_async_inode_return_delegation(state->inode,
1822 &data->o_res.delegation);
Andy Adamsone23008e2012-10-02 21:07:32 -04001823}
1824
1825/*
1826 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1827 * and update the nfs4_state.
1828 */
1829static struct nfs4_state *
1830_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1831{
1832 struct inode *inode = data->state->inode;
1833 struct nfs4_state *state = data->state;
1834 int ret;
1835
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001836 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001837 if (data->rpc_status)
1838 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001839 /* cached opens have already been processed */
1840 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001841 }
1842
Andy Adamsone23008e2012-10-02 21:07:32 -04001843 ret = nfs_refresh_inode(inode, &data->f_attr);
1844 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001845 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001846
1847 if (data->o_res.delegation_type != 0)
1848 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001849update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001850 update_open_stateid(state, &data->o_res.stateid, NULL,
1851 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001852 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001853
1854 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001855}
1856
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001857static struct inode *
1858nfs4_opendata_get_inode(struct nfs4_opendata *data)
1859{
1860 struct inode *inode;
1861
1862 switch (data->o_arg.claim) {
1863 case NFS4_OPEN_CLAIM_NULL:
1864 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1865 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1866 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1867 return ERR_PTR(-EAGAIN);
1868 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1869 &data->f_attr, data->f_label);
1870 break;
1871 default:
1872 inode = d_inode(data->dentry);
1873 ihold(inode);
1874 nfs_refresh_inode(inode, &data->f_attr);
1875 }
1876 return inode;
1877}
1878
Andy Adamsone23008e2012-10-02 21:07:32 -04001879static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001880nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1881{
1882 struct nfs4_state *state;
1883 struct inode *inode;
1884
1885 inode = nfs4_opendata_get_inode(data);
1886 if (IS_ERR(inode))
1887 return ERR_CAST(inode);
1888 if (data->state != NULL && data->state->inode == inode) {
1889 state = data->state;
1890 atomic_inc(&state->count);
1891 } else
1892 state = nfs4_get_open_state(inode, data->owner);
1893 iput(inode);
1894 if (state == NULL)
1895 state = ERR_PTR(-ENOMEM);
1896 return state;
1897}
1898
Andy Adamsone23008e2012-10-02 21:07:32 -04001899static struct nfs4_state *
1900_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001901{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001902 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001903
Trond Myklebustaac00a82007-07-05 19:02:21 -04001904 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001905 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001906 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001907 goto out;
1908 }
1909
Trond Myklebust75e8c482017-08-08 10:38:07 -04001910 state = nfs4_opendata_find_nfs4_state(data);
1911 if (IS_ERR(state))
1912 goto out;
1913
Andy Adamsone23008e2012-10-02 21:07:32 -04001914 if (data->o_res.delegation_type != 0)
1915 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001916 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001917 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001918out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001919 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001920 return state;
1921}
1922
Andy Adamsone23008e2012-10-02 21:07:32 -04001923static struct nfs4_state *
1924nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1925{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001926 struct nfs4_state *ret;
1927
Andy Adamsone23008e2012-10-02 21:07:32 -04001928 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001929 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1930 else
1931 ret = _nfs4_opendata_to_nfs4_state(data);
1932 nfs4_sequence_free_slot(&data->o_res.seq_res);
1933 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001934}
1935
Trond Myklebust0de43972018-09-02 15:57:01 -04001936static struct nfs_open_context *
1937nfs4_state_find_open_context_mode(struct nfs4_state *state, fmode_t mode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001938{
1939 struct nfs_inode *nfsi = NFS_I(state->inode);
1940 struct nfs_open_context *ctx;
1941
Trond Myklebust0de43972018-09-02 15:57:01 -04001942 rcu_read_lock();
1943 list_for_each_entry_rcu(ctx, &nfsi->open_files, list) {
Trond Myklebust864472e2006-01-03 09:55:15 +01001944 if (ctx->state != state)
1945 continue;
Trond Myklebust0de43972018-09-02 15:57:01 -04001946 if ((ctx->mode & mode) != mode)
1947 continue;
1948 if (!get_nfs_open_context(ctx))
1949 continue;
1950 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001951 return ctx;
1952 }
Trond Myklebust0de43972018-09-02 15:57:01 -04001953 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001954 return ERR_PTR(-ENOENT);
1955}
1956
Trond Myklebust0de43972018-09-02 15:57:01 -04001957static struct nfs_open_context *
1958nfs4_state_find_open_context(struct nfs4_state *state)
1959{
1960 struct nfs_open_context *ctx;
1961
1962 ctx = nfs4_state_find_open_context_mode(state, FMODE_READ|FMODE_WRITE);
1963 if (!IS_ERR(ctx))
1964 return ctx;
1965 ctx = nfs4_state_find_open_context_mode(state, FMODE_WRITE);
1966 if (!IS_ERR(ctx))
1967 return ctx;
1968 return nfs4_state_find_open_context_mode(state, FMODE_READ);
1969}
1970
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001971static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1972 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001973{
1974 struct nfs4_opendata *opendata;
1975
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001976 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001977 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001978 if (opendata == NULL)
1979 return ERR_PTR(-ENOMEM);
1980 opendata->state = state;
1981 atomic_inc(&state->count);
1982 return opendata;
1983}
1984
Trond Myklebust24311f82015-09-20 10:50:17 -04001985static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1986 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001987{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001988 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001989 int ret;
1990
Trond Myklebust24311f82015-09-20 10:50:17 -04001991 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001992 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001993 opendata->o_arg.open_flags = 0;
1994 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001995 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1996 NFS_SB(opendata->dentry->d_sb),
1997 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001998 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1999 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
2000 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002001 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002002 if (ret != 0)
2003 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002004 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002005 if (IS_ERR(newstate))
2006 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04002007 if (newstate != opendata->state)
2008 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04002009 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04002010 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01002011}
2012
2013static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
2014{
Trond Myklebust864472e2006-01-03 09:55:15 +01002015 int ret;
2016
Trond Myklebust4f14c192014-02-12 19:15:06 -05002017 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
2018 clear_bit(NFS_O_RDWR_STATE, &state->flags);
2019 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2020 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002021 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04002022 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002023 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01002024 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04002025 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2026 if (ret != 0)
2027 return ret;
2028 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2029 if (ret != 0)
2030 return ret;
2031 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
2032 if (ret != 0)
2033 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002034 /*
2035 * We may have performed cached opens for all three recoveries.
2036 * Check if we need to update the current stateid.
2037 */
2038 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05002039 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002040 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002041 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05002042 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04002043 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002044 }
Trond Myklebust864472e2006-01-03 09:55:15 +01002045 return 0;
2046}
2047
Linus Torvalds1da177e2005-04-16 15:20:36 -07002048/*
2049 * OPEN_RECLAIM:
2050 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002052static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002053{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002054 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01002055 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002056 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 int status;
2058
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002059 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2060 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002061 if (IS_ERR(opendata))
2062 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002063 rcu_read_lock();
2064 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05002065 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04002066 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04002067 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01002068 opendata->o_arg.u.delegation_type = delegation_type;
2069 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002070 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002071 return status;
2072}
2073
Trond Myklebust539cd032007-06-05 11:46:42 -04002074static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075{
2076 struct nfs_server *server = NFS_SERVER(state->inode);
2077 struct nfs4_exception exception = { };
2078 int err;
2079 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002080 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002081 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002082 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2083 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002084 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002085 break;
2086 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002087 } while (exception.retry);
2088 return err;
2089}
2090
Trond Myklebust864472e2006-01-03 09:55:15 +01002091static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2092{
2093 struct nfs_open_context *ctx;
2094 int ret;
2095
2096 ctx = nfs4_state_find_open_context(state);
2097 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002098 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002099 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002100 put_nfs_open_context(ctx);
2101 return ret;
2102}
2103
NeilBrowndce26302017-12-13 09:57:09 +11002104static 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 -07002105{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002106 switch (err) {
2107 default:
2108 printk(KERN_ERR "NFS: %s: unhandled error "
2109 "%d.\n", __func__, err);
2110 case 0:
2111 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002112 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002113 case -ESTALE:
2114 break;
2115 case -NFS4ERR_BADSESSION:
2116 case -NFS4ERR_BADSLOT:
2117 case -NFS4ERR_BAD_HIGH_SLOT:
2118 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2119 case -NFS4ERR_DEADSESSION:
2120 set_bit(NFS_DELEGATED_STATE, &state->flags);
2121 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2122 return -EAGAIN;
2123 case -NFS4ERR_STALE_CLIENTID:
2124 case -NFS4ERR_STALE_STATEID:
2125 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002126 /* Don't recall a delegation if it was lost */
2127 nfs4_schedule_lease_recovery(server->nfs_client);
2128 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002129 case -NFS4ERR_MOVED:
2130 nfs4_schedule_migration_recovery(server);
2131 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002132 case -NFS4ERR_LEASE_MOVED:
2133 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2134 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002135 case -NFS4ERR_DELEG_REVOKED:
2136 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002137 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002138 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002139 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002140 nfs_inode_find_state_and_recover(state->inode,
2141 stateid);
2142 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002143 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002144 case -NFS4ERR_DELAY:
2145 case -NFS4ERR_GRACE:
2146 set_bit(NFS_DELEGATED_STATE, &state->flags);
2147 ssleep(1);
2148 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002149 case -ENOMEM:
2150 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002151 if (fl) {
2152 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2153 if (lsp)
2154 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2155 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002156 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002157 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002158 return err;
2159}
2160
Trond Myklebust24311f82015-09-20 10:50:17 -04002161int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2162 struct nfs4_state *state, const nfs4_stateid *stateid,
2163 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002164{
2165 struct nfs_server *server = NFS_SERVER(state->inode);
2166 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002167 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002168
2169 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2170 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2171 if (IS_ERR(opendata))
2172 return PTR_ERR(opendata);
2173 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002174 nfs_state_clear_delegation(state);
Trond Myklebust24311f82015-09-20 10:50:17 -04002175 switch (type & (FMODE_READ|FMODE_WRITE)) {
2176 case FMODE_READ|FMODE_WRITE:
2177 case FMODE_WRITE:
2178 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2179 if (err)
2180 break;
2181 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2182 if (err)
2183 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002184 /* Fall through */
Trond Myklebust24311f82015-09-20 10:50:17 -04002185 case FMODE_READ:
2186 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2187 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002188 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002189 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002190}
2191
Chuck Leverbe05c862013-08-09 12:49:47 -04002192static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2193{
2194 struct nfs4_opendata *data = calldata;
2195
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002196 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2197 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002198}
2199
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002200static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2201{
2202 struct nfs4_opendata *data = calldata;
2203
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002204 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002205
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002206 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002207 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002208 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002209 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002210 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002211 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002212 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002213}
2214
2215static void nfs4_open_confirm_release(void *calldata)
2216{
2217 struct nfs4_opendata *data = calldata;
2218 struct nfs4_state *state = NULL;
2219
2220 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002221 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002222 goto out_free;
2223 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002224 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002225 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002226 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002227 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002228 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002229out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002230 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002231}
2232
2233static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002234 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002235 .rpc_call_done = nfs4_open_confirm_done,
2236 .rpc_release = nfs4_open_confirm_release,
2237};
2238
2239/*
2240 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2241 */
2242static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2243{
David Howells2b0143b2015-03-17 22:25:59 +00002244 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002245 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002246 struct rpc_message msg = {
2247 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2248 .rpc_argp = &data->c_arg,
2249 .rpc_resp = &data->c_res,
2250 .rpc_cred = data->owner->so_cred,
2251 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002252 struct rpc_task_setup task_setup_data = {
2253 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002254 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002255 .callback_ops = &nfs4_open_confirm_ops,
2256 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002257 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002258 .flags = RPC_TASK_ASYNC,
2259 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002260 int status;
2261
Anna Schumakerfba83f32018-05-04 16:22:50 -04002262 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2263 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002264 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002265 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002266 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002267 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002268 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002269 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002270 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002271 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002272 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002273 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002274 smp_wmb();
2275 } else
2276 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002277 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278 return status;
2279}
2280
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002282{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 struct nfs4_opendata *data = calldata;
2284 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002285 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002286 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002287
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002288 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002289 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002290 /*
2291 * Check if we still need to send an OPEN call, or if we can use
2292 * a delegation instead.
2293 */
2294 if (data->state != NULL) {
2295 struct nfs_delegation *delegation;
2296
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002297 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002298 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002299 rcu_read_lock();
2300 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002301 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002302 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002303 rcu_read_unlock();
2304 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002305 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002306 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002307 switch (claim) {
2308 default:
2309 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002310 case NFS4_OPEN_CLAIM_PREVIOUS:
2311 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2312 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002313 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05002314 /* Fall through */
Trond Myklebust8188df12013-04-23 14:31:19 -04002315 case NFS4_OPEN_CLAIM_FH:
2316 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002317 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002318 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002319 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002320 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002321 &data->o_res.seq_res,
2322 task) != 0)
2323 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002324
2325 /* Set the create mode (note dependency on the session type) */
2326 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2327 if (data->o_arg.open_flags & O_EXCL) {
2328 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2329 if (nfs4_has_persistent_session(clp))
2330 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2331 else if (clp->cl_mvops->minor_version > 0)
2332 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2333 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002334 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002335unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002336 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002337 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002338out_no_action:
2339 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002340out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002341 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002342}
2343
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002344static void nfs4_open_done(struct rpc_task *task, void *calldata)
2345{
2346 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002348 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002349
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002350 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002351 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002352
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002353 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002354 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2355 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002356 case S_IFREG:
2357 break;
2358 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002359 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002360 break;
2361 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002362 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002363 break;
2364 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002365 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002366 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002367 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002368 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002369 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2370 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002371 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002372 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002373}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002374
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002375static void nfs4_open_release(void *calldata)
2376{
2377 struct nfs4_opendata *data = calldata;
2378 struct nfs4_state *state = NULL;
2379
2380 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002381 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002382 goto out_free;
2383 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002384 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002385 goto out_free;
2386 /* In case we need an open_confirm, no cleanup! */
2387 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2388 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002389 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002390 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002391 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002392out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002393 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002394}
2395
2396static const struct rpc_call_ops nfs4_open_ops = {
2397 .rpc_call_prepare = nfs4_open_prepare,
2398 .rpc_call_done = nfs4_open_done,
2399 .rpc_release = nfs4_open_release,
2400};
2401
Fred Isaman3b65a302016-09-19 10:06:49 -04002402static int nfs4_run_open_task(struct nfs4_opendata *data,
2403 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002404{
David Howells2b0143b2015-03-17 22:25:59 +00002405 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002406 struct nfs_server *server = NFS_SERVER(dir);
2407 struct nfs_openargs *o_arg = &data->o_arg;
2408 struct nfs_openres *o_res = &data->o_res;
2409 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002410 struct rpc_message msg = {
2411 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2412 .rpc_argp = o_arg,
2413 .rpc_resp = o_res,
2414 .rpc_cred = data->owner->so_cred,
2415 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002416 struct rpc_task_setup task_setup_data = {
2417 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002418 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002419 .callback_ops = &nfs4_open_ops,
2420 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002421 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002422 .flags = RPC_TASK_ASYNC,
2423 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002424 int status;
2425
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002426 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002427 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002428 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002429 data->cancelled = false;
2430 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002431 if (!ctx) {
2432 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002433 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002434 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002435 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002436 pnfs_lgopen_prepare(data, ctx);
2437 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002438 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002439 if (IS_ERR(task))
2440 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002441 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002442 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002443 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002444 smp_wmb();
2445 } else
2446 status = data->rpc_status;
2447 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002448
2449 return status;
2450}
2451
2452static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2453{
David Howells2b0143b2015-03-17 22:25:59 +00002454 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002455 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002456 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002457
Fred Isaman3b65a302016-09-19 10:06:49 -04002458 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002459 if (status != 0 || !data->rpc_done)
2460 return status;
2461
Trond Myklebust6926afd2012-01-07 13:22:46 -05002462 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2463
Anna Schumakerd7e98252017-01-11 16:13:29 -05002464 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002465 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002466
2467 return status;
2468}
2469
Trond Myklebustf3792d62014-07-10 08:54:32 -04002470/*
2471 * Additional permission checks in order to distinguish between an
2472 * open for read, and an open for execute. This works around the
2473 * fact that NFSv4 OPEN treats read and execute permissions as being
2474 * the same.
2475 * Note that in the non-execute case, we want to turn off permission
2476 * checking if we just created a new file (POSIX open() semantics).
2477 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002478static int nfs4_opendata_access(struct rpc_cred *cred,
2479 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002480 struct nfs4_state *state, fmode_t fmode,
2481 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002482{
2483 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002484 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002485
2486 /* access call failed or for some reason the server doesn't
2487 * support any access modes -- defer access call until later */
2488 if (opendata->o_res.access_supported == 0)
2489 return 0;
2490
2491 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002492 /*
2493 * Use openflags to check for exec, because fmode won't
2494 * always have FMODE_EXEC set when file open for exec.
2495 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002496 if (openflags & __FMODE_EXEC) {
2497 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002498 if (S_ISDIR(state->inode->i_mode))
2499 mask = NFS4_ACCESS_LOOKUP;
2500 else
2501 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002502 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002503 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002504
2505 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002506 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2507 nfs_access_add_cache(state->inode, &cache);
2508
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002509 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2510 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002511 return 0;
2512
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002513 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002514}
2515
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002516/*
2517 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2518 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002519static int _nfs4_proc_open(struct nfs4_opendata *data,
2520 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002521{
David Howells2b0143b2015-03-17 22:25:59 +00002522 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002523 struct nfs_server *server = NFS_SERVER(dir);
2524 struct nfs_openargs *o_arg = &data->o_arg;
2525 struct nfs_openres *o_res = &data->o_res;
2526 int status;
2527
Fred Isaman3b65a302016-09-19 10:06:49 -04002528 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002529 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002530 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002531 if (status != 0) {
2532 if (status == -NFS4ERR_BADNAME &&
2533 !(o_arg->open_flags & O_CREAT))
2534 return -ENOENT;
2535 return status;
2536 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002537
Trond Myklebust6926afd2012-01-07 13:22:46 -05002538 nfs_fattr_map_and_free_names(server, &data->f_attr);
2539
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002540 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002541 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002542 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002543 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002544 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002545 if (data->file_created ||
2546 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002547 update_changeattr(dir, &o_res->cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04002548 o_res->f_attr->time_start, 0);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002549 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002550 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2551 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002553 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002554 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002555 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002556 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002557 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2558 nfs4_sequence_free_slot(&o_res->seq_res);
Trond Myklebusta841b542018-04-07 13:50:59 -04002559 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr,
2560 o_res->f_label, NULL);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002561 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002562 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002563}
2564
Linus Torvalds1da177e2005-04-16 15:20:36 -07002565/*
2566 * OPEN_EXPIRED:
2567 * reclaim state on the server after a network partition.
2568 * Assumes caller holds the appropriate lock
2569 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002570static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002572 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002573 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002574
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002575 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002576 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002577 if (IS_ERR(opendata))
2578 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002579 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002580 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002581 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002582 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002583 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002584}
2585
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002586static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002587{
Trond Myklebust539cd032007-06-05 11:46:42 -04002588 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002589 struct nfs4_exception exception = { };
2590 int err;
2591
2592 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002593 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002594 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002595 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2596 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002597 switch (err) {
2598 default:
2599 goto out;
2600 case -NFS4ERR_GRACE:
2601 case -NFS4ERR_DELAY:
2602 nfs4_handle_exception(server, err, &exception);
2603 err = 0;
2604 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002605 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002606out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002607 return err;
2608}
2609
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2611{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002613 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614
Trond Myklebust864472e2006-01-03 09:55:15 +01002615 ctx = nfs4_state_find_open_context(state);
2616 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002617 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002618 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002619 put_nfs_open_context(ctx);
2620 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621}
2622
Trond Myklebust41020b62016-09-22 13:38:58 -04002623static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2624 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002625{
Trond Myklebust41020b62016-09-22 13:38:58 -04002626 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002627 nfs_state_clear_delegation(state);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002628}
2629
2630static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2631{
2632 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002633 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002634}
2635
2636static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2637{
2638 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2639 nfs40_clear_delegation_stateid(state);
2640 return nfs4_open_expired(sp, state);
2641}
2642
Trond Myklebust45870d62016-09-22 13:38:59 -04002643static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2644 nfs4_stateid *stateid,
2645 struct rpc_cred *cred)
2646{
2647 return -NFS4ERR_BAD_STATEID;
2648}
2649
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002650#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002651static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2652 nfs4_stateid *stateid,
2653 struct rpc_cred *cred)
2654{
2655 int status;
2656
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002657 switch (stateid->type) {
2658 default:
2659 break;
2660 case NFS4_INVALID_STATEID_TYPE:
2661 case NFS4_SPECIAL_STATEID_TYPE:
2662 return -NFS4ERR_BAD_STATEID;
2663 case NFS4_REVOKED_STATEID_TYPE:
2664 goto out_free;
2665 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002666
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002667 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002668 switch (status) {
2669 case -NFS4ERR_EXPIRED:
2670 case -NFS4ERR_ADMIN_REVOKED:
2671 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002672 break;
2673 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002674 return status;
2675 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002676out_free:
2677 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002678 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002679 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002680}
2681
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002682static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002683{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002684 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002685 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002686 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002687 struct rpc_cred *cred;
2688 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002689
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002690 /* Get the delegation credential for use by test/free_stateid */
2691 rcu_read_lock();
2692 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002693 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002694 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002695 nfs_state_clear_delegation(state);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002696 return;
2697 }
2698
2699 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebust994b15b2018-09-05 14:07:14 -04002700 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002701 rcu_read_unlock();
Trond Myklebust9f0c5122018-09-05 14:07:15 -04002702 nfs_state_clear_delegation(state);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002703 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002704 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002705
Trond Myklebust994b15b2018-09-05 14:07:14 -04002706 if (!test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2707 &delegation->flags)) {
2708 rcu_read_unlock();
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002709 return;
2710 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002711
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002712 cred = get_rpccred(delegation->cred);
2713 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002714 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002715 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002716 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002717 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002718
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002719 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002720}
2721
2722/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002723 * nfs41_check_expired_locks - possibly free a lock stateid
2724 *
2725 * @state: NFSv4 state for an inode
2726 *
2727 * Returns NFS_OK if recovery for this stateid is now finished.
2728 * Otherwise a negative NFS4ERR value is returned.
2729 */
2730static int nfs41_check_expired_locks(struct nfs4_state *state)
2731{
2732 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002733 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002734 struct nfs_server *server = NFS_SERVER(state->inode);
2735
2736 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2737 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002738
2739 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002740 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2741 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2742 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2743
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002744 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002745 spin_unlock(&state->state_lock);
2746
2747 nfs4_put_lock_state(prev);
2748 prev = lsp;
2749
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002750 status = nfs41_test_and_free_expired_stateid(server,
2751 &lsp->ls_stateid,
2752 cred);
2753 trace_nfs4_test_lock_stateid(state, lsp, status);
2754 if (status == -NFS4ERR_EXPIRED ||
2755 status == -NFS4ERR_BAD_STATEID) {
2756 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002757 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002758 if (!recover_lost_locks)
2759 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2760 } else if (status != NFS_OK) {
2761 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002762 nfs4_put_lock_state(prev);
2763 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002764 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002765 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002766 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002767 }
2768 spin_unlock(&state->state_lock);
2769 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002770out:
2771 return ret;
2772}
2773
2774/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002775 * nfs41_check_open_stateid - possibly free an open stateid
2776 *
2777 * @state: NFSv4 state for an inode
2778 *
2779 * Returns NFS_OK if recovery for this stateid is now finished.
2780 * Otherwise a negative NFS4ERR value is returned.
2781 */
2782static int nfs41_check_open_stateid(struct nfs4_state *state)
2783{
2784 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002785 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002786 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002787 int status;
2788
Trond Myklebustb134fc42016-09-22 13:39:16 -04002789 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002790 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2791 if (nfs4_have_delegation(state->inode, state->state))
2792 return NFS_OK;
2793 return -NFS4ERR_OPENMODE;
2794 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002795 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002796 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002797 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002798 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002799 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002800 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2801 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2802 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002803 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002804 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002805 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002806 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002807 if (nfs_open_stateid_recover_openmode(state))
2808 return -NFS4ERR_OPENMODE;
2809 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002810}
2811
2812static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2813{
Chuck Levereb64cf92012-07-11 16:30:05 -04002814 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002815
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002816 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002817 status = nfs41_check_expired_locks(state);
2818 if (status != NFS_OK)
2819 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002820 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002821 if (status != NFS_OK)
2822 status = nfs4_open_expired(sp, state);
2823 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002824}
2825#endif
2826
Linus Torvalds1da177e2005-04-16 15:20:36 -07002827/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002828 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2829 * fields corresponding to attributes that were used to store the verifier.
2830 * Make sure we clobber those fields in the later setattr call
2831 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002832static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002833 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002834{
Trond Myklebust609339c2018-03-28 16:18:17 -04002835 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2836 __u32 attrset[3];
2837 unsigned ret;
2838 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002839
Trond Myklebust609339c2018-03-28 16:18:17 -04002840 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2841 attrset[i] = opendata->o_res.attrset[i];
2842 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2843 attrset[i] &= ~bitmask[i];
2844 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002845
Trond Myklebust609339c2018-03-28 16:18:17 -04002846 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2847 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002848
Trond Myklebust609339c2018-03-28 16:18:17 -04002849 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2850 if (sattr->ia_valid & ATTR_ATIME_SET)
2851 ret |= ATTR_ATIME_SET;
2852 else
2853 ret |= ATTR_ATIME;
2854 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002855
Trond Myklebust609339c2018-03-28 16:18:17 -04002856 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2857 if (sattr->ia_valid & ATTR_MTIME_SET)
2858 ret |= ATTR_MTIME_SET;
2859 else
2860 ret |= ATTR_MTIME;
2861 }
2862
2863 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002864 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002865 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002866}
2867
Trond Myklebustc21443c2013-02-07 14:26:21 -05002868static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2869 fmode_t fmode,
2870 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002871 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002872{
2873 struct nfs4_state_owner *sp = opendata->owner;
2874 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002875 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002876 struct nfs4_state *state;
2877 unsigned int seq;
2878 int ret;
2879
2880 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2881
Fred Isaman3b65a302016-09-19 10:06:49 -04002882 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002883 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002884 goto out;
2885
Trond Myklebustae55e592018-05-22 11:17:16 -04002886 state = _nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002887 ret = PTR_ERR(state);
2888 if (IS_ERR(state))
2889 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002890 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002891 if (server->caps & NFS_CAP_POSIX_LOCK)
2892 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002893 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2894 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002895
Trond Myklebust275bb302013-05-29 13:11:28 -04002896 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002897 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002898 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002899 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002900 alias = d_exact_alias(dentry, state->inode);
2901 if (!alias)
2902 alias = d_splice_alias(igrab(state->inode), dentry);
2903 /* d_splice_alias() can't fail here - it's a non-directory */
2904 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002905 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002906 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002907 }
2908 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002909 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002910 }
2911
Trond Myklebustaf9b6d72018-06-29 12:45:53 -04002912 /* Parse layoutget results before we check for access */
2913 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
2914
Trond Myklebustc21443c2013-02-07 14:26:21 -05002915 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2916 if (ret != 0)
2917 goto out;
2918
David Howells2b0143b2015-03-17 22:25:59 +00002919 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002920 nfs_inode_attach_open_context(ctx);
2921 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2922 nfs4_schedule_stateid_recovery(server, state);
2923 }
Fred Isaman2409a972016-10-06 12:11:21 -04002924
Trond Myklebustc21443c2013-02-07 14:26:21 -05002925out:
Trond Myklebustae55e592018-05-22 11:17:16 -04002926 nfs4_sequence_free_slot(&opendata->o_res.seq_res);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002927 return ret;
2928}
2929
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002930/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002931 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932 */
Andy Adamson82be4172012-05-23 05:02:35 -04002933static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002934 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002935 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002936 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002937 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938{
2939 struct nfs4_state_owner *sp;
2940 struct nfs4_state *state = NULL;
2941 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002942 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002943 struct dentry *dentry = ctx->dentry;
2944 struct rpc_cred *cred = ctx->cred;
2945 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2946 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002947 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002948 struct iattr *sattr = c->sattr;
2949 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002950 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002951 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002952
2953 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002954 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002955 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2956 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2958 goto out_err;
2959 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002960 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002961 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002962 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002963 if (d_really_is_positive(dentry))
2964 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002965 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002966 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002967 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002968 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2969 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002970 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002971 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972
David Quigley14c43f72013-05-22 12:50:43 -04002973 if (label) {
2974 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2975 if (IS_ERR(olabel)) {
2976 status = PTR_ERR(olabel);
2977 goto err_opendata_put;
2978 }
2979 }
2980
Trond Myklebuste911b812014-03-26 13:24:37 -07002981 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2982 if (!opendata->f_attr.mdsthreshold) {
2983 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2984 if (!opendata->f_attr.mdsthreshold)
2985 goto err_free_label;
2986 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002987 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002988 }
David Howells2b0143b2015-03-17 22:25:59 +00002989 if (d_really_is_positive(dentry))
2990 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002991
Trond Myklebust3efb9722013-05-29 13:17:04 -04002992 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002993 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002994 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002995 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002996
NeilBrownefcbc042015-07-30 13:00:56 +10002997 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002998 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002999 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003000 /*
3001 * send create attributes which was not set by open
3002 * with an extra setattr.
3003 */
Trond Myklebust609339c2018-03-28 16:18:17 -04003004 if (attrs || label) {
3005 unsigned ia_old = sattr->ia_valid;
3006
3007 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003008 nfs_fattr_init(opendata->o_res.f_attr);
3009 status = nfs4_do_setattr(state->inode, cred,
3010 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003011 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02003012 if (status == 0) {
3013 nfs_setattr_update_inode(state->inode, sattr,
3014 opendata->o_res.f_attr);
3015 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
3016 }
Trond Myklebust609339c2018-03-28 16:18:17 -04003017 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04003018 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04003019 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08003020 if (opened && opendata->file_created)
Al Viro73a09dd2018-06-08 13:22:02 -04003021 *opened = 1;
Andy Adamson82be4172012-05-23 05:02:35 -04003022
Trond Myklebuste911b812014-03-26 13:24:37 -07003023 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04003024 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07003025 opendata->f_attr.mdsthreshold = NULL;
3026 }
Andy Adamson82be4172012-05-23 05:02:35 -04003027
David Quigley14c43f72013-05-22 12:50:43 -04003028 nfs4_label_free(olabel);
3029
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003030 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003031 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003032 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04003033err_free_label:
3034 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04003035err_opendata_put:
3036 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01003037err_put_state_owner:
3038 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003039out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003040 return status;
3041}
3042
3043
Andy Adamson82be4172012-05-23 05:02:35 -04003044static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04003045 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04003046 int flags,
3047 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003048 struct nfs4_label *label,
3049 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003050{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003051 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003052 struct nfs4_exception exception = { };
3053 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003054 struct nfs4_open_createattrs c = {
3055 .label = label,
3056 .sattr = sattr,
3057 .verf = {
3058 [0] = (__u32)jiffies,
3059 [1] = (__u32)current->pid,
3060 },
3061 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003062 int status;
3063
3064 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05003065 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04003066 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04003067 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003068 if (status == 0)
3069 break;
3070 /* NOTE: BAD_SEQID means the server and client disagree about the
3071 * book-keeping w.r.t. state-changing operations
3072 * (OPEN/CLOSE/LOCK/LOCKU...)
3073 * It is actually a sign of a bug on the client or on the server.
3074 *
3075 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07003076 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077 * have unhashed the old state_owner for us, and that we can
3078 * therefore safely retry using a new one. We should still warn
3079 * the user though...
3080 */
3081 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04003082 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04003083 " returned a bad sequence-id error!\n",
3084 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 exception.retry = 1;
3086 continue;
3087 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003088 /*
3089 * BAD_STATEID on OPEN means that the server cancelled our
3090 * state before it received the OPEN_CONFIRM.
3091 * Recover by retrying the request as per the discussion
3092 * on Page 181 of RFC3530.
3093 */
3094 if (status == -NFS4ERR_BAD_STATEID) {
3095 exception.retry = 1;
3096 continue;
3097 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003098 if (status == -EAGAIN) {
3099 /* We must have found a delegation */
3100 exception.retry = 1;
3101 continue;
3102 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003103 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3104 continue;
3105 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 status, &exception));
3107 } while (exception.retry);
3108 return res;
3109}
3110
Trond Myklebust8487c472016-06-26 08:44:35 -04003111static int _nfs4_do_setattr(struct inode *inode,
3112 struct nfs_setattrargs *arg,
3113 struct nfs_setattrres *res,
3114 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003115 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003116{
3117 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003118 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003119 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3120 .rpc_argp = arg,
3121 .rpc_resp = res,
3122 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003123 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003124 struct rpc_cred *delegation_cred = NULL;
3125 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003126 bool truncate;
3127 int status;
3128
3129 nfs_fattr_init(res->fattr);
3130
3131 /* Servers should only apply open mode checks for file size changes */
3132 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003133 if (!truncate)
3134 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003135
Trond Myklebust991eedb2018-04-09 11:15:30 -04003136 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003137 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003138 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003139 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003140 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003141 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003142 l_ctx = nfs_get_lock_context(ctx);
3143 if (IS_ERR(l_ctx))
3144 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003145 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3146 &arg->stateid, &delegation_cred);
3147 nfs_put_lock_context(l_ctx);
3148 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003149 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003150 } else {
3151zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003152 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003153 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003154 if (delegation_cred)
3155 msg.rpc_cred = delegation_cred;
3156
3157 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3158
3159 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003160 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003161 renew_lease(server, timestamp);
3162 trace_nfs4_setattr(inode, &arg->stateid, status);
3163 return status;
3164}
3165
3166static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3167 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003168 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003169 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003171 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust30846df2018-04-07 13:44:28 -04003172 __u32 bitmask[NFS4_BITMASK_SZ];
NeilBrown29b59f92016-10-13 15:26:47 +11003173 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003174 struct nfs_setattrargs arg = {
3175 .fh = NFS_FH(inode),
3176 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177 .server = server,
Trond Myklebust30846df2018-04-07 13:44:28 -04003178 .bitmask = bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003179 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003180 };
3181 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003182 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003183 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003184 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003185 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003186 struct nfs4_exception exception = {
3187 .state = state,
3188 .inode = inode,
3189 .stateid = &arg.stateid,
3190 };
3191 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003192
Linus Torvalds1da177e2005-04-16 15:20:36 -07003193 do {
Trond Myklebust30846df2018-04-07 13:44:28 -04003194 nfs4_bitmap_copy_adjust_setattr(bitmask,
3195 nfs4_bitmask(server, olabel),
3196 inode);
3197
NeilBrown29b59f92016-10-13 15:26:47 +11003198 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003199 switch (err) {
3200 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003201 if (!(sattr->ia_valid & ATTR_SIZE)) {
3202 pr_warn_once("NFSv4: server %s is incorrectly "
3203 "applying open mode checks to "
3204 "a SETATTR that is not "
3205 "changing file size.\n",
3206 server->nfs_client->cl_hostname);
3207 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003208 if (state && !(state->state & FMODE_WRITE)) {
3209 err = -EBADF;
3210 if (sattr->ia_valid & ATTR_OPEN)
3211 err = -EACCES;
3212 goto out;
3213 }
3214 }
3215 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003216 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003217out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003218 return err;
3219}
3220
Peng Tao500d7012015-09-22 11:35:22 +08003221static bool
3222nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3223{
3224 if (inode == NULL || !nfs_have_layout(inode))
3225 return false;
3226
3227 return pnfs_wait_on_layoutreturn(inode, task);
3228}
3229
Linus Torvalds1da177e2005-04-16 15:20:36 -07003230struct nfs4_closedata {
3231 struct inode *inode;
3232 struct nfs4_state *state;
3233 struct nfs_closeargs arg;
3234 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003235 struct {
3236 struct nfs4_layoutreturn_args arg;
3237 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003238 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003239 u32 roc_barrier;
3240 bool roc;
3241 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003242 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003243 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003244};
3245
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003246static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003247{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003248 struct nfs4_closedata *calldata = data;
3249 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003250 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003251
Trond Myklebustcf805162016-11-15 14:56:07 -05003252 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003253 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3254 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003255 nfs4_put_open_state(calldata->state);
3256 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003257 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003258 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003259 kfree(calldata);
3260}
3261
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003262static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003264 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003267 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003268 struct nfs4_exception exception = {
3269 .state = state,
3270 .inode = calldata->inode,
3271 .stateid = &calldata->arg.stateid,
3272 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273
Chuck Levera3ca5652012-03-01 17:00:40 -05003274 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003275 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3276 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003277 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003278
3279 /* Handle Layoutreturn errors */
3280 if (calldata->arg.lr_args && task->tk_status != 0) {
3281 switch (calldata->res.lr_ret) {
3282 default:
3283 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3284 break;
3285 case 0:
3286 calldata->arg.lr_args = NULL;
3287 calldata->res.lr_res = NULL;
3288 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003289 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04003290 if (nfs4_layoutreturn_refresh_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04003291 &calldata->arg.lr_args->range,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003292 calldata->inode))
3293 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003294 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003295 case -NFS4ERR_ADMIN_REVOKED:
3296 case -NFS4ERR_DELEG_REVOKED:
3297 case -NFS4ERR_EXPIRED:
3298 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003299 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3300 case -NFS4ERR_WRONG_CRED:
3301 calldata->arg.lr_args = NULL;
3302 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003303 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003304 }
3305 }
3306
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003307 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003308 * the state_owner. we keep this around to process errors
3309 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003310 switch (task->tk_status) {
3311 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003312 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003313 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003314 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003315 case -NFS4ERR_ACCESS:
3316 if (calldata->arg.bitmask != NULL) {
3317 calldata->arg.bitmask = NULL;
3318 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003319 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003320
3321 }
3322 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003323 case -NFS4ERR_OLD_STATEID:
3324 /* Did we race with OPEN? */
3325 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003326 state))
3327 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003328 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003329 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003331 case -NFS4ERR_EXPIRED:
3332 nfs4_free_revoked_stateid(server,
3333 &calldata->arg.stateid,
3334 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003335 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003336 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003337 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003339 task->tk_status = nfs4_async_handle_exception(task,
3340 server, task->tk_status, &exception);
3341 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003342 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003343 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003344 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3345 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003346out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003347 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003348 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003349 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003350 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003351 return;
3352lr_restart:
3353 calldata->res.lr_ret = 0;
3354out_restart:
3355 task->tk_status = 0;
3356 rpc_restart_call_prepare(task);
3357 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003358}
3359
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003360static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003361{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003362 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003363 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003364 struct inode *inode = calldata->inode;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003365 struct pnfs_layout_hdr *lo;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003366 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003367 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003368
Chuck Levera3ca5652012-03-01 17:00:40 -05003369 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003370 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003371 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003372
Trond Myklebust88069f72009-12-08 08:33:16 -05003373 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003374 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003375 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3376 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3377 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003378 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003379 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003380 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003381 if (state->n_rdonly == 0)
3382 call_close |= is_rdonly;
3383 else if (is_rdonly)
3384 calldata->arg.fmode |= FMODE_READ;
3385 if (state->n_wronly == 0)
3386 call_close |= is_wronly;
3387 else if (is_wronly)
3388 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003389 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3390 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003391 } else if (is_rdwr)
3392 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3393
Trond Myklebust5cc78612016-11-14 11:19:56 -05003394 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003395 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003396 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003397 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003398
3399 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003400 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003401 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003402 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003403
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003404 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003405 nfs_release_seqid(calldata->arg.seqid);
3406 goto out_wait;
3407 }
3408
Trond Myklebustc8bf7072018-06-15 16:31:02 -04003409 lo = calldata->arg.lr_args ? calldata->arg.lr_args->layout : NULL;
3410 if (lo && !pnfs_layout_is_valid(lo)) {
3411 calldata->arg.lr_args = NULL;
3412 calldata->res.lr_res = NULL;
3413 }
3414
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003415 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003416 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003417
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003418 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003419 /* Close-to-open cache consistency revalidation */
3420 if (!nfs4_have_delegation(inode, FMODE_READ))
3421 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3422 else
3423 calldata->arg.bitmask = NULL;
3424 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003425
Trond Myklebust6ae37332015-01-30 14:21:14 -05003426 calldata->arg.share_access =
3427 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3428 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003429
Trond Myklebustd8d84982016-12-19 12:14:44 -05003430 if (calldata->res.fattr == NULL)
3431 calldata->arg.bitmask = NULL;
3432 else if (calldata->arg.bitmask == NULL)
3433 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003434 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003435 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003436 &calldata->arg.seq_args,
3437 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003438 task) != 0)
3439 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003440 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003441 return;
3442out_no_action:
3443 task->tk_action = NULL;
3444out_wait:
3445 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446}
3447
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003448static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003449 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003450 .rpc_call_done = nfs4_close_done,
3451 .rpc_release = nfs4_free_closedata,
3452};
3453
Linus Torvalds1da177e2005-04-16 15:20:36 -07003454/*
3455 * It is possible for data to be read/written from a mem-mapped file
3456 * after the sys_close call (which hits the vfs layer as a flush).
3457 * This means that we can't safely call nfsv4 close on a file until
3458 * the inode is cleared. This in turn means that we are not good
3459 * NFSv4 citizens - we do not indicate to the server to update the file's
3460 * share state even when we are done with one of the three share
3461 * stateid's in the inode.
3462 *
3463 * NOTE: Caller must be holding the sp->so_owner semaphore!
3464 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003465int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003467 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003468 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003470 struct nfs4_state_owner *sp = state->owner;
3471 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003472 struct rpc_message msg = {
3473 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3474 .rpc_cred = state->owner->so_cred,
3475 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003476 struct rpc_task_setup task_setup_data = {
3477 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003478 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003479 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003480 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003481 .flags = RPC_TASK_ASYNC,
3482 };
Trond Myklebust95121352005-10-18 14:20:12 -07003483 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003485 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3486 &task_setup_data.rpc_client, &msg);
3487
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003488 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003490 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003491 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003492 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003493 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003494 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003495 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3496 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003497 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003498 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3499 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003500 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003501 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003502 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003503 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003504 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003505 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003506 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003507 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003508 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003509 calldata->lr.roc = pnfs_roc(state->inode,
3510 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3511 if (calldata->lr.roc) {
3512 calldata->arg.lr_args = &calldata->lr.arg;
3513 calldata->res.lr_res = &calldata->lr.res;
3514 }
Al Viro643168c2011-06-22 18:20:23 -04003515 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003516
Trond Myklebust1174dd12010-12-21 10:52:24 -05003517 msg.rpc_argp = &calldata->arg;
3518 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003519 task_setup_data.callback_data = calldata;
3520 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003521 if (IS_ERR(task))
3522 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003523 status = 0;
3524 if (wait)
3525 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003526 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003527 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003528out_free_calldata:
3529 kfree(calldata);
3530out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003531 nfs4_put_open_state(state);
3532 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003533 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003534}
3535
Trond Myklebust2b484292010-09-17 10:56:51 -04003536static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003537nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3538 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003539{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003541 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3542
3543 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544
Trond Myklebust565277f2007-10-15 18:17:53 -04003545 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003546 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003547
3548 nfs4_label_release_security(label);
3549
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003550 if (IS_ERR(state))
3551 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003552 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003553}
3554
Trond Myklebust1185a552009-12-03 15:54:02 -05003555static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003556{
3557 if (ctx->state == NULL)
3558 return;
3559 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003560 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003561 else
Al Viro643168c2011-06-22 18:20:23 -04003562 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003563}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564
Trond Myklebustb944dba2013-11-04 15:20:20 -05003565#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3566#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003567#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003568
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3570{
Kinglong Mee8c612822015-08-26 21:12:58 +08003571 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003572 struct nfs4_server_caps_arg args = {
3573 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003574 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003575 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003576 struct nfs4_server_caps_res res = {};
3577 struct rpc_message msg = {
3578 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003579 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003580 .rpc_resp = &res,
3581 };
3582 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003583 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584
Kinglong Mee8c612822015-08-26 21:12:58 +08003585 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3586 FATTR4_WORD0_FH_EXPIRE_TYPE |
3587 FATTR4_WORD0_LINK_SUPPORT |
3588 FATTR4_WORD0_SYMLINK_SUPPORT |
3589 FATTR4_WORD0_ACLSUPPORT;
3590 if (minorversion)
3591 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3592
Bryan Schumaker7c513052011-03-24 17:12:24 +00003593 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003594 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003595 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003596 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003597 case 0:
3598 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3599 res.attr_bitmask[2] = 0;
3600 break;
3601 case 1:
3602 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3603 break;
3604 case 2:
3605 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3606 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003608 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3609 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3610 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3611 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003612 NFS_CAP_CTIME|NFS_CAP_MTIME|
3613 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003614 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3615 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003616 server->caps |= NFS_CAP_ACLS;
3617 if (res.has_links != 0)
3618 server->caps |= NFS_CAP_HARDLINKS;
3619 if (res.has_symlinks != 0)
3620 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003621 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3622 server->caps |= NFS_CAP_FILEID;
3623 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3624 server->caps |= NFS_CAP_MODE;
3625 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3626 server->caps |= NFS_CAP_NLINK;
3627 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3628 server->caps |= NFS_CAP_OWNER;
3629 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3630 server->caps |= NFS_CAP_OWNER_GROUP;
3631 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3632 server->caps |= NFS_CAP_ATIME;
3633 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3634 server->caps |= NFS_CAP_CTIME;
3635 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3636 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003637#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3638 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3639 server->caps |= NFS_CAP_SECURITY_LABEL;
3640#endif
3641 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3642 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003643 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003644
Trond Myklebusta65318b2009-03-11 14:10:28 -04003645 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3646 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3647 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003648 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003649
3650 /* Avoid a regression due to buggy server */
3651 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3652 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003653 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3654 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003655
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003657 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003659
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660 return status;
3661}
3662
Trond Myklebust55a97592006-06-09 09:34:19 -04003663int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003664{
3665 struct nfs4_exception exception = { };
3666 int err;
3667 do {
3668 err = nfs4_handle_exception(server,
3669 _nfs4_server_capabilities(server, fhandle),
3670 &exception);
3671 } while (exception.retry);
3672 return err;
3673}
3674
3675static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3676 struct nfs_fsinfo *info)
3677{
David Quigleyaa9c2662013-05-22 12:50:44 -04003678 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003679 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003680 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003681 };
3682 struct nfs4_lookup_res res = {
3683 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003684 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003685 .fh = fhandle,
3686 };
3687 struct rpc_message msg = {
3688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3689 .rpc_argp = &args,
3690 .rpc_resp = &res,
3691 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003692
David Quigleyaa9c2662013-05-22 12:50:44 -04003693 bitmask[0] = nfs4_fattr_bitmap[0];
3694 bitmask[1] = nfs4_fattr_bitmap[1];
3695 /*
3696 * Process the label in the upcoming getfattr
3697 */
3698 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3699
Trond Myklebust0e574af2005-10-27 22:12:38 -04003700 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003701 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003702}
3703
3704static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3705 struct nfs_fsinfo *info)
3706{
3707 struct nfs4_exception exception = { };
3708 int err;
3709 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003710 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003711 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003712 switch (err) {
3713 case 0:
3714 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003715 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003716 default:
3717 err = nfs4_handle_exception(server, err, &exception);
3718 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003720out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721 return err;
3722}
3723
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003724static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3725 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3726{
Trond Myklebustc2190662013-08-26 19:23:04 -04003727 struct rpc_auth_create_args auth_args = {
3728 .pseudoflavor = flavor,
3729 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003730 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003731
Trond Myklebustc2190662013-08-26 19:23:04 -04003732 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003733 if (IS_ERR(auth))
3734 return -EACCES;
3735 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003736}
3737
Chuck Lever9a744ba2013-03-16 15:56:02 -04003738/*
3739 * Retry pseudoroot lookup with various security flavors. We do this when:
3740 *
3741 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3742 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3743 *
3744 * Returns zero on success, or a negative NFS4ERR value, or a
3745 * negative errno value.
3746 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003747static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003748 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003749{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003750 /* Per 3530bis 15.33.5 */
3751 static const rpc_authflavor_t flav_array[] = {
3752 RPC_AUTH_GSS_KRB5P,
3753 RPC_AUTH_GSS_KRB5I,
3754 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003755 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003756 RPC_AUTH_NULL,
3757 };
3758 int status = -EPERM;
3759 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003760
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003761 if (server->auth_info.flavor_len > 0) {
3762 /* try each flavor specified by user */
3763 for (i = 0; i < server->auth_info.flavor_len; i++) {
3764 status = nfs4_lookup_root_sec(server, fhandle, info,
3765 server->auth_info.flavors[i]);
3766 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3767 continue;
3768 break;
3769 }
3770 } else {
3771 /* no flavors specified by user, try default list */
3772 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3773 status = nfs4_lookup_root_sec(server, fhandle, info,
3774 flav_array[i]);
3775 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3776 continue;
3777 break;
3778 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003779 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003780
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003781 /*
3782 * -EACCESS could mean that the user doesn't have correct permissions
3783 * to access the mount. It could also mean that we tried to mount
3784 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3785 * existing mount programs don't handle -EACCES very well so it should
3786 * be mapped to -EPERM instead.
3787 */
3788 if (status == -EACCES)
3789 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003790 return status;
3791}
3792
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003793/**
3794 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3795 * @server: initialized nfs_server handle
3796 * @fhandle: we fill in the pseudo-fs root file handle
3797 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003798 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003799 *
3800 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003801 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003802int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003803 struct nfs_fsinfo *info,
3804 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003805{
Andre Przywarac7757072015-04-23 17:17:40 +01003806 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003807
Andre Przywarac7757072015-04-23 17:17:40 +01003808 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003809 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003810
3811 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003812 status = server->nfs_client->cl_mvops->find_root_sec(server,
3813 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003814
Linus Torvalds1da177e2005-04-16 15:20:36 -07003815 if (status == 0)
3816 status = nfs4_server_capabilities(server, fhandle);
3817 if (status == 0)
3818 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003819
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003820 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821}
3822
Bryan Schumakerbae36242012-05-10 15:07:31 -04003823static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3824 struct nfs_fsinfo *info)
3825{
3826 int error;
3827 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003828 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003829
3830 error = nfs4_server_capabilities(server, mntfh);
3831 if (error < 0) {
3832 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3833 return error;
3834 }
3835
David Quigley14c43f72013-05-22 12:50:43 -04003836 label = nfs4_label_alloc(server, GFP_KERNEL);
3837 if (IS_ERR(label))
3838 return PTR_ERR(label);
3839
Trond Myklebusta841b542018-04-07 13:50:59 -04003840 error = nfs4_proc_getattr(server, mntfh, fattr, label, NULL);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003841 if (error < 0) {
3842 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003843 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003844 }
3845
3846 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3847 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3848 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3849
David Quigley14c43f72013-05-22 12:50:43 -04003850err_free_label:
3851 nfs4_label_free(label);
3852
Bryan Schumakerbae36242012-05-10 15:07:31 -04003853 return error;
3854}
3855
Manoj Naik6b97fd32006-06-09 09:34:29 -04003856/*
3857 * Get locations and (maybe) other attributes of a referral.
3858 * Note that we'll actually follow the referral later when
3859 * we detect fsid mismatch in inode revalidation
3860 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003861static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3862 const struct qstr *name, struct nfs_fattr *fattr,
3863 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003864{
3865 int status = -ENOMEM;
3866 struct page *page = NULL;
3867 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003868
3869 page = alloc_page(GFP_KERNEL);
3870 if (page == NULL)
3871 goto out;
3872 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3873 if (locations == NULL)
3874 goto out;
3875
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003876 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003877 if (status != 0)
3878 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003879
3880 /*
3881 * If the fsid didn't change, this is a migration event, not a
3882 * referral. Cause us to drop into the exception handler, which
3883 * will kick off migration recovery.
3884 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003885 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003886 dprintk("%s: server did not return a different fsid for"
3887 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003888 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003889 goto out;
3890 }
Andy Adamson533eb462011-06-13 18:25:56 -04003891 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3892 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003893
Andy Adamson533eb462011-06-13 18:25:56 -04003894 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003895 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003896 memset(fhandle, 0, sizeof(struct nfs_fh));
3897out:
3898 if (page)
3899 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003900 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003901 return status;
3902}
3903
David Quigley1775fd32013-05-22 12:50:42 -04003904static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003905 struct nfs_fattr *fattr, struct nfs4_label *label,
3906 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003907{
Trond Myklebust771734f2018-04-07 13:54:23 -04003908 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003909 struct nfs4_getattr_arg args = {
3910 .fh = fhandle,
Trond Myklebust771734f2018-04-07 13:54:23 -04003911 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003912 };
3913 struct nfs4_getattr_res res = {
3914 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003915 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 .server = server,
3917 };
3918 struct rpc_message msg = {
3919 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3920 .rpc_argp = &args,
3921 .rpc_resp = &res,
3922 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003923
Trond Myklebust771734f2018-04-07 13:54:23 -04003924 nfs4_bitmap_copy_adjust(bitmask, nfs4_bitmask(server, label), inode);
David Quigleyaa9c2662013-05-22 12:50:44 -04003925
Trond Myklebust0e574af2005-10-27 22:12:38 -04003926 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003927 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003928}
3929
David Quigley1775fd32013-05-22 12:50:42 -04003930static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebusta841b542018-04-07 13:50:59 -04003931 struct nfs_fattr *fattr, struct nfs4_label *label,
3932 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003933{
3934 struct nfs4_exception exception = { };
3935 int err;
3936 do {
Trond Myklebusta841b542018-04-07 13:50:59 -04003937 err = _nfs4_proc_getattr(server, fhandle, fattr, label, inode);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003938 trace_nfs4_getattr(server, fhandle, fattr, err);
3939 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003940 &exception);
3941 } while (exception.retry);
3942 return err;
3943}
3944
3945/*
3946 * The file is not closed if it is opened due to the a request to change
3947 * the size of the file. The open call will not be needed once the
3948 * VFS layer lookup-intents are implemented.
3949 *
3950 * Close is called when the inode is destroyed.
3951 * If we haven't opened the file for O_WRONLY, we
3952 * need to in the size_change case to obtain a stateid.
3953 *
3954 * Got race?
3955 * Because OPEN is always done by name in nfsv4, it is
3956 * possible that we opened a different file by the same
3957 * name. We can recognize this race condition, but we
3958 * can't do anything about it besides returning an error.
3959 *
3960 * This will be fixed with VFS changes (lookup-intent).
3961 */
3962static int
3963nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3964 struct iattr *sattr)
3965{
David Howells2b0143b2015-03-17 22:25:59 +00003966 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003967 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003968 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003969 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003970 int status;
3971
Peng Tao88ac8152014-09-12 11:04:10 +08003972 if (pnfs_ld_layoutret_on_setattr(inode) &&
3973 sattr->ia_valid & ATTR_SIZE &&
3974 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003975 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003976
Trond Myklebust0e574af2005-10-27 22:12:38 -04003977 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978
Andy Adamson26699402012-05-30 16:12:24 -04003979 /* Deal with open(O_TRUNC) */
3980 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003981 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003982
3983 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003984 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003985 return 0;
3986
Trond Myklebustd5308382005-11-04 15:33:38 -05003987 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003988 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003989
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003990 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003991 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003992 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003993 }
3994
David Quigley14c43f72013-05-22 12:50:43 -04003995 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3996 if (IS_ERR(label))
3997 return PTR_ERR(label);
3998
Trond Myklebust199366f2018-03-20 16:43:18 -04003999 /* Return any delegations if we're going to change ACLs */
4000 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004001 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04004002
NeilBrown29b59f92016-10-13 15:26:47 +11004003 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04004004 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05004005 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04004006 nfs_setsecurity(inode, fattr, label);
4007 }
David Quigley14c43f72013-05-22 12:50:43 -04004008 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004009 return status;
4010}
4011
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004012static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
4013 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004014 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04004015{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004016 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04004017 int status;
4018 struct nfs4_lookup_arg args = {
4019 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004020 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04004021 .name = name,
4022 };
4023 struct nfs4_lookup_res res = {
4024 .server = server,
4025 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04004026 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04004027 .fh = fhandle,
4028 };
4029 struct rpc_message msg = {
4030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
4031 .rpc_argp = &args,
4032 .rpc_resp = &res,
4033 };
4034
David Quigleyaa9c2662013-05-22 12:50:44 -04004035 args.bitmask = nfs4_bitmask(server, label);
4036
David Howells2b3de442006-08-22 20:06:09 -04004037 nfs_fattr_init(fattr);
4038
Linus Torvalds1da177e2005-04-16 15:20:36 -07004039 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004040 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004041 dprintk("NFS reply lookup: %d\n", status);
4042 return status;
4043}
4044
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004045static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004046{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004047 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004048 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00004049 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
4050 fattr->nlink = 2;
4051}
4052
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004053static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004054 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04004055 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004056{
4057 struct nfs4_exception exception = { };
4058 struct rpc_clnt *client = *clnt;
4059 int err;
4060 do {
David Quigley1775fd32013-05-22 12:50:42 -04004061 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004062 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004063 switch (err) {
4064 case -NFS4ERR_BADNAME:
4065 err = -ENOENT;
4066 goto out;
4067 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004068 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02004069 if (err == -NFS4ERR_MOVED)
4070 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004071 goto out;
4072 case -NFS4ERR_WRONGSEC:
4073 err = -EPERM;
4074 if (client != *clnt)
4075 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04004076 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004077 if (IS_ERR(client))
4078 return PTR_ERR(client);
4079
4080 exception.retry = 1;
4081 break;
4082 default:
4083 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
4084 }
4085 } while (exception.retry);
4086
4087out:
4088 if (err == 0)
4089 *clnt = client;
4090 else if (client != *clnt)
4091 rpc_shutdown_client(client);
4092
4093 return err;
4094}
4095
Al Virobeffb8f2016-07-20 16:34:42 -04004096static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04004097 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4098 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004100 int status;
4101 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004102
David Quigley1775fd32013-05-22 12:50:42 -04004103 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004104 if (client != NFS_CLIENT(dir)) {
4105 rpc_shutdown_client(client);
4106 nfs_fixup_secinfo_attributes(fattr);
4107 }
4108 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004109}
4110
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004111struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004112nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004113 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4114{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004115 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004116 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004117
David Quigley1775fd32013-05-22 12:50:42 -04004118 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004119 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004120 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004121 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004122}
4123
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004124static int _nfs4_proc_lookupp(struct inode *inode,
4125 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4126 struct nfs4_label *label)
4127{
4128 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4129 struct nfs_server *server = NFS_SERVER(inode);
4130 int status;
4131 struct nfs4_lookupp_arg args = {
4132 .bitmask = server->attr_bitmask,
4133 .fh = NFS_FH(inode),
4134 };
4135 struct nfs4_lookupp_res res = {
4136 .server = server,
4137 .fattr = fattr,
4138 .label = label,
4139 .fh = fhandle,
4140 };
4141 struct rpc_message msg = {
4142 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4143 .rpc_argp = &args,
4144 .rpc_resp = &res,
4145 };
4146
4147 args.bitmask = nfs4_bitmask(server, label);
4148
4149 nfs_fattr_init(fattr);
4150
4151 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4152 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4153 &res.seq_res, 0);
4154 dprintk("NFS reply lookupp: %d\n", status);
4155 return status;
4156}
4157
4158static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4159 struct nfs_fattr *fattr, struct nfs4_label *label)
4160{
4161 struct nfs4_exception exception = { };
4162 int err;
4163 do {
4164 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4165 trace_nfs4_lookupp(inode, err);
4166 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4167 &exception);
4168 } while (exception.retry);
4169 return err;
4170}
4171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4173{
Trond Myklebust76b32992007-08-10 17:45:11 -04004174 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 struct nfs4_accessargs args = {
4176 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004177 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004179 struct nfs4_accessres res = {
4180 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004181 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 struct rpc_message msg = {
4183 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4184 .rpc_argp = &args,
4185 .rpc_resp = &res,
4186 .rpc_cred = entry->cred,
4187 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004188 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189
Trond Myklebust7c672652018-06-04 15:00:53 -04004190 if (!nfs4_have_delegation(inode, FMODE_READ)) {
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004191 res.fattr = nfs_alloc_fattr();
4192 if (res.fattr == NULL)
4193 return -ENOMEM;
4194 args.bitmask = server->cache_consistency_bitmask;
4195 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004196
Bryan Schumaker7c513052011-03-24 17:12:24 +00004197 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004199 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004200 if (res.fattr)
4201 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004203 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 return status;
4205}
4206
4207static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4208{
4209 struct nfs4_exception exception = { };
4210 int err;
4211 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004212 err = _nfs4_proc_access(inode, entry);
4213 trace_nfs4_access(inode, err);
4214 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 &exception);
4216 } while (exception.retry);
4217 return err;
4218}
4219
4220/*
4221 * TODO: For the time being, we don't try to get any attributes
4222 * along with any of the zero-copy operations READ, READDIR,
4223 * READLINK, WRITE.
4224 *
4225 * In the case of the first three, we want to put the GETATTR
4226 * after the read-type operation -- this is because it is hard
4227 * to predict the length of a GETATTR response in v4, and thus
4228 * align the READ data correctly. This means that the GETATTR
4229 * may end up partially falling into the page cache, and we should
4230 * shift it into the 'tail' of the xdr_buf before processing.
4231 * To do this efficiently, we need to know the total length
4232 * of data received, which doesn't seem to be available outside
4233 * of the RPC layer.
4234 *
4235 * In the case of WRITE, we also want to put the GETATTR after
4236 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004237 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238 *
4239 * Both of these changes to the XDR layer would in fact be quite
4240 * minor, but I decided to leave them for a subsequent patch.
4241 */
4242static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4243 unsigned int pgbase, unsigned int pglen)
4244{
4245 struct nfs4_readlink args = {
4246 .fh = NFS_FH(inode),
4247 .pgbase = pgbase,
4248 .pglen = pglen,
4249 .pages = &page,
4250 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004251 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 struct rpc_message msg = {
4253 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4254 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004255 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 };
4257
Bryan Schumaker7c513052011-03-24 17:12:24 +00004258 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 -07004259}
4260
4261static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4262 unsigned int pgbase, unsigned int pglen)
4263{
4264 struct nfs4_exception exception = { };
4265 int err;
4266 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004267 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4268 trace_nfs4_readlink(inode, err);
4269 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 &exception);
4271 } while (exception.retry);
4272 return err;
4273}
4274
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004276 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004278static int
4279nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004280 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004282 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004283 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004284 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286 int status = 0;
4287
NeilBrown532d4de2016-10-13 15:26:47 +11004288 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004289 if (IS_ERR(ctx))
4290 return PTR_ERR(ctx);
4291
David Quigleyaa9c2662013-05-22 12:50:44 -04004292 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4293
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004294 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4295 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004296 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297 if (IS_ERR(state)) {
4298 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004299 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004301out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004302 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004303 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004304 return status;
4305}
4306
Trond Myklebust3c591172018-07-31 15:54:10 -04004307static int
4308_nfs4_proc_remove(struct inode *dir, const struct qstr *name, u32 ftype)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309{
Trond Myklebust16e42952005-10-27 22:12:44 -04004310 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004311 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004312 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004313 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004314 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004315 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004316 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004317 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004318 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004319 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4320 .rpc_argp = &args,
4321 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004322 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004323 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004324 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004325
Bryan Schumaker7c513052011-03-24 17:12:24 +00004326 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust3c591172018-07-31 15:54:10 -04004327 if (status == 0) {
4328 spin_lock(&dir->i_lock);
Trond Myklebust5636ec42018-07-31 15:54:11 -04004329 update_changeattr_locked(dir, &res.cinfo, timestamp, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004330 /* Removing a directory decrements nlink in the parent */
4331 if (ftype == NF4DIR && dir->i_nlink > 2)
4332 nfs4_dec_nlink_locked(dir);
4333 spin_unlock(&dir->i_lock);
4334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 return status;
4336}
4337
Trond Myklebust912678d2018-03-20 16:43:15 -04004338static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4339{
4340 struct nfs4_exception exception = { };
4341 struct inode *inode = d_inode(dentry);
4342 int err;
4343
Trond Myklebustc01d3642018-03-20 16:43:20 -04004344 if (inode) {
4345 if (inode->i_nlink == 1)
4346 nfs4_inode_return_delegation(inode);
4347 else
4348 nfs4_inode_make_writeable(inode);
4349 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004350 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004351 err = _nfs4_proc_remove(dir, &dentry->d_name, NF4REG);
Trond Myklebust912678d2018-03-20 16:43:15 -04004352 trace_nfs4_remove(dir, &dentry->d_name, err);
4353 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4354 &exception);
4355 } while (exception.retry);
4356 return err;
4357}
4358
4359static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360{
4361 struct nfs4_exception exception = { };
4362 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004363
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 do {
Trond Myklebust3c591172018-07-31 15:54:10 -04004365 err = _nfs4_proc_remove(dir, name, NF4DIR);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004366 trace_nfs4_remove(dir, name, err);
4367 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 &exception);
4369 } while (exception.retry);
4370 return err;
4371}
4372
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004373static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4374 struct dentry *dentry,
4375 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004376{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004377 struct nfs_removeargs *args = msg->rpc_argp;
4378 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004379
Trond Myklebust977fcc22018-03-20 16:43:17 -04004380 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004381 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004382 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004383
4384 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004385
4386 if (inode)
4387 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004388}
4389
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004390static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4391{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004392 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004393 &data->args.seq_args,
4394 &data->res.seq_res,
4395 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004396}
4397
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004398static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004399{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004400 struct nfs_unlinkdata *data = task->tk_calldata;
4401 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004402
Trond Myklebust14516c32010-07-31 14:29:06 -04004403 if (!nfs4_sequence_done(task, &res->seq_res))
4404 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004405 if (nfs4_async_handle_error(task, res->server, NULL,
4406 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004407 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004408 if (task->tk_status == 0)
Trond Myklebust5636ec42018-07-31 15:54:11 -04004409 update_changeattr(dir, &res->cinfo,
4410 res->dir_attr->time_start, 0);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004411 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004412}
4413
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004414static void nfs4_proc_rename_setup(struct rpc_message *msg,
4415 struct dentry *old_dentry,
4416 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004417{
Jeff Laytond3d41522010-09-17 17:31:57 -04004418 struct nfs_renameargs *arg = msg->rpc_argp;
4419 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004420 struct inode *old_inode = d_inode(old_dentry);
4421 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004422
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004423 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004424 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004425 if (new_inode)
4426 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004427 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004428 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004429 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004430}
4431
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004432static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4433{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004434 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004435 &data->args.seq_args,
4436 &data->res.seq_res,
4437 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004438}
4439
4440static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4441 struct inode *new_dir)
4442{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004443 struct nfs_renamedata *data = task->tk_calldata;
4444 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004445
4446 if (!nfs4_sequence_done(task, &res->seq_res))
4447 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004448 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004449 return 0;
4450
Trond Myklebustc733c492017-01-11 12:32:26 -05004451 if (task->tk_status == 0) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004452 if (new_dir != old_dir) {
4453 /* Note: If we moved a directory, nlink will change */
4454 update_changeattr(old_dir, &res->old_cinfo,
4455 res->old_fattr->time_start,
4456 NFS_INO_INVALID_OTHER);
4457 update_changeattr(new_dir, &res->new_cinfo,
4458 res->new_fattr->time_start,
4459 NFS_INO_INVALID_OTHER);
4460 } else
4461 update_changeattr(old_dir, &res->old_cinfo,
4462 res->old_fattr->time_start,
4463 0);
Trond Myklebustc733c492017-01-11 12:32:26 -05004464 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004465 return 1;
4466}
4467
Al Virobeffb8f2016-07-20 16:34:42 -04004468static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004470 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004471 __u32 bitmask[NFS4_BITMASK_SZ];
Linus Torvalds1da177e2005-04-16 15:20:36 -07004472 struct nfs4_link_arg arg = {
4473 .fh = NFS_FH(inode),
4474 .dir_fh = NFS_FH(dir),
4475 .name = name,
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004476 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004478 struct nfs4_link_res res = {
4479 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004480 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004481 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 struct rpc_message msg = {
4483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4484 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004485 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004487 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488
Trond Myklebust136f2622010-04-16 16:22:49 -04004489 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004490 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004491 goto out;
4492
David Quigley14c43f72013-05-22 12:50:43 -04004493 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4494 if (IS_ERR(res.label)) {
4495 status = PTR_ERR(res.label);
4496 goto out;
4497 }
4498
Trond Myklebustc01d3642018-03-20 16:43:20 -04004499 nfs4_inode_make_writeable(inode);
Trond Myklebust2f28dc32018-04-08 21:06:40 -04004500 nfs4_bitmap_copy_adjust_setattr(bitmask, nfs4_bitmask(server, res.label), inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004501
Bryan Schumaker7c513052011-03-24 17:12:24 +00004502 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004503 if (!status) {
Trond Myklebust5636ec42018-07-31 15:54:11 -04004504 update_changeattr(dir, &res.cinfo, res.fattr->time_start, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004505 status = nfs_post_op_update_inode(inode, res.fattr);
4506 if (!status)
4507 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004508 }
David Quigley14c43f72013-05-22 12:50:43 -04004509
4510
4511 nfs4_label_free(res.label);
4512
Trond Myklebust136f2622010-04-16 16:22:49 -04004513out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004514 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 return status;
4516}
4517
Al Virobeffb8f2016-07-20 16:34:42 -04004518static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
4520 struct nfs4_exception exception = { };
4521 int err;
4522 do {
4523 err = nfs4_handle_exception(NFS_SERVER(inode),
4524 _nfs4_proc_link(inode, dir, name),
4525 &exception);
4526 } while (exception.retry);
4527 return err;
4528}
4529
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004530struct nfs4_createdata {
4531 struct rpc_message msg;
4532 struct nfs4_create_arg arg;
4533 struct nfs4_create_res res;
4534 struct nfs_fh fh;
4535 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004536 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004537};
4538
4539static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004540 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004541{
4542 struct nfs4_createdata *data;
4543
4544 data = kzalloc(sizeof(*data), GFP_KERNEL);
4545 if (data != NULL) {
4546 struct nfs_server *server = NFS_SERVER(dir);
4547
David Quigley14c43f72013-05-22 12:50:43 -04004548 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4549 if (IS_ERR(data->label))
4550 goto out_free;
4551
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004552 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4553 data->msg.rpc_argp = &data->arg;
4554 data->msg.rpc_resp = &data->res;
4555 data->arg.dir_fh = NFS_FH(dir);
4556 data->arg.server = server;
4557 data->arg.name = name;
4558 data->arg.attrs = sattr;
4559 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004560 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004561 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004562 data->res.server = server;
4563 data->res.fh = &data->fh;
4564 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004565 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004566 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004567 }
4568 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004569out_free:
4570 kfree(data);
4571 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004572}
4573
4574static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4575{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004576 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004577 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004578 if (status == 0) {
Trond Myklebust3c591172018-07-31 15:54:10 -04004579 spin_lock(&dir->i_lock);
4580 update_changeattr_locked(dir, &data->res.dir_cinfo,
Trond Myklebust5636ec42018-07-31 15:54:11 -04004581 data->res.fattr->time_start, 0);
Trond Myklebust3c591172018-07-31 15:54:10 -04004582 /* Creating a directory bumps nlink in the parent */
4583 if (data->arg.ftype == NF4DIR)
4584 nfs4_inc_nlink_locked(dir);
4585 spin_unlock(&dir->i_lock);
David Quigley1775fd32013-05-22 12:50:42 -04004586 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004587 }
4588 return status;
4589}
4590
4591static void nfs4_free_createdata(struct nfs4_createdata *data)
4592{
David Quigley14c43f72013-05-22 12:50:43 -04004593 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004594 kfree(data);
4595}
4596
Chuck Lever4f390c12006-08-22 20:06:22 -04004597static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004598 struct page *page, unsigned int len, struct iattr *sattr,
4599 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004600{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004601 struct nfs4_createdata *data;
4602 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603
Chuck Lever94a6d752006-08-22 20:06:23 -04004604 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004605 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004606
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004607 status = -ENOMEM;
4608 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4609 if (data == NULL)
4610 goto out;
4611
4612 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4613 data->arg.u.symlink.pages = &page;
4614 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004615 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004616
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004617 status = nfs4_do_create(dir, dentry, data);
4618
4619 nfs4_free_createdata(data);
4620out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 return status;
4622}
4623
Chuck Lever4f390c12006-08-22 20:06:22 -04004624static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004625 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626{
4627 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004628 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004630
4631 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4632
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004634 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4635 trace_nfs4_symlink(dir, &dentry->d_name, err);
4636 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 &exception);
4638 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004639
4640 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return err;
4642}
4643
4644static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004645 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004646{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004647 struct nfs4_createdata *data;
4648 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004650 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4651 if (data == NULL)
4652 goto out;
4653
David Quigley1775fd32013-05-22 12:50:42 -04004654 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004655 status = nfs4_do_create(dir, dentry, data);
4656
4657 nfs4_free_createdata(data);
4658out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 return status;
4660}
4661
4662static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4663 struct iattr *sattr)
4664{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004665 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004667 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004668 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004669
David Quigleyaa9c2662013-05-22 12:50:44 -04004670 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4671
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004672 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4673 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004674 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004675 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4676 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4677 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 &exception);
4679 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004680 nfs4_label_release_security(label);
4681
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 return err;
4683}
4684
4685static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004686 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687{
David Howells2b0143b2015-03-17 22:25:59 +00004688 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 struct nfs4_readdir_arg args = {
4690 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004691 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004692 .pgbase = 0,
4693 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004694 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004695 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 };
4697 struct nfs4_readdir_res res;
4698 struct rpc_message msg = {
4699 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4700 .rpc_argp = &args,
4701 .rpc_resp = &res,
4702 .rpc_cred = cred,
4703 };
4704 int status;
4705
Al Viro6de14722013-09-16 10:53:17 -04004706 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4707 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004708 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004709 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004711 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 -05004712 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004713 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004714 status += args.pgbase;
4715 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004716
4717 nfs_invalidate_atime(dir);
4718
Harvey Harrison3110ff82008-05-02 13:42:44 -07004719 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004720 return status;
4721}
4722
4723static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004724 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725{
4726 struct nfs4_exception exception = { };
4727 int err;
4728 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004729 err = _nfs4_proc_readdir(dentry, cred, cookie,
4730 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004731 trace_nfs4_readdir(d_inode(dentry), err);
4732 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004733 &exception);
4734 } while (exception.retry);
4735 return err;
4736}
4737
4738static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004739 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004740{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004741 struct nfs4_createdata *data;
4742 int mode = sattr->ia_mode;
4743 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004744
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004745 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4746 if (data == NULL)
4747 goto out;
4748
Linus Torvalds1da177e2005-04-16 15:20:36 -07004749 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004750 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004752 data->arg.ftype = NF4BLK;
4753 data->arg.u.device.specdata1 = MAJOR(rdev);
4754 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755 }
4756 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004757 data->arg.ftype = NF4CHR;
4758 data->arg.u.device.specdata1 = MAJOR(rdev);
4759 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004760 } else if (!S_ISSOCK(mode)) {
4761 status = -EINVAL;
4762 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 }
David Quigley1775fd32013-05-22 12:50:42 -04004764
David Quigleyaa9c2662013-05-22 12:50:44 -04004765 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004766 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004767out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004768 nfs4_free_createdata(data);
4769out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 return status;
4771}
4772
4773static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4774 struct iattr *sattr, dev_t rdev)
4775{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004776 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004778 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004779 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004780
David Quigleyaa9c2662013-05-22 12:50:44 -04004781 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4782
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004783 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4784 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004786 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4787 trace_nfs4_mknod(dir, &dentry->d_name, err);
4788 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004789 &exception);
4790 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004791
4792 nfs4_label_release_security(label);
4793
Linus Torvalds1da177e2005-04-16 15:20:36 -07004794 return err;
4795}
4796
4797static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4798 struct nfs_fsstat *fsstat)
4799{
4800 struct nfs4_statfs_arg args = {
4801 .fh = fhandle,
4802 .bitmask = server->attr_bitmask,
4803 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004804 struct nfs4_statfs_res res = {
4805 .fsstat = fsstat,
4806 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807 struct rpc_message msg = {
4808 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4809 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004810 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811 };
4812
Trond Myklebust0e574af2005-10-27 22:12:38 -04004813 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004814 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815}
4816
4817static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4818{
4819 struct nfs4_exception exception = { };
4820 int err;
4821 do {
4822 err = nfs4_handle_exception(server,
4823 _nfs4_proc_statfs(server, fhandle, fsstat),
4824 &exception);
4825 } while (exception.retry);
4826 return err;
4827}
4828
4829static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4830 struct nfs_fsinfo *fsinfo)
4831{
4832 struct nfs4_fsinfo_arg args = {
4833 .fh = fhandle,
4834 .bitmask = server->attr_bitmask,
4835 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004836 struct nfs4_fsinfo_res res = {
4837 .fsinfo = fsinfo,
4838 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839 struct rpc_message msg = {
4840 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4841 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004842 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004843 };
4844
Bryan Schumaker7c513052011-03-24 17:12:24 +00004845 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004846}
4847
4848static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4849{
4850 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004851 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 int err;
4853
4854 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004855 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004856 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004857 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004858 nfs4_set_lease_period(server->nfs_client,
4859 fsinfo->lease_time * HZ,
4860 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004861 break;
4862 }
4863 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004864 } while (exception.retry);
4865 return err;
4866}
4867
4868static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4869{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004870 int error;
4871
Trond Myklebust0e574af2005-10-27 22:12:38 -04004872 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004873 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004874 if (error == 0) {
4875 /* block layout checks this! */
4876 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004877 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004878 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004879
4880 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004881}
4882
4883static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4884 struct nfs_pathconf *pathconf)
4885{
4886 struct nfs4_pathconf_arg args = {
4887 .fh = fhandle,
4888 .bitmask = server->attr_bitmask,
4889 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004890 struct nfs4_pathconf_res res = {
4891 .pathconf = pathconf,
4892 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893 struct rpc_message msg = {
4894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4895 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004896 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004897 };
4898
4899 /* None of the pathconf attributes are mandatory to implement */
4900 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4901 memset(pathconf, 0, sizeof(*pathconf));
4902 return 0;
4903 }
4904
Trond Myklebust0e574af2005-10-27 22:12:38 -04004905 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004906 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907}
4908
4909static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4910 struct nfs_pathconf *pathconf)
4911{
4912 struct nfs4_exception exception = { };
4913 int err;
4914
4915 do {
4916 err = nfs4_handle_exception(server,
4917 _nfs4_proc_pathconf(server, fhandle, pathconf),
4918 &exception);
4919 } while (exception.retry);
4920 return err;
4921}
4922
Trond Myklebust5521abf2013-03-16 20:54:34 -04004923int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004924 const struct nfs_open_context *ctx,
4925 const struct nfs_lock_context *l_ctx,
4926 fmode_t fmode)
4927{
NeilBrown17393472016-10-13 15:26:47 +11004928 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004929}
4930EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4931
Trond Myklebust5521abf2013-03-16 20:54:34 -04004932static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4933 const struct nfs_open_context *ctx,
4934 const struct nfs_lock_context *l_ctx,
4935 fmode_t fmode)
4936{
4937 nfs4_stateid current_stateid;
4938
Trond Myklebuste1253be2014-03-05 08:44:23 -05004939 /* If the current stateid represents a lost lock, then exit */
4940 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4941 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004942 return nfs4_stateid_match(stateid, &current_stateid);
4943}
4944
4945static bool nfs4_error_stateid_expired(int err)
4946{
4947 switch (err) {
4948 case -NFS4ERR_DELEG_REVOKED:
4949 case -NFS4ERR_ADMIN_REVOKED:
4950 case -NFS4ERR_BAD_STATEID:
4951 case -NFS4ERR_STALE_STATEID:
4952 case -NFS4ERR_OLD_STATEID:
4953 case -NFS4ERR_OPENMODE:
4954 case -NFS4ERR_EXPIRED:
4955 return true;
4956 }
4957 return false;
4958}
4959
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004960static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004961{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004962 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004963
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004964 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004965 if (task->tk_status < 0) {
4966 struct nfs4_exception exception = {
4967 .inode = hdr->inode,
4968 .state = hdr->args.context->state,
4969 .stateid = &hdr->args.stateid,
4970 };
4971 task->tk_status = nfs4_async_handle_exception(task,
4972 server, task->tk_status, &exception);
4973 if (exception.retry) {
4974 rpc_restart_call_prepare(task);
4975 return -EAGAIN;
4976 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004978
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004980 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004981 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004982}
4983
Trond Myklebust5521abf2013-03-16 20:54:34 -04004984static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004985 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004986{
4987
4988 if (!nfs4_error_stateid_expired(task->tk_status) ||
4989 nfs4_stateid_is_current(&args->stateid,
4990 args->context,
4991 args->lock_context,
4992 FMODE_READ))
4993 return false;
4994 rpc_restart_call_prepare(task);
4995 return true;
4996}
4997
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004998static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004999{
5000
5001 dprintk("--> %s\n", __func__);
5002
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005003 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005004 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005005 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005006 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04005007 if (task->tk_status > 0)
5008 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005009 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5010 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00005011}
5012
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005013static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
5014 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005016 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04005017 if (!hdr->pgio_done_cb)
5018 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005019 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005020 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021}
5022
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005023static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
5024 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04005025{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005026 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005027 &hdr->args.seq_args,
5028 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04005029 task))
NeilBrownef1820f2013-09-04 17:04:49 +10005030 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005031 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
5032 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04005033 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10005034 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005035 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10005036 return -EIO;
5037 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005038}
5039
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005040static int nfs4_write_done_cb(struct rpc_task *task,
5041 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005043 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10005044
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005045 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04005046 if (task->tk_status < 0) {
5047 struct nfs4_exception exception = {
5048 .inode = hdr->inode,
5049 .state = hdr->args.context->state,
5050 .stateid = &hdr->args.stateid,
5051 };
5052 task->tk_status = nfs4_async_handle_exception(task,
5053 NFS_SERVER(inode), task->tk_status,
5054 &exception);
5055 if (exception.retry) {
5056 rpc_restart_call_prepare(task);
5057 return -EAGAIN;
5058 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07005059 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005060 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005061 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05005062 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005063 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005064 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065}
5066
Trond Myklebust5521abf2013-03-16 20:54:34 -04005067static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04005068 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04005069{
5070
5071 if (!nfs4_error_stateid_expired(task->tk_status) ||
5072 nfs4_stateid_is_current(&args->stateid,
5073 args->context,
5074 args->lock_context,
5075 FMODE_WRITE))
5076 return false;
5077 rpc_restart_call_prepare(task);
5078 return true;
5079}
5080
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005081static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00005082{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005083 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00005084 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005085 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04005086 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005087 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
5088 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00005089}
5090
Trond Myklebust5a37f852012-04-28 14:55:16 -04005091static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005092bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00005093{
Trond Myklebust5a37f852012-04-28 14:55:16 -04005094 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005095 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04005096 return false;
5097 /* Otherwise, request attributes if and only if we don't hold
5098 * a delegation
5099 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04005100 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00005101}
Fred Isamana69aef12011-03-03 15:13:47 +00005102
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005103static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005104 struct rpc_message *msg,
5105 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005106{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005107 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005108
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005109 if (!nfs4_write_need_cache_consistency_data(hdr)) {
5110 hdr->args.bitmask = NULL;
5111 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00005112 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005113 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04005114
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04005115 if (!hdr->pgio_done_cb)
5116 hdr->pgio_done_cb = nfs4_write_done_cb;
5117 hdr->res.server = server;
5118 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005119
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005120 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005121 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005122 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123}
5124
Fred Isaman0b7c0152012-04-20 14:47:39 -04005125static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5126{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005127 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005128 &data->args.seq_args,
5129 &data->res.seq_res,
5130 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131}
5132
Fred Isaman0b7c0152012-04-20 14:47:39 -04005133static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005134{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005135 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005136
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005137 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005138 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5139 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005140 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005141 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005142 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005143 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005144}
5145
Fred Isaman0b7c0152012-04-20 14:47:39 -04005146static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005147{
5148 if (!nfs4_sequence_done(task, &data->res.seq_res))
5149 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005150 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005151}
5152
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005153static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5154 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005155{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005156 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005157
Fred Isaman0b7c0152012-04-20 14:47:39 -04005158 if (data->commit_done_cb == NULL)
5159 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005160 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005161 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005162 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005163 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005164}
5165
Olga Kornievskaia6b8d84e2018-07-09 15:13:36 -04005166static int _nfs4_proc_commit(struct file *dst, struct nfs_commitargs *args,
5167 struct nfs_commitres *res)
5168{
5169 struct inode *dst_inode = file_inode(dst);
5170 struct nfs_server *server = NFS_SERVER(dst_inode);
5171 struct rpc_message msg = {
5172 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT],
5173 .rpc_argp = args,
5174 .rpc_resp = res,
5175 };
5176
5177 args->fh = NFS_FH(dst_inode);
5178 return nfs4_call_sync(server->client, server, &msg,
5179 &args->seq_args, &res->seq_res, 1);
5180}
5181
5182int nfs4_proc_commit(struct file *dst, __u64 offset, __u32 count, struct nfs_commitres *res)
5183{
5184 struct nfs_commitargs args = {
5185 .offset = offset,
5186 .count = count,
5187 };
5188 struct nfs_server *dst_server = NFS_SERVER(file_inode(dst));
5189 struct nfs4_exception exception = { };
5190 int status;
5191
5192 do {
5193 status = _nfs4_proc_commit(dst, &args, res);
5194 status = nfs4_handle_exception(dst_server, status, &exception);
5195 } while (exception.retry);
5196
5197 return status;
5198}
5199
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005200struct nfs4_renewdata {
5201 struct nfs_client *client;
5202 unsigned long timestamp;
5203};
5204
Linus Torvalds1da177e2005-04-16 15:20:36 -07005205/*
5206 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5207 * standalone procedure for queueing an asynchronous RENEW.
5208 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005209static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005210{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005211 struct nfs4_renewdata *data = calldata;
5212 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005213
Elena Reshetova212bf412017-10-20 12:53:38 +03005214 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005215 nfs4_schedule_state_renewal(clp);
5216 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005217 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005218}
5219
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005220static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005221{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005222 struct nfs4_renewdata *data = calldata;
5223 struct nfs_client *clp = data->client;
5224 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005225
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005226 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005227 switch (task->tk_status) {
5228 case 0:
5229 break;
5230 case -NFS4ERR_LEASE_MOVED:
5231 nfs4_schedule_lease_moved_recovery(clp);
5232 break;
5233 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005234 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005235 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5236 return;
5237 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005238 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005239 return;
5240 }
5241 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005242 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005243 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005244}
5245
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005246static const struct rpc_call_ops nfs4_renew_ops = {
5247 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005248 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005249};
5250
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005251static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005252{
5253 struct rpc_message msg = {
5254 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5255 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005256 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005257 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005258 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005259
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005260 if (renew_flags == 0)
5261 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005262 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005263 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005264 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005265 if (data == NULL) {
5266 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005267 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005268 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005269 data->client = clp;
5270 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005271 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005272 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005273}
5274
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005275static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005276{
5277 struct rpc_message msg = {
5278 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5279 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005280 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005281 };
5282 unsigned long now = jiffies;
5283 int status;
5284
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005285 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005286 if (status < 0)
5287 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005288 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005289 return 0;
5290}
5291
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005292static inline int nfs4_server_supports_acls(struct nfs_server *server)
5293{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005294 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005295}
5296
Trond Myklebust21f498c2012-08-24 10:59:25 -04005297/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5298 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005299 * the stack.
5300 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005301#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005302
Neil Hormane9e3d722011-03-04 19:26:03 -05005303static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005304 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005305{
5306 struct page *newpage, **spages;
5307 int rc = 0;
5308 size_t len;
5309 spages = pages;
5310
5311 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005312 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005313 newpage = alloc_page(GFP_KERNEL);
5314
5315 if (newpage == NULL)
5316 goto unwind;
5317 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005318 buf += len;
5319 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005320 *pages++ = newpage;
5321 rc++;
5322 } while (buflen != 0);
5323
5324 return rc;
5325
5326unwind:
5327 for(; rc > 0; rc--)
5328 __free_page(spages[rc-1]);
5329 return -ENOMEM;
5330}
5331
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005332struct nfs4_cached_acl {
5333 int cached;
5334 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005335 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005336};
5337
5338static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005339{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005340 struct nfs_inode *nfsi = NFS_I(inode);
5341
5342 spin_lock(&inode->i_lock);
5343 kfree(nfsi->nfs4_acl);
5344 nfsi->nfs4_acl = acl;
5345 spin_unlock(&inode->i_lock);
5346}
5347
5348static void nfs4_zap_acl_attr(struct inode *inode)
5349{
5350 nfs4_set_cached_acl(inode, NULL);
5351}
5352
5353static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5354{
5355 struct nfs_inode *nfsi = NFS_I(inode);
5356 struct nfs4_cached_acl *acl;
5357 int ret = -ENOENT;
5358
5359 spin_lock(&inode->i_lock);
5360 acl = nfsi->nfs4_acl;
5361 if (acl == NULL)
5362 goto out;
5363 if (buf == NULL) /* user is just asking for length */
5364 goto out_len;
5365 if (acl->cached == 0)
5366 goto out;
5367 ret = -ERANGE; /* see getxattr(2) man page */
5368 if (acl->len > buflen)
5369 goto out;
5370 memcpy(buf, acl->data, acl->len);
5371out_len:
5372 ret = acl->len;
5373out:
5374 spin_unlock(&inode->i_lock);
5375 return ret;
5376}
5377
Sachin Prabhu5794d212012-04-17 14:36:40 +01005378static 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 +00005379{
5380 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005381 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005382
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005383 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005384 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005385 if (acl == NULL)
5386 goto out;
5387 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005388 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005389 } else {
5390 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5391 if (acl == NULL)
5392 goto out;
5393 acl->cached = 0;
5394 }
5395 acl->len = acl_len;
5396out:
5397 nfs4_set_cached_acl(inode, acl);
5398}
5399
Andy Adamsonbf118a32011-12-07 11:55:27 -05005400/*
5401 * The getxattr API returns the required buffer length when called with a
5402 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5403 * the required buf. On a NULL buf, we send a page of data to the server
5404 * guessing that the ACL request can be serviced by a page. If so, we cache
5405 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5406 * the cache. If not so, we throw away the page, and cache the required
5407 * length. The next getxattr call will then produce another round trip to
5408 * the server, this time with the input buf of the required size.
5409 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005410static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005411{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005412 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005413 struct nfs_getaclargs args = {
5414 .fh = NFS_FH(inode),
5415 .acl_pages = pages,
5416 .acl_len = buflen,
5417 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005418 struct nfs_getaclres res = {
5419 .acl_len = buflen,
5420 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005421 struct rpc_message msg = {
5422 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5423 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005424 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005425 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005426 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005427 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005428
Trond Myklebust21f498c2012-08-24 10:59:25 -04005429 if (npages > ARRAY_SIZE(pages))
5430 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005431
Andy Adamsonbf118a32011-12-07 11:55:27 -05005432 for (i = 0; i < npages; i++) {
5433 pages[i] = alloc_page(GFP_KERNEL);
5434 if (!pages[i])
5435 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005436 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005437
5438 /* for decoding across pages */
5439 res.acl_scratch = alloc_page(GFP_KERNEL);
5440 if (!res.acl_scratch)
5441 goto out_free;
5442
Andy Adamsonbf118a32011-12-07 11:55:27 -05005443 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005444
Peng Taode040be2012-01-10 22:42:47 +08005445 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005446 __func__, buf, buflen, npages, args.acl_len);
5447 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5448 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005449 if (ret)
5450 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005451
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005452 /* Handle the case where the passed-in buffer is too short */
5453 if (res.acl_flags & NFS4_ACL_TRUNC) {
5454 /* Did the user only issue a request for the acl length? */
5455 if (buf == NULL)
5456 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005457 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005458 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005459 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005460 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005461 if (buf) {
5462 if (res.acl_len > buflen) {
5463 ret = -ERANGE;
5464 goto out_free;
5465 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005466 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005467 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005468out_ok:
5469 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005470out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005471 for (i = 0; i < npages; i++)
5472 if (pages[i])
5473 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005474 if (res.acl_scratch)
5475 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005476 return ret;
5477}
5478
Trond Myklebust16b42892006-08-24 12:27:15 -04005479static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5480{
5481 struct nfs4_exception exception = { };
5482 ssize_t ret;
5483 do {
5484 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005485 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005486 if (ret >= 0)
5487 break;
5488 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5489 } while (exception.retry);
5490 return ret;
5491}
5492
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005493static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5494{
5495 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005496 int ret;
5497
5498 if (!nfs4_server_supports_acls(server))
5499 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005500 ret = nfs_revalidate_inode(server, inode);
5501 if (ret < 0)
5502 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005503 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5504 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005505 ret = nfs4_read_cached_acl(inode, buf, buflen);
5506 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005507 /* -ENOENT is returned if there is no ACL or if there is an ACL
5508 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005509 return ret;
5510 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005511}
5512
Trond Myklebust16b42892006-08-24 12:27:15 -04005513static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005514{
5515 struct nfs_server *server = NFS_SERVER(inode);
5516 struct page *pages[NFS4ACL_MAXPAGES];
5517 struct nfs_setaclargs arg = {
5518 .fh = NFS_FH(inode),
5519 .acl_pages = pages,
5520 .acl_len = buflen,
5521 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005522 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005523 struct rpc_message msg = {
5524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5525 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005526 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005527 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005528 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005529 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005530
5531 if (!nfs4_server_supports_acls(server))
5532 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005533 if (npages > ARRAY_SIZE(pages))
5534 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005535 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005536 if (i < 0)
5537 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005538 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005539 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005540
5541 /*
5542 * Free each page after tx, so the only ref left is
5543 * held by the network stack
5544 */
5545 for (; i > 0; i--)
5546 put_page(pages[i-1]);
5547
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005548 /*
5549 * Acl update can result in inode attribute update.
5550 * so mark the attribute cache invalid.
5551 */
5552 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005553 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005554 | NFS_INO_INVALID_CTIME
5555 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005556 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005557 nfs_access_zap_cache(inode);
5558 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005559 return ret;
5560}
5561
Trond Myklebust16b42892006-08-24 12:27:15 -04005562static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5563{
5564 struct nfs4_exception exception = { };
5565 int err;
5566 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005567 err = __nfs4_proc_set_acl(inode, buf, buflen);
5568 trace_nfs4_set_acl(inode, err);
5569 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005570 &exception);
5571 } while (exception.retry);
5572 return err;
5573}
5574
David Quigleyaa9c2662013-05-22 12:50:44 -04005575#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5576static int _nfs4_get_security_label(struct inode *inode, void *buf,
5577 size_t buflen)
5578{
5579 struct nfs_server *server = NFS_SERVER(inode);
5580 struct nfs_fattr fattr;
5581 struct nfs4_label label = {0, 0, buflen, buf};
5582
5583 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005584 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005585 .fh = NFS_FH(inode),
5586 .bitmask = bitmask,
5587 };
5588 struct nfs4_getattr_res res = {
5589 .fattr = &fattr,
5590 .label = &label,
5591 .server = server,
5592 };
5593 struct rpc_message msg = {
5594 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005595 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005596 .rpc_resp = &res,
5597 };
5598 int ret;
5599
5600 nfs_fattr_init(&fattr);
5601
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005602 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005603 if (ret)
5604 return ret;
5605 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5606 return -ENOENT;
5607 if (buflen < label.len)
5608 return -ERANGE;
5609 return 0;
5610}
5611
5612static int nfs4_get_security_label(struct inode *inode, void *buf,
5613 size_t buflen)
5614{
5615 struct nfs4_exception exception = { };
5616 int err;
5617
5618 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5619 return -EOPNOTSUPP;
5620
5621 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005622 err = _nfs4_get_security_label(inode, buf, buflen);
5623 trace_nfs4_get_security_label(inode, err);
5624 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005625 &exception);
5626 } while (exception.retry);
5627 return err;
5628}
5629
5630static int _nfs4_do_set_security_label(struct inode *inode,
5631 struct nfs4_label *ilabel,
5632 struct nfs_fattr *fattr,
5633 struct nfs4_label *olabel)
5634{
5635
5636 struct iattr sattr = {0};
5637 struct nfs_server *server = NFS_SERVER(inode);
5638 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005639 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005640 .fh = NFS_FH(inode),
5641 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005642 .server = server,
5643 .bitmask = bitmask,
5644 .label = ilabel,
5645 };
5646 struct nfs_setattrres res = {
5647 .fattr = fattr,
5648 .label = olabel,
5649 .server = server,
5650 };
5651 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5653 .rpc_argp = &arg,
5654 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005655 };
5656 int status;
5657
Jeff Layton12207f62013-11-01 10:49:32 -04005658 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005659
Jeff Layton12207f62013-11-01 10:49:32 -04005660 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005661 if (status)
5662 dprintk("%s failed: %d\n", __func__, status);
5663
5664 return status;
5665}
5666
5667static int nfs4_do_set_security_label(struct inode *inode,
5668 struct nfs4_label *ilabel,
5669 struct nfs_fattr *fattr,
5670 struct nfs4_label *olabel)
5671{
5672 struct nfs4_exception exception = { };
5673 int err;
5674
5675 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005676 err = _nfs4_do_set_security_label(inode, ilabel,
5677 fattr, olabel);
5678 trace_nfs4_set_security_label(inode, err);
5679 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005680 &exception);
5681 } while (exception.retry);
5682 return err;
5683}
5684
5685static int
Al Viro59301222016-05-27 10:19:30 -04005686nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005687{
5688 struct nfs4_label ilabel, *olabel = NULL;
5689 struct nfs_fattr fattr;
5690 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005691 int status;
5692
5693 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5694 return -EOPNOTSUPP;
5695
5696 nfs_fattr_init(&fattr);
5697
5698 ilabel.pi = 0;
5699 ilabel.lfs = 0;
5700 ilabel.label = (char *)buf;
5701 ilabel.len = buflen;
5702
5703 cred = rpc_lookup_cred();
5704 if (IS_ERR(cred))
5705 return PTR_ERR(cred);
5706
5707 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5708 if (IS_ERR(olabel)) {
5709 status = -PTR_ERR(olabel);
5710 goto out;
5711 }
5712
5713 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5714 if (status == 0)
5715 nfs_setsecurity(inode, &fattr, olabel);
5716
5717 nfs4_label_free(olabel);
5718out:
5719 put_rpccred(cred);
5720 return status;
5721}
5722#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5723
5724
Chuck Leverf0920752012-05-21 22:45:41 -04005725static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5726 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005727{
5728 __be32 verf[2];
5729
Chuck Lever2c820d92012-05-21 22:45:33 -04005730 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5731 /* An impossible timestamp guarantees this value
5732 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005733 verf[0] = cpu_to_be32(U32_MAX);
5734 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005735 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005736 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005737 u64 ns = ktime_to_ns(nn->boot_time);
5738
5739 verf[0] = cpu_to_be32(ns >> 32);
5740 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005741 }
Chuck Levercd937102012-03-02 17:14:31 -05005742 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5743}
5744
Jeff Laytona3192682015-06-09 19:43:59 -04005745static int
5746nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005747{
Jeff Laytona3192682015-06-09 19:43:59 -04005748 size_t len;
5749 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005750
Trond Myklebustceb3a162015-01-03 15:16:04 -05005751 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005752 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005753
Jeff Laytona3192682015-06-09 19:43:59 -04005754 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005755 len = 14 +
5756 strlen(clp->cl_rpcclient->cl_nodename) +
5757 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005758 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
Jeff Laytona3192682015-06-09 19:43:59 -04005759 1;
5760 rcu_read_unlock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005761 if (nfs4_client_id_uniquifier[0] != '\0')
5762 len += strlen(nfs4_client_id_uniquifier) + 1;
Jeff Laytona3192682015-06-09 19:43:59 -04005763 if (len > NFS4_OPAQUE_LIMIT + 1)
5764 return -EINVAL;
5765
5766 /*
5767 * Since this string is allocated at mount time, and held until the
5768 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5769 * about a memory-reclaim deadlock.
5770 */
5771 str = kmalloc(len, GFP_KERNEL);
5772 if (!str)
5773 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005774
Chuck Levere984a552012-09-14 17:24:21 -04005775 rcu_read_lock();
Chuck Lever848a4eb2018-06-04 10:53:29 -04005776 if (nfs4_client_id_uniquifier[0] != '\0')
Chuck Lever025bb9f2018-06-04 10:53:34 -04005777 scnprintf(str, len, "Linux NFSv4.0 %s/%s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005778 clp->cl_rpcclient->cl_nodename,
5779 nfs4_client_id_uniquifier,
5780 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005781 RPC_DISPLAY_ADDR));
Chuck Lever848a4eb2018-06-04 10:53:29 -04005782 else
Chuck Lever025bb9f2018-06-04 10:53:34 -04005783 scnprintf(str, len, "Linux NFSv4.0 %s/%s",
Chuck Lever848a4eb2018-06-04 10:53:29 -04005784 clp->cl_rpcclient->cl_nodename,
5785 rpc_peeraddr2str(clp->cl_rpcclient,
Chuck Lever025bb9f2018-06-04 10:53:34 -04005786 RPC_DISPLAY_ADDR));
Chuck Levere984a552012-09-14 17:24:21 -04005787 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005788
Jeff Laytona3192682015-06-09 19:43:59 -04005789 clp->cl_owner_id = str;
5790 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005791}
5792
Jeff Layton873e3852015-06-09 19:44:00 -04005793static int
5794nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005795{
Jeff Layton873e3852015-06-09 19:44:00 -04005796 size_t len;
5797 char *str;
5798
5799 len = 10 + 10 + 1 + 10 + 1 +
5800 strlen(nfs4_client_id_uniquifier) + 1 +
5801 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5802
5803 if (len > NFS4_OPAQUE_LIMIT + 1)
5804 return -EINVAL;
5805
5806 /*
5807 * Since this string is allocated at mount time, and held until the
5808 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5809 * about a memory-reclaim deadlock.
5810 */
5811 str = kmalloc(len, GFP_KERNEL);
5812 if (!str)
5813 return -ENOMEM;
5814
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005815 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005816 clp->rpc_ops->version, clp->cl_minorversion,
5817 nfs4_client_id_uniquifier,
5818 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005819 clp->cl_owner_id = str;
5820 return 0;
5821}
5822
5823static int
5824nfs4_init_uniform_client_string(struct nfs_client *clp)
5825{
Jeff Layton873e3852015-06-09 19:44:00 -04005826 size_t len;
5827 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005828
5829 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005830 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005831
5832 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005833 return nfs4_init_uniquifier_client_string(clp);
5834
5835 len = 10 + 10 + 1 + 10 + 1 +
5836 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5837
5838 if (len > NFS4_OPAQUE_LIMIT + 1)
5839 return -EINVAL;
5840
5841 /*
5842 * Since this string is allocated at mount time, and held until the
5843 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5844 * about a memory-reclaim deadlock.
5845 */
5846 str = kmalloc(len, GFP_KERNEL);
5847 if (!str)
5848 return -ENOMEM;
5849
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005850 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005851 clp->rpc_ops->version, clp->cl_minorversion,
5852 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005853 clp->cl_owner_id = str;
5854 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005855}
5856
Chuck Lever706cb8d2014-03-12 12:51:47 -04005857/*
5858 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5859 * services. Advertise one based on the address family of the
5860 * clientaddr.
5861 */
5862static unsigned int
5863nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5864{
5865 if (strchr(clp->cl_ipaddr, ':') != NULL)
5866 return scnprintf(buf, len, "tcp6");
5867 else
5868 return scnprintf(buf, len, "tcp");
5869}
5870
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005871static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5872{
5873 struct nfs4_setclientid *sc = calldata;
5874
5875 if (task->tk_status == 0)
5876 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5877}
5878
5879static const struct rpc_call_ops nfs4_setclientid_ops = {
5880 .rpc_call_done = nfs4_setclientid_done,
5881};
5882
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005883/**
5884 * nfs4_proc_setclientid - Negotiate client ID
5885 * @clp: state data structure
5886 * @program: RPC program for NFSv4 callback service
5887 * @port: IP port number for NFS4 callback service
5888 * @cred: RPC credential to use for this call
5889 * @res: where to place the result
5890 *
5891 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5892 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005893int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5894 unsigned short port, struct rpc_cred *cred,
5895 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005896{
5897 nfs4_verifier sc_verifier;
5898 struct nfs4_setclientid setclientid = {
5899 .sc_verifier = &sc_verifier,
5900 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005901 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005902 };
5903 struct rpc_message msg = {
5904 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5905 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005906 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005907 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005908 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005909 struct rpc_task *task;
5910 struct rpc_task_setup task_setup_data = {
5911 .rpc_client = clp->cl_rpcclient,
5912 .rpc_message = &msg,
5913 .callback_ops = &nfs4_setclientid_ops,
5914 .callback_data = &setclientid,
5915 .flags = RPC_TASK_TIMEOUT,
5916 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005917 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005918
Chuck Leverde734832012-07-11 16:30:50 -04005919 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005920 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005921
5922 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5923 status = nfs4_init_uniform_client_string(clp);
5924 else
Jeff Laytona3192682015-06-09 19:43:59 -04005925 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005926
5927 if (status)
5928 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005929
Chuck Leverde734832012-07-11 16:30:50 -04005930 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005931 setclientid.sc_netid_len =
5932 nfs4_init_callback_netid(clp,
5933 setclientid.sc_netid,
5934 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005935 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005936 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005937 clp->cl_ipaddr, port >> 8, port & 255);
5938
Jeff Layton3a6bb732015-06-09 19:43:57 -04005939 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005940 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005941 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005942 task = rpc_run_task(&task_setup_data);
5943 if (IS_ERR(task)) {
5944 status = PTR_ERR(task);
5945 goto out;
5946 }
5947 status = task->tk_status;
5948 if (setclientid.sc_cred) {
5949 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5950 put_rpccred(setclientid.sc_cred);
5951 }
5952 rpc_put_task(task);
5953out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005954 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005955 dprintk("NFS reply setclientid: %d\n", status);
5956 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005957}
5958
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005959/**
5960 * nfs4_proc_setclientid_confirm - Confirm client ID
5961 * @clp: state data structure
5962 * @res: result of a previous SETCLIENTID
5963 * @cred: RPC credential to use for this call
5964 *
5965 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5966 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005967int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005968 struct nfs4_setclientid_res *arg,
5969 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005970{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005971 struct rpc_message msg = {
5972 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005973 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005974 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005975 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976 int status;
5977
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005978 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5979 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5980 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005981 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005982 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005983 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 return status;
5985}
5986
Trond Myklebustfe650402006-01-03 09:55:18 +01005987struct nfs4_delegreturndata {
5988 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005989 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005990 struct nfs_fh fh;
5991 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005992 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005993 struct {
5994 struct nfs4_layoutreturn_args arg;
5995 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005996 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005997 u32 roc_barrier;
5998 bool roc;
5999 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006000 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01006001 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08006002 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01006003};
6004
Trond Myklebustfe650402006-01-03 09:55:18 +01006005static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
6006{
6007 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05006008 struct nfs4_exception exception = {
6009 .inode = data->inode,
6010 .stateid = &data->stateid,
6011 };
Andy Adamson938e1012009-04-01 09:22:28 -04006012
Trond Myklebust14516c32010-07-31 14:29:06 -04006013 if (!nfs4_sequence_done(task, &data->res.seq_res))
6014 return;
Andy Adamson938e1012009-04-01 09:22:28 -04006015
Trond Myklebustca8acf82013-08-13 10:36:56 -04006016 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05006017
6018 /* Handle Layoutreturn errors */
6019 if (data->args.lr_args && task->tk_status != 0) {
6020 switch(data->res.lr_ret) {
6021 default:
6022 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
6023 break;
6024 case 0:
6025 data->args.lr_args = NULL;
6026 data->res.lr_res = NULL;
6027 break;
Trond Myklebust73800202017-11-06 15:28:07 -05006028 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04006029 if (nfs4_layoutreturn_refresh_stateid(&data->args.lr_args->stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04006030 &data->args.lr_args->range,
Trond Myklebust140087fd2017-11-06 15:28:10 -05006031 data->inode))
6032 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05006033 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05006034 case -NFS4ERR_ADMIN_REVOKED:
6035 case -NFS4ERR_DELEG_REVOKED:
6036 case -NFS4ERR_EXPIRED:
6037 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05006038 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
6039 case -NFS4ERR_WRONG_CRED:
6040 data->args.lr_args = NULL;
6041 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006042 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006043 }
6044 }
6045
Ricardo Labiaga79708862009-12-07 09:23:21 -05006046 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05006047 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01006048 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05006049 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05006050 case -NFS4ERR_ADMIN_REVOKED:
6051 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04006052 case -NFS4ERR_EXPIRED:
6053 nfs4_free_revoked_stateid(data->res.server,
6054 data->args.stateid,
6055 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05006056 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05006057 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006058 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05006059 task->tk_status = 0;
6060 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006061 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05006062 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
6063 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05006064 task->tk_status = 0;
6065 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006066 case -NFS4ERR_ACCESS:
6067 if (data->args.bitmask) {
6068 data->args.bitmask = NULL;
6069 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006070 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05006071 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05006072 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05006073 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05006074 task->tk_status = nfs4_async_handle_exception(task,
6075 data->res.server, task->tk_status,
6076 &exception);
6077 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05006078 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05006079 }
6080 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05006081 return;
6082lr_restart:
6083 data->res.lr_ret = 0;
6084out_restart:
6085 task->tk_status = 0;
6086 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006087}
6088
6089static void nfs4_delegreturn_release(void *calldata)
6090{
Peng Tao039b7562014-07-03 13:05:02 +08006091 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006092 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08006093
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006094 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05006095 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006096 pnfs_roc_release(&data->lr.arg, &data->lr.res,
6097 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05006098 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006099 nfs_iput_and_deactive(inode);
6100 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006101 kfree(calldata);
6102}
6103
Andy Adamson938e1012009-04-01 09:22:28 -04006104static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
6105{
6106 struct nfs4_delegreturndata *d_data;
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006107 struct pnfs_layout_hdr *lo;
Andy Adamson938e1012009-04-01 09:22:28 -04006108
6109 d_data = (struct nfs4_delegreturndata *)data;
6110
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006111 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08006112 return;
6113
Trond Myklebustc8bf7072018-06-15 16:31:02 -04006114 lo = d_data->args.lr_args ? d_data->args.lr_args->layout : NULL;
6115 if (lo && !pnfs_layout_is_valid(lo)) {
6116 d_data->args.lr_args = NULL;
6117 d_data->res.lr_res = NULL;
6118 }
6119
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006120 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006121 &d_data->args.seq_args,
6122 &d_data->res.seq_res,
6123 task);
Andy Adamson938e1012009-04-01 09:22:28 -04006124}
Andy Adamson938e1012009-04-01 09:22:28 -04006125
Jesper Juhlc8d149f2006-03-20 13:44:07 -05006126static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04006127 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01006128 .rpc_call_done = nfs4_delegreturn_done,
6129 .rpc_release = nfs4_delegreturn_release,
6130};
6131
Trond Myklebuste6f81072008-01-24 18:14:34 -05006132static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01006133{
6134 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01006135 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01006136 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006137 struct rpc_message msg = {
6138 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
6139 .rpc_cred = cred,
6140 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006141 struct rpc_task_setup task_setup_data = {
6142 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04006143 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006144 .callback_ops = &nfs4_delegreturn_ops,
6145 .flags = RPC_TASK_ASYNC,
6146 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05006147 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01006148
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006149 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01006150 if (data == NULL)
6151 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006152 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05006153
6154 nfs4_state_protect(server->nfs_client,
6155 NFS_SP4_MACH_CRED_CLEANUP,
6156 &task_setup_data.rpc_client, &msg);
6157
Trond Myklebustfe650402006-01-03 09:55:18 +01006158 data->args.fhandle = &data->fh;
6159 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04006160 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01006161 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05006162 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01006163 data->res.fattr = &data->fattr;
6164 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05006165 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04006166 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006167 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01006168 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01006169 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006170 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05006171 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006172 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006173 if (data->lr.roc) {
6174 data->args.lr_args = &data->lr.arg;
6175 data->res.lr_res = &data->lr.res;
6176 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006177 } else if (data->lr.roc) {
6178 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6179 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006180 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006181
Trond Myklebustc970aa82007-07-14 15:39:59 -04006182 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006183 msg.rpc_argp = &data->args;
6184 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006185 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006186 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006187 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006188 if (!issync)
6189 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006190 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006191 if (status != 0)
6192 goto out;
6193 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006194out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006195 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006196 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006197}
6198
Trond Myklebuste6f81072008-01-24 18:14:34 -05006199int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006200{
6201 struct nfs_server *server = NFS_SERVER(inode);
6202 struct nfs4_exception exception = { };
6203 int err;
6204 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006205 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006206 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207 switch (err) {
6208 case -NFS4ERR_STALE_STATEID:
6209 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210 case 0:
6211 return 0;
6212 }
6213 err = nfs4_handle_exception(server, err, &exception);
6214 } while (exception.retry);
6215 return err;
6216}
6217
Linus Torvalds1da177e2005-04-16 15:20:36 -07006218static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6219{
6220 struct inode *inode = state->inode;
6221 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006222 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006223 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006224 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006225 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006227 struct nfs_lockt_res res = {
6228 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006229 };
6230 struct rpc_message msg = {
6231 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006232 .rpc_argp = &arg,
6233 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006234 .rpc_cred = state->owner->so_cred,
6235 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006236 struct nfs4_lock_state *lsp;
6237 int status;
6238
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006239 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006240 status = nfs4_set_lock_state(state, request);
6241 if (status != 0)
6242 goto out;
6243 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006244 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006245 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006246 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006247 switch (status) {
6248 case 0:
6249 request->fl_type = F_UNLCK;
6250 break;
6251 case -NFS4ERR_DENIED:
6252 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006253 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006254 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006255 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006256out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006257 return status;
6258}
6259
6260static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6261{
6262 struct nfs4_exception exception = { };
6263 int err;
6264
6265 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006266 err = _nfs4_proc_getlk(state, cmd, request);
6267 trace_nfs4_get_lock(request, state, cmd, err);
6268 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269 &exception);
6270 } while (exception.retry);
6271 return err;
6272}
6273
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006274struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006275 struct nfs_locku_args arg;
6276 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006277 struct nfs4_lock_state *lsp;
6278 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006279 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006280 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006281 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006282 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006283};
6284
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006285static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6286 struct nfs_open_context *ctx,
6287 struct nfs4_lock_state *lsp,
6288 struct nfs_seqid *seqid)
6289{
6290 struct nfs4_unlockdata *p;
6291 struct inode *inode = lsp->ls_state->inode;
6292
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006293 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006294 if (p == NULL)
6295 return NULL;
6296 p->arg.fh = NFS_FH(inode);
6297 p->arg.fl = &p->fl;
6298 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006299 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006300 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006301 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006302 /* Ensure we don't close file until we're done freeing locks! */
6303 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006304 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006305 memcpy(&p->fl, fl, sizeof(p->fl));
6306 p->server = NFS_SERVER(inode);
6307 return p;
6308}
6309
Trond Myklebust06f814a2006-01-03 09:55:07 +01006310static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006311{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006312 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006313 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006314 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006315 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006316 put_nfs_open_context(calldata->ctx);
6317 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006318}
6319
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006320static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006321{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006322 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006323 struct nfs4_exception exception = {
6324 .inode = calldata->lsp->ls_state->inode,
6325 .stateid = &calldata->arg.stateid,
6326 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006327
Trond Myklebust14516c32010-07-31 14:29:06 -04006328 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6329 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006330 switch (task->tk_status) {
6331 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006332 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006333 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006334 if (nfs4_update_lock_stateid(calldata->lsp,
6335 &calldata->res.stateid))
6336 break;
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006337 /* Fall through */
Trond Myklebust26d36302016-09-22 13:39:05 -04006338 case -NFS4ERR_ADMIN_REVOKED:
6339 case -NFS4ERR_EXPIRED:
6340 nfs4_free_revoked_stateid(calldata->server,
6341 &calldata->arg.stateid,
6342 task->tk_msg.rpc_cred);
Gustavo A. R. Silva01e03bd2018-07-31 21:18:44 -05006343 /* Fall through */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006344 case -NFS4ERR_BAD_STATEID:
6345 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006346 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006347 if (!nfs4_stateid_match(&calldata->arg.stateid,
6348 &calldata->lsp->ls_stateid))
6349 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006350 break;
6351 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006352 task->tk_status = nfs4_async_handle_exception(task,
6353 calldata->server, task->tk_status,
6354 &exception);
6355 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006356 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006357 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006358 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006359}
6360
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006361static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006362{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006363 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006364
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006365 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6366 nfs_async_iocounter_wait(task, calldata->l_ctx))
6367 return;
6368
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006369 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006370 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006371 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006372 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006373 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006374 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006375 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006376 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006377 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006378 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006379 &calldata->res.seq_res,
6380 task) != 0)
6381 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006382 return;
6383out_no_action:
6384 task->tk_action = NULL;
6385out_wait:
6386 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006387}
6388
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006389static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006390 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006391 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006392 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006393};
6394
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006395static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6396 struct nfs_open_context *ctx,
6397 struct nfs4_lock_state *lsp,
6398 struct nfs_seqid *seqid)
6399{
6400 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006401 struct rpc_message msg = {
6402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6403 .rpc_cred = ctx->cred,
6404 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006405 struct rpc_task_setup task_setup_data = {
6406 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006407 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006408 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006409 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006410 .flags = RPC_TASK_ASYNC,
6411 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006412
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006413 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6414 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6415
Frank Filz137d6ac2007-07-09 15:32:29 -07006416 /* Ensure this is an unlock - when canceling a lock, the
6417 * canceled lock is passed in, and it won't be an unlock.
6418 */
6419 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006420 if (fl->fl_flags & FL_CLOSE)
6421 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006422
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006423 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6424 if (data == NULL) {
6425 nfs_free_seqid(seqid);
6426 return ERR_PTR(-ENOMEM);
6427 }
6428
Anna Schumakerfba83f32018-05-04 16:22:50 -04006429 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006430 msg.rpc_argp = &data->arg;
6431 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006432 task_setup_data.callback_data = data;
6433 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006434}
6435
Linus Torvalds1da177e2005-04-16 15:20:36 -07006436static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6437{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006438 struct inode *inode = state->inode;
6439 struct nfs4_state_owner *sp = state->owner;
6440 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006441 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006442 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006443 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006444 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006445 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006446 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006447
Trond Myklebust9b073572006-06-29 16:38:34 -04006448 status = nfs4_set_lock_state(state, request);
6449 /* Unlock _before_ we do the RPC call */
6450 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006451 /* Exclude nfs_delegation_claim_locks() */
6452 mutex_lock(&sp->so_delegreturn_mutex);
6453 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006454 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006455 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006456 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006457 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006458 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006459 }
6460 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006461 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006462 if (status != 0)
6463 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006464 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006465 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006466 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6467 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006468 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6469 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006470 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006471 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006472 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006473 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006474 status = PTR_ERR(task);
6475 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006476 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006477 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006478 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006479out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006480 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006481 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006482 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006483}
6484
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006485struct nfs4_lockdata {
6486 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006487 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006488 struct nfs4_lock_state *lsp;
6489 struct nfs_open_context *ctx;
6490 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006491 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006492 int rpc_status;
6493 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006494 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006495};
6496
6497static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006498 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6499 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006500{
6501 struct nfs4_lockdata *p;
6502 struct inode *inode = lsp->ls_state->inode;
6503 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006504 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006505
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006506 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006507 if (p == NULL)
6508 return NULL;
6509
6510 p->arg.fh = NFS_FH(inode);
6511 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006512 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006513 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006514 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006515 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6516 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006517 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006518 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006519 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006520 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006521 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006522 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006523 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006524 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006525 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006526 p->ctx = get_nfs_open_context(ctx);
6527 memcpy(&p->fl, fl, sizeof(p->fl));
6528 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006529out_free_seqid:
6530 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006531out_free:
6532 kfree(p);
6533 return NULL;
6534}
6535
6536static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6537{
6538 struct nfs4_lockdata *data = calldata;
6539 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006540
Harvey Harrison3110ff82008-05-02 13:42:44 -07006541 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006542 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006543 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006544 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006545 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006546 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006547 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006548 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006549 nfs4_stateid_copy(&data->arg.open_stateid,
6550 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006551 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006552 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006553 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006554 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006555 nfs4_stateid_copy(&data->arg.lock_stateid,
6556 &data->lsp->ls_stateid);
6557 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006558 if (!nfs4_valid_open_stateid(state)) {
6559 data->rpc_status = -EBADF;
6560 task->tk_action = NULL;
6561 goto out_release_open_seqid;
6562 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006563 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006564 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006565 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006566 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006567 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006568 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006569out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006570 nfs_release_seqid(data->arg.open_seqid);
6571out_release_lock_seqid:
6572 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006573out_wait:
6574 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006575 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006576}
6577
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006578static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6579{
6580 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006581 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006582
Harvey Harrison3110ff82008-05-02 13:42:44 -07006583 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006584
Trond Myklebust14516c32010-07-31 14:29:06 -04006585 if (!nfs4_sequence_done(task, &data->res.seq_res))
6586 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006587
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006588 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006589 switch (task->tk_status) {
6590 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006591 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006592 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006593 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006594 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006595 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006596 goto out_restart;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006597 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006598 if (data->arg.new_lock_owner != 0) {
6599 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6600 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6601 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006602 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6603 goto out_restart;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006604 break;
6605 case -NFS4ERR_BAD_STATEID:
6606 case -NFS4ERR_OLD_STATEID:
6607 case -NFS4ERR_STALE_STATEID:
6608 case -NFS4ERR_EXPIRED:
6609 if (data->arg.new_lock_owner != 0) {
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006610 if (!nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006611 &lsp->ls_state->open_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006612 goto out_restart;
6613 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006614 &lsp->ls_stateid))
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006615 goto out_restart;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006616 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006617out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006618 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006619 return;
6620out_restart:
6621 if (!data->cancelled)
6622 rpc_restart_call_prepare(task);
6623 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006624}
6625
6626static void nfs4_lock_release(void *calldata)
6627{
6628 struct nfs4_lockdata *data = calldata;
6629
Harvey Harrison3110ff82008-05-02 13:42:44 -07006630 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006631 nfs_free_seqid(data->arg.open_seqid);
Trond Myklebust6ea76bf2018-07-29 22:21:22 -04006632 if (data->cancelled && data->rpc_status == 0) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006633 struct rpc_task *task;
6634 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6635 data->arg.lock_seqid);
6636 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006637 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006638 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006639 } else
6640 nfs_free_seqid(data->arg.lock_seqid);
6641 nfs4_put_lock_state(data->lsp);
6642 put_nfs_open_context(data->ctx);
6643 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006644 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006645}
6646
6647static const struct rpc_call_ops nfs4_lock_ops = {
6648 .rpc_call_prepare = nfs4_lock_prepare,
6649 .rpc_call_done = nfs4_lock_done,
6650 .rpc_release = nfs4_lock_release,
6651};
6652
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006653static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6654{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006655 switch (error) {
6656 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006657 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006658 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006659 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006660 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006661 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006662 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006663 break;
6664 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006665 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006666 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006667 };
6668}
6669
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006670static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006671{
6672 struct nfs4_lockdata *data;
6673 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006674 struct rpc_message msg = {
6675 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6676 .rpc_cred = state->owner->so_cred,
6677 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006678 struct rpc_task_setup task_setup_data = {
6679 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006680 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006681 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006682 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006683 .flags = RPC_TASK_ASYNC,
6684 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006685 int ret;
6686
Harvey Harrison3110ff82008-05-02 13:42:44 -07006687 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006688 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006689 fl->fl_u.nfs4_fl.owner,
6690 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006691 if (data == NULL)
6692 return -ENOMEM;
6693 if (IS_SETLKW(cmd))
6694 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006695 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6696 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006697 msg.rpc_argp = &data->arg;
6698 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006699 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006700 if (recovery_type > NFS_LOCK_NEW) {
6701 if (recovery_type == NFS_LOCK_RECLAIM)
6702 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006703 } else
6704 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006705 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006706 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006707 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006708 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006709 if (ret == 0) {
6710 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006711 if (ret)
6712 nfs4_handle_setlk_error(data->server, data->lsp,
6713 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006714 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006715 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006716 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006717 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006718 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006719 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006720}
6721
6722static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6723{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006724 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006725 struct nfs4_exception exception = {
6726 .inode = state->inode,
6727 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006728 int err;
6729
6730 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006731 /* Cache the lock if possible... */
6732 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6733 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006734 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006735 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006736 break;
6737 nfs4_handle_exception(server, err, &exception);
6738 } while (exception.retry);
6739 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006740}
6741
6742static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6743{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006744 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006745 struct nfs4_exception exception = {
6746 .inode = state->inode,
6747 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006748 int err;
6749
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006750 err = nfs4_set_lock_state(state, request);
6751 if (err != 0)
6752 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006753 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006754 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6755 return 0;
6756 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006757 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006758 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6759 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006760 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006761 switch (err) {
6762 default:
6763 goto out;
6764 case -NFS4ERR_GRACE:
6765 case -NFS4ERR_DELAY:
6766 nfs4_handle_exception(server, err, &exception);
6767 err = 0;
6768 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006769 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006770out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006771 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006772}
6773
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006774#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006775static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6776{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006777 struct nfs4_lock_state *lsp;
6778 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006779
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006780 status = nfs4_set_lock_state(state, request);
6781 if (status != 0)
6782 return status;
6783 lsp = request->fl_u.nfs4_fl.owner;
6784 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6785 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6786 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006787 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006788}
6789#endif
6790
Linus Torvalds1da177e2005-04-16 15:20:36 -07006791static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6792{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006793 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006794 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006795 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006796 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006797
Trond Myklebust01c3b862006-06-29 16:38:39 -04006798 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006799 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006800 if (status < 0)
6801 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006802 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006803 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006804 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006805 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006806 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006807 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006808 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006809 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006810 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006811 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006812 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006813 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006814 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006815 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006816out:
6817 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006818 return status;
6819}
6820
6821static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6822{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006823 struct nfs4_exception exception = {
6824 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006825 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006826 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006827 int err;
6828
6829 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006830 err = _nfs4_proc_setlk(state, cmd, request);
6831 if (err == -NFS4ERR_DENIED)
6832 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006833 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006834 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006835 } while (exception.retry);
6836 return err;
6837}
6838
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006839#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6840#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6841
6842static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006843nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6844 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006845{
6846 int status = -ERESTARTSYS;
6847 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6848
6849 while(!signalled()) {
6850 status = nfs4_proc_setlk(state, cmd, request);
6851 if ((status != -EAGAIN) || IS_SETLK(cmd))
6852 break;
6853 freezable_schedule_timeout_interruptible(timeout);
6854 timeout *= 2;
6855 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6856 status = -ERESTARTSYS;
6857 }
6858 return status;
6859}
6860
Jeff Laytona1d617d82016-09-17 18:17:39 -04006861#ifdef CONFIG_NFS_V4_1
6862struct nfs4_lock_waiter {
6863 struct task_struct *task;
6864 struct inode *inode;
6865 struct nfs_lowner *owner;
6866 bool notified;
6867};
6868
6869static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006870nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006871{
6872 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006873 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006874
Jeff Layton57174592018-03-18 08:37:03 -04006875 /* NULL key means to wake up everyone */
6876 if (key) {
6877 struct cb_notify_lock_args *cbnl = key;
6878 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6879 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006880
Jeff Layton57174592018-03-18 08:37:03 -04006881 /* Only wake if the callback was for the same owner. */
6882 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6883 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006884
Jeff Layton57174592018-03-18 08:37:03 -04006885 /* Make sure it's for the right inode */
6886 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6887 return 0;
6888
6889 waiter->notified = true;
6890 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006891
6892 /* override "private" so we can use default_wake_function */
6893 wait->private = waiter->task;
6894 ret = autoremove_wake_function(wait, mode, flags, key);
6895 wait->private = waiter;
6896 return ret;
6897}
6898
6899static int
6900nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6901{
6902 int status = -ERESTARTSYS;
6903 unsigned long flags;
6904 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6905 struct nfs_server *server = NFS_SERVER(state->inode);
6906 struct nfs_client *clp = server->nfs_client;
6907 wait_queue_head_t *q = &clp->cl_lock_waitq;
6908 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6909 .id = lsp->ls_seqid.owner_id,
6910 .s_dev = server->s_dev };
6911 struct nfs4_lock_waiter waiter = { .task = current,
6912 .inode = state->inode,
6913 .owner = &owner,
6914 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006915 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006916
6917 /* Don't bother with waitqueue if we don't expect a callback */
6918 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6919 return nfs4_retry_setlk_simple(state, cmd, request);
6920
6921 init_wait(&wait);
6922 wait.private = &waiter;
6923 wait.func = nfs4_wake_lock_waiter;
6924 add_wait_queue(q, &wait);
6925
6926 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006927 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006928 status = nfs4_proc_setlk(state, cmd, request);
6929 if ((status != -EAGAIN) || IS_SETLK(cmd))
6930 break;
6931
6932 status = -ERESTARTSYS;
6933 spin_lock_irqsave(&q->lock, flags);
6934 if (waiter.notified) {
6935 spin_unlock_irqrestore(&q->lock, flags);
6936 continue;
6937 }
6938 set_current_state(TASK_INTERRUPTIBLE);
6939 spin_unlock_irqrestore(&q->lock, flags);
6940
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006941 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006942 }
6943
6944 finish_wait(q, &wait);
6945 return status;
6946}
6947#else /* !CONFIG_NFS_V4_1 */
6948static inline int
6949nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6950{
6951 return nfs4_retry_setlk_simple(state, cmd, request);
6952}
6953#endif
6954
Linus Torvalds1da177e2005-04-16 15:20:36 -07006955static int
6956nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6957{
6958 struct nfs_open_context *ctx;
6959 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006960 int status;
6961
6962 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006963 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006964 state = ctx->state;
6965
Trond Myklebustd9531262009-07-21 19:22:38 -04006966 if (IS_GETLK(cmd)) {
6967 if (state != NULL)
6968 return nfs4_proc_getlk(state, F_GETLK, request);
6969 return 0;
6970 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006971
6972 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6973 return -EINVAL;
6974
Trond Myklebustd9531262009-07-21 19:22:38 -04006975 if (request->fl_type == F_UNLCK) {
6976 if (state != NULL)
6977 return nfs4_proc_unlck(state, cmd, request);
6978 return 0;
6979 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006980
Trond Myklebustd9531262009-07-21 19:22:38 -04006981 if (state == NULL)
6982 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006983
6984 if ((request->fl_flags & FL_POSIX) &&
6985 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6986 return -ENOLCK;
6987
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006988 /*
6989 * Don't rely on the VFS having checked the file open mode,
6990 * since it won't do this for flock() locks.
6991 */
6992 switch (request->fl_type) {
6993 case F_RDLCK:
6994 if (!(filp->f_mode & FMODE_READ))
6995 return -EBADF;
6996 break;
6997 case F_WRLCK:
6998 if (!(filp->f_mode & FMODE_WRITE))
6999 return -EBADF;
7000 }
7001
Jeff Layton1ea67db2016-09-17 18:17:37 -04007002 status = nfs4_set_lock_state(state, request);
7003 if (status != 0)
7004 return status;
7005
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04007006 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07007007}
7008
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007009int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05007010{
7011 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05007012 int err;
7013
7014 err = nfs4_set_lock_state(state, fl);
7015 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04007016 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04007017 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11007018 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05007019}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007020
Trond Myklebustcf470c32012-03-07 13:49:12 -05007021struct nfs_release_lockowner_data {
7022 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007023 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007024 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007025 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04007026 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007027};
7028
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007029static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
7030{
7031 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007032 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007033 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
7034 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007035 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04007036 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007037}
7038
7039static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
7040{
7041 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04007042 struct nfs_server *server = data->server;
7043
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007044 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04007045
7046 switch (task->tk_status) {
7047 case 0:
7048 renew_lease(server, data->timestamp);
7049 break;
7050 case -NFS4ERR_STALE_CLIENTID:
7051 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08007052 nfs4_schedule_lease_recovery(server->nfs_client);
7053 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04007054 case -NFS4ERR_LEASE_MOVED:
7055 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10007056 if (nfs4_async_handle_error(task, server,
7057 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04007058 rpc_restart_call_prepare(task);
7059 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007060}
7061
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007062static void nfs4_release_lockowner_release(void *calldata)
7063{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007064 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007065 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007066 kfree(calldata);
7067}
7068
Trond Myklebust17280172012-03-11 13:11:00 -04007069static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007070 .rpc_call_prepare = nfs4_release_lockowner_prepare,
7071 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007072 .rpc_release = nfs4_release_lockowner_release,
7073};
7074
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007075static void
7076nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007077{
Trond Myklebustcf470c32012-03-07 13:49:12 -05007078 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007079 struct rpc_message msg = {
7080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
7081 };
7082
7083 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007084 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007085
Trond Myklebustcf470c32012-03-07 13:49:12 -05007086 data = kmalloc(sizeof(*data), GFP_NOFS);
7087 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04007088 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007089 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04007090 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05007091 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
7092 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
7093 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04007094
Trond Myklebustcf470c32012-03-07 13:49:12 -05007095 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08007096 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04007097 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05007098 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04007099}
7100
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00007101#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
7102
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007103static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007104 struct dentry *unused, struct inode *inode,
7105 const char *key, const void *buf,
7106 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007107{
Al Viro59301222016-05-27 10:19:30 -04007108 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007109}
7110
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007111static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007112 struct dentry *unused, struct inode *inode,
7113 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007114{
Al Virob2968212016-04-10 20:48:24 -04007115 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007116}
7117
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007118static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007119{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01007120 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00007121}
7122
David Quigleyc9bccef2013-05-22 12:50:45 -04007123#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04007124
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007125static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04007126 struct dentry *unused, struct inode *inode,
7127 const char *key, const void *buf,
7128 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04007129{
7130 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04007131 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007132
7133 return -EOPNOTSUPP;
7134}
7135
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02007136static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04007137 struct dentry *unused, struct inode *inode,
7138 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04007139{
7140 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04007141 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04007142 return -EOPNOTSUPP;
7143}
7144
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007145static ssize_t
7146nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04007147{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007148 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04007149
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007150 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
7151 len = security_inode_listsecurity(inode, list, list_len);
7152 if (list_len && len > list_len)
7153 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04007154 }
7155 return len;
7156}
7157
7158static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
7159 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04007160 .get = nfs4_xattr_get_nfs4_label,
7161 .set = nfs4_xattr_set_nfs4_label,
7162};
David Quigleyc9bccef2013-05-22 12:50:45 -04007163
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01007164#else
7165
7166static ssize_t
7167nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
7168{
7169 return 0;
7170}
7171
7172#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04007173
Andy Adamson533eb462011-06-13 18:25:56 -04007174/*
7175 * nfs_fhget will use either the mounted_on_fileid or the fileid
7176 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007177static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
7178{
Andy Adamson533eb462011-06-13 18:25:56 -04007179 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
7180 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7181 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007182 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007183 return;
7184
7185 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007186 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007187 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7188 fattr->nlink = 2;
7189}
7190
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007191static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7192 const struct qstr *name,
7193 struct nfs4_fs_locations *fs_locations,
7194 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007195{
7196 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007197 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007198 struct nfs4_fs_locations_arg args = {
7199 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007200 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007201 .page = page,
7202 .bitmask = bitmask,
7203 };
Benny Halevy22958462009-04-01 09:22:02 -04007204 struct nfs4_fs_locations_res res = {
7205 .fs_locations = fs_locations,
7206 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007207 struct rpc_message msg = {
7208 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7209 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007210 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007211 };
7212 int status;
7213
Harvey Harrison3110ff82008-05-02 13:42:44 -07007214 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007215
Chuck Leverc05cefc2017-11-05 15:45:22 -05007216 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7217 bitmask[1] = nfs4_fattr_bitmap[1];
7218
Andy Adamson533eb462011-06-13 18:25:56 -04007219 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7220 * is not supported */
7221 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007222 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007223 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007224 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007225
Trond Myklebustc228fd32007-01-13 02:28:11 -05007226 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007227 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007228 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007229 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007230 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007231 return status;
7232}
7233
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007234int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7235 const struct qstr *name,
7236 struct nfs4_fs_locations *fs_locations,
7237 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007238{
7239 struct nfs4_exception exception = { };
7240 int err;
7241 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007242 err = _nfs4_proc_fs_locations(client, dir, name,
7243 fs_locations, page);
7244 trace_nfs4_get_fs_locations(dir, name, err);
7245 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007246 &exception);
7247 } while (exception.retry);
7248 return err;
7249}
7250
Chuck Leverb03d7352013-10-17 14:12:50 -04007251/*
7252 * This operation also signals the server that this client is
7253 * performing migration recovery. The server can stop returning
7254 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7255 * appended to this compound to identify the client ID which is
7256 * performing recovery.
7257 */
7258static int _nfs40_proc_get_locations(struct inode *inode,
7259 struct nfs4_fs_locations *locations,
7260 struct page *page, struct rpc_cred *cred)
7261{
7262 struct nfs_server *server = NFS_SERVER(inode);
7263 struct rpc_clnt *clnt = server->client;
7264 u32 bitmask[2] = {
7265 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7266 };
7267 struct nfs4_fs_locations_arg args = {
7268 .clientid = server->nfs_client->cl_clientid,
7269 .fh = NFS_FH(inode),
7270 .page = page,
7271 .bitmask = bitmask,
7272 .migration = 1, /* skip LOOKUP */
7273 .renew = 1, /* append RENEW */
7274 };
7275 struct nfs4_fs_locations_res res = {
7276 .fs_locations = locations,
7277 .migration = 1,
7278 .renew = 1,
7279 };
7280 struct rpc_message msg = {
7281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7282 .rpc_argp = &args,
7283 .rpc_resp = &res,
7284 .rpc_cred = cred,
7285 };
7286 unsigned long now = jiffies;
7287 int status;
7288
7289 nfs_fattr_init(&locations->fattr);
7290 locations->server = server;
7291 locations->nlocations = 0;
7292
Anna Schumakerfba83f32018-05-04 16:22:50 -04007293 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007294 status = nfs4_call_sync_sequence(clnt, server, &msg,
7295 &args.seq_args, &res.seq_res);
7296 if (status)
7297 return status;
7298
7299 renew_lease(server, now);
7300 return 0;
7301}
7302
7303#ifdef CONFIG_NFS_V4_1
7304
7305/*
7306 * This operation also signals the server that this client is
7307 * performing migration recovery. The server can stop asserting
7308 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7309 * performing this operation is identified in the SEQUENCE
7310 * operation in this compound.
7311 *
7312 * When the client supports GETATTR(fs_locations_info), it can
7313 * be plumbed in here.
7314 */
7315static int _nfs41_proc_get_locations(struct inode *inode,
7316 struct nfs4_fs_locations *locations,
7317 struct page *page, struct rpc_cred *cred)
7318{
7319 struct nfs_server *server = NFS_SERVER(inode);
7320 struct rpc_clnt *clnt = server->client;
7321 u32 bitmask[2] = {
7322 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7323 };
7324 struct nfs4_fs_locations_arg args = {
7325 .fh = NFS_FH(inode),
7326 .page = page,
7327 .bitmask = bitmask,
7328 .migration = 1, /* skip LOOKUP */
7329 };
7330 struct nfs4_fs_locations_res res = {
7331 .fs_locations = locations,
7332 .migration = 1,
7333 };
7334 struct rpc_message msg = {
7335 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7336 .rpc_argp = &args,
7337 .rpc_resp = &res,
7338 .rpc_cred = cred,
7339 };
7340 int status;
7341
7342 nfs_fattr_init(&locations->fattr);
7343 locations->server = server;
7344 locations->nlocations = 0;
7345
Anna Schumakerfba83f32018-05-04 16:22:50 -04007346 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007347 status = nfs4_call_sync_sequence(clnt, server, &msg,
7348 &args.seq_args, &res.seq_res);
7349 if (status == NFS4_OK &&
7350 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7351 status = -NFS4ERR_LEASE_MOVED;
7352 return status;
7353}
7354
7355#endif /* CONFIG_NFS_V4_1 */
7356
7357/**
7358 * nfs4_proc_get_locations - discover locations for a migrated FSID
7359 * @inode: inode on FSID that is migrating
7360 * @locations: result of query
7361 * @page: buffer
7362 * @cred: credential to use for this operation
7363 *
7364 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7365 * operation failed, or a negative errno if a local error occurred.
7366 *
7367 * On success, "locations" is filled in, but if the server has
7368 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7369 * asserted.
7370 *
7371 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7372 * from this client that require migration recovery.
7373 */
7374int nfs4_proc_get_locations(struct inode *inode,
7375 struct nfs4_fs_locations *locations,
7376 struct page *page, struct rpc_cred *cred)
7377{
7378 struct nfs_server *server = NFS_SERVER(inode);
7379 struct nfs_client *clp = server->nfs_client;
7380 const struct nfs4_mig_recovery_ops *ops =
7381 clp->cl_mvops->mig_recovery_ops;
7382 struct nfs4_exception exception = { };
7383 int status;
7384
7385 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7386 (unsigned long long)server->fsid.major,
7387 (unsigned long long)server->fsid.minor,
7388 clp->cl_hostname);
7389 nfs_display_fhandle(NFS_FH(inode), __func__);
7390
7391 do {
7392 status = ops->get_locations(inode, locations, page, cred);
7393 if (status != -NFS4ERR_DELAY)
7394 break;
7395 nfs4_handle_exception(server, status, &exception);
7396 } while (exception.retry);
7397 return status;
7398}
7399
Chuck Lever44c99932013-10-17 14:13:30 -04007400/*
7401 * This operation also signals the server that this client is
7402 * performing "lease moved" recovery. The server can stop
7403 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7404 * is appended to this compound to identify the client ID which is
7405 * performing recovery.
7406 */
7407static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7408{
7409 struct nfs_server *server = NFS_SERVER(inode);
7410 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7411 struct rpc_clnt *clnt = server->client;
7412 struct nfs4_fsid_present_arg args = {
7413 .fh = NFS_FH(inode),
7414 .clientid = clp->cl_clientid,
7415 .renew = 1, /* append RENEW */
7416 };
7417 struct nfs4_fsid_present_res res = {
7418 .renew = 1,
7419 };
7420 struct rpc_message msg = {
7421 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7422 .rpc_argp = &args,
7423 .rpc_resp = &res,
7424 .rpc_cred = cred,
7425 };
7426 unsigned long now = jiffies;
7427 int status;
7428
7429 res.fh = nfs_alloc_fhandle();
7430 if (res.fh == NULL)
7431 return -ENOMEM;
7432
Anna Schumakerfba83f32018-05-04 16:22:50 -04007433 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007434 status = nfs4_call_sync_sequence(clnt, server, &msg,
7435 &args.seq_args, &res.seq_res);
7436 nfs_free_fhandle(res.fh);
7437 if (status)
7438 return status;
7439
7440 do_renew_lease(clp, now);
7441 return 0;
7442}
7443
7444#ifdef CONFIG_NFS_V4_1
7445
7446/*
7447 * This operation also signals the server that this client is
7448 * performing "lease moved" recovery. The server can stop asserting
7449 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7450 * this operation is identified in the SEQUENCE operation in this
7451 * compound.
7452 */
7453static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7454{
7455 struct nfs_server *server = NFS_SERVER(inode);
7456 struct rpc_clnt *clnt = server->client;
7457 struct nfs4_fsid_present_arg args = {
7458 .fh = NFS_FH(inode),
7459 };
7460 struct nfs4_fsid_present_res res = {
7461 };
7462 struct rpc_message msg = {
7463 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7464 .rpc_argp = &args,
7465 .rpc_resp = &res,
7466 .rpc_cred = cred,
7467 };
7468 int status;
7469
7470 res.fh = nfs_alloc_fhandle();
7471 if (res.fh == NULL)
7472 return -ENOMEM;
7473
Anna Schumakerfba83f32018-05-04 16:22:50 -04007474 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007475 status = nfs4_call_sync_sequence(clnt, server, &msg,
7476 &args.seq_args, &res.seq_res);
7477 nfs_free_fhandle(res.fh);
7478 if (status == NFS4_OK &&
7479 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7480 status = -NFS4ERR_LEASE_MOVED;
7481 return status;
7482}
7483
7484#endif /* CONFIG_NFS_V4_1 */
7485
7486/**
7487 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7488 * @inode: inode on FSID to check
7489 * @cred: credential to use for this operation
7490 *
7491 * Server indicates whether the FSID is present, moved, or not
7492 * recognized. This operation is necessary to clear a LEASE_MOVED
7493 * condition for this client ID.
7494 *
7495 * Returns NFS4_OK if the FSID is present on this server,
7496 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7497 * NFS4ERR code if some error occurred on the server, or a
7498 * negative errno if a local failure occurred.
7499 */
7500int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7501{
7502 struct nfs_server *server = NFS_SERVER(inode);
7503 struct nfs_client *clp = server->nfs_client;
7504 const struct nfs4_mig_recovery_ops *ops =
7505 clp->cl_mvops->mig_recovery_ops;
7506 struct nfs4_exception exception = { };
7507 int status;
7508
7509 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7510 (unsigned long long)server->fsid.major,
7511 (unsigned long long)server->fsid.minor,
7512 clp->cl_hostname);
7513 nfs_display_fhandle(NFS_FH(inode), __func__);
7514
7515 do {
7516 status = ops->fsid_present(inode, cred);
7517 if (status != -NFS4ERR_DELAY)
7518 break;
7519 nfs4_handle_exception(server, status, &exception);
7520 } while (exception.retry);
7521 return status;
7522}
7523
Andy Adamson5ec16a82013-08-08 10:57:55 -04007524/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007525 * If 'use_integrity' is true and the state managment nfs_client
7526 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7527 * and the machine credential as per RFC3530bis and RFC5661 Security
7528 * Considerations sections. Otherwise, just use the user cred with the
7529 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007530 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007531static 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 +00007532{
7533 int status;
7534 struct nfs4_secinfo_arg args = {
7535 .dir_fh = NFS_FH(dir),
7536 .name = name,
7537 };
7538 struct nfs4_secinfo_res res = {
7539 .flavors = flavors,
7540 };
7541 struct rpc_message msg = {
7542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7543 .rpc_argp = &args,
7544 .rpc_resp = &res,
7545 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007546 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007547 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007548
7549 if (use_integrity) {
7550 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007551 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7552 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007553 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007554
7555 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007556
7557 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7558 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7559
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007560 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7561 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007562 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007563
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007564 if (cred)
7565 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007566
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007567 return status;
7568}
7569
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007570int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7571 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007572{
7573 struct nfs4_exception exception = { };
7574 int err;
7575 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007576 err = -NFS4ERR_WRONGSEC;
7577
7578 /* try to use integrity protection with machine cred */
7579 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7580 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7581
7582 /*
7583 * if unable to use integrity protection, or SECINFO with
7584 * integrity protection returns NFS4ERR_WRONGSEC (which is
7585 * disallowed by spec, but exists in deployed servers) use
7586 * the current filesystem's rpc_client and the user cred.
7587 */
7588 if (err == -NFS4ERR_WRONGSEC)
7589 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7590
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007591 trace_nfs4_secinfo(dir, name, err);
7592 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007593 &exception);
7594 } while (exception.retry);
7595 return err;
7596}
7597
Andy Adamson557134a2009-04-01 09:21:53 -04007598#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007599/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007600 * Check the exchange flags returned by the server for invalid flags, having
7601 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7602 * DS flags set.
7603 */
7604static int nfs4_check_cl_exchange_flags(u32 flags)
7605{
7606 if (flags & ~EXCHGID4_FLAG_MASK_R)
7607 goto out_inval;
7608 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7609 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7610 goto out_inval;
7611 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7612 goto out_inval;
7613 return NFS_OK;
7614out_inval:
7615 return -NFS4ERR_INVAL;
7616}
7617
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007618static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007619nfs41_same_server_scope(struct nfs41_server_scope *a,
7620 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007621{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007622 if (a->server_scope_sz != b->server_scope_sz)
7623 return false;
7624 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007625}
7626
Andy Adamson02a95de2016-02-05 16:08:37 -05007627static void
7628nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7629{
7630}
7631
7632static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7633 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7634};
7635
Andy Adamson357f54d2010-12-14 10:11:57 -05007636/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007637 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007638 *
7639 * The 4.1 client currently uses the same TCP connection for the
7640 * fore and backchannel.
7641 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007642static
7643int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7644 struct rpc_xprt *xprt,
7645 struct nfs_client *clp,
7646 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007647{
7648 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007649 struct nfs41_bind_conn_to_session_args args = {
7650 .client = clp,
7651 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7652 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007653 struct nfs41_bind_conn_to_session_res res;
7654 struct rpc_message msg = {
7655 .rpc_proc =
7656 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007657 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007658 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007659 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007660 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007661 struct rpc_task_setup task_setup_data = {
7662 .rpc_client = clnt,
7663 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007664 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007665 .rpc_message = &msg,
7666 .flags = RPC_TASK_TIMEOUT,
7667 };
7668 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007669
Trond Myklebust71a097c2015-02-18 09:27:18 -08007670 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7671 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7672 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007673
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007674 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7675 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7676 args.dir = NFS4_CDFC4_FORE;
7677
7678 task = rpc_run_task(&task_setup_data);
7679 if (!IS_ERR(task)) {
7680 status = task->tk_status;
7681 rpc_put_task(task);
7682 } else
7683 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007684 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007685 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007686 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007687 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7688 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007689 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007690 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007691 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007692 dprintk("NFS: %s: Unexpected direction from server\n",
7693 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007694 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007695 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007696 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007697 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7698 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007699 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007700 }
7701 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007702
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007703 return status;
7704}
7705
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007706struct rpc_bind_conn_calldata {
7707 struct nfs_client *clp;
7708 struct rpc_cred *cred;
7709};
7710
7711static int
7712nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7713 struct rpc_xprt *xprt,
7714 void *calldata)
7715{
7716 struct rpc_bind_conn_calldata *p = calldata;
7717
7718 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7719}
7720
7721int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7722{
7723 struct rpc_bind_conn_calldata data = {
7724 .clp = clp,
7725 .cred = cred,
7726 };
7727 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7728 nfs4_proc_bind_conn_to_session_callback, &data);
7729}
7730
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007731/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007732 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7733 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007734 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007735static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7736 .how = SP4_MACH_CRED,
7737 .enforce.u.words = {
7738 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7739 1 << (OP_EXCHANGE_ID - 32) |
7740 1 << (OP_CREATE_SESSION - 32) |
7741 1 << (OP_DESTROY_SESSION - 32) |
7742 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007743 },
7744 .allow.u.words = {
7745 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007746 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007747 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007748 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007749 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007750 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007751 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007752 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007753 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007754 1 << (OP_FREE_STATEID - 32) |
7755 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007756 }
7757};
7758
7759/*
7760 * Select the state protection mode for client `clp' given the server results
7761 * from exchange_id in `sp'.
7762 *
7763 * Returns 0 on success, negative errno otherwise.
7764 */
7765static int nfs4_sp4_select_mode(struct nfs_client *clp,
7766 struct nfs41_state_protection *sp)
7767{
7768 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7769 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7770 1 << (OP_EXCHANGE_ID - 32) |
7771 1 << (OP_CREATE_SESSION - 32) |
7772 1 << (OP_DESTROY_SESSION - 32) |
7773 1 << (OP_DESTROY_CLIENTID - 32)
7774 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007775 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007776 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007777 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007778
7779 if (sp->how == SP4_MACH_CRED) {
7780 /* Print state protect result */
7781 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7782 for (i = 0; i <= LAST_NFS4_OP; i++) {
7783 if (test_bit(i, sp->enforce.u.longs))
7784 dfprintk(MOUNT, " enforce op %d\n", i);
7785 if (test_bit(i, sp->allow.u.longs))
7786 dfprintk(MOUNT, " allow op %d\n", i);
7787 }
7788
7789 /* make sure nothing is on enforce list that isn't supported */
7790 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7791 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7792 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007793 ret = -EINVAL;
7794 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007795 }
7796 }
7797
7798 /*
7799 * Minimal mode - state operations are allowed to use machine
7800 * credential. Note this already happens by default, so the
7801 * client doesn't have to do anything more than the negotiation.
7802 *
7803 * NOTE: we don't care if EXCHANGE_ID is in the list -
7804 * we're already using the machine cred for exchange_id
7805 * and will never use a different cred.
7806 */
7807 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7808 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7809 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7810 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7811 dfprintk(MOUNT, "sp4_mach_cred:\n");
7812 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007813 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007814 } else {
7815 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007816 ret = -EINVAL;
7817 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007818 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007819
7820 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007821 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7822 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007823 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7824 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007825 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007826 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007827
Andrew Elble99ade3c2015-12-02 09:39:51 -05007828 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7829 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007830 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007831 }
7832
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007833 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7834 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7835 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007836 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007837 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007838
7839 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7840 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7841 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007842 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007843 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007844
7845 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7846 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007847 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007848 }
7849
7850 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7851 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007852 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007853 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007854 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007855out:
7856 clp->cl_sp4_flags = flags;
Wei Yongjun72bf75c2018-08-02 05:42:04 +00007857 return ret;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007858}
7859
Andy Adamson8d89bd72016-09-09 09:22:18 -04007860struct nfs41_exchange_id_data {
7861 struct nfs41_exchange_id_res res;
7862 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007863};
7864
Andy Adamson8d89bd72016-09-09 09:22:18 -04007865static void nfs4_exchange_id_release(void *data)
7866{
7867 struct nfs41_exchange_id_data *cdata =
7868 (struct nfs41_exchange_id_data *)data;
7869
Olga Kornievskaia63513232017-03-13 10:36:19 -04007870 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007871 kfree(cdata->res.impl_id);
7872 kfree(cdata->res.server_scope);
7873 kfree(cdata->res.server_owner);
7874 kfree(cdata);
7875}
7876
7877static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007878 .rpc_release = nfs4_exchange_id_release,
7879};
7880
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007881/*
7882 * _nfs4_proc_exchange_id()
7883 *
7884 * Wrapper for EXCHANGE_ID operation.
7885 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007886static struct rpc_task *
7887nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007888 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007889{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007890 struct rpc_message msg = {
7891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007892 .rpc_cred = cred,
7893 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007894 struct rpc_task_setup task_setup_data = {
7895 .rpc_client = clp->cl_rpcclient,
7896 .callback_ops = &nfs4_exchange_id_call_ops,
7897 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007898 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007899 };
7900 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007901 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007902
Elena Reshetova212bf412017-10-20 12:53:38 +03007903 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007904 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007905
Trond Myklebust9c760d12017-07-31 18:38:50 -04007906 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007907 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007908 if (!calldata)
7909 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007910
Trond Myklebustfd405592017-08-01 16:02:47 -04007911 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007912
7913 status = nfs4_init_uniform_client_string(clp);
7914 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007915 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007916
Andy Adamson8d89bd72016-09-09 09:22:18 -04007917 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7918 GFP_NOFS);
7919 status = -ENOMEM;
7920 if (unlikely(calldata->res.server_owner == NULL))
7921 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007922
Andy Adamson8d89bd72016-09-09 09:22:18 -04007923 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007924 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007925 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007926 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007927
Andy Adamson8d89bd72016-09-09 09:22:18 -04007928 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7929 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007930 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007931
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007932 switch (sp4_how) {
7933 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007934 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007935 break;
7936
7937 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007938 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007939 break;
7940
7941 default:
7942 /* unsupported! */
7943 WARN_ON_ONCE(1);
7944 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007945 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007946 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007947 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007948 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007949 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007950 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7951 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007952 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007953 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007954 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7955 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007956#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007957 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007958#endif
7959 msg.rpc_argp = &calldata->args;
7960 msg.rpc_resp = &calldata->res;
7961 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007962
Trond Myklebust9c760d12017-07-31 18:38:50 -04007963 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007964
7965out_impl_id:
7966 kfree(calldata->res.impl_id);
7967out_server_scope:
7968 kfree(calldata->res.server_scope);
7969out_server_owner:
7970 kfree(calldata->res.server_owner);
7971out_calldata:
7972 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007973out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007974 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007975 return ERR_PTR(status);
7976}
7977
7978/*
7979 * _nfs4_proc_exchange_id()
7980 *
7981 * Wrapper for EXCHANGE_ID operation.
7982 */
7983static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7984 u32 sp4_how)
7985{
7986 struct rpc_task *task;
7987 struct nfs41_exchange_id_args *argp;
7988 struct nfs41_exchange_id_res *resp;
7989 int status;
7990
7991 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7992 if (IS_ERR(task))
7993 return PTR_ERR(task);
7994
7995 argp = task->tk_msg.rpc_argp;
7996 resp = task->tk_msg.rpc_resp;
7997 status = task->tk_status;
7998 if (status != 0)
7999 goto out;
8000
8001 status = nfs4_check_cl_exchange_flags(resp->flags);
8002 if (status != 0)
8003 goto out;
8004
8005 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
8006 if (status != 0)
8007 goto out;
8008
8009 clp->cl_clientid = resp->clientid;
8010 clp->cl_exchange_flags = resp->flags;
8011 clp->cl_seqid = resp->seqid;
8012 /* Client ID is not confirmed */
8013 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
8014 clear_bit(NFS4_SESSION_ESTABLISHED,
8015 &clp->cl_session->session_state);
8016
8017 if (clp->cl_serverscope != NULL &&
8018 !nfs41_same_server_scope(clp->cl_serverscope,
8019 resp->server_scope)) {
8020 dprintk("%s: server_scope mismatch detected\n",
8021 __func__);
8022 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
8023 }
8024
8025 swap(clp->cl_serverowner, resp->server_owner);
8026 swap(clp->cl_serverscope, resp->server_scope);
8027 swap(clp->cl_implid, resp->impl_id);
8028
8029 /* Save the EXCHANGE_ID verifier session trunk tests */
8030 memcpy(clp->cl_confirm.data, argp->verifier.data,
8031 sizeof(clp->cl_confirm.data));
8032out:
8033 trace_nfs4_exchange_id(clp, status);
8034 rpc_put_task(task);
8035 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04008036}
8037
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008038/*
8039 * nfs4_proc_exchange_id()
8040 *
8041 * Returns zero, a negative errno, or a negative NFS4ERR status code.
8042 *
8043 * Since the clientid has expired, all compounds using sessions
8044 * associated with the stale clientid will be returning
8045 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
8046 * be in some phase of session reset.
8047 *
8048 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
8049 */
8050int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
8051{
8052 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
8053 int status;
8054
8055 /* try SP4_MACH_CRED if krb5i/p */
8056 if (authflavor == RPC_AUTH_GSS_KRB5I ||
8057 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04008058 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008059 if (!status)
8060 return 0;
8061 }
8062
8063 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008064 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04008065}
8066
Andy Adamson04fa2c62016-09-09 09:22:29 -04008067/**
8068 * nfs4_test_session_trunk
8069 *
8070 * This is an add_xprt_test() test function called from
8071 * rpc_clnt_setup_test_and_add_xprt.
8072 *
8073 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
8074 * and is dereferrenced in nfs4_exchange_id_release
8075 *
8076 * Upon success, add the new transport to the rpc_clnt
8077 *
8078 * @clnt: struct rpc_clnt to get new transport
8079 * @xprt: the rpc_xprt to test
8080 * @data: call data for _nfs4_proc_exchange_id.
8081 */
8082int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
8083 void *data)
8084{
8085 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04008086 struct rpc_task *task;
8087 int status;
8088
Andy Adamson04fa2c62016-09-09 09:22:29 -04008089 u32 sp4_how;
8090
8091 dprintk("--> %s try %s\n", __func__,
8092 xprt->address_strings[RPC_DISPLAY_ADDR]);
8093
8094 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
8095
8096 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04008097 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
8098 if (IS_ERR(task))
8099 return PTR_ERR(task);
8100
8101 status = task->tk_status;
8102 if (status == 0)
8103 status = nfs4_detect_session_trunking(adata->clp,
8104 task->tk_msg.rpc_resp, xprt);
8105
8106 rpc_put_task(task);
8107 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04008108}
8109EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
8110
Trond Myklebust66245532012-05-25 17:18:09 -04008111static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
8112 struct rpc_cred *cred)
8113{
8114 struct rpc_message msg = {
8115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
8116 .rpc_argp = clp,
8117 .rpc_cred = cred,
8118 };
8119 int status;
8120
8121 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008122 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04008123 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04008124 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04008125 "DESTROY_CLIENTID.", status, clp->cl_hostname);
8126 return status;
8127}
8128
8129static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
8130 struct rpc_cred *cred)
8131{
8132 unsigned int loop;
8133 int ret;
8134
8135 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
8136 ret = _nfs4_proc_destroy_clientid(clp, cred);
8137 switch (ret) {
8138 case -NFS4ERR_DELAY:
8139 case -NFS4ERR_CLIENTID_BUSY:
8140 ssleep(1);
8141 break;
8142 default:
8143 return ret;
8144 }
8145 }
8146 return 0;
8147}
8148
8149int nfs4_destroy_clientid(struct nfs_client *clp)
8150{
8151 struct rpc_cred *cred;
8152 int ret = 0;
8153
8154 if (clp->cl_mvops->minor_version < 1)
8155 goto out;
8156 if (clp->cl_exchange_flags == 0)
8157 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04008158 if (clp->cl_preserve_clid)
8159 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04008160 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04008161 ret = nfs4_proc_destroy_clientid(clp, cred);
8162 if (cred)
8163 put_rpccred(cred);
8164 switch (ret) {
8165 case 0:
8166 case -NFS4ERR_STALE_CLIENTID:
8167 clp->cl_exchange_flags = 0;
8168 }
8169out:
8170 return ret;
8171}
8172
Andy Adamson2050f0c2009-04-01 09:22:30 -04008173struct nfs4_get_lease_time_data {
8174 struct nfs4_get_lease_time_args *args;
8175 struct nfs4_get_lease_time_res *res;
8176 struct nfs_client *clp;
8177};
8178
8179static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8180 void *calldata)
8181{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008182 struct nfs4_get_lease_time_data *data =
8183 (struct nfs4_get_lease_time_data *)calldata;
8184
8185 dprintk("--> %s\n", __func__);
8186 /* just setup sequence, do not trigger session recovery
8187 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008188 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008189 &data->args->la_seq_args,
8190 &data->res->lr_seq_res,
8191 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008192 dprintk("<-- %s\n", __func__);
8193}
8194
8195/*
8196 * Called from nfs4_state_manager thread for session setup, so don't recover
8197 * from sequence operation or clientid errors.
8198 */
8199static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8200{
8201 struct nfs4_get_lease_time_data *data =
8202 (struct nfs4_get_lease_time_data *)calldata;
8203
8204 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008205 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8206 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008207 switch (task->tk_status) {
8208 case -NFS4ERR_DELAY:
8209 case -NFS4ERR_GRACE:
8210 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8211 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8212 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008213 /* fall through */
8214 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008215 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008216 return;
8217 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008218 dprintk("<-- %s\n", __func__);
8219}
8220
Trond Myklebust17280172012-03-11 13:11:00 -04008221static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008222 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8223 .rpc_call_done = nfs4_get_lease_time_done,
8224};
8225
8226int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8227{
8228 struct rpc_task *task;
8229 struct nfs4_get_lease_time_args args;
8230 struct nfs4_get_lease_time_res res = {
8231 .lr_fsinfo = fsinfo,
8232 };
8233 struct nfs4_get_lease_time_data data = {
8234 .args = &args,
8235 .res = &res,
8236 .clp = clp,
8237 };
8238 struct rpc_message msg = {
8239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8240 .rpc_argp = &args,
8241 .rpc_resp = &res,
8242 };
8243 struct rpc_task_setup task_setup = {
8244 .rpc_client = clp->cl_rpcclient,
8245 .rpc_message = &msg,
8246 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008247 .callback_data = &data,
8248 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008249 };
8250 int status;
8251
Anna Schumakerfba83f32018-05-04 16:22:50 -04008252 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008253 task = rpc_run_task(&task_setup);
8254
8255 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008256 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008257
Anna Schumakerf6148712017-04-07 14:15:23 -04008258 status = task->tk_status;
8259 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008260 return status;
8261}
8262
Andy Adamsonfc931582009-04-01 09:22:31 -04008263/*
8264 * Initialize the values to be used by the client in CREATE_SESSION
8265 * If nfs4_init_session set the fore channel request and response sizes,
8266 * use them.
8267 *
8268 * Set the back channel max_resp_sz_cached to zero to force the client to
8269 * always set csa_cachethis to FALSE because the current implementation
8270 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8271 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008272static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8273 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008274{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008275 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008276 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008277
Andy Adamson18aad3d2013-06-26 12:21:49 -04008278 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8279 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8280
Andy Adamsonfc931582009-04-01 09:22:31 -04008281 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008282 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8283 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008284 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008285 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008286
8287 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008288 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008289 __func__,
8290 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008291 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008292
8293 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008294 args->bc_attrs.max_rqst_sz = max_bc_payload;
8295 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008296 args->bc_attrs.max_resp_sz_cached = 0;
8297 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust62421cd2018-08-11 11:52:39 -04008298 args->bc_attrs.max_reqs = max_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008299
8300 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8301 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8302 __func__,
8303 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8304 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8305 args->bc_attrs.max_reqs);
8306}
8307
Trond Myklebust79969dd2015-02-18 11:30:18 -08008308static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8309 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008310{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008311 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008312 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008313
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008314 if (rcvd->max_resp_sz > sent->max_resp_sz)
8315 return -EINVAL;
8316 /*
8317 * Our requested max_ops is the minimum we need; we're not
8318 * prepared to break up compounds into smaller pieces than that.
8319 * So, no point even trying to continue if the server won't
8320 * cooperate:
8321 */
8322 if (rcvd->max_ops < sent->max_ops)
8323 return -EINVAL;
8324 if (rcvd->max_reqs == 0)
8325 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008326 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8327 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008328 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008329}
8330
Trond Myklebust79969dd2015-02-18 11:30:18 -08008331static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8332 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008333{
8334 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008335 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008336
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008337 if (!(res->flags & SESSION4_BACK_CHAN))
8338 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008339 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8340 return -EINVAL;
8341 if (rcvd->max_resp_sz < sent->max_resp_sz)
8342 return -EINVAL;
8343 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8344 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008345 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008346 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008347 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008348 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008349out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008350 return 0;
8351}
Andy Adamson8d353012009-04-01 09:22:32 -04008352
Andy Adamson8d353012009-04-01 09:22:32 -04008353static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008354 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008355{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008356 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008357
Trond Myklebust79969dd2015-02-18 11:30:18 -08008358 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008359 if (ret)
8360 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008361 return nfs4_verify_back_channel_attrs(args, res);
8362}
8363
8364static void nfs4_update_session(struct nfs4_session *session,
8365 struct nfs41_create_session_res *res)
8366{
8367 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008368 /* Mark client id and session as being confirmed */
8369 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8370 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008371 session->flags = res->flags;
8372 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008373 if (res->flags & SESSION4_BACK_CHAN)
8374 memcpy(&session->bc_attrs, &res->bc_attrs,
8375 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008376}
8377
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008378static int _nfs4_proc_create_session(struct nfs_client *clp,
8379 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008380{
8381 struct nfs4_session *session = clp->cl_session;
8382 struct nfs41_create_session_args args = {
8383 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008384 .clientid = clp->cl_clientid,
8385 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008386 .cb_program = NFS4_CALLBACK,
8387 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008388 struct nfs41_create_session_res res;
8389
Andy Adamsonfc931582009-04-01 09:22:31 -04008390 struct rpc_message msg = {
8391 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8392 .rpc_argp = &args,
8393 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008394 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008395 };
8396 int status;
8397
Chuck Lever6b26cc82016-05-02 14:40:40 -04008398 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008399 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008400
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008401 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008402 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008403
Trond Myklebustb519d402016-09-11 14:50:01 -04008404 switch (status) {
8405 case -NFS4ERR_STALE_CLIENTID:
8406 case -NFS4ERR_DELAY:
8407 case -ETIMEDOUT:
8408 case -EACCES:
8409 case -EAGAIN:
8410 goto out;
8411 };
8412
8413 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008414 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008415 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008416 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008417 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008418 if (status)
8419 goto out;
8420 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008421 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008422out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008423 return status;
8424}
8425
8426/*
8427 * Issues a CREATE_SESSION operation to the server.
8428 * It is the responsibility of the caller to verify the session is
8429 * expired before calling this routine.
8430 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008431int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008432{
8433 int status;
8434 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008435 struct nfs4_session *session = clp->cl_session;
8436
8437 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8438
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008439 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008440 if (status)
8441 goto out;
8442
Andy Adamsonaacd5532011-11-09 13:58:21 -05008443 /* Init or reset the session slot tables */
8444 status = nfs4_setup_session_slot_tables(session);
8445 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008446 if (status)
8447 goto out;
8448
8449 ptr = (unsigned *)&session->sess_id.data[0];
8450 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8451 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008452out:
8453 dprintk("<-- %s\n", __func__);
8454 return status;
8455}
8456
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008457/*
8458 * Issue the over-the-wire RPC DESTROY_SESSION.
8459 * The caller must serialize access to this routine.
8460 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008461int nfs4_proc_destroy_session(struct nfs4_session *session,
8462 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008463{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008464 struct rpc_message msg = {
8465 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8466 .rpc_argp = session,
8467 .rpc_cred = cred,
8468 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008469 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008470
8471 dprintk("--> nfs4_proc_destroy_session\n");
8472
8473 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008474 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8475 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008476
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008477 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008478 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008479
8480 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008481 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008482 "Session has been destroyed regardless...\n", status);
8483
8484 dprintk("<-- nfs4_proc_destroy_session\n");
8485 return status;
8486}
8487
Trond Myklebust7b38c362012-05-23 13:23:31 -04008488/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008489 * Renew the cl_session lease.
8490 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008491struct nfs4_sequence_data {
8492 struct nfs_client *clp;
8493 struct nfs4_sequence_args args;
8494 struct nfs4_sequence_res res;
8495};
8496
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008497static void nfs41_sequence_release(void *data)
8498{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008499 struct nfs4_sequence_data *calldata = data;
8500 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008501
Elena Reshetova212bf412017-10-20 12:53:38 +03008502 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008503 nfs4_schedule_state_renewal(clp);
8504 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008505 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008506}
8507
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008508static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8509{
8510 switch(task->tk_status) {
8511 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008512 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8513 return -EAGAIN;
8514 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008515 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008516 }
8517 return 0;
8518}
8519
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008520static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008521{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008522 struct nfs4_sequence_data *calldata = data;
8523 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008524
Trond Myklebust14516c32010-07-31 14:29:06 -04008525 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8526 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008527
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008528 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008529 if (task->tk_status < 0) {
8530 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008531 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008532 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008533
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008534 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8535 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008536 return;
8537 }
8538 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008539 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008540out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008541 dprintk("<-- %s\n", __func__);
8542}
8543
8544static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8545{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008546 struct nfs4_sequence_data *calldata = data;
8547 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008548 struct nfs4_sequence_args *args;
8549 struct nfs4_sequence_res *res;
8550
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008551 args = task->tk_msg.rpc_argp;
8552 res = task->tk_msg.rpc_resp;
8553
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008554 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008555}
8556
8557static const struct rpc_call_ops nfs41_sequence_ops = {
8558 .rpc_call_done = nfs41_sequence_call_done,
8559 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008560 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008561};
8562
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008563static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8564 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008565 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008566 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008567{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008568 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008569 struct rpc_message msg = {
8570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8571 .rpc_cred = cred,
8572 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008573 struct rpc_task_setup task_setup_data = {
8574 .rpc_client = clp->cl_rpcclient,
8575 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008576 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008577 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008578 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008579 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008580
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008581 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008582 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008583 goto out_err;
8584
8585 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008586 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008587 if (calldata == NULL)
8588 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008589 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008590 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008591 msg.rpc_argp = &calldata->args;
8592 msg.rpc_resp = &calldata->res;
8593 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008594 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008595
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008596 ret = rpc_run_task(&task_setup_data);
8597 if (IS_ERR(ret))
8598 goto out_err;
8599 return ret;
8600out_put_clp:
8601 nfs_put_client(clp);
8602out_err:
8603 nfs41_release_slot(slot);
8604 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008605}
8606
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008607static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008608{
8609 struct rpc_task *task;
8610 int ret = 0;
8611
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008612 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008613 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008614 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008615 if (IS_ERR(task))
8616 ret = PTR_ERR(task);
8617 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008618 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008619 dprintk("<-- %s status=%d\n", __func__, ret);
8620 return ret;
8621}
8622
8623static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8624{
8625 struct rpc_task *task;
8626 int ret;
8627
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008628 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008629 if (IS_ERR(task)) {
8630 ret = PTR_ERR(task);
8631 goto out;
8632 }
8633 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008634 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008635 ret = task->tk_status;
8636 rpc_put_task(task);
8637out:
8638 dprintk("<-- %s status=%d\n", __func__, ret);
8639 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008640}
8641
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008642struct nfs4_reclaim_complete_data {
8643 struct nfs_client *clp;
8644 struct nfs41_reclaim_complete_args arg;
8645 struct nfs41_reclaim_complete_res res;
8646};
8647
8648static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8649{
8650 struct nfs4_reclaim_complete_data *calldata = data;
8651
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008652 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008653 &calldata->arg.seq_args,
8654 &calldata->res.seq_res,
8655 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008656}
8657
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008658static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8659{
8660 switch(task->tk_status) {
8661 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008662 wake_up_all(&clp->cl_lock_waitq);
8663 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008664 case -NFS4ERR_COMPLETE_ALREADY:
8665 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8666 break;
8667 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008668 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008669 /* fall through */
8670 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008671 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008672 case -NFS4ERR_BADSESSION:
8673 case -NFS4ERR_DEADSESSION:
8674 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8675 nfs4_schedule_session_recovery(clp->cl_session,
8676 task->tk_status);
8677 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008678 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008679 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008680 }
8681 return 0;
8682}
8683
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008684static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8685{
8686 struct nfs4_reclaim_complete_data *calldata = data;
8687 struct nfs_client *clp = calldata->clp;
8688 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8689
8690 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008691 if (!nfs41_sequence_done(task, res))
8692 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008693
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008694 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008695 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8696 rpc_restart_call_prepare(task);
8697 return;
8698 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008699 dprintk("<-- %s\n", __func__);
8700}
8701
8702static void nfs4_free_reclaim_complete_data(void *data)
8703{
8704 struct nfs4_reclaim_complete_data *calldata = data;
8705
8706 kfree(calldata);
8707}
8708
8709static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8710 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8711 .rpc_call_done = nfs4_reclaim_complete_done,
8712 .rpc_release = nfs4_free_reclaim_complete_data,
8713};
8714
8715/*
8716 * Issue a global reclaim complete.
8717 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008718static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8719 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008720{
8721 struct nfs4_reclaim_complete_data *calldata;
8722 struct rpc_task *task;
8723 struct rpc_message msg = {
8724 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008725 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008726 };
8727 struct rpc_task_setup task_setup_data = {
8728 .rpc_client = clp->cl_rpcclient,
8729 .rpc_message = &msg,
8730 .callback_ops = &nfs4_reclaim_complete_call_ops,
8731 .flags = RPC_TASK_ASYNC,
8732 };
8733 int status = -ENOMEM;
8734
8735 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008736 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008737 if (calldata == NULL)
8738 goto out;
8739 calldata->clp = clp;
8740 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008741
Anna Schumakerfba83f32018-05-04 16:22:50 -04008742 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008743 msg.rpc_argp = &calldata->arg;
8744 msg.rpc_resp = &calldata->res;
8745 task_setup_data.callback_data = calldata;
8746 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008747 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008748 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008749 goto out;
8750 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008751 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008752 if (status == 0)
8753 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008754 rpc_put_task(task);
8755out:
8756 dprintk("<-- %s status=%d\n", __func__, status);
8757 return status;
8758}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008759
8760static void
8761nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8762{
8763 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008764 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008765
8766 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008767 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008768 &lgp->res.seq_res, task);
8769 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008770}
8771
8772static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8773{
8774 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008775
8776 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008777 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008778 dprintk("<-- %s\n", __func__);
8779}
8780
8781static int
8782nfs4_layoutget_handle_exception(struct rpc_task *task,
8783 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8784{
Trond Myklebustee314c22012-10-01 17:25:48 -07008785 struct inode *inode = lgp->args.inode;
8786 struct nfs_server *server = NFS_SERVER(inode);
8787 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008788 int nfs4err = task->tk_status;
8789 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008790 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008791
Boaz Harroshed7e5422014-01-22 20:34:54 +02008792 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008793
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008794 nfs4_sequence_free_slot(&lgp->res.seq_res);
8795
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008796 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008797 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008798 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008799
8800 /*
8801 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8802 * on the file. set tk_status to -ENODATA to tell upper layer to
8803 * retry go inband.
8804 */
8805 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008806 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008807 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008808 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008809 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8810 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8811 */
8812 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008813 status = -EOVERFLOW;
8814 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008815 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008816 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008817 * (or clients) writing to the same RAID stripe except when
8818 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008819 *
8820 * Treat it like we would RECALLCONFLICT -- we retry for a little
8821 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008822 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008823 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008824 if (lgp->args.minlength == 0) {
8825 status = -EOVERFLOW;
8826 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008827 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008828 status = -EBUSY;
8829 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008830 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008831 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008832 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008833 case -NFS4ERR_DELEG_REVOKED:
8834 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008835 case -NFS4ERR_EXPIRED:
8836 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008837 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008838 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008839 lo = NFS_I(inode)->layout;
8840 /* If the open stateid was bad, then recover it. */
8841 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008842 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008843 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008844 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008845 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008846 break;
8847 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008848
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008849 /*
8850 * Mark the bad layout state as invalid, then retry
8851 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008852 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008853 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008854 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008855 pnfs_free_lseg_list(&head);
8856 status = -EAGAIN;
8857 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008858 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008859
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008860 err = nfs4_handle_exception(server, nfs4err, exception);
8861 if (!status) {
8862 if (exception->retry)
8863 status = -EAGAIN;
8864 else
8865 status = err;
8866 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008867out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008868 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008869 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008870}
8871
Fred Isamandacb4522016-09-19 17:47:09 -04008872size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008873{
8874 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8875 return nfs_page_array_len(0, max_resp_sz);
8876}
8877
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008878static void nfs4_layoutget_release(void *calldata)
8879{
8880 struct nfs4_layoutget *lgp = calldata;
8881
8882 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008883 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008884 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008885 dprintk("<-- %s\n", __func__);
8886}
8887
8888static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8889 .rpc_call_prepare = nfs4_layoutget_prepare,
8890 .rpc_call_done = nfs4_layoutget_done,
8891 .rpc_release = nfs4_layoutget_release,
8892};
8893
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008894struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008895nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008896{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008897 struct inode *inode = lgp->args.inode;
8898 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008899 struct rpc_task *task;
8900 struct rpc_message msg = {
8901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8902 .rpc_argp = &lgp->args,
8903 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008904 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008905 };
8906 struct rpc_task_setup task_setup_data = {
8907 .rpc_client = server->client,
8908 .rpc_message = &msg,
8909 .callback_ops = &nfs4_layoutget_call_ops,
8910 .callback_data = lgp,
8911 .flags = RPC_TASK_ASYNC,
8912 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008913 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008914 struct nfs4_exception exception = {
8915 .inode = inode,
8916 .timeout = *timeout,
8917 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008918 int status = 0;
8919
8920 dprintk("--> %s\n", __func__);
8921
Peng Tao4bd5a982014-11-17 11:05:17 +08008922 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8923 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8924
Anna Schumakerfba83f32018-05-04 16:22:50 -04008925 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008926
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008927 task = rpc_run_task(&task_setup_data);
8928 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008929 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008930 status = rpc_wait_for_completion_task(task);
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008931 if (status != 0)
8932 goto out;
8933
8934 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8935 if (task->tk_status < 0 || lgp->res.layoutp->len == 0) {
Jeff Layton183d9e72016-05-17 12:28:47 -04008936 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8937 *timeout = exception.timeout;
Trond Myklebust2dbf8df2018-06-15 15:58:45 -04008938 } else
8939 lseg = pnfs_layout_process(lgp);
8940out:
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008941 trace_nfs4_layoutget(lgp->args.ctx,
8942 &lgp->args.range,
8943 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008944 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008945 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008946
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008947 rpc_put_task(task);
8948 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008949 if (status)
8950 return ERR_PTR(status);
8951 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008952}
8953
Benny Halevycbe82602011-05-22 19:52:37 +03008954static void
8955nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8956{
8957 struct nfs4_layoutreturn *lrp = calldata;
8958
8959 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008960 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008961 &lrp->args.seq_args,
8962 &lrp->res.seq_res,
8963 task);
Trond Myklebustc8bf7072018-06-15 16:31:02 -04008964 if (!pnfs_layout_is_valid(lrp->args.layout))
8965 rpc_exit(task, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008966}
8967
8968static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8969{
8970 struct nfs4_layoutreturn *lrp = calldata;
8971 struct nfs_server *server;
8972
8973 dprintk("--> %s\n", __func__);
8974
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008975 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008976 return;
8977
8978 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008979 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008980 case -NFS4ERR_OLD_STATEID:
Trond Myklebustc16467d2018-07-29 22:39:15 -04008981 if (nfs4_layoutreturn_refresh_stateid(&lrp->args.stateid,
Trond Myklebustecf84022018-08-15 21:35:46 -04008982 &lrp->args.range,
Trond Myklebustff905142017-11-06 15:28:08 -05008983 lrp->args.inode))
8984 goto out_restart;
8985 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008986 default:
8987 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008988 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008989 case 0:
8990 break;
8991 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008992 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008993 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008994 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008995 }
Benny Halevycbe82602011-05-22 19:52:37 +03008996 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008997 return;
8998out_restart:
8999 task->tk_status = 0;
9000 nfs4_sequence_free_slot(&lrp->res.seq_res);
9001 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03009002}
9003
9004static void nfs4_layoutreturn_release(void *calldata)
9005{
9006 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04009007 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03009008
9009 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05009010 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04009011 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04009012 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04009013 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
9014 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05009015 pnfs_put_layout_hdr(lrp->args.layout);
9016 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03009017 kfree(calldata);
9018 dprintk("<-- %s\n", __func__);
9019}
9020
9021static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
9022 .rpc_call_prepare = nfs4_layoutreturn_prepare,
9023 .rpc_call_done = nfs4_layoutreturn_done,
9024 .rpc_release = nfs4_layoutreturn_release,
9025};
9026
Peng Tao6c166052014-11-17 09:30:40 +08009027int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03009028{
9029 struct rpc_task *task;
9030 struct rpc_message msg = {
9031 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
9032 .rpc_argp = &lrp->args,
9033 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04009034 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03009035 };
9036 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04009037 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03009038 .rpc_message = &msg,
9039 .callback_ops = &nfs4_layoutreturn_call_ops,
9040 .callback_data = lrp,
9041 };
Peng Tao6c166052014-11-17 09:30:40 +08009042 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03009043
Andrew Elble99ade3c2015-12-02 09:39:51 -05009044 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
9045 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
9046 &task_setup_data.rpc_client, &msg);
9047
Benny Halevycbe82602011-05-22 19:52:37 +03009048 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009049 if (!sync) {
9050 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
9051 if (!lrp->inode) {
9052 nfs4_layoutreturn_release(lrp);
9053 return -EAGAIN;
9054 }
9055 task_setup_data.flags |= RPC_TASK_ASYNC;
9056 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009057 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03009058 task = rpc_run_task(&task_setup_data);
9059 if (IS_ERR(task))
9060 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05009061 if (sync)
9062 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009063 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03009064 dprintk("<-- %s status=%d\n", __func__, status);
9065 rpc_put_task(task);
9066 return status;
9067}
9068
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009069static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009070_nfs4_proc_getdeviceinfo(struct nfs_server *server,
9071 struct pnfs_device *pdev,
9072 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009073{
9074 struct nfs4_getdeviceinfo_args args = {
9075 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04009076 .notify_types = NOTIFY_DEVICEID4_CHANGE |
9077 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009078 };
9079 struct nfs4_getdeviceinfo_res res = {
9080 .pdev = pdev,
9081 };
9082 struct rpc_message msg = {
9083 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
9084 .rpc_argp = &args,
9085 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009086 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009087 };
9088 int status;
9089
9090 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00009091 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04009092 if (res.notification & ~args.notify_types)
9093 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04009094 if (res.notification != args.notify_types)
9095 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04009096
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009097 dprintk("<-- %s status=%d\n", __func__, status);
9098
9099 return status;
9100}
9101
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009102int nfs4_proc_getdeviceinfo(struct nfs_server *server,
9103 struct pnfs_device *pdev,
9104 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009105{
9106 struct nfs4_exception exception = { };
9107 int err;
9108
9109 do {
9110 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04009111 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04009112 &exception);
9113 } while (exception.retry);
9114 return err;
9115}
9116EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
9117
Andy Adamson863a3c62011-03-23 13:27:54 +00009118static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
9119{
9120 struct nfs4_layoutcommit_data *data = calldata;
9121 struct nfs_server *server = NFS_SERVER(data->args.inode);
9122
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009123 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04009124 &data->args.seq_args,
9125 &data->res.seq_res,
9126 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009127}
9128
9129static void
9130nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9131{
9132 struct nfs4_layoutcommit_data *data = calldata;
9133 struct nfs_server *server = NFS_SERVER(data->args.inode);
9134
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009135 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009136 return;
9137
9138 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009139 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9140 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9141 case -NFS4ERR_BADLAYOUT: /* no layout */
9142 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009143 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009144 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009145 break;
9146 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009147 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009148 rpc_restart_call_prepare(task);
9149 return;
9150 }
9151 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009152}
9153
9154static void nfs4_layoutcommit_release(void *calldata)
9155{
9156 struct nfs4_layoutcommit_data *data = calldata;
9157
Andy Adamsondb29c082011-07-30 20:52:38 -04009158 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009159 nfs_post_op_update_inode_force_wcc(data->args.inode,
9160 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009161 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009162 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009163 kfree(data);
9164}
9165
9166static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9167 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9168 .rpc_call_done = nfs4_layoutcommit_done,
9169 .rpc_release = nfs4_layoutcommit_release,
9170};
9171
9172int
Andy Adamsonef311532011-03-12 02:58:10 -05009173nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009174{
9175 struct rpc_message msg = {
9176 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9177 .rpc_argp = &data->args,
9178 .rpc_resp = &data->res,
9179 .rpc_cred = data->cred,
9180 };
9181 struct rpc_task_setup task_setup_data = {
9182 .task = &data->task,
9183 .rpc_client = NFS_CLIENT(data->args.inode),
9184 .rpc_message = &msg,
9185 .callback_ops = &nfs4_layoutcommit_ops,
9186 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009187 };
9188 struct rpc_task *task;
9189 int status = 0;
9190
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009191 dprintk("NFS: initiating layoutcommit call. sync %d "
9192 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009193 data->args.lastbytewritten,
9194 data->args.inode->i_ino);
9195
Trond Myklebust472e2592015-02-05 16:50:30 -05009196 if (!sync) {
9197 data->inode = nfs_igrab_and_active(data->args.inode);
9198 if (data->inode == NULL) {
9199 nfs4_layoutcommit_release(data);
9200 return -EAGAIN;
9201 }
9202 task_setup_data.flags = RPC_TASK_ASYNC;
9203 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009204 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009205 task = rpc_run_task(&task_setup_data);
9206 if (IS_ERR(task))
9207 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009208 if (sync)
9209 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009210 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009211 dprintk("%s: status %d\n", __func__, status);
9212 rpc_put_task(task);
9213 return status;
9214}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009215
Andy Adamson97431202013-08-08 10:57:56 -04009216/**
9217 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9218 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9219 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009220static int
9221_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009222 struct nfs_fsinfo *info,
9223 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009224{
9225 struct nfs41_secinfo_no_name_args args = {
9226 .style = SECINFO_STYLE_CURRENT_FH,
9227 };
9228 struct nfs4_secinfo_res res = {
9229 .flavors = flavors,
9230 };
9231 struct rpc_message msg = {
9232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9233 .rpc_argp = &args,
9234 .rpc_resp = &res,
9235 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009236 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009237 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009238 int status;
9239
9240 if (use_integrity) {
9241 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009242 cred = nfs4_get_clid_cred(server->nfs_client);
9243 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009244 }
9245
9246 dprintk("--> %s\n", __func__);
9247 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9248 &res.seq_res, 0);
9249 dprintk("<-- %s status=%d\n", __func__, status);
9250
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009251 if (cred)
9252 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009253
9254 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009255}
9256
9257static int
9258nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9259 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9260{
9261 struct nfs4_exception exception = { };
9262 int err;
9263 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009264 /* first try using integrity protection */
9265 err = -NFS4ERR_WRONGSEC;
9266
9267 /* try to use integrity protection with machine cred */
9268 if (_nfs4_is_integrity_protected(server->nfs_client))
9269 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9270 flavors, true);
9271
9272 /*
9273 * if unable to use integrity protection, or SECINFO with
9274 * integrity protection returns NFS4ERR_WRONGSEC (which is
9275 * disallowed by spec, but exists in deployed servers) use
9276 * the current filesystem's rpc_client and the user cred.
9277 */
9278 if (err == -NFS4ERR_WRONGSEC)
9279 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9280 flavors, false);
9281
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009282 switch (err) {
9283 case 0:
9284 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009285 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009286 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009287 default:
9288 err = nfs4_handle_exception(server, err, &exception);
9289 }
9290 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009291out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009292 return err;
9293}
9294
9295static int
9296nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9297 struct nfs_fsinfo *info)
9298{
9299 int err;
9300 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009301 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009302 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009303 struct nfs4_secinfo4 *secinfo;
9304 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009305
9306 page = alloc_page(GFP_KERNEL);
9307 if (!page) {
9308 err = -ENOMEM;
9309 goto out;
9310 }
9311
9312 flavors = page_address(page);
9313 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9314
9315 /*
9316 * Fall back on "guess and check" method if
9317 * the server doesn't support SECINFO_NO_NAME
9318 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009319 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009320 err = nfs4_find_root_sec(server, fhandle, info);
9321 goto out_freepage;
9322 }
9323 if (err)
9324 goto out_freepage;
9325
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009326 for (i = 0; i < flavors->num_flavors; i++) {
9327 secinfo = &flavors->flavors[i];
9328
9329 switch (secinfo->flavor) {
9330 case RPC_AUTH_NULL:
9331 case RPC_AUTH_UNIX:
9332 case RPC_AUTH_GSS:
9333 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9334 &secinfo->flavor_info);
9335 break;
9336 default:
9337 flavor = RPC_AUTH_MAXFLAVOR;
9338 break;
9339 }
9340
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009341 if (!nfs_auth_info_match(&server->auth_info, flavor))
9342 flavor = RPC_AUTH_MAXFLAVOR;
9343
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009344 if (flavor != RPC_AUTH_MAXFLAVOR) {
9345 err = nfs4_lookup_root_sec(server, fhandle,
9346 info, flavor);
9347 if (!err)
9348 break;
9349 }
9350 }
9351
9352 if (flavor == RPC_AUTH_MAXFLAVOR)
9353 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009354
9355out_freepage:
9356 put_page(page);
9357 if (err == -EACCES)
9358 return -EPERM;
9359out:
9360 return err;
9361}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009362
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009363static int _nfs41_test_stateid(struct nfs_server *server,
9364 nfs4_stateid *stateid,
9365 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009366{
9367 int status;
9368 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009369 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009370 };
9371 struct nfs41_test_stateid_res res;
9372 struct rpc_message msg = {
9373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9374 .rpc_argp = &args,
9375 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009376 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009377 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009378 struct rpc_clnt *rpc_client = server->client;
9379
9380 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9381 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009382
Chuck Lever38527b12012-07-11 16:30:23 -04009383 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009384 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009385 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009386 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009387 if (status != NFS_OK) {
9388 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009389 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009390 }
9391 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009392 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009393}
9394
Trond Myklebust43912bb2016-09-22 13:38:56 -04009395static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9396 int err, struct nfs4_exception *exception)
9397{
9398 exception->retry = 0;
9399 switch(err) {
9400 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009401 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009402 nfs4_handle_exception(server, err, exception);
9403 break;
9404 case -NFS4ERR_BADSESSION:
9405 case -NFS4ERR_BADSLOT:
9406 case -NFS4ERR_BAD_HIGH_SLOT:
9407 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9408 case -NFS4ERR_DEADSESSION:
9409 nfs4_do_handle_exception(server, err, exception);
9410 }
9411}
9412
Chuck Lever38527b12012-07-11 16:30:23 -04009413/**
9414 * nfs41_test_stateid - perform a TEST_STATEID operation
9415 *
9416 * @server: server / transport on which to perform the operation
9417 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009418 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009419 *
9420 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9421 * Otherwise a negative NFS4ERR value is returned if the operation
9422 * failed or the state ID is not currently valid.
9423 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009424static int nfs41_test_stateid(struct nfs_server *server,
9425 nfs4_stateid *stateid,
9426 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009427{
9428 struct nfs4_exception exception = { };
9429 int err;
9430 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009431 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009432 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009433 } while (exception.retry);
9434 return err;
9435}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009436
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009437struct nfs_free_stateid_data {
9438 struct nfs_server *server;
9439 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009440 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009441};
9442
9443static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9444{
9445 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009446 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009447 &data->args.seq_args,
9448 &data->res.seq_res,
9449 task);
9450}
9451
9452static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9453{
9454 struct nfs_free_stateid_data *data = calldata;
9455
9456 nfs41_sequence_done(task, &data->res.seq_res);
9457
9458 switch (task->tk_status) {
9459 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009460 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009461 rpc_restart_call_prepare(task);
9462 }
9463}
9464
9465static void nfs41_free_stateid_release(void *calldata)
9466{
9467 kfree(calldata);
9468}
9469
Trond Myklebust17f26b12013-08-21 15:48:42 -04009470static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009471 .rpc_call_prepare = nfs41_free_stateid_prepare,
9472 .rpc_call_done = nfs41_free_stateid_done,
9473 .rpc_release = nfs41_free_stateid_release,
9474};
9475
Anna Schumaker2f261022018-05-15 13:03:39 -04009476/**
9477 * nfs41_free_stateid - perform a FREE_STATEID operation
9478 *
9479 * @server: server / transport on which to perform the operation
9480 * @stateid: state ID to release
9481 * @cred: credential
9482 * @is_recovery: set to true if this call needs to be privileged
9483 *
9484 * Note: this function is always asynchronous.
9485 */
9486static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009487 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009488 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009489 bool privileged)
9490{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009491 struct rpc_message msg = {
9492 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009493 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009494 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009495 struct rpc_task_setup task_setup = {
9496 .rpc_client = server->client,
9497 .rpc_message = &msg,
9498 .callback_ops = &nfs41_free_stateid_ops,
9499 .flags = RPC_TASK_ASYNC,
9500 };
9501 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009502 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009503
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009504 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9505 &task_setup.rpc_client, &msg);
9506
Chuck Lever38527b12012-07-11 16:30:23 -04009507 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009508 data = kmalloc(sizeof(*data), GFP_NOFS);
9509 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009510 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009511 data->server = server;
9512 nfs4_stateid_copy(&data->args.stateid, stateid);
9513
9514 task_setup.callback_data = data;
9515
9516 msg.rpc_argp = &data->args;
9517 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009518 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009519 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009520 if (IS_ERR(task))
9521 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009522 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009523 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009524}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009525
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009526static void
9527nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009528{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009529 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009530
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009531 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009532 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009533}
9534
Trond Myklebust36281ca2012-03-04 18:13:56 -05009535static bool nfs41_match_stateid(const nfs4_stateid *s1,
9536 const nfs4_stateid *s2)
9537{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009538 if (s1->type != s2->type)
9539 return false;
9540
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009541 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009542 return false;
9543
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009544 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009545 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009546
Anna Schumaker045c5512017-01-11 16:59:48 -05009547 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009548}
9549
Andy Adamson557134a2009-04-01 09:21:53 -04009550#endif /* CONFIG_NFS_V4_1 */
9551
Trond Myklebust36281ca2012-03-04 18:13:56 -05009552static bool nfs4_match_stateid(const nfs4_stateid *s1,
9553 const nfs4_stateid *s2)
9554{
Trond Myklebustf597c532012-03-04 18:13:56 -05009555 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009556}
9557
9558
Trond Myklebust17280172012-03-11 13:11:00 -04009559static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009560 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009561 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009562 .recover_open = nfs4_open_reclaim,
9563 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009564 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009565 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009566};
9567
Andy Adamson591d71c2009-04-01 09:22:47 -04009568#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009569static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009570 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9571 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9572 .recover_open = nfs4_open_reclaim,
9573 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009574 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009575 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009576 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009577};
9578#endif /* CONFIG_NFS_V4_1 */
9579
Trond Myklebust17280172012-03-11 13:11:00 -04009580static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009581 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009582 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009583 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009584 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009585 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009586};
9587
Andy Adamson591d71c2009-04-01 09:22:47 -04009588#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009589static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009590 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9591 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009592 .recover_open = nfs41_open_expired,
9593 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009594 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009595};
9596#endif /* CONFIG_NFS_V4_1 */
9597
Trond Myklebust17280172012-03-11 13:11:00 -04009598static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009599 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009600 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009601 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009602};
9603
9604#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009605static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009606 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009607 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009608 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009609};
9610#endif
9611
Chuck Leverec011fe2013-10-17 14:12:39 -04009612static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009613 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009614 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009615};
9616
9617#if defined(CONFIG_NFS_V4_1)
9618static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009619 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009620 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009621};
9622#endif /* CONFIG_NFS_V4_1 */
9623
Trond Myklebust97dc1352010-06-16 09:52:26 -04009624static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9625 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009626 .init_caps = NFS_CAP_READDIRPLUS
9627 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009628 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009629 .init_client = nfs40_init_client,
9630 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009631 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009632 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009633 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009634 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009635 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009636 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009637 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9638 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9639 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009640 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009641};
9642
9643#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009644static struct nfs_seqid *
9645nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9646{
9647 return NULL;
9648}
9649
Trond Myklebust97dc1352010-06-16 09:52:26 -04009650static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9651 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009652 .init_caps = NFS_CAP_READDIRPLUS
9653 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009654 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009655 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009656 | NFS_CAP_ATOMIC_OPEN_V1
9657 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009658 .init_client = nfs41_init_client,
9659 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009660 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009661 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009662 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009663 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009664 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009665 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009666 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009667 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9668 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9669 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009670 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009671};
9672#endif
9673
Steve Dickson42c2c422013-05-22 12:50:38 -04009674#if defined(CONFIG_NFS_V4_2)
9675static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9676 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009677 .init_caps = NFS_CAP_READDIRPLUS
9678 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009679 | NFS_CAP_POSIX_LOCK
9680 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009681 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009682 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009683 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009684 | NFS_CAP_COPY
Olga Kornievskaiacb95dee2018-07-09 15:13:29 -04009685 | NFS_CAP_OFFLOAD_CANCEL
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009686 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009687 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009688 | NFS_CAP_LAYOUTSTATS
9689 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009690 .init_client = nfs41_init_client,
9691 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009692 .match_stateid = nfs41_match_stateid,
9693 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009694 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009695 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009696 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009697 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009698 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009699 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9700 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9701 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009702 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009703};
9704#endif
9705
Trond Myklebust97dc1352010-06-16 09:52:26 -04009706const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9707 [0] = &nfs_v4_0_minor_ops,
9708#if defined(CONFIG_NFS_V4_1)
9709 [1] = &nfs_v4_1_minor_ops,
9710#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009711#if defined(CONFIG_NFS_V4_2)
9712 [2] = &nfs_v4_2_minor_ops,
9713#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009714};
9715
Trond Myklebust13997822016-07-24 17:10:52 -04009716static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009717{
9718 ssize_t error, error2;
9719
9720 error = generic_listxattr(dentry, list, size);
9721 if (error < 0)
9722 return error;
9723 if (list) {
9724 list += error;
9725 size -= error;
9726 }
9727
9728 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9729 if (error2 < 0)
9730 return error2;
9731 return error + error2;
9732}
9733
Trond Myklebust17f26b12013-08-21 15:48:42 -04009734static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009735 .create = nfs_create,
9736 .lookup = nfs_lookup,
9737 .atomic_open = nfs_atomic_open,
9738 .link = nfs_link,
9739 .unlink = nfs_unlink,
9740 .symlink = nfs_symlink,
9741 .mkdir = nfs_mkdir,
9742 .rmdir = nfs_rmdir,
9743 .mknod = nfs_mknod,
9744 .rename = nfs_rename,
9745 .permission = nfs_permission,
9746 .getattr = nfs_getattr,
9747 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009748 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009749};
9750
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009751static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009752 .permission = nfs_permission,
9753 .getattr = nfs_getattr,
9754 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009755 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009756};
9757
David Howells509de812006-08-22 20:06:11 -04009758const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009759 .version = 4, /* protocol version */
9760 .dentry_ops = &nfs4_dentry_operations,
9761 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009762 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009763 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009764 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009765 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009766 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009767 .getattr = nfs4_proc_getattr,
9768 .setattr = nfs4_proc_setattr,
9769 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009770 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009771 .access = nfs4_proc_access,
9772 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009773 .create = nfs4_proc_create,
9774 .remove = nfs4_proc_remove,
9775 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009776 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009777 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009778 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009779 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009780 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009781 .link = nfs4_proc_link,
9782 .symlink = nfs4_proc_symlink,
9783 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009784 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009785 .readdir = nfs4_proc_readdir,
9786 .mknod = nfs4_proc_mknod,
9787 .statfs = nfs4_proc_statfs,
9788 .fsinfo = nfs4_proc_fsinfo,
9789 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009790 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009791 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009792 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009793 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009794 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009795 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009796 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009797 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009798 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009799 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009800 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009801 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009802 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009803 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009804 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009805 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009806 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009807 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009808 .create_server = nfs4_create_server,
9809 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009810};
9811
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009812static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009813 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009814 .list = nfs4_xattr_list_nfs4_acl,
9815 .get = nfs4_xattr_get_nfs4_acl,
9816 .set = nfs4_xattr_set_nfs4_acl,
9817};
9818
9819const struct xattr_handler *nfs4_xattr_handlers[] = {
9820 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009821#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9822 &nfs4_xattr_nfs4_label_handler,
9823#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009824 NULL
9825};
9826
Linus Torvalds1da177e2005-04-16 15:20:36 -07009827/*
9828 * Local variables:
9829 * c-basic-offset: 8
9830 * End:
9831 */