blob: 553cb8f9819463b4f6593997ddffb0d3c55b2307 [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 Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400100static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
101 struct rpc_cred *cred,
102 struct nfs4_slot *slot,
103 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400104static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
105 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400106static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
107 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400108#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400109
110#ifdef CONFIG_NFS_V4_SECURITY_LABEL
111static inline struct nfs4_label *
112nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
114{
115 int err;
116
117 if (label == NULL)
118 return NULL;
119
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
121 return NULL;
122
David Quigleyaa9c2662013-05-22 12:50:44 -0400123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
125 if (err == 0)
126 return label;
127
128 return NULL;
129}
130static inline void
131nfs4_label_release_security(struct nfs4_label *label)
132{
133 if (label)
134 security_release_secctx(label->label, label->len);
135}
136static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{
138 if (label)
139 return server->attr_bitmask;
140
141 return server->attr_bitmask_nl;
142}
143#else
144static inline struct nfs4_label *
145nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
147{ return NULL; }
148static inline void
149nfs4_label_release_security(struct nfs4_label *label)
150{ return; }
151static inline u32 *
152nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153{ return server->attr_bitmask; }
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500157static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebust52567b02009-10-23 14:46:42 -0400159 if (err >= -1000)
160 return err;
161 switch (err) {
162 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000166 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400167 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000168 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
171 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400172 case -NFS4ERR_SHARE_DENIED:
173 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400176 case -NFS4ERR_FILE_OPEN:
177 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700180 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * This is our standard bitmap for GETATTR requests.
188 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400189const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Trond Myklebust1549210f2012-06-05 09:16:47 -0400210static const u32 nfs4_pnfs_open_bitmap[3] = {
211 FATTR4_WORD0_TYPE
212 | FATTR4_WORD0_CHANGE
213 | FATTR4_WORD0_SIZE
214 | FATTR4_WORD0_FSID
215 | FATTR4_WORD0_FILEID,
216 FATTR4_WORD1_MODE
217 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
228#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400229};
230
Andy Adamsone23008e2012-10-02 21:07:32 -0400231static const u32 nfs4_open_noattr_bitmap[3] = {
232 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400233 | FATTR4_WORD0_FILEID,
234};
235
David Quigleya09df2c2013-05-22 12:50:41 -0400236const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_MAXLINK
247 | FATTR4_WORD0_MAXNAME,
248 0
249};
250
Fred Isamandae100c2011-07-30 20:52:37 -0400251const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700255 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800258 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
David Quigleya09df2c2013-05-22 12:50:41 -0400261const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500262 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400280 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700281 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000284 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
286 return;
287 }
288
289 readdir->cookie = 0;
290 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
291 if (cookie == 2)
292 return;
293
294 /*
295 * NFSv4 servers do not return entries for '.' and '..'
296 * Therefore, we fake these entries here. We let '.'
297 * have cookie 0 and '..' have cookie 1. Note that
298 * when talking to the server, we always send cookie 0
299 * instead of 1 or 2.
300 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800301 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (cookie == 0) {
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_one; /* cookie, second word */
307 *p++ = xdr_one; /* entry len */
308 memcpy(p, ".\0\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400311 *p++ = htonl(attrs); /* bitmap */
312 *p++ = htonl(12); /* attribute buffer length */
313 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000314 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 *p++ = xdr_one; /* next */
318 *p++ = xdr_zero; /* cookie, first word */
319 *p++ = xdr_two; /* cookie, second word */
320 *p++ = xdr_two; /* entry len */
321 memcpy(p, "..\0\0", 4); /* entry */
322 p++;
323 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400324 *p++ = htonl(attrs); /* bitmap */
325 *p++ = htonl(12); /* attribute buffer length */
326 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000327 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 readdir->pgbase = (char *)p - (char *)start;
330 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800331 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Trond Myklebust26d36302016-09-22 13:39:05 -0400334static void nfs4_test_and_free_stateid(struct nfs_server *server,
335 nfs4_stateid *stateid,
336 struct rpc_cred *cred)
337{
338 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
339
340 ops->test_and_free_expired(server, stateid, cred);
341}
342
343static void __nfs4_free_revoked_stateid(struct nfs_server *server,
344 nfs4_stateid *stateid,
345 struct rpc_cred *cred)
346{
347 stateid->type = NFS4_REVOKED_STATEID_TYPE;
348 nfs4_test_and_free_stateid(server, stateid, cred);
349}
350
351static void nfs4_free_revoked_stateid(struct nfs_server *server,
352 const nfs4_stateid *stateid,
353 struct rpc_cred *cred)
354{
355 nfs4_stateid tmp;
356
357 nfs4_stateid_copy(&tmp, stateid);
358 __nfs4_free_revoked_stateid(server, &tmp, cred);
359}
360
NeilBrown8478eaa2014-09-18 16:09:27 +1000361static long nfs4_update_delay(long *timeout)
362{
363 long ret;
364 if (!timeout)
365 return NFS4_POLL_RETRY_MAX;
366 if (*timeout <= 0)
367 *timeout = NFS4_POLL_RETRY_MIN;
368 if (*timeout > NFS4_POLL_RETRY_MAX)
369 *timeout = NFS4_POLL_RETRY_MAX;
370 ret = *timeout;
371 *timeout <<= 1;
372 return ret;
373}
374
Trond Myklebust65de8722008-12-23 15:21:44 -0500375static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
376{
377 int res = 0;
378
379 might_sleep();
380
NeilBrown8478eaa2014-09-18 16:09:27 +1000381 freezable_schedule_timeout_killable_unsafe(
382 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 if (fatal_signal_pending(current))
384 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500385 return res;
386}
387
388/* This is the error handling routine for processes that are allowed
389 * to sleep.
390 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400391static int nfs4_do_handle_exception(struct nfs_server *server,
392 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500393{
394 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500395 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400396 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500398 int ret = errorcode;
399
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400400 exception->delay = 0;
401 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400403
404 if (stateid == NULL && state != NULL)
405 stateid = &state->stateid;
406
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 switch(errorcode) {
408 case 0:
409 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_DELEG_REVOKED:
411 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400412 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400413 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400414 if (inode != NULL && stateid != NULL) {
415 nfs_inode_find_state_and_recover(inode,
416 stateid);
417 goto wait_on_recovery;
418 }
419 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400420 if (inode) {
421 int err;
422
423 err = nfs_async_inode_return_delegation(inode,
424 stateid);
425 if (err == 0)
426 goto wait_on_recovery;
427 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
428 exception->retry = 1;
429 break;
430 }
431 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 if (state == NULL)
433 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400434 ret = nfs4_schedule_stateid_recovery(server, state);
435 if (ret < 0)
436 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500437 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500438 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500439 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500440 nfs4_schedule_lease_recovery(clp);
441 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400442 case -NFS4ERR_MOVED:
443 ret = nfs4_schedule_migration_recovery(server);
444 if (ret < 0)
445 break;
446 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400447 case -NFS4ERR_LEASE_MOVED:
448 nfs4_schedule_lease_moved_recovery(clp);
449 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500450#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400451 case -NFS4ERR_BADSESSION:
452 case -NFS4ERR_BADSLOT:
453 case -NFS4ERR_BAD_HIGH_SLOT:
454 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
455 case -NFS4ERR_DEADSESSION:
456 case -NFS4ERR_SEQ_FALSE_RETRY:
457 case -NFS4ERR_SEQ_MISORDERED:
458 dprintk("%s ERROR: %d Reset session\n", __func__,
459 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400460 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400461 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500462#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500463 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500464 if (exception->timeout > HZ) {
465 /* We have retried a decent amount, time to
466 * fail
467 */
468 ret = -EBUSY;
469 break;
470 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400472 nfs_inc_server_stats(server, NFSIOS_DELAY);
473 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400474 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400475 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400476 exception->delay = 1;
477 return 0;
478
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400479 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500480 case -NFS4ERR_OLD_STATEID:
481 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800482 break;
483 case -NFS4ERR_BADOWNER:
484 /* The following works around a Linux server bug! */
485 case -NFS4ERR_BADNAME:
486 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
487 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
488 exception->retry = 1;
489 printk(KERN_WARNING "NFS: v4 server %s "
490 "does not accept raw "
491 "uid/gids. "
492 "Reenabling the idmapper.\n",
493 server->nfs_client->cl_hostname);
494 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500495 }
496 /* We failed to handle the error */
497 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500498wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400499 exception->recovering = 1;
500 return 0;
501}
502
503/* This is the error handling routine for processes that are allowed
504 * to sleep.
505 */
506int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
507{
508 struct nfs_client *clp = server->nfs_client;
509 int ret;
510
511 ret = nfs4_do_handle_exception(server, errorcode, exception);
512 if (exception->delay) {
513 ret = nfs4_delay(server->client, &exception->timeout);
514 goto out_retry;
515 }
516 if (exception->recovering) {
517 ret = nfs4_wait_clnt_recover(clp);
518 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
519 return -EIO;
520 goto out_retry;
521 }
522 return ret;
523out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500524 if (ret == 0)
525 exception->retry = 1;
526 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500527}
528
Trond Myklebust037fc982015-09-20 15:51:00 -0400529static int
530nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
531 int errorcode, struct nfs4_exception *exception)
532{
533 struct nfs_client *clp = server->nfs_client;
534 int ret;
535
536 ret = nfs4_do_handle_exception(server, errorcode, exception);
537 if (exception->delay) {
538 rpc_delay(task, nfs4_update_delay(&exception->timeout));
539 goto out_retry;
540 }
541 if (exception->recovering) {
542 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
543 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
544 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
545 goto out_retry;
546 }
547 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
548 ret = -EIO;
549 return ret;
550out_retry:
551 if (ret == 0)
552 exception->retry = 1;
553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500585 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400586}
Trond Myklebust65de8722008-12-23 15:21:44 -0500587
Trond Myklebust452e9352010-07-31 14:29:06 -0400588static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
594}
595
Trond Myklebust452e9352010-07-31 14:29:06 -0400596static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597{
Trond Myklebustbe824162015-07-05 14:50:46 -0400598 struct nfs_client *clp = server->nfs_client;
599
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400602}
603
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400604struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
608};
609
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800610void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400611 struct nfs4_sequence_res *res, int cache_reply,
612 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400613{
614 args->sa_slot = NULL;
615 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400616 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400617
618 res->sr_slot = NULL;
619}
620
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400621static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400622{
623 struct nfs4_slot *slot = res->sr_slot;
624 struct nfs4_slot_table *tbl;
625
Chuck Lever3bd23842013-08-09 12:49:19 -0400626 tbl = slot->table;
627 spin_lock(&tbl->slot_tbl_lock);
628 if (!nfs41_wake_and_assign_slot(tbl, slot))
629 nfs4_free_slot(tbl, slot);
630 spin_unlock(&tbl->slot_tbl_lock);
631
632 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400633}
634
635static int nfs40_sequence_done(struct rpc_task *task,
636 struct nfs4_sequence_res *res)
637{
638 if (res->sr_slot != NULL)
639 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400640 return 1;
641}
642
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400643#if defined(CONFIG_NFS_V4_1)
644
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400645static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400646{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500647 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400648 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500649 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400650
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400651 if (!slot)
652 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500653 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500654 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500655
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400656 /* Bump the slot sequence number */
657 if (slot->seq_done)
658 slot->seq_nr++;
659 slot->seq_done = 0;
660
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500661 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500662 /* Be nice to the server: try to ensure that the last transmitted
663 * value for highest_user_slotid <= target_highest_slotid
664 */
665 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
666 send_new_highest_used_slotid = true;
667
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500668 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500669 send_new_highest_used_slotid = false;
670 goto out_unlock;
671 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500672 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500673
674 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
675 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500676out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500677 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500678 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400679 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400680 if (waitqueue_active(&tbl->slot_waitq))
681 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400682}
683
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400684static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
685{
686 nfs41_release_slot(res->sr_slot);
687 res->sr_slot = NULL;
688}
689
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400690static int nfs41_sequence_process(struct rpc_task *task,
691 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400692{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500693 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500694 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400695 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500696 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500697 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400698
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500699 if (slot == NULL)
700 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400701 /* don't increment the sequence number if the task wasn't sent */
702 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703 goto out;
704
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500705 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500706
Trond Myklebustac20d162012-12-15 15:36:07 -0500707 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400708 if (res->sr_status != -NFS4ERR_DELAY)
709 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500710 interrupted = true;
711 }
712
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400713 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500714 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400715 switch (res->sr_status) {
716 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400717 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400718 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500719 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500720 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500721 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400722 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
723 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500724 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400725 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500726 case 1:
727 /*
728 * sr_status remains 1 if an RPC level error occurred.
729 * The server may or may not have processed the sequence
730 * operation..
731 * Mark the slot as having hosted an interrupted RPC call.
732 */
733 slot->interrupted = 1;
734 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400735 case -NFS4ERR_DELAY:
736 /* The server detected a resend of the RPC call and
737 * returned NFS4ERR_DELAY as per Section 2.10.6.2
738 * of RFC5661.
739 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500740 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400741 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500742 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500743 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400744 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500745 case -NFS4ERR_BADSLOT:
746 /*
747 * The slot id we used was probably retired. Try again
748 * using a different slot id.
749 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400750 if (slot->seq_nr < slot->table->target_highest_slotid)
751 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500752 goto retry_nowait;
753 case -NFS4ERR_SEQ_MISORDERED:
754 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500755 * Was the last operation on this sequence interrupted?
756 * If so, retry after bumping the sequence number.
757 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400758 if (interrupted)
759 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500760 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500761 * Could this slot have been previously retired?
762 * If so, then the server may be expecting seq_nr = 1!
763 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500764 if (slot->seq_nr != 1) {
765 slot->seq_nr = 1;
766 goto retry_nowait;
767 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400768 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500769 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400770 if (interrupted)
771 goto retry_new_seq;
772 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400773 default:
774 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400775 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400776 }
777out:
778 /* The session may be reset by one of the error handlers. */
779 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500780out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500781 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400782session_recover:
783 nfs4_schedule_session_recovery(session, res->sr_status);
784 goto retry_nowait;
785retry_new_seq:
786 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500787retry_nowait:
788 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400789 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500790 task->tk_status = 0;
791 ret = 0;
792 }
793 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400794out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400795 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400796 goto out;
797 rpc_delay(task, NFS4_POLL_RETRY_MAX);
798 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400799}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400800
801int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
802{
803 if (!nfs41_sequence_process(task, res))
804 return 0;
805 if (res->sr_slot != NULL)
806 nfs41_sequence_free_slot(res);
807 return 1;
808
809}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500810EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400811
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400812static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
813{
814 if (res->sr_slot == NULL)
815 return 1;
816 if (res->sr_slot->table->session != NULL)
817 return nfs41_sequence_process(task, res);
818 return nfs40_sequence_done(task, res);
819}
820
821static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
822{
823 if (res->sr_slot != NULL) {
824 if (res->sr_slot->table->session != NULL)
825 nfs41_sequence_free_slot(res);
826 else
827 nfs40_sequence_free_slot(res);
828 }
829}
830
Peng Tao2c4b1312014-06-11 05:24:16 +0800831int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400832{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500833 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400834 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400835 if (!res->sr_slot->table->session)
836 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400837 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400838}
Peng Tao2c4b1312014-06-11 05:24:16 +0800839EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400840
Andy Adamsonce5039c2009-04-01 09:22:13 -0400841static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
842{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400843 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400844
Trond Myklebust035168ab2010-06-16 09:52:26 -0400845 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
846
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500847 nfs4_setup_sequence(data->seq_server->nfs_client,
848 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400849}
850
Andy Adamson69ab40c2009-04-01 09:22:19 -0400851static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
852{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400853 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400854
Trond Myklebust14516c32010-07-31 14:29:06 -0400855 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400856}
857
Trond Myklebust17280172012-03-11 13:11:00 -0400858static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400859 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400860 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400861};
862
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400863static void
864nfs4_sequence_process_interrupted(struct nfs_client *client,
865 struct nfs4_slot *slot, struct rpc_cred *cred)
866{
867 struct rpc_task *task;
868
869 task = _nfs41_proc_sequence(client, cred, slot, true);
870 if (!IS_ERR(task))
871 rpc_put_task_async(task);
872}
873
Chuck Lever3bd23842013-08-09 12:49:19 -0400874#else /* !CONFIG_NFS_V4_1 */
875
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400876static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
877{
878 return nfs40_sequence_done(task, res);
879}
880
881static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
882{
883 if (res->sr_slot != NULL)
884 nfs40_sequence_free_slot(res);
885}
886
Peng Tao2c4b1312014-06-11 05:24:16 +0800887int nfs4_sequence_done(struct rpc_task *task,
888 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400889{
Chuck Lever3bd23842013-08-09 12:49:19 -0400890 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400891}
Peng Tao2c4b1312014-06-11 05:24:16 +0800892EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400893
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400894static void
895nfs4_sequence_process_interrupted(struct nfs_client *client,
896 struct nfs4_slot *slot, struct rpc_cred *cred)
897{
898 WARN_ON_ONCE(1);
899 slot->interrupted = 0;
900}
901
Chuck Lever3bd23842013-08-09 12:49:19 -0400902#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400903
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400904static
905void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
906 struct nfs4_sequence_res *res,
907 struct nfs4_slot *slot)
908{
909 if (!slot)
910 return;
911 slot->privileged = args->sa_privileged ? 1 : 0;
912 args->sa_slot = slot;
913
914 res->sr_slot = slot;
915 res->sr_timestamp = jiffies;
916 res->sr_status_flags = 0;
917 res->sr_status = 1;
918
919}
920
921int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500922 struct nfs4_sequence_args *args,
923 struct nfs4_sequence_res *res,
924 struct rpc_task *task)
925{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500926 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500927 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500928 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500929
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500930 /* slot already allocated? */
931 if (res->sr_slot != NULL)
932 goto out_start;
933
Anna Schumaker76ee0352017-01-10 16:49:31 -0500934 if (session) {
935 tbl = &session->fc_slot_table;
936 task->tk_timeout = 0;
937 }
938
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400939 for (;;) {
940 spin_lock(&tbl->slot_tbl_lock);
941 /* The state manager will wait until the slot table is empty */
942 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
943 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500944
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400945 slot = nfs4_alloc_slot(tbl);
946 if (IS_ERR(slot)) {
947 /* Try again in 1/4 second */
948 if (slot == ERR_PTR(-ENOMEM))
949 task->tk_timeout = HZ >> 2;
950 goto out_sleep;
951 }
952 spin_unlock(&tbl->slot_tbl_lock);
953
954 if (likely(!slot->interrupted))
955 break;
956 nfs4_sequence_process_interrupted(client,
957 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500958 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500959
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400960 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500961
Anna Schumakerad05cc02017-01-11 13:37:06 -0500962 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500963out_start:
964 rpc_call_start(task);
965 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500966
967out_sleep:
968 if (args->sa_privileged)
969 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
970 NULL, RPC_PRIORITY_PRIVILEGED);
971 else
972 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
973 spin_unlock(&tbl->slot_tbl_lock);
974 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500975}
976EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
977
Chuck Lever9915ea72013-08-09 12:48:27 -0400978static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
979{
980 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500981 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400982 data->seq_args, data->seq_res, task);
983}
984
985static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
986{
987 struct nfs4_call_sync_data *data = calldata;
988 nfs4_sequence_done(task, data->seq_res);
989}
990
991static const struct rpc_call_ops nfs40_call_sync_ops = {
992 .rpc_call_prepare = nfs40_call_sync_prepare,
993 .rpc_call_done = nfs40_call_sync_done,
994};
995
Trond Myklebust2ced46c2007-07-03 23:48:13 -0400996static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -0400997 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -0500998 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +0100999 struct nfs4_sequence_args *args,
1000 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001001{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001002 int ret;
1003 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001004 struct nfs_client *clp = server->nfs_client;
1005 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 .seq_server = server,
1007 .seq_args = args,
1008 .seq_res = res,
1009 };
1010 struct rpc_task_setup task_setup = {
1011 .rpc_client = clnt,
1012 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001013 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001014 .callback_data = &data
1015 };
1016
1017 task = rpc_run_task(&task_setup);
1018 if (IS_ERR(task))
1019 ret = PTR_ERR(task);
1020 else {
1021 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001022 rpc_put_task(task);
1023 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001024 return ret;
1025}
1026
Bryan Schumaker7c513052011-03-24 17:12:24 +00001027int nfs4_call_sync(struct rpc_clnt *clnt,
1028 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001029 struct rpc_message *msg,
1030 struct nfs4_sequence_args *args,
1031 struct nfs4_sequence_res *res,
1032 int cache_reply)
1033{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001034 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001035 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001036}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001038static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1039 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001040{
1041 struct nfs_inode *nfsi = NFS_I(dir);
1042
1043 spin_lock(&dir->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04001044 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1045 | NFS_INO_INVALID_MTIME
1046 | NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001047 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001048 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1049 nfsi->attrtimeo_timestamp = jiffies;
1050 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001052 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001053 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1054 NFS_INO_INVALID_ACL;
1055 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001056 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001057 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001058 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001059 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 spin_unlock(&dir->i_lock);
1061}
1062
1063struct nfs4_opendata {
1064 struct kref kref;
1065 struct nfs_openargs o_arg;
1066 struct nfs_openres o_res;
1067 struct nfs_open_confirmargs c_arg;
1068 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001069 struct nfs4_string owner_name;
1070 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001071 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001073 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001075 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct nfs4_state_owner *owner;
1077 struct nfs4_state *state;
1078 struct iattr attrs;
1079 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001080 bool rpc_done;
1081 bool file_created;
1082 bool is_recover;
1083 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001084 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001086
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001087struct nfs4_open_createattrs {
1088 struct nfs4_label *label;
1089 struct iattr *sattr;
1090 const __u32 verf[2];
1091};
1092
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001093static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1094 int err, struct nfs4_exception *exception)
1095{
1096 if (err != -EINVAL)
1097 return false;
1098 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1099 return false;
1100 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1101 exception->retry = 1;
1102 return true;
1103}
1104
Trond Myklebust6ae37332015-01-30 14:21:14 -05001105static u32
1106nfs4_map_atomic_open_share(struct nfs_server *server,
1107 fmode_t fmode, int openflags)
1108{
1109 u32 res = 0;
1110
1111 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1112 case FMODE_READ:
1113 res = NFS4_SHARE_ACCESS_READ;
1114 break;
1115 case FMODE_WRITE:
1116 res = NFS4_SHARE_ACCESS_WRITE;
1117 break;
1118 case FMODE_READ|FMODE_WRITE:
1119 res = NFS4_SHARE_ACCESS_BOTH;
1120 }
1121 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1122 goto out;
1123 /* Want no delegation if we're using O_DIRECT */
1124 if (openflags & O_DIRECT)
1125 res |= NFS4_SHARE_WANT_NO_DELEG;
1126out:
1127 return res;
1128}
1129
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001130static enum open_claim_type4
1131nfs4_map_atomic_open_claim(struct nfs_server *server,
1132 enum open_claim_type4 claim)
1133{
1134 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1135 return claim;
1136 switch (claim) {
1137 default:
1138 return claim;
1139 case NFS4_OPEN_CLAIM_FH:
1140 return NFS4_OPEN_CLAIM_NULL;
1141 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1142 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1143 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1144 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1145 }
1146}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
1148static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001149{
1150 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001151 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001152 p->o_res.seqid = p->o_arg.seqid;
1153 p->c_res.seqid = p->c_arg.seqid;
1154 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001155 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001157 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001158}
1159
Al Viro82a2c1b2011-06-22 18:30:55 -04001160static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001161 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001162 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001163 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001164 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165{
Al Viro82a2c1b2011-06-22 18:30:55 -04001166 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001167 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001168 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001169 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001170 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001171 struct nfs4_opendata *p;
1172
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001173 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001174 if (p == NULL)
1175 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001176
1177 p->f_label = nfs4_label_alloc(server, gfp_mask);
1178 if (IS_ERR(p->f_label))
1179 goto err_free_p;
1180
Kinglong Meea49c2692015-07-27 15:31:38 +08001181 p->a_label = nfs4_label_alloc(server, gfp_mask);
1182 if (IS_ERR(p->a_label))
1183 goto err_free_f;
1184
Trond Myklebust63f5f792015-01-23 19:19:25 -05001185 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1186 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001187 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001188 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001189 nfs_sb_active(dentry->d_sb);
1190 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001191 p->dir = parent;
1192 p->owner = sp;
1193 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001194 p->o_arg.open_flags = flags;
1195 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001196 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001197 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001198 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1199 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001200 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1201 * will return permission denied for all bits until close */
1202 if (!(flags & O_EXCL)) {
1203 /* ask server to check for all possible rights as results
1204 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001205 switch (p->o_arg.claim) {
1206 default:
1207 break;
1208 case NFS4_OPEN_CLAIM_NULL:
1209 case NFS4_OPEN_CLAIM_FH:
1210 p->o_arg.access = NFS4_ACCESS_READ |
1211 NFS4_ACCESS_MODIFY |
1212 NFS4_ACCESS_EXTEND |
1213 NFS4_ACCESS_EXECUTE;
1214 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001215 }
David Howells7539bba2006-08-22 20:06:09 -04001216 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001217 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1218 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001219 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001220 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001221 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001222 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001223 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001224 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001225 case NFS4_OPEN_CLAIM_NULL:
1226 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1227 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1228 p->o_arg.fh = NFS_FH(dir);
1229 break;
1230 case NFS4_OPEN_CLAIM_PREVIOUS:
1231 case NFS4_OPEN_CLAIM_FH:
1232 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1233 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001234 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001235 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001236 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001237 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001238 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001239
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001240 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001241 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001242 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001243 p->c_arg.fh = &p->o_res.fh;
1244 p->c_arg.stateid = &p->o_res.stateid;
1245 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001246 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001247 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001248 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001249
1250err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001251 nfs4_label_free(p->a_label);
1252err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001253 nfs4_label_free(p->f_label);
1254err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001255 kfree(p);
1256err:
1257 dput(parent);
1258 return NULL;
1259}
1260
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001261static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001262{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 struct nfs4_opendata *p = container_of(kref,
1264 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001265 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001266
1267 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001268 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001269 if (p->state != NULL)
1270 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001271 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001272
Kinglong Meea49c2692015-07-27 15:31:38 +08001273 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001274 nfs4_label_free(p->f_label);
1275
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001276 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001277 dput(p->dentry);
1278 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001279 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001280 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001281 kfree(p);
1282}
1283
1284static void nfs4_opendata_put(struct nfs4_opendata *p)
1285{
1286 if (p != NULL)
1287 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001288}
1289
Trond Myklebust24311f82015-09-20 10:50:17 -04001290static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1291 fmode_t fmode)
1292{
1293 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1294 case FMODE_READ|FMODE_WRITE:
1295 return state->n_rdwr != 0;
1296 case FMODE_WRITE:
1297 return state->n_wronly != 0;
1298 case FMODE_READ:
1299 return state->n_rdonly != 0;
1300 }
1301 WARN_ON_ONCE(1);
1302 return false;
1303}
1304
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001305static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001306{
1307 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001308
Trond Myklebust536e43d2012-01-17 22:04:26 -05001309 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310 goto out;
1311 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001312 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001313 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1314 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001315 break;
1316 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001317 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1318 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001319 break;
1320 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001321 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1322 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001323 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001324out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 return ret;
1326}
1327
Trond Myklebust2a606182015-08-19 22:30:00 -05001328static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1329 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001330{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001331 if (delegation == NULL)
1332 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001333 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001334 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001335 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1336 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001337 switch (claim) {
1338 case NFS4_OPEN_CLAIM_NULL:
1339 case NFS4_OPEN_CLAIM_FH:
1340 break;
1341 case NFS4_OPEN_CLAIM_PREVIOUS:
1342 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1343 break;
1344 default:
1345 return 0;
1346 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001347 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001348 return 1;
1349}
1350
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001351static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001352{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001354 case FMODE_WRITE:
1355 state->n_wronly++;
1356 break;
1357 case FMODE_READ:
1358 state->n_rdonly++;
1359 break;
1360 case FMODE_READ|FMODE_WRITE:
1361 state->n_rdwr++;
1362 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001363 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001364}
1365
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001366#ifdef CONFIG_NFS_V4_1
1367static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1368{
1369 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1370 return true;
1371 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1372 return true;
1373 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1374 return true;
1375 return false;
1376}
1377#endif /* CONFIG_NFS_V4_1 */
1378
Trond Myklebustc9399f22017-11-06 15:28:01 -05001379static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1380{
1381 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1382 wake_up_all(&state->waitq);
1383}
1384
1385static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1386 const nfs4_stateid *stateid)
1387{
1388 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1389 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1390
1391 if (stateid_seqid == state_seqid + 1U ||
1392 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1393 nfs_state_log_update_open_stateid(state);
1394 else
1395 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1396}
1397
Trond Myklebust4f14c192014-02-12 19:15:06 -05001398static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001399{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001400 struct nfs_client *clp = state->owner->so_server->nfs_client;
1401 bool need_recover = false;
1402
1403 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1404 need_recover = true;
1405 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1406 need_recover = true;
1407 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1408 need_recover = true;
1409 if (need_recover)
1410 nfs4_state_mark_reclaim_nograce(clp, state);
1411}
1412
Trond Myklebustc9399f22017-11-06 15:28:01 -05001413/*
1414 * Check for whether or not the caller may update the open stateid
1415 * to the value passed in by stateid.
1416 *
1417 * Note: This function relies heavily on the server implementing
1418 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1419 * correctly.
1420 * i.e. The stateid seqids have to be initialised to 1, and
1421 * are then incremented on every state transition.
1422 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001423static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001424 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001425{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001426 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1427 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1428 if (stateid->seqid == cpu_to_be32(1))
1429 nfs_state_log_update_open_stateid(state);
1430 else
1431 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001432 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001433 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001434
1435 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1436 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001437 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001438 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001439 return false;
1440}
1441
Trond Myklebustf95549c2015-01-23 18:06:09 -05001442static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1443{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001444 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1445 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001446 if (state->n_wronly)
1447 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1448 if (state->n_rdonly)
1449 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1450 if (state->n_rdwr)
1451 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001452 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001453}
1454
Trond Myklebust226056c2014-02-11 10:41:07 -05001455static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1456 nfs4_stateid *stateid, fmode_t fmode)
1457{
1458 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1459 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1460 case FMODE_WRITE:
1461 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1462 break;
1463 case FMODE_READ:
1464 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1465 break;
1466 case 0:
1467 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1468 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1469 clear_bit(NFS_OPEN_STATE, &state->flags);
1470 }
1471 if (stateid == NULL)
1472 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001473 /* Handle OPEN+OPEN_DOWNGRADE races */
1474 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1475 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001476 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001477 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001478 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001479 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001480 nfs4_stateid_copy(&state->stateid, stateid);
1481 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001482 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001483out:
1484 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001485}
1486
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001487static void nfs_clear_open_stateid(struct nfs4_state *state,
1488 nfs4_stateid *arg_stateid,
1489 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001490{
1491 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001492 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1493 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1494 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001495 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001496 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1497 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001498}
1499
Trond Myklebust1393d962016-09-22 13:39:13 -04001500static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001501 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001502{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001503 DEFINE_WAIT(wait);
1504 int status = 0;
1505 for (;;) {
1506
1507 if (!nfs_need_update_open_stateid(state, stateid))
1508 return;
1509 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001510 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001511 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001512 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001513 /* Rely on seqids for serialisation with NFSv4.0 */
1514 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1515 break;
1516
1517 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1518 /*
1519 * Ensure we process the state changes in the same order
1520 * in which the server processed them by delaying the
1521 * update of the stateid until we are in sequence.
1522 */
1523 write_sequnlock(&state->seqlock);
1524 spin_unlock(&state->owner->so_lock);
1525 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001526 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001527 if (!signal_pending(current)) {
1528 if (schedule_timeout(5*HZ) == 0)
1529 status = -EAGAIN;
1530 else
1531 status = 0;
1532 } else
1533 status = -EINTR;
1534 finish_wait(&state->waitq, &wait);
1535 rcu_read_lock();
1536 spin_lock(&state->owner->so_lock);
1537 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001538 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001539
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001540 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1541 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001542 nfs4_stateid_copy(freeme, &state->open_stateid);
1543 nfs_test_and_clear_all_open_stateid(state);
1544 }
1545
Trond Myklebuste999e802014-02-10 18:20:47 -05001546 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1547 nfs4_stateid_copy(&state->stateid, stateid);
1548 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001549 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001550 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001551}
1552
Trond Myklebustc9399f22017-11-06 15:28:01 -05001553static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001554 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001555 fmode_t fmode,
1556 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001557{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001558 /*
1559 * Protect the call to nfs4_state_set_mode_locked and
1560 * serialise the stateid update
1561 */
1562 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1564 switch (fmode) {
1565 case FMODE_READ:
1566 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1567 break;
1568 case FMODE_WRITE:
1569 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1570 break;
1571 case FMODE_READ|FMODE_WRITE:
1572 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001573 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001574 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001575 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001576}
1577
1578static void nfs_state_set_delegation(struct nfs4_state *state,
1579 const nfs4_stateid *deleg_stateid,
1580 fmode_t fmode)
1581{
1582 /*
1583 * Protect the call to nfs4_state_set_mode_locked and
1584 * serialise the stateid update
1585 */
1586 write_seqlock(&state->seqlock);
1587 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1588 set_bit(NFS_DELEGATED_STATE, &state->flags);
1589 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001590}
1591
Trond Myklebust1393d962016-09-22 13:39:13 -04001592static int update_open_stateid(struct nfs4_state *state,
1593 const nfs4_stateid *open_stateid,
1594 const nfs4_stateid *delegation,
1595 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001596{
Trond Myklebust1393d962016-09-22 13:39:13 -04001597 struct nfs_server *server = NFS_SERVER(state->inode);
1598 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001599 struct nfs_inode *nfsi = NFS_I(state->inode);
1600 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001601 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001602 int ret = 0;
1603
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001604 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001605
1606 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001607 spin_lock(&state->owner->so_lock);
1608 if (open_stateid != NULL) {
1609 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1610 ret = 1;
1611 }
1612
Trond Myklebust34310432008-12-23 15:21:38 -05001613 deleg_cur = rcu_dereference(nfsi->delegation);
1614 if (deleg_cur == NULL)
1615 goto no_delegation;
1616
1617 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001618 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001619 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001620 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001621 goto no_delegation_unlock;
1622
1623 if (delegation == NULL)
1624 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001625 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001626 goto no_delegation_unlock;
1627
Trond Myklebustb7391f42008-12-23 15:21:52 -05001628 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001629 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001630 ret = 1;
1631no_delegation_unlock:
1632 spin_unlock(&deleg_cur->lock);
1633no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001634 if (ret)
1635 update_open_stateflags(state, fmode);
1636 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001637 rcu_read_unlock();
1638
Trond Myklebust4f14c192014-02-12 19:15:06 -05001639 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001640 nfs4_schedule_state_manager(clp);
1641 if (freeme.type != 0)
1642 nfs4_test_and_free_stateid(server, &freeme,
1643 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001644
1645 return ret;
1646}
1647
Trond Myklebust39071e62015-01-24 15:07:56 -05001648static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1649 const nfs4_stateid *stateid)
1650{
1651 struct nfs4_state *state = lsp->ls_state;
1652 bool ret = false;
1653
1654 spin_lock(&state->state_lock);
1655 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1656 goto out_noupdate;
1657 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1658 goto out_noupdate;
1659 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1660 ret = true;
1661out_noupdate:
1662 spin_unlock(&state->state_lock);
1663 return ret;
1664}
Trond Myklebust34310432008-12-23 15:21:38 -05001665
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001666static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001667{
1668 struct nfs_delegation *delegation;
1669
Trond Myklebustf5086242018-03-20 16:43:13 -04001670 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671 rcu_read_lock();
1672 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001673 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001674 rcu_read_unlock();
1675 return;
1676 }
1677 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001678 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679}
1680
Trond Myklebust6ee41262007-07-08 14:11:36 -04001681static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001682{
1683 struct nfs4_state *state = opendata->state;
1684 struct nfs_inode *nfsi = NFS_I(state->inode);
1685 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001686 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001687 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001688 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001689 nfs4_stateid stateid;
1690 int ret = -EAGAIN;
1691
Trond Myklebustaac00a82007-07-05 19:02:21 -04001692 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001693 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001694 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001696 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001700 rcu_read_lock();
1701 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001702 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001703 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001704 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001705 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001706 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001707 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001709 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001710 if (!opendata->is_recover) {
1711 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1712 if (ret != 0)
1713 goto out;
1714 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001715 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001716
1717 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001718 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001719 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001720 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001721out:
1722 return ERR_PTR(ret);
1723out_return_state:
1724 atomic_inc(&state->count);
1725 return state;
1726}
1727
Andy Adamsone23008e2012-10-02 21:07:32 -04001728static void
1729nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1730{
1731 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1732 struct nfs_delegation *delegation;
1733 int delegation_flags = 0;
1734
1735 rcu_read_lock();
1736 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1737 if (delegation)
1738 delegation_flags = delegation->flags;
1739 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001740 switch (data->o_arg.claim) {
1741 default:
1742 break;
1743 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1744 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001745 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1746 "returning a delegation for "
1747 "OPEN(CLAIM_DELEGATE_CUR)\n",
1748 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001749 return;
1750 }
1751 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001752 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001753 data->owner->so_cred,
1754 data->o_res.delegation_type,
1755 &data->o_res.delegation,
1756 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001757 else
1758 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001759 data->owner->so_cred,
1760 data->o_res.delegation_type,
1761 &data->o_res.delegation,
1762 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001763}
1764
1765/*
1766 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1767 * and update the nfs4_state.
1768 */
1769static struct nfs4_state *
1770_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1771{
1772 struct inode *inode = data->state->inode;
1773 struct nfs4_state *state = data->state;
1774 int ret;
1775
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001776 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001777 if (data->rpc_status)
1778 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001779 /* cached opens have already been processed */
1780 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001781 }
1782
Andy Adamsone23008e2012-10-02 21:07:32 -04001783 ret = nfs_refresh_inode(inode, &data->f_attr);
1784 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001785 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001786
1787 if (data->o_res.delegation_type != 0)
1788 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001789update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001790 update_open_stateid(state, &data->o_res.stateid, NULL,
1791 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001792 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001793
1794 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001795}
1796
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001797static struct inode *
1798nfs4_opendata_get_inode(struct nfs4_opendata *data)
1799{
1800 struct inode *inode;
1801
1802 switch (data->o_arg.claim) {
1803 case NFS4_OPEN_CLAIM_NULL:
1804 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1805 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1806 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1807 return ERR_PTR(-EAGAIN);
1808 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1809 &data->f_attr, data->f_label);
1810 break;
1811 default:
1812 inode = d_inode(data->dentry);
1813 ihold(inode);
1814 nfs_refresh_inode(inode, &data->f_attr);
1815 }
1816 return inode;
1817}
1818
Andy Adamsone23008e2012-10-02 21:07:32 -04001819static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001820nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1821{
1822 struct nfs4_state *state;
1823 struct inode *inode;
1824
1825 inode = nfs4_opendata_get_inode(data);
1826 if (IS_ERR(inode))
1827 return ERR_CAST(inode);
1828 if (data->state != NULL && data->state->inode == inode) {
1829 state = data->state;
1830 atomic_inc(&state->count);
1831 } else
1832 state = nfs4_get_open_state(inode, data->owner);
1833 iput(inode);
1834 if (state == NULL)
1835 state = ERR_PTR(-ENOMEM);
1836 return state;
1837}
1838
Andy Adamsone23008e2012-10-02 21:07:32 -04001839static struct nfs4_state *
1840_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001842 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001843
Trond Myklebustaac00a82007-07-05 19:02:21 -04001844 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001845 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001846 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001847 goto out;
1848 }
1849
Trond Myklebust75e8c482017-08-08 10:38:07 -04001850 state = nfs4_opendata_find_nfs4_state(data);
1851 if (IS_ERR(state))
1852 goto out;
1853
Andy Adamsone23008e2012-10-02 21:07:32 -04001854 if (data->o_res.delegation_type != 0)
1855 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001856 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001857 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001858out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001859 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001860 return state;
1861}
1862
Andy Adamsone23008e2012-10-02 21:07:32 -04001863static struct nfs4_state *
1864nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1865{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001866 struct nfs4_state *ret;
1867
Andy Adamsone23008e2012-10-02 21:07:32 -04001868 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001869 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1870 else
1871 ret = _nfs4_opendata_to_nfs4_state(data);
1872 nfs4_sequence_free_slot(&data->o_res.seq_res);
1873 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001874}
1875
Trond Myklebust864472e2006-01-03 09:55:15 +01001876static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1877{
1878 struct nfs_inode *nfsi = NFS_I(state->inode);
1879 struct nfs_open_context *ctx;
1880
1881 spin_lock(&state->inode->i_lock);
1882 list_for_each_entry(ctx, &nfsi->open_files, list) {
1883 if (ctx->state != state)
1884 continue;
1885 get_nfs_open_context(ctx);
1886 spin_unlock(&state->inode->i_lock);
1887 return ctx;
1888 }
1889 spin_unlock(&state->inode->i_lock);
1890 return ERR_PTR(-ENOENT);
1891}
1892
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001893static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1894 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001895{
1896 struct nfs4_opendata *opendata;
1897
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001898 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001899 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001900 if (opendata == NULL)
1901 return ERR_PTR(-ENOMEM);
1902 opendata->state = state;
1903 atomic_inc(&state->count);
1904 return opendata;
1905}
1906
Trond Myklebust24311f82015-09-20 10:50:17 -04001907static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1908 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001909{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001910 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001911 int ret;
1912
Trond Myklebust24311f82015-09-20 10:50:17 -04001913 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001914 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001915 opendata->o_arg.open_flags = 0;
1916 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001917 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1918 NFS_SB(opendata->dentry->d_sb),
1919 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001920 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1921 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1922 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001923 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 if (ret != 0)
1925 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001926 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001927 if (IS_ERR(newstate))
1928 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001929 if (newstate != opendata->state)
1930 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001931 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001932 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001933}
1934
1935static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1936{
Trond Myklebust864472e2006-01-03 09:55:15 +01001937 int ret;
1938
Trond Myklebust4f14c192014-02-12 19:15:06 -05001939 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1940 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1941 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1942 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001943 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001944 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001945 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001946 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001947 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1948 if (ret != 0)
1949 return ret;
1950 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1951 if (ret != 0)
1952 return ret;
1953 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1954 if (ret != 0)
1955 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001956 /*
1957 * We may have performed cached opens for all three recoveries.
1958 * Check if we need to update the current stateid.
1959 */
1960 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001961 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001962 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001963 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001964 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001965 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001966 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001967 return 0;
1968}
1969
Linus Torvalds1da177e2005-04-16 15:20:36 -07001970/*
1971 * OPEN_RECLAIM:
1972 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001974static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001976 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001977 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001978 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 int status;
1980
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001981 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1982 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001983 if (IS_ERR(opendata))
1984 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001985 rcu_read_lock();
1986 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001987 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001988 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001989 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001990 opendata->o_arg.u.delegation_type = delegation_type;
1991 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001992 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001993 return status;
1994}
1995
Trond Myklebust539cd032007-06-05 11:46:42 -04001996static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 struct nfs_server *server = NFS_SERVER(state->inode);
1999 struct nfs4_exception exception = { };
2000 int err;
2001 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002002 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002003 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002004 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2005 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002006 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002007 break;
2008 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 } while (exception.retry);
2010 return err;
2011}
2012
Trond Myklebust864472e2006-01-03 09:55:15 +01002013static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2014{
2015 struct nfs_open_context *ctx;
2016 int ret;
2017
2018 ctx = nfs4_state_find_open_context(state);
2019 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002020 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002021 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002022 put_nfs_open_context(ctx);
2023 return ret;
2024}
2025
NeilBrowndce26302017-12-13 09:57:09 +11002026static 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 -07002027{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002028 switch (err) {
2029 default:
2030 printk(KERN_ERR "NFS: %s: unhandled error "
2031 "%d.\n", __func__, err);
2032 case 0:
2033 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002034 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002035 case -ESTALE:
2036 break;
2037 case -NFS4ERR_BADSESSION:
2038 case -NFS4ERR_BADSLOT:
2039 case -NFS4ERR_BAD_HIGH_SLOT:
2040 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2041 case -NFS4ERR_DEADSESSION:
2042 set_bit(NFS_DELEGATED_STATE, &state->flags);
2043 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2044 return -EAGAIN;
2045 case -NFS4ERR_STALE_CLIENTID:
2046 case -NFS4ERR_STALE_STATEID:
2047 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002048 /* Don't recall a delegation if it was lost */
2049 nfs4_schedule_lease_recovery(server->nfs_client);
2050 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002051 case -NFS4ERR_MOVED:
2052 nfs4_schedule_migration_recovery(server);
2053 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002054 case -NFS4ERR_LEASE_MOVED:
2055 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2056 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002057 case -NFS4ERR_DELEG_REVOKED:
2058 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002059 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002060 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002061 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002062 nfs_inode_find_state_and_recover(state->inode,
2063 stateid);
2064 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002065 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002066 case -NFS4ERR_DELAY:
2067 case -NFS4ERR_GRACE:
2068 set_bit(NFS_DELEGATED_STATE, &state->flags);
2069 ssleep(1);
2070 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002071 case -ENOMEM:
2072 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002073 if (fl) {
2074 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2075 if (lsp)
2076 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2077 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002078 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002079 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002080 return err;
2081}
2082
Trond Myklebust24311f82015-09-20 10:50:17 -04002083int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2084 struct nfs4_state *state, const nfs4_stateid *stateid,
2085 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002086{
2087 struct nfs_server *server = NFS_SERVER(state->inode);
2088 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002089 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002090
2091 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2092 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2093 if (IS_ERR(opendata))
2094 return PTR_ERR(opendata);
2095 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002096 write_seqlock(&state->seqlock);
2097 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2098 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002099 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2100 switch (type & (FMODE_READ|FMODE_WRITE)) {
2101 case FMODE_READ|FMODE_WRITE:
2102 case FMODE_WRITE:
2103 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2104 if (err)
2105 break;
2106 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2107 if (err)
2108 break;
2109 case FMODE_READ:
2110 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2111 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002112 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002113 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002114}
2115
Chuck Leverbe05c862013-08-09 12:49:47 -04002116static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2117{
2118 struct nfs4_opendata *data = calldata;
2119
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002120 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2121 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002122}
2123
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002124static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2125{
2126 struct nfs4_opendata *data = calldata;
2127
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002128 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002129
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002130 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002131 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002132 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002133 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002134 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002135 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002136 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002137}
2138
2139static void nfs4_open_confirm_release(void *calldata)
2140{
2141 struct nfs4_opendata *data = calldata;
2142 struct nfs4_state *state = NULL;
2143
2144 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002145 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002146 goto out_free;
2147 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002148 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002149 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002150 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002151 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002152 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002153out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002154 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002155}
2156
2157static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002158 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002159 .rpc_call_done = nfs4_open_confirm_done,
2160 .rpc_release = nfs4_open_confirm_release,
2161};
2162
2163/*
2164 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2165 */
2166static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2167{
David Howells2b0143b2015-03-17 22:25:59 +00002168 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002169 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002170 struct rpc_message msg = {
2171 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2172 .rpc_argp = &data->c_arg,
2173 .rpc_resp = &data->c_res,
2174 .rpc_cred = data->owner->so_cred,
2175 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002176 struct rpc_task_setup task_setup_data = {
2177 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002178 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 .callback_ops = &nfs4_open_confirm_ops,
2180 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002181 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002182 .flags = RPC_TASK_ASYNC,
2183 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002184 int status;
2185
Anna Schumakerfba83f32018-05-04 16:22:50 -04002186 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2187 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002188 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002189 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002190 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002191 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002192 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002193 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002194 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002195 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002196 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002197 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002198 smp_wmb();
2199 } else
2200 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002201 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002202 return status;
2203}
2204
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002205static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002206{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002207 struct nfs4_opendata *data = calldata;
2208 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002209 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002210 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002211
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002212 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002213 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002214 /*
2215 * Check if we still need to send an OPEN call, or if we can use
2216 * a delegation instead.
2217 */
2218 if (data->state != NULL) {
2219 struct nfs_delegation *delegation;
2220
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002221 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002222 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002223 rcu_read_lock();
2224 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002225 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002226 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002227 rcu_read_unlock();
2228 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002229 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002230 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002231 switch (claim) {
2232 default:
2233 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002234 case NFS4_OPEN_CLAIM_PREVIOUS:
2235 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2236 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002237 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002238 case NFS4_OPEN_CLAIM_FH:
2239 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002240 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002241 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002242 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002243 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002244 &data->o_res.seq_res,
2245 task) != 0)
2246 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002247
2248 /* Set the create mode (note dependency on the session type) */
2249 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2250 if (data->o_arg.open_flags & O_EXCL) {
2251 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2252 if (nfs4_has_persistent_session(clp))
2253 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2254 else if (clp->cl_mvops->minor_version > 0)
2255 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2256 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002257 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002258unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002259 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002260 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002261out_no_action:
2262 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002263out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002264 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002265}
2266
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002267static void nfs4_open_done(struct rpc_task *task, void *calldata)
2268{
2269 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002270
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002271 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002272
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002273 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002274 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002275
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002276 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002277 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2278 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002279 case S_IFREG:
2280 break;
2281 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002282 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002283 break;
2284 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002285 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002286 break;
2287 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002288 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002289 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002290 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002291 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002292 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2293 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002294 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002295 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002296}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002297
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002298static void nfs4_open_release(void *calldata)
2299{
2300 struct nfs4_opendata *data = calldata;
2301 struct nfs4_state *state = NULL;
2302
2303 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002304 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002305 goto out_free;
2306 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002307 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002308 goto out_free;
2309 /* In case we need an open_confirm, no cleanup! */
2310 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2311 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002312 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002313 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002314 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002315out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002316 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002317}
2318
2319static const struct rpc_call_ops nfs4_open_ops = {
2320 .rpc_call_prepare = nfs4_open_prepare,
2321 .rpc_call_done = nfs4_open_done,
2322 .rpc_release = nfs4_open_release,
2323};
2324
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002325static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002326{
David Howells2b0143b2015-03-17 22:25:59 +00002327 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002328 struct nfs_server *server = NFS_SERVER(dir);
2329 struct nfs_openargs *o_arg = &data->o_arg;
2330 struct nfs_openres *o_res = &data->o_res;
2331 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002332 struct rpc_message msg = {
2333 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2334 .rpc_argp = o_arg,
2335 .rpc_resp = o_res,
2336 .rpc_cred = data->owner->so_cred,
2337 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002338 struct rpc_task_setup task_setup_data = {
2339 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002340 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002341 .callback_ops = &nfs4_open_ops,
2342 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002343 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002344 .flags = RPC_TASK_ASYNC,
2345 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002346 int status;
2347
Anna Schumakerfba83f32018-05-04 16:22:50 -04002348 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, isrecover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002349 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002350 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002351 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002352 data->cancelled = false;
2353 data->is_recover = false;
Anna Schumakerfba83f32018-05-04 16:22:50 -04002354 if (isrecover)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002355 data->is_recover = true;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002356 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002357 if (IS_ERR(task))
2358 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002359 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002360 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002361 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002362 smp_wmb();
2363 } else
2364 status = data->rpc_status;
2365 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002366
2367 return status;
2368}
2369
2370static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2371{
David Howells2b0143b2015-03-17 22:25:59 +00002372 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002373 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002374 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002375
2376 status = nfs4_run_open_task(data, 1);
2377 if (status != 0 || !data->rpc_done)
2378 return status;
2379
Trond Myklebust6926afd2012-01-07 13:22:46 -05002380 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2381
Anna Schumakerd7e98252017-01-11 16:13:29 -05002382 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002383 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002384
2385 return status;
2386}
2387
Trond Myklebustf3792d62014-07-10 08:54:32 -04002388/*
2389 * Additional permission checks in order to distinguish between an
2390 * open for read, and an open for execute. This works around the
2391 * fact that NFSv4 OPEN treats read and execute permissions as being
2392 * the same.
2393 * Note that in the non-execute case, we want to turn off permission
2394 * checking if we just created a new file (POSIX open() semantics).
2395 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002396static int nfs4_opendata_access(struct rpc_cred *cred,
2397 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002398 struct nfs4_state *state, fmode_t fmode,
2399 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002400{
2401 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002402 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002403
2404 /* access call failed or for some reason the server doesn't
2405 * support any access modes -- defer access call until later */
2406 if (opendata->o_res.access_supported == 0)
2407 return 0;
2408
2409 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002410 /*
2411 * Use openflags to check for exec, because fmode won't
2412 * always have FMODE_EXEC set when file open for exec.
2413 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002414 if (openflags & __FMODE_EXEC) {
2415 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002416 if (S_ISDIR(state->inode->i_mode))
2417 mask = NFS4_ACCESS_LOOKUP;
2418 else
2419 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002420 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002421 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002422
2423 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2425 nfs_access_add_cache(state->inode, &cache);
2426
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002427 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2428 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002429 return 0;
2430
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002431 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002432}
2433
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002434/*
2435 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2436 */
2437static int _nfs4_proc_open(struct nfs4_opendata *data)
2438{
David Howells2b0143b2015-03-17 22:25:59 +00002439 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002440 struct nfs_server *server = NFS_SERVER(dir);
2441 struct nfs_openargs *o_arg = &data->o_arg;
2442 struct nfs_openres *o_res = &data->o_res;
2443 int status;
2444
2445 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002446 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002447 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002448 if (status != 0) {
2449 if (status == -NFS4ERR_BADNAME &&
2450 !(o_arg->open_flags & O_CREAT))
2451 return -ENOENT;
2452 return status;
2453 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002454
Trond Myklebust6926afd2012-01-07 13:22:46 -05002455 nfs_fattr_map_and_free_names(server, &data->f_attr);
2456
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002457 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002458 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002459 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002460 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002461 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002462 if (data->file_created ||
2463 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002464 update_changeattr(dir, &o_res->cinfo,
2465 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002466 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002467 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2468 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002469 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002470 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002471 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002472 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002474 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2475 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002476 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002477 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002478 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002479}
2480
Linus Torvalds1da177e2005-04-16 15:20:36 -07002481/*
2482 * OPEN_EXPIRED:
2483 * reclaim state on the server after a network partition.
2484 * Assumes caller holds the appropriate lock
2485 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002486static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002488 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002489 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002491 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002492 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002493 if (IS_ERR(opendata))
2494 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002495 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002496 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002497 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002498 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002499 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002500}
2501
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002502static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002503{
Trond Myklebust539cd032007-06-05 11:46:42 -04002504 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002505 struct nfs4_exception exception = { };
2506 int err;
2507
2508 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002509 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002510 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002511 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2512 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002513 switch (err) {
2514 default:
2515 goto out;
2516 case -NFS4ERR_GRACE:
2517 case -NFS4ERR_DELAY:
2518 nfs4_handle_exception(server, err, &exception);
2519 err = 0;
2520 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002521 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002522out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002523 return err;
2524}
2525
Linus Torvalds1da177e2005-04-16 15:20:36 -07002526static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2527{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002528 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002529 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530
Trond Myklebust864472e2006-01-03 09:55:15 +01002531 ctx = nfs4_state_find_open_context(state);
2532 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002533 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002534 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002535 put_nfs_open_context(ctx);
2536 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537}
2538
Trond Myklebust41020b62016-09-22 13:38:58 -04002539static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2540 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002541{
Trond Myklebust41020b62016-09-22 13:38:58 -04002542 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002543 write_seqlock(&state->seqlock);
2544 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2545 write_sequnlock(&state->seqlock);
2546 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2547}
2548
2549static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2550{
2551 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002552 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002553}
2554
2555static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2556{
2557 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2558 nfs40_clear_delegation_stateid(state);
2559 return nfs4_open_expired(sp, state);
2560}
2561
Trond Myklebust45870d62016-09-22 13:38:59 -04002562static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2563 nfs4_stateid *stateid,
2564 struct rpc_cred *cred)
2565{
2566 return -NFS4ERR_BAD_STATEID;
2567}
2568
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002569#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002570static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2571 nfs4_stateid *stateid,
2572 struct rpc_cred *cred)
2573{
2574 int status;
2575
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002576 switch (stateid->type) {
2577 default:
2578 break;
2579 case NFS4_INVALID_STATEID_TYPE:
2580 case NFS4_SPECIAL_STATEID_TYPE:
2581 return -NFS4ERR_BAD_STATEID;
2582 case NFS4_REVOKED_STATEID_TYPE:
2583 goto out_free;
2584 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002585
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002586 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002587 switch (status) {
2588 case -NFS4ERR_EXPIRED:
2589 case -NFS4ERR_ADMIN_REVOKED:
2590 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002591 break;
2592 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002593 return status;
2594 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002595out_free:
2596 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002597 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002598 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002599}
2600
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002601static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002602{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002603 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002604 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002605 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002606 struct rpc_cred *cred;
2607 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002608
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002609 /* Get the delegation credential for use by test/free_stateid */
2610 rcu_read_lock();
2611 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002612 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002613 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002614 return;
2615 }
2616
2617 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002618 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2619 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2620 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002621 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002622 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002623 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002624 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002625
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002626 cred = get_rpccred(delegation->cred);
2627 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002628 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002629 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002630 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002631 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002632
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002633 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002634}
2635
2636/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002637 * nfs41_check_expired_locks - possibly free a lock stateid
2638 *
2639 * @state: NFSv4 state for an inode
2640 *
2641 * Returns NFS_OK if recovery for this stateid is now finished.
2642 * Otherwise a negative NFS4ERR value is returned.
2643 */
2644static int nfs41_check_expired_locks(struct nfs4_state *state)
2645{
2646 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002647 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002648 struct nfs_server *server = NFS_SERVER(state->inode);
2649
2650 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2651 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002652
2653 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002654 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2655 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2656 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2657
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002658 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002659 spin_unlock(&state->state_lock);
2660
2661 nfs4_put_lock_state(prev);
2662 prev = lsp;
2663
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002664 status = nfs41_test_and_free_expired_stateid(server,
2665 &lsp->ls_stateid,
2666 cred);
2667 trace_nfs4_test_lock_stateid(state, lsp, status);
2668 if (status == -NFS4ERR_EXPIRED ||
2669 status == -NFS4ERR_BAD_STATEID) {
2670 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002671 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002672 if (!recover_lost_locks)
2673 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2674 } else if (status != NFS_OK) {
2675 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002676 nfs4_put_lock_state(prev);
2677 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002678 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002679 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002680 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002681 }
2682 spin_unlock(&state->state_lock);
2683 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002684out:
2685 return ret;
2686}
2687
2688/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002689 * nfs41_check_open_stateid - possibly free an open stateid
2690 *
2691 * @state: NFSv4 state for an inode
2692 *
2693 * Returns NFS_OK if recovery for this stateid is now finished.
2694 * Otherwise a negative NFS4ERR value is returned.
2695 */
2696static int nfs41_check_open_stateid(struct nfs4_state *state)
2697{
2698 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002699 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002700 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002701 int status;
2702
Trond Myklebustb134fc42016-09-22 13:39:16 -04002703 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002704 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2705 if (nfs4_have_delegation(state->inode, state->state))
2706 return NFS_OK;
2707 return -NFS4ERR_OPENMODE;
2708 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002709 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002710 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002711 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002712 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002713 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002714 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2715 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2716 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002717 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002718 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002719 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002720 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002721 if (nfs_open_stateid_recover_openmode(state))
2722 return -NFS4ERR_OPENMODE;
2723 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002724}
2725
2726static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2727{
Chuck Levereb64cf92012-07-11 16:30:05 -04002728 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002729
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002730 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002731 status = nfs41_check_expired_locks(state);
2732 if (status != NFS_OK)
2733 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002734 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002735 if (status != NFS_OK)
2736 status = nfs4_open_expired(sp, state);
2737 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002738}
2739#endif
2740
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002742 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2743 * fields corresponding to attributes that were used to store the verifier.
2744 * Make sure we clobber those fields in the later setattr call
2745 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002746static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002747 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002748{
Trond Myklebust609339c2018-03-28 16:18:17 -04002749 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2750 __u32 attrset[3];
2751 unsigned ret;
2752 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002753
Trond Myklebust609339c2018-03-28 16:18:17 -04002754 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2755 attrset[i] = opendata->o_res.attrset[i];
2756 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2757 attrset[i] &= ~bitmask[i];
2758 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002759
Trond Myklebust609339c2018-03-28 16:18:17 -04002760 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2761 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002762
Trond Myklebust609339c2018-03-28 16:18:17 -04002763 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2764 if (sattr->ia_valid & ATTR_ATIME_SET)
2765 ret |= ATTR_ATIME_SET;
2766 else
2767 ret |= ATTR_ATIME;
2768 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002769
Trond Myklebust609339c2018-03-28 16:18:17 -04002770 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2771 if (sattr->ia_valid & ATTR_MTIME_SET)
2772 ret |= ATTR_MTIME_SET;
2773 else
2774 ret |= ATTR_MTIME;
2775 }
2776
2777 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002778 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002779 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002780}
2781
Trond Myklebustc21443c2013-02-07 14:26:21 -05002782static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2783 fmode_t fmode,
2784 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002785 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002786{
2787 struct nfs4_state_owner *sp = opendata->owner;
2788 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002789 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002790 struct nfs4_state *state;
2791 unsigned int seq;
2792 int ret;
2793
2794 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2795
2796 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002797 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002798 goto out;
2799
2800 state = nfs4_opendata_to_nfs4_state(opendata);
2801 ret = PTR_ERR(state);
2802 if (IS_ERR(state))
2803 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002804 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002805 if (server->caps & NFS_CAP_POSIX_LOCK)
2806 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002807 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2808 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002809
Trond Myklebust275bb302013-05-29 13:11:28 -04002810 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002811 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002812 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002813 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002814 alias = d_exact_alias(dentry, state->inode);
2815 if (!alias)
2816 alias = d_splice_alias(igrab(state->inode), dentry);
2817 /* d_splice_alias() can't fail here - it's a non-directory */
2818 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002819 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002820 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002821 }
2822 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002823 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002824 }
2825
Trond Myklebustc21443c2013-02-07 14:26:21 -05002826 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2827 if (ret != 0)
2828 goto out;
2829
David Howells2b0143b2015-03-17 22:25:59 +00002830 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002831 nfs_inode_attach_open_context(ctx);
2832 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2833 nfs4_schedule_stateid_recovery(server, state);
2834 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002835out:
2836 return ret;
2837}
2838
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002839/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002840 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841 */
Andy Adamson82be4172012-05-23 05:02:35 -04002842static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002843 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002844 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002845 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002846 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002847{
2848 struct nfs4_state_owner *sp;
2849 struct nfs4_state *state = NULL;
2850 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002851 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002852 struct dentry *dentry = ctx->dentry;
2853 struct rpc_cred *cred = ctx->cred;
2854 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2855 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002856 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002857 struct iattr *sattr = c->sattr;
2858 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002859 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002860 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
2862 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002863 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002864 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2865 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2867 goto out_err;
2868 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002869 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002870 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002871 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002872 if (d_really_is_positive(dentry))
2873 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002874 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002875 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002876 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002877 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2878 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002879 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002880 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002881
David Quigley14c43f72013-05-22 12:50:43 -04002882 if (label) {
2883 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2884 if (IS_ERR(olabel)) {
2885 status = PTR_ERR(olabel);
2886 goto err_opendata_put;
2887 }
2888 }
2889
Trond Myklebuste911b812014-03-26 13:24:37 -07002890 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2891 if (!opendata->f_attr.mdsthreshold) {
2892 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2893 if (!opendata->f_attr.mdsthreshold)
2894 goto err_free_label;
2895 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002896 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002897 }
David Howells2b0143b2015-03-17 22:25:59 +00002898 if (d_really_is_positive(dentry))
2899 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002900
Trond Myklebust3efb9722013-05-29 13:17:04 -04002901 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002902 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002903 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002904 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002905
NeilBrownefcbc042015-07-30 13:00:56 +10002906 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002907 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002908 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002909 /*
2910 * send create attributes which was not set by open
2911 * with an extra setattr.
2912 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002913 if (attrs || label) {
2914 unsigned ia_old = sattr->ia_valid;
2915
2916 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002917 nfs_fattr_init(opendata->o_res.f_attr);
2918 status = nfs4_do_setattr(state->inode, cred,
2919 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002920 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002921 if (status == 0) {
2922 nfs_setattr_update_inode(state->inode, sattr,
2923 opendata->o_res.f_attr);
2924 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2925 }
Trond Myklebust609339c2018-03-28 16:18:17 -04002926 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04002927 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002928 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002929 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002930 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002931
Trond Myklebuste911b812014-03-26 13:24:37 -07002932 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002933 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002934 opendata->f_attr.mdsthreshold = NULL;
2935 }
Andy Adamson82be4172012-05-23 05:02:35 -04002936
David Quigley14c43f72013-05-22 12:50:43 -04002937 nfs4_label_free(olabel);
2938
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002939 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002940 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002942err_free_label:
2943 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002944err_opendata_put:
2945 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002946err_put_state_owner:
2947 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002949 return status;
2950}
2951
2952
Andy Adamson82be4172012-05-23 05:02:35 -04002953static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002954 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002955 int flags,
2956 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002957 struct nfs4_label *label,
2958 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002960 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 struct nfs4_exception exception = { };
2962 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002963 struct nfs4_open_createattrs c = {
2964 .label = label,
2965 .sattr = sattr,
2966 .verf = {
2967 [0] = (__u32)jiffies,
2968 [1] = (__u32)current->pid,
2969 },
2970 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002971 int status;
2972
2973 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002974 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002975 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002976 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002977 if (status == 0)
2978 break;
2979 /* NOTE: BAD_SEQID means the server and client disagree about the
2980 * book-keeping w.r.t. state-changing operations
2981 * (OPEN/CLOSE/LOCK/LOCKU...)
2982 * It is actually a sign of a bug on the client or on the server.
2983 *
2984 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002985 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002986 * have unhashed the old state_owner for us, and that we can
2987 * therefore safely retry using a new one. We should still warn
2988 * the user though...
2989 */
2990 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002991 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002992 " returned a bad sequence-id error!\n",
2993 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002994 exception.retry = 1;
2995 continue;
2996 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002997 /*
2998 * BAD_STATEID on OPEN means that the server cancelled our
2999 * state before it received the OPEN_CONFIRM.
3000 * Recover by retrying the request as per the discussion
3001 * on Page 181 of RFC3530.
3002 */
3003 if (status == -NFS4ERR_BAD_STATEID) {
3004 exception.retry = 1;
3005 continue;
3006 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003007 if (status == -EAGAIN) {
3008 /* We must have found a delegation */
3009 exception.retry = 1;
3010 continue;
3011 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003012 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3013 continue;
3014 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003015 status, &exception));
3016 } while (exception.retry);
3017 return res;
3018}
3019
Trond Myklebust8487c472016-06-26 08:44:35 -04003020static int _nfs4_do_setattr(struct inode *inode,
3021 struct nfs_setattrargs *arg,
3022 struct nfs_setattrres *res,
3023 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003024 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003025{
3026 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003027 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003028 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3029 .rpc_argp = arg,
3030 .rpc_resp = res,
3031 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003032 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003033 struct rpc_cred *delegation_cred = NULL;
3034 unsigned long timestamp = jiffies;
3035 fmode_t fmode;
3036 bool truncate;
3037 int status;
3038
3039 nfs_fattr_init(res->fattr);
3040
3041 /* Servers should only apply open mode checks for file size changes */
3042 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3043 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3044
3045 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3046 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003047 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003048 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003049 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003050 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003051 l_ctx = nfs_get_lock_context(ctx);
3052 if (IS_ERR(l_ctx))
3053 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003054 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3055 &arg->stateid, &delegation_cred);
3056 nfs_put_lock_context(l_ctx);
3057 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003058 return -EBADF;
3059 } else
3060 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3061 if (delegation_cred)
3062 msg.rpc_cred = delegation_cred;
3063
3064 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3065
3066 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003067 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003068 renew_lease(server, timestamp);
3069 trace_nfs4_setattr(inode, &arg->stateid, status);
3070 return status;
3071}
3072
3073static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3074 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003075 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003076 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003077{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003078 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003079 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003080 struct nfs_setattrargs arg = {
3081 .fh = NFS_FH(inode),
3082 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003083 .server = server,
3084 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003085 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003086 };
3087 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003089 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003091 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003092 struct nfs4_exception exception = {
3093 .state = state,
3094 .inode = inode,
3095 .stateid = &arg.stateid,
3096 };
3097 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003098
David Quigleyaa9c2662013-05-22 12:50:44 -04003099 arg.bitmask = nfs4_bitmask(server, ilabel);
3100 if (ilabel)
3101 arg.bitmask = nfs4_bitmask(server, olabel);
3102
Linus Torvalds1da177e2005-04-16 15:20:36 -07003103 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003104 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003105 switch (err) {
3106 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003107 if (!(sattr->ia_valid & ATTR_SIZE)) {
3108 pr_warn_once("NFSv4: server %s is incorrectly "
3109 "applying open mode checks to "
3110 "a SETATTR that is not "
3111 "changing file size.\n",
3112 server->nfs_client->cl_hostname);
3113 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003114 if (state && !(state->state & FMODE_WRITE)) {
3115 err = -EBADF;
3116 if (sattr->ia_valid & ATTR_OPEN)
3117 err = -EACCES;
3118 goto out;
3119 }
3120 }
3121 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003123out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 return err;
3125}
3126
Peng Tao500d7012015-09-22 11:35:22 +08003127static bool
3128nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3129{
3130 if (inode == NULL || !nfs_have_layout(inode))
3131 return false;
3132
3133 return pnfs_wait_on_layoutreturn(inode, task);
3134}
3135
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136struct nfs4_closedata {
3137 struct inode *inode;
3138 struct nfs4_state *state;
3139 struct nfs_closeargs arg;
3140 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003141 struct {
3142 struct nfs4_layoutreturn_args arg;
3143 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003144 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003145 u32 roc_barrier;
3146 bool roc;
3147 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003148 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003149 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003150};
3151
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003152static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003153{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003154 struct nfs4_closedata *calldata = data;
3155 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003156 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003157
Trond Myklebustcf805162016-11-15 14:56:07 -05003158 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003159 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3160 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003161 nfs4_put_open_state(calldata->state);
3162 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003163 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003164 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003165 kfree(calldata);
3166}
3167
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003168static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003170 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003172 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003173 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003174 struct nfs4_exception exception = {
3175 .state = state,
3176 .inode = calldata->inode,
3177 .stateid = &calldata->arg.stateid,
3178 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179
Chuck Levera3ca5652012-03-01 17:00:40 -05003180 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003181 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3182 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003183 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003184
3185 /* Handle Layoutreturn errors */
3186 if (calldata->arg.lr_args && task->tk_status != 0) {
3187 switch (calldata->res.lr_ret) {
3188 default:
3189 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3190 break;
3191 case 0:
3192 calldata->arg.lr_args = NULL;
3193 calldata->res.lr_res = NULL;
3194 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003195 case -NFS4ERR_OLD_STATEID:
3196 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003197 calldata->inode))
3198 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003199 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003200 case -NFS4ERR_ADMIN_REVOKED:
3201 case -NFS4ERR_DELEG_REVOKED:
3202 case -NFS4ERR_EXPIRED:
3203 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003204 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3205 case -NFS4ERR_WRONG_CRED:
3206 calldata->arg.lr_args = NULL;
3207 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003208 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003209 }
3210 }
3211
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003212 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 * the state_owner. we keep this around to process errors
3214 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 switch (task->tk_status) {
3216 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003217 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003218 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003219 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003220 case -NFS4ERR_ACCESS:
3221 if (calldata->arg.bitmask != NULL) {
3222 calldata->arg.bitmask = NULL;
3223 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003224 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003225
3226 }
3227 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003228 case -NFS4ERR_OLD_STATEID:
3229 /* Did we race with OPEN? */
3230 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003231 state))
3232 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003233 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003234 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003235 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003236 case -NFS4ERR_EXPIRED:
3237 nfs4_free_revoked_stateid(server,
3238 &calldata->arg.stateid,
3239 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003240 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003241 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003242 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003243 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003244 task->tk_status = nfs4_async_handle_exception(task,
3245 server, task->tk_status, &exception);
3246 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003247 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003248 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003249 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3250 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003251out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003252 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003253 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003254 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003255 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003256 return;
3257lr_restart:
3258 calldata->res.lr_ret = 0;
3259out_restart:
3260 task->tk_status = 0;
3261 rpc_restart_call_prepare(task);
3262 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003263}
3264
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003265static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003266{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003267 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003268 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003269 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003270 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003271 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003272
Chuck Levera3ca5652012-03-01 17:00:40 -05003273 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003274 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003275 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003276
Trond Myklebust88069f72009-12-08 08:33:16 -05003277 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003278 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003279 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3280 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3281 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003282 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003283 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003284 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003285 if (state->n_rdonly == 0)
3286 call_close |= is_rdonly;
3287 else if (is_rdonly)
3288 calldata->arg.fmode |= FMODE_READ;
3289 if (state->n_wronly == 0)
3290 call_close |= is_wronly;
3291 else if (is_wronly)
3292 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003293 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3294 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003295 } else if (is_rdwr)
3296 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3297
Trond Myklebust5cc78612016-11-14 11:19:56 -05003298 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003299 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003300 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003301 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003302
3303 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003304 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003305 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003306 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003307
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003308 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003309 nfs_release_seqid(calldata->arg.seqid);
3310 goto out_wait;
3311 }
3312
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003313 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003314 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003315
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003316 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003317 /* Close-to-open cache consistency revalidation */
3318 if (!nfs4_have_delegation(inode, FMODE_READ))
3319 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3320 else
3321 calldata->arg.bitmask = NULL;
3322 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003323
Trond Myklebust6ae37332015-01-30 14:21:14 -05003324 calldata->arg.share_access =
3325 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3326 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003327
Trond Myklebustd8d84982016-12-19 12:14:44 -05003328 if (calldata->res.fattr == NULL)
3329 calldata->arg.bitmask = NULL;
3330 else if (calldata->arg.bitmask == NULL)
3331 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003332 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003333 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003334 &calldata->arg.seq_args,
3335 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003336 task) != 0)
3337 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003338 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003339 return;
3340out_no_action:
3341 task->tk_action = NULL;
3342out_wait:
3343 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003344}
3345
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003346static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003347 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003348 .rpc_call_done = nfs4_close_done,
3349 .rpc_release = nfs4_free_closedata,
3350};
3351
Linus Torvalds1da177e2005-04-16 15:20:36 -07003352/*
3353 * It is possible for data to be read/written from a mem-mapped file
3354 * after the sys_close call (which hits the vfs layer as a flush).
3355 * This means that we can't safely call nfsv4 close on a file until
3356 * the inode is cleared. This in turn means that we are not good
3357 * NFSv4 citizens - we do not indicate to the server to update the file's
3358 * share state even when we are done with one of the three share
3359 * stateid's in the inode.
3360 *
3361 * NOTE: Caller must be holding the sp->so_owner semaphore!
3362 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003363int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003364{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003365 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003366 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003367 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003368 struct nfs4_state_owner *sp = state->owner;
3369 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003370 struct rpc_message msg = {
3371 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3372 .rpc_cred = state->owner->so_cred,
3373 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003374 struct rpc_task_setup task_setup_data = {
3375 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003376 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003377 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003378 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003379 .flags = RPC_TASK_ASYNC,
3380 };
Trond Myklebust95121352005-10-18 14:20:12 -07003381 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003382
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003383 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3384 &task_setup_data.rpc_client, &msg);
3385
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003386 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003387 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003388 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003389 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003390 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003391 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003392 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003393 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3394 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003395 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003396 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3397 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003398 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003399 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003400 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003401 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003402 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003403 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003404 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003405 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003406 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003407 calldata->lr.roc = pnfs_roc(state->inode,
3408 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3409 if (calldata->lr.roc) {
3410 calldata->arg.lr_args = &calldata->lr.arg;
3411 calldata->res.lr_res = &calldata->lr.res;
3412 }
Al Viro643168c2011-06-22 18:20:23 -04003413 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003414
Trond Myklebust1174dd12010-12-21 10:52:24 -05003415 msg.rpc_argp = &calldata->arg;
3416 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003417 task_setup_data.callback_data = calldata;
3418 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003419 if (IS_ERR(task))
3420 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003421 status = 0;
3422 if (wait)
3423 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003424 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003425 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003426out_free_calldata:
3427 kfree(calldata);
3428out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003429 nfs4_put_open_state(state);
3430 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003431 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003432}
3433
Trond Myklebust2b484292010-09-17 10:56:51 -04003434static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003435nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3436 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003438 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003439 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3440
3441 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003442
Trond Myklebust565277f2007-10-15 18:17:53 -04003443 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003444 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003445
3446 nfs4_label_release_security(label);
3447
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003448 if (IS_ERR(state))
3449 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003450 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451}
3452
Trond Myklebust1185a552009-12-03 15:54:02 -05003453static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003454{
3455 if (ctx->state == NULL)
3456 return;
3457 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003458 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003459 else
Al Viro643168c2011-06-22 18:20:23 -04003460 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003461}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462
Trond Myklebustb944dba2013-11-04 15:20:20 -05003463#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3464#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003465#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003466
Linus Torvalds1da177e2005-04-16 15:20:36 -07003467static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3468{
Kinglong Mee8c612822015-08-26 21:12:58 +08003469 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003470 struct nfs4_server_caps_arg args = {
3471 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003472 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003473 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474 struct nfs4_server_caps_res res = {};
3475 struct rpc_message msg = {
3476 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003477 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 .rpc_resp = &res,
3479 };
3480 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003481 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003482
Kinglong Mee8c612822015-08-26 21:12:58 +08003483 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3484 FATTR4_WORD0_FH_EXPIRE_TYPE |
3485 FATTR4_WORD0_LINK_SUPPORT |
3486 FATTR4_WORD0_SYMLINK_SUPPORT |
3487 FATTR4_WORD0_ACLSUPPORT;
3488 if (minorversion)
3489 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3490
Bryan Schumaker7c513052011-03-24 17:12:24 +00003491 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003492 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003493 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003494 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003495 case 0:
3496 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3497 res.attr_bitmask[2] = 0;
3498 break;
3499 case 1:
3500 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3501 break;
3502 case 2:
3503 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3504 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003505 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003506 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3507 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3508 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3509 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003510 NFS_CAP_CTIME|NFS_CAP_MTIME|
3511 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003512 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3513 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003514 server->caps |= NFS_CAP_ACLS;
3515 if (res.has_links != 0)
3516 server->caps |= NFS_CAP_HARDLINKS;
3517 if (res.has_symlinks != 0)
3518 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003519 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3520 server->caps |= NFS_CAP_FILEID;
3521 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3522 server->caps |= NFS_CAP_MODE;
3523 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3524 server->caps |= NFS_CAP_NLINK;
3525 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3526 server->caps |= NFS_CAP_OWNER;
3527 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3528 server->caps |= NFS_CAP_OWNER_GROUP;
3529 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3530 server->caps |= NFS_CAP_ATIME;
3531 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3532 server->caps |= NFS_CAP_CTIME;
3533 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3534 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003535#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3536 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3537 server->caps |= NFS_CAP_SECURITY_LABEL;
3538#endif
3539 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3540 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003541 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003542
Trond Myklebusta65318b2009-03-11 14:10:28 -04003543 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3544 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3545 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003546 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003547
3548 /* Avoid a regression due to buggy server */
3549 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3550 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003551 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3552 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003553
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003555 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003557
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 return status;
3559}
3560
Trond Myklebust55a97592006-06-09 09:34:19 -04003561int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003562{
3563 struct nfs4_exception exception = { };
3564 int err;
3565 do {
3566 err = nfs4_handle_exception(server,
3567 _nfs4_server_capabilities(server, fhandle),
3568 &exception);
3569 } while (exception.retry);
3570 return err;
3571}
3572
3573static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3574 struct nfs_fsinfo *info)
3575{
David Quigleyaa9c2662013-05-22 12:50:44 -04003576 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003578 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 };
3580 struct nfs4_lookup_res res = {
3581 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003582 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003583 .fh = fhandle,
3584 };
3585 struct rpc_message msg = {
3586 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3587 .rpc_argp = &args,
3588 .rpc_resp = &res,
3589 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003590
David Quigleyaa9c2662013-05-22 12:50:44 -04003591 bitmask[0] = nfs4_fattr_bitmap[0];
3592 bitmask[1] = nfs4_fattr_bitmap[1];
3593 /*
3594 * Process the label in the upcoming getfattr
3595 */
3596 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3597
Trond Myklebust0e574af2005-10-27 22:12:38 -04003598 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003599 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003600}
3601
3602static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3603 struct nfs_fsinfo *info)
3604{
3605 struct nfs4_exception exception = { };
3606 int err;
3607 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003608 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003609 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003610 switch (err) {
3611 case 0:
3612 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003613 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003614 default:
3615 err = nfs4_handle_exception(server, err, &exception);
3616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003618out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 return err;
3620}
3621
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003622static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3623 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3624{
Trond Myklebustc2190662013-08-26 19:23:04 -04003625 struct rpc_auth_create_args auth_args = {
3626 .pseudoflavor = flavor,
3627 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003628 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003629
Trond Myklebustc2190662013-08-26 19:23:04 -04003630 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003631 if (IS_ERR(auth))
3632 return -EACCES;
3633 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003634}
3635
Chuck Lever9a744ba2013-03-16 15:56:02 -04003636/*
3637 * Retry pseudoroot lookup with various security flavors. We do this when:
3638 *
3639 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3640 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3641 *
3642 * Returns zero on success, or a negative NFS4ERR value, or a
3643 * negative errno value.
3644 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003645static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003646 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003647{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003648 /* Per 3530bis 15.33.5 */
3649 static const rpc_authflavor_t flav_array[] = {
3650 RPC_AUTH_GSS_KRB5P,
3651 RPC_AUTH_GSS_KRB5I,
3652 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003653 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003654 RPC_AUTH_NULL,
3655 };
3656 int status = -EPERM;
3657 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003658
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003659 if (server->auth_info.flavor_len > 0) {
3660 /* try each flavor specified by user */
3661 for (i = 0; i < server->auth_info.flavor_len; i++) {
3662 status = nfs4_lookup_root_sec(server, fhandle, info,
3663 server->auth_info.flavors[i]);
3664 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3665 continue;
3666 break;
3667 }
3668 } else {
3669 /* no flavors specified by user, try default list */
3670 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3671 status = nfs4_lookup_root_sec(server, fhandle, info,
3672 flav_array[i]);
3673 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3674 continue;
3675 break;
3676 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003677 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003678
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003679 /*
3680 * -EACCESS could mean that the user doesn't have correct permissions
3681 * to access the mount. It could also mean that we tried to mount
3682 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3683 * existing mount programs don't handle -EACCES very well so it should
3684 * be mapped to -EPERM instead.
3685 */
3686 if (status == -EACCES)
3687 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003688 return status;
3689}
3690
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003691/**
3692 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3693 * @server: initialized nfs_server handle
3694 * @fhandle: we fill in the pseudo-fs root file handle
3695 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003696 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003697 *
3698 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003699 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003700int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003701 struct nfs_fsinfo *info,
3702 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003703{
Andre Przywarac7757072015-04-23 17:17:40 +01003704 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003705
Andre Przywarac7757072015-04-23 17:17:40 +01003706 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003707 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003708
3709 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003710 status = server->nfs_client->cl_mvops->find_root_sec(server,
3711 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003712
Linus Torvalds1da177e2005-04-16 15:20:36 -07003713 if (status == 0)
3714 status = nfs4_server_capabilities(server, fhandle);
3715 if (status == 0)
3716 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003717
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003718 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003719}
3720
Bryan Schumakerbae36242012-05-10 15:07:31 -04003721static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3722 struct nfs_fsinfo *info)
3723{
3724 int error;
3725 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003726 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003727
3728 error = nfs4_server_capabilities(server, mntfh);
3729 if (error < 0) {
3730 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3731 return error;
3732 }
3733
David Quigley14c43f72013-05-22 12:50:43 -04003734 label = nfs4_label_alloc(server, GFP_KERNEL);
3735 if (IS_ERR(label))
3736 return PTR_ERR(label);
3737
David Quigley1775fd32013-05-22 12:50:42 -04003738 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003739 if (error < 0) {
3740 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003741 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003742 }
3743
3744 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3745 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3746 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3747
David Quigley14c43f72013-05-22 12:50:43 -04003748err_free_label:
3749 nfs4_label_free(label);
3750
Bryan Schumakerbae36242012-05-10 15:07:31 -04003751 return error;
3752}
3753
Manoj Naik6b97fd32006-06-09 09:34:29 -04003754/*
3755 * Get locations and (maybe) other attributes of a referral.
3756 * Note that we'll actually follow the referral later when
3757 * we detect fsid mismatch in inode revalidation
3758 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003759static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3760 const struct qstr *name, struct nfs_fattr *fattr,
3761 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003762{
3763 int status = -ENOMEM;
3764 struct page *page = NULL;
3765 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003766
3767 page = alloc_page(GFP_KERNEL);
3768 if (page == NULL)
3769 goto out;
3770 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3771 if (locations == NULL)
3772 goto out;
3773
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003774 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003775 if (status != 0)
3776 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003777
3778 /*
3779 * If the fsid didn't change, this is a migration event, not a
3780 * referral. Cause us to drop into the exception handler, which
3781 * will kick off migration recovery.
3782 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003783 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003784 dprintk("%s: server did not return a different fsid for"
3785 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003786 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003787 goto out;
3788 }
Andy Adamson533eb462011-06-13 18:25:56 -04003789 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3790 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003791
Andy Adamson533eb462011-06-13 18:25:56 -04003792 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003793 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003794 memset(fhandle, 0, sizeof(struct nfs_fh));
3795out:
3796 if (page)
3797 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003798 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003799 return status;
3800}
3801
David Quigley1775fd32013-05-22 12:50:42 -04003802static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3803 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003804{
3805 struct nfs4_getattr_arg args = {
3806 .fh = fhandle,
3807 .bitmask = server->attr_bitmask,
3808 };
3809 struct nfs4_getattr_res res = {
3810 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003811 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812 .server = server,
3813 };
3814 struct rpc_message msg = {
3815 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3816 .rpc_argp = &args,
3817 .rpc_resp = &res,
3818 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003819
3820 args.bitmask = nfs4_bitmask(server, label);
3821
Trond Myklebust0e574af2005-10-27 22:12:38 -04003822 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003823 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003824}
3825
David Quigley1775fd32013-05-22 12:50:42 -04003826static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3827 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003828{
3829 struct nfs4_exception exception = { };
3830 int err;
3831 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003832 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3833 trace_nfs4_getattr(server, fhandle, fattr, err);
3834 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835 &exception);
3836 } while (exception.retry);
3837 return err;
3838}
3839
3840/*
3841 * The file is not closed if it is opened due to the a request to change
3842 * the size of the file. The open call will not be needed once the
3843 * VFS layer lookup-intents are implemented.
3844 *
3845 * Close is called when the inode is destroyed.
3846 * If we haven't opened the file for O_WRONLY, we
3847 * need to in the size_change case to obtain a stateid.
3848 *
3849 * Got race?
3850 * Because OPEN is always done by name in nfsv4, it is
3851 * possible that we opened a different file by the same
3852 * name. We can recognize this race condition, but we
3853 * can't do anything about it besides returning an error.
3854 *
3855 * This will be fixed with VFS changes (lookup-intent).
3856 */
3857static int
3858nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3859 struct iattr *sattr)
3860{
David Howells2b0143b2015-03-17 22:25:59 +00003861 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003862 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003863 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003864 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003865 int status;
3866
Peng Tao88ac8152014-09-12 11:04:10 +08003867 if (pnfs_ld_layoutret_on_setattr(inode) &&
3868 sattr->ia_valid & ATTR_SIZE &&
3869 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003870 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003871
Trond Myklebust0e574af2005-10-27 22:12:38 -04003872 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003873
Andy Adamson26699402012-05-30 16:12:24 -04003874 /* Deal with open(O_TRUNC) */
3875 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003876 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003877
3878 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003879 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003880 return 0;
3881
Trond Myklebustd5308382005-11-04 15:33:38 -05003882 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003883 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003884
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003885 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003886 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003887 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003888 }
3889
David Quigley14c43f72013-05-22 12:50:43 -04003890 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3891 if (IS_ERR(label))
3892 return PTR_ERR(label);
3893
Trond Myklebust199366f2018-03-20 16:43:18 -04003894 /* Return any delegations if we're going to change ACLs */
3895 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003896 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003897
NeilBrown29b59f92016-10-13 15:26:47 +11003898 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003899 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003900 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003901 nfs_setsecurity(inode, fattr, label);
3902 }
David Quigley14c43f72013-05-22 12:50:43 -04003903 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003904 return status;
3905}
3906
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003907static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3908 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003909 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003910{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003911 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003912 int status;
3913 struct nfs4_lookup_arg args = {
3914 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003915 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003916 .name = name,
3917 };
3918 struct nfs4_lookup_res res = {
3919 .server = server,
3920 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003921 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003922 .fh = fhandle,
3923 };
3924 struct rpc_message msg = {
3925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3926 .rpc_argp = &args,
3927 .rpc_resp = &res,
3928 };
3929
David Quigleyaa9c2662013-05-22 12:50:44 -04003930 args.bitmask = nfs4_bitmask(server, label);
3931
David Howells2b3de442006-08-22 20:06:09 -04003932 nfs_fattr_init(fattr);
3933
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003935 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 dprintk("NFS reply lookup: %d\n", status);
3937 return status;
3938}
3939
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003940static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003941{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003942 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003943 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003944 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3945 fattr->nlink = 2;
3946}
3947
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003948static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003949 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003950 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003951{
3952 struct nfs4_exception exception = { };
3953 struct rpc_clnt *client = *clnt;
3954 int err;
3955 do {
David Quigley1775fd32013-05-22 12:50:42 -04003956 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003957 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003958 switch (err) {
3959 case -NFS4ERR_BADNAME:
3960 err = -ENOENT;
3961 goto out;
3962 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003963 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003964 if (err == -NFS4ERR_MOVED)
3965 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003966 goto out;
3967 case -NFS4ERR_WRONGSEC:
3968 err = -EPERM;
3969 if (client != *clnt)
3970 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003971 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003972 if (IS_ERR(client))
3973 return PTR_ERR(client);
3974
3975 exception.retry = 1;
3976 break;
3977 default:
3978 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3979 }
3980 } while (exception.retry);
3981
3982out:
3983 if (err == 0)
3984 *clnt = client;
3985 else if (client != *clnt)
3986 rpc_shutdown_client(client);
3987
3988 return err;
3989}
3990
Al Virobeffb8f2016-07-20 16:34:42 -04003991static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003992 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3993 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003994{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003995 int status;
3996 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003997
David Quigley1775fd32013-05-22 12:50:42 -04003998 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003999 if (client != NFS_CLIENT(dir)) {
4000 rpc_shutdown_client(client);
4001 nfs_fixup_secinfo_attributes(fattr);
4002 }
4003 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004004}
4005
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004006struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004007nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004008 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4009{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004010 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004011 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004012
David Quigley1775fd32013-05-22 12:50:42 -04004013 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004014 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004015 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004016 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004017}
4018
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004019static int _nfs4_proc_lookupp(struct inode *inode,
4020 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4021 struct nfs4_label *label)
4022{
4023 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4024 struct nfs_server *server = NFS_SERVER(inode);
4025 int status;
4026 struct nfs4_lookupp_arg args = {
4027 .bitmask = server->attr_bitmask,
4028 .fh = NFS_FH(inode),
4029 };
4030 struct nfs4_lookupp_res res = {
4031 .server = server,
4032 .fattr = fattr,
4033 .label = label,
4034 .fh = fhandle,
4035 };
4036 struct rpc_message msg = {
4037 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4038 .rpc_argp = &args,
4039 .rpc_resp = &res,
4040 };
4041
4042 args.bitmask = nfs4_bitmask(server, label);
4043
4044 nfs_fattr_init(fattr);
4045
4046 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4047 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4048 &res.seq_res, 0);
4049 dprintk("NFS reply lookupp: %d\n", status);
4050 return status;
4051}
4052
4053static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4054 struct nfs_fattr *fattr, struct nfs4_label *label)
4055{
4056 struct nfs4_exception exception = { };
4057 int err;
4058 do {
4059 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4060 trace_nfs4_lookupp(inode, err);
4061 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4062 &exception);
4063 } while (exception.retry);
4064 return err;
4065}
4066
Linus Torvalds1da177e2005-04-16 15:20:36 -07004067static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4068{
Trond Myklebust76b32992007-08-10 17:45:11 -04004069 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004070 struct nfs4_accessargs args = {
4071 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004072 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004073 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004074 struct nfs4_accessres res = {
4075 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004076 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 struct rpc_message msg = {
4078 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4079 .rpc_argp = &args,
4080 .rpc_resp = &res,
4081 .rpc_cred = entry->cred,
4082 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004083 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004085 if (!nfs_have_delegated_attributes(inode)) {
4086 res.fattr = nfs_alloc_fattr();
4087 if (res.fattr == NULL)
4088 return -ENOMEM;
4089 args.bitmask = server->cache_consistency_bitmask;
4090 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004091
Bryan Schumaker7c513052011-03-24 17:12:24 +00004092 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004094 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004095 if (res.fattr)
4096 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004098 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 return status;
4100}
4101
4102static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4103{
4104 struct nfs4_exception exception = { };
4105 int err;
4106 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004107 err = _nfs4_proc_access(inode, entry);
4108 trace_nfs4_access(inode, err);
4109 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 &exception);
4111 } while (exception.retry);
4112 return err;
4113}
4114
4115/*
4116 * TODO: For the time being, we don't try to get any attributes
4117 * along with any of the zero-copy operations READ, READDIR,
4118 * READLINK, WRITE.
4119 *
4120 * In the case of the first three, we want to put the GETATTR
4121 * after the read-type operation -- this is because it is hard
4122 * to predict the length of a GETATTR response in v4, and thus
4123 * align the READ data correctly. This means that the GETATTR
4124 * may end up partially falling into the page cache, and we should
4125 * shift it into the 'tail' of the xdr_buf before processing.
4126 * To do this efficiently, we need to know the total length
4127 * of data received, which doesn't seem to be available outside
4128 * of the RPC layer.
4129 *
4130 * In the case of WRITE, we also want to put the GETATTR after
4131 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004132 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004133 *
4134 * Both of these changes to the XDR layer would in fact be quite
4135 * minor, but I decided to leave them for a subsequent patch.
4136 */
4137static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4138 unsigned int pgbase, unsigned int pglen)
4139{
4140 struct nfs4_readlink args = {
4141 .fh = NFS_FH(inode),
4142 .pgbase = pgbase,
4143 .pglen = pglen,
4144 .pages = &page,
4145 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004146 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147 struct rpc_message msg = {
4148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4149 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004150 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151 };
4152
Bryan Schumaker7c513052011-03-24 17:12:24 +00004153 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 -07004154}
4155
4156static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4157 unsigned int pgbase, unsigned int pglen)
4158{
4159 struct nfs4_exception exception = { };
4160 int err;
4161 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004162 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4163 trace_nfs4_readlink(inode, err);
4164 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004165 &exception);
4166 } while (exception.retry);
4167 return err;
4168}
4169
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004171 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173static int
4174nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004175 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004177 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004178 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004179 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004181 int status = 0;
4182
NeilBrown532d4de2016-10-13 15:26:47 +11004183 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004184 if (IS_ERR(ctx))
4185 return PTR_ERR(ctx);
4186
David Quigleyaa9c2662013-05-22 12:50:44 -04004187 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4188
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004189 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4190 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004191 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004192 if (IS_ERR(state)) {
4193 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004194 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004196out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004197 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004198 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 return status;
4200}
4201
Al Virobeffb8f2016-07-20 16:34:42 -04004202static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203{
Trond Myklebust16e42952005-10-27 22:12:44 -04004204 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004205 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004207 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004209 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004210 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004211 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004212 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004213 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4214 .rpc_argp = &args,
4215 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004216 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004217 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004218 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004219
Bryan Schumaker7c513052011-03-24 17:12:24 +00004220 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004221 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004222 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 return status;
4224}
4225
Trond Myklebust912678d2018-03-20 16:43:15 -04004226static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4227{
4228 struct nfs4_exception exception = { };
4229 struct inode *inode = d_inode(dentry);
4230 int err;
4231
Trond Myklebustc01d3642018-03-20 16:43:20 -04004232 if (inode) {
4233 if (inode->i_nlink == 1)
4234 nfs4_inode_return_delegation(inode);
4235 else
4236 nfs4_inode_make_writeable(inode);
4237 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004238 do {
4239 err = _nfs4_proc_remove(dir, &dentry->d_name);
4240 trace_nfs4_remove(dir, &dentry->d_name, err);
4241 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4242 &exception);
4243 } while (exception.retry);
4244 return err;
4245}
4246
4247static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248{
4249 struct nfs4_exception exception = { };
4250 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004251
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004253 err = _nfs4_proc_remove(dir, name);
4254 trace_nfs4_remove(dir, name, err);
4255 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256 &exception);
4257 } while (exception.retry);
4258 return err;
4259}
4260
Trond Myklebust977fcc22018-03-20 16:43:17 -04004261static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004263 struct nfs_removeargs *args = msg->rpc_argp;
4264 struct nfs_removeres *res = msg->rpc_resp;
Trond Myklebust977fcc22018-03-20 16:43:17 -04004265 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266
Trond Myklebust977fcc22018-03-20 16:43:17 -04004267 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004269 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004270
4271 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004272
4273 if (inode)
4274 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004277static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4278{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004279 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004280 &data->args.seq_args,
4281 &data->res.seq_res,
4282 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004283}
4284
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004285static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004286{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004287 struct nfs_unlinkdata *data = task->tk_calldata;
4288 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004289
Trond Myklebust14516c32010-07-31 14:29:06 -04004290 if (!nfs4_sequence_done(task, &res->seq_res))
4291 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004292 if (nfs4_async_handle_error(task, res->server, NULL,
4293 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004294 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004295 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004296 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004297 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298}
4299
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004300static void nfs4_proc_rename_setup(struct rpc_message *msg,
4301 struct dentry *old_dentry,
4302 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004303{
Jeff Laytond3d41522010-09-17 17:31:57 -04004304 struct nfs_renameargs *arg = msg->rpc_argp;
4305 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004306 struct inode *old_inode = d_inode(old_dentry);
4307 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004308
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004309 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004310 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004311 if (new_inode)
4312 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004313 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004314 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004315 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004316}
4317
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004318static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4319{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004320 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004321 &data->args.seq_args,
4322 &data->res.seq_res,
4323 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004324}
4325
4326static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4327 struct inode *new_dir)
4328{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004329 struct nfs_renamedata *data = task->tk_calldata;
4330 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004331
4332 if (!nfs4_sequence_done(task, &res->seq_res))
4333 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004334 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004335 return 0;
4336
Trond Myklebustc733c492017-01-11 12:32:26 -05004337 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004338 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004339 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004340 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004341 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004342 return 1;
4343}
4344
Al Virobeffb8f2016-07-20 16:34:42 -04004345static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004346{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004347 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348 struct nfs4_link_arg arg = {
4349 .fh = NFS_FH(inode),
4350 .dir_fh = NFS_FH(dir),
4351 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004352 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004354 struct nfs4_link_res res = {
4355 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004356 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004357 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004358 struct rpc_message msg = {
4359 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4360 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004361 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004362 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004363 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364
Trond Myklebust136f2622010-04-16 16:22:49 -04004365 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004366 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004367 goto out;
4368
David Quigley14c43f72013-05-22 12:50:43 -04004369 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4370 if (IS_ERR(res.label)) {
4371 status = PTR_ERR(res.label);
4372 goto out;
4373 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004374 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004375
Trond Myklebustc01d3642018-03-20 16:43:20 -04004376 nfs4_inode_make_writeable(inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004377
Bryan Schumaker7c513052011-03-24 17:12:24 +00004378 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004379 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004380 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004381 status = nfs_post_op_update_inode(inode, res.fattr);
4382 if (!status)
4383 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004384 }
David Quigley14c43f72013-05-22 12:50:43 -04004385
4386
4387 nfs4_label_free(res.label);
4388
Trond Myklebust136f2622010-04-16 16:22:49 -04004389out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004390 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004391 return status;
4392}
4393
Al Virobeffb8f2016-07-20 16:34:42 -04004394static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004395{
4396 struct nfs4_exception exception = { };
4397 int err;
4398 do {
4399 err = nfs4_handle_exception(NFS_SERVER(inode),
4400 _nfs4_proc_link(inode, dir, name),
4401 &exception);
4402 } while (exception.retry);
4403 return err;
4404}
4405
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004406struct nfs4_createdata {
4407 struct rpc_message msg;
4408 struct nfs4_create_arg arg;
4409 struct nfs4_create_res res;
4410 struct nfs_fh fh;
4411 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004412 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004413};
4414
4415static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004416 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004417{
4418 struct nfs4_createdata *data;
4419
4420 data = kzalloc(sizeof(*data), GFP_KERNEL);
4421 if (data != NULL) {
4422 struct nfs_server *server = NFS_SERVER(dir);
4423
David Quigley14c43f72013-05-22 12:50:43 -04004424 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4425 if (IS_ERR(data->label))
4426 goto out_free;
4427
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004428 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4429 data->msg.rpc_argp = &data->arg;
4430 data->msg.rpc_resp = &data->res;
4431 data->arg.dir_fh = NFS_FH(dir);
4432 data->arg.server = server;
4433 data->arg.name = name;
4434 data->arg.attrs = sattr;
4435 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004436 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004437 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004438 data->res.server = server;
4439 data->res.fh = &data->fh;
4440 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004441 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004442 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004443 }
4444 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004445out_free:
4446 kfree(data);
4447 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004448}
4449
4450static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4451{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004452 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004453 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004454 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004455 update_changeattr(dir, &data->res.dir_cinfo,
4456 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004457 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004458 }
4459 return status;
4460}
4461
4462static void nfs4_free_createdata(struct nfs4_createdata *data)
4463{
David Quigley14c43f72013-05-22 12:50:43 -04004464 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004465 kfree(data);
4466}
4467
Chuck Lever4f390c12006-08-22 20:06:22 -04004468static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004469 struct page *page, unsigned int len, struct iattr *sattr,
4470 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004471{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004472 struct nfs4_createdata *data;
4473 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474
Chuck Lever94a6d752006-08-22 20:06:23 -04004475 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004476 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004477
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004478 status = -ENOMEM;
4479 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4480 if (data == NULL)
4481 goto out;
4482
4483 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4484 data->arg.u.symlink.pages = &page;
4485 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004486 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004488 status = nfs4_do_create(dir, dentry, data);
4489
4490 nfs4_free_createdata(data);
4491out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 return status;
4493}
4494
Chuck Lever4f390c12006-08-22 20:06:22 -04004495static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004496 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497{
4498 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004499 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004501
4502 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4503
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004505 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4506 trace_nfs4_symlink(dir, &dentry->d_name, err);
4507 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004508 &exception);
4509 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004510
4511 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 return err;
4513}
4514
4515static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004516 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004517{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004518 struct nfs4_createdata *data;
4519 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004521 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4522 if (data == NULL)
4523 goto out;
4524
David Quigley1775fd32013-05-22 12:50:42 -04004525 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004526 status = nfs4_do_create(dir, dentry, data);
4527
4528 nfs4_free_createdata(data);
4529out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return status;
4531}
4532
4533static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4534 struct iattr *sattr)
4535{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004536 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004538 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004540
David Quigleyaa9c2662013-05-22 12:50:44 -04004541 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4542
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004543 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4544 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004545 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004546 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4547 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4548 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004549 &exception);
4550 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004551 nfs4_label_release_security(label);
4552
Linus Torvalds1da177e2005-04-16 15:20:36 -07004553 return err;
4554}
4555
4556static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004557 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558{
David Howells2b0143b2015-03-17 22:25:59 +00004559 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 struct nfs4_readdir_arg args = {
4561 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004562 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 .pgbase = 0,
4564 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004565 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004566 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 };
4568 struct nfs4_readdir_res res;
4569 struct rpc_message msg = {
4570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4571 .rpc_argp = &args,
4572 .rpc_resp = &res,
4573 .rpc_cred = cred,
4574 };
4575 int status;
4576
Al Viro6de14722013-09-16 10:53:17 -04004577 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4578 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004579 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004580 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004582 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 -05004583 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004584 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004585 status += args.pgbase;
4586 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004587
4588 nfs_invalidate_atime(dir);
4589
Harvey Harrison3110ff82008-05-02 13:42:44 -07004590 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 return status;
4592}
4593
4594static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004595 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004596{
4597 struct nfs4_exception exception = { };
4598 int err;
4599 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004600 err = _nfs4_proc_readdir(dentry, cred, cookie,
4601 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004602 trace_nfs4_readdir(d_inode(dentry), err);
4603 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004604 &exception);
4605 } while (exception.retry);
4606 return err;
4607}
4608
4609static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004610 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004612 struct nfs4_createdata *data;
4613 int mode = sattr->ia_mode;
4614 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004615
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004616 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4617 if (data == NULL)
4618 goto out;
4619
Linus Torvalds1da177e2005-04-16 15:20:36 -07004620 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004621 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004623 data->arg.ftype = NF4BLK;
4624 data->arg.u.device.specdata1 = MAJOR(rdev);
4625 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004626 }
4627 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004628 data->arg.ftype = NF4CHR;
4629 data->arg.u.device.specdata1 = MAJOR(rdev);
4630 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004631 } else if (!S_ISSOCK(mode)) {
4632 status = -EINVAL;
4633 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004634 }
David Quigley1775fd32013-05-22 12:50:42 -04004635
David Quigleyaa9c2662013-05-22 12:50:44 -04004636 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004637 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004638out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004639 nfs4_free_createdata(data);
4640out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 return status;
4642}
4643
4644static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4645 struct iattr *sattr, dev_t rdev)
4646{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004647 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004649 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004651
David Quigleyaa9c2662013-05-22 12:50:44 -04004652 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4653
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004654 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4655 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004656 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004657 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4658 trace_nfs4_mknod(dir, &dentry->d_name, err);
4659 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660 &exception);
4661 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004662
4663 nfs4_label_release_security(label);
4664
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 return err;
4666}
4667
4668static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4669 struct nfs_fsstat *fsstat)
4670{
4671 struct nfs4_statfs_arg args = {
4672 .fh = fhandle,
4673 .bitmask = server->attr_bitmask,
4674 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004675 struct nfs4_statfs_res res = {
4676 .fsstat = fsstat,
4677 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 struct rpc_message msg = {
4679 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4680 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004681 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004682 };
4683
Trond Myklebust0e574af2005-10-27 22:12:38 -04004684 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004685 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004686}
4687
4688static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4689{
4690 struct nfs4_exception exception = { };
4691 int err;
4692 do {
4693 err = nfs4_handle_exception(server,
4694 _nfs4_proc_statfs(server, fhandle, fsstat),
4695 &exception);
4696 } while (exception.retry);
4697 return err;
4698}
4699
4700static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4701 struct nfs_fsinfo *fsinfo)
4702{
4703 struct nfs4_fsinfo_arg args = {
4704 .fh = fhandle,
4705 .bitmask = server->attr_bitmask,
4706 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004707 struct nfs4_fsinfo_res res = {
4708 .fsinfo = fsinfo,
4709 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004710 struct rpc_message msg = {
4711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4712 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004713 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004714 };
4715
Bryan Schumaker7c513052011-03-24 17:12:24 +00004716 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717}
4718
4719static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4720{
4721 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004722 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004723 int err;
4724
4725 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004726 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004727 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004728 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004729 nfs4_set_lease_period(server->nfs_client,
4730 fsinfo->lease_time * HZ,
4731 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004732 break;
4733 }
4734 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735 } while (exception.retry);
4736 return err;
4737}
4738
4739static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4740{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004741 int error;
4742
Trond Myklebust0e574af2005-10-27 22:12:38 -04004743 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004744 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004745 if (error == 0) {
4746 /* block layout checks this! */
4747 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004748 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004749 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004750
4751 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004752}
4753
4754static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4755 struct nfs_pathconf *pathconf)
4756{
4757 struct nfs4_pathconf_arg args = {
4758 .fh = fhandle,
4759 .bitmask = server->attr_bitmask,
4760 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004761 struct nfs4_pathconf_res res = {
4762 .pathconf = pathconf,
4763 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004764 struct rpc_message msg = {
4765 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4766 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004767 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004768 };
4769
4770 /* None of the pathconf attributes are mandatory to implement */
4771 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4772 memset(pathconf, 0, sizeof(*pathconf));
4773 return 0;
4774 }
4775
Trond Myklebust0e574af2005-10-27 22:12:38 -04004776 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004777 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004778}
4779
4780static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4781 struct nfs_pathconf *pathconf)
4782{
4783 struct nfs4_exception exception = { };
4784 int err;
4785
4786 do {
4787 err = nfs4_handle_exception(server,
4788 _nfs4_proc_pathconf(server, fhandle, pathconf),
4789 &exception);
4790 } while (exception.retry);
4791 return err;
4792}
4793
Trond Myklebust5521abf2013-03-16 20:54:34 -04004794int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004795 const struct nfs_open_context *ctx,
4796 const struct nfs_lock_context *l_ctx,
4797 fmode_t fmode)
4798{
NeilBrown17393472016-10-13 15:26:47 +11004799 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004800}
4801EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4802
Trond Myklebust5521abf2013-03-16 20:54:34 -04004803static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4804 const struct nfs_open_context *ctx,
4805 const struct nfs_lock_context *l_ctx,
4806 fmode_t fmode)
4807{
4808 nfs4_stateid current_stateid;
4809
Trond Myklebuste1253be2014-03-05 08:44:23 -05004810 /* If the current stateid represents a lost lock, then exit */
4811 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4812 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004813 return nfs4_stateid_match(stateid, &current_stateid);
4814}
4815
4816static bool nfs4_error_stateid_expired(int err)
4817{
4818 switch (err) {
4819 case -NFS4ERR_DELEG_REVOKED:
4820 case -NFS4ERR_ADMIN_REVOKED:
4821 case -NFS4ERR_BAD_STATEID:
4822 case -NFS4ERR_STALE_STATEID:
4823 case -NFS4ERR_OLD_STATEID:
4824 case -NFS4ERR_OPENMODE:
4825 case -NFS4ERR_EXPIRED:
4826 return true;
4827 }
4828 return false;
4829}
4830
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004831static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004832{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004833 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004835 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004836 if (task->tk_status < 0) {
4837 struct nfs4_exception exception = {
4838 .inode = hdr->inode,
4839 .state = hdr->args.context->state,
4840 .stateid = &hdr->args.stateid,
4841 };
4842 task->tk_status = nfs4_async_handle_exception(task,
4843 server, task->tk_status, &exception);
4844 if (exception.retry) {
4845 rpc_restart_call_prepare(task);
4846 return -EAGAIN;
4847 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004848 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004849
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004851 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004852 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004853}
4854
Trond Myklebust5521abf2013-03-16 20:54:34 -04004855static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004856 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004857{
4858
4859 if (!nfs4_error_stateid_expired(task->tk_status) ||
4860 nfs4_stateid_is_current(&args->stateid,
4861 args->context,
4862 args->lock_context,
4863 FMODE_READ))
4864 return false;
4865 rpc_restart_call_prepare(task);
4866 return true;
4867}
4868
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004869static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004870{
4871
4872 dprintk("--> %s\n", __func__);
4873
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004874 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004875 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004876 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004877 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004878 if (task->tk_status > 0)
4879 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004880 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4881 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004882}
4883
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004884static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4885 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004888 if (!hdr->pgio_done_cb)
4889 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004890 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004891 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
4893
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004894static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4895 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004896{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004897 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004898 &hdr->args.seq_args,
4899 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004900 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004901 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004902 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4903 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004904 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004905 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004906 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004907 return -EIO;
4908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909}
4910
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004911static int nfs4_write_done_cb(struct rpc_task *task,
4912 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004914 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004915
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004916 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004917 if (task->tk_status < 0) {
4918 struct nfs4_exception exception = {
4919 .inode = hdr->inode,
4920 .state = hdr->args.context->state,
4921 .stateid = &hdr->args.stateid,
4922 };
4923 task->tk_status = nfs4_async_handle_exception(task,
4924 NFS_SERVER(inode), task->tk_status,
4925 &exception);
4926 if (exception.retry) {
4927 rpc_restart_call_prepare(task);
4928 return -EAGAIN;
4929 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004930 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004931 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004932 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004933 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004934 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004935 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004936}
4937
Trond Myklebust5521abf2013-03-16 20:54:34 -04004938static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004939 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004940{
4941
4942 if (!nfs4_error_stateid_expired(task->tk_status) ||
4943 nfs4_stateid_is_current(&args->stateid,
4944 args->context,
4945 args->lock_context,
4946 FMODE_WRITE))
4947 return false;
4948 rpc_restart_call_prepare(task);
4949 return true;
4950}
4951
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004952static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004953{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004954 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004955 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004956 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004957 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004958 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4959 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004960}
4961
Trond Myklebust5a37f852012-04-28 14:55:16 -04004962static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004963bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004964{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004965 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004966 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004967 return false;
4968 /* Otherwise, request attributes if and only if we don't hold
4969 * a delegation
4970 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004971 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004972}
Fred Isamana69aef12011-03-03 15:13:47 +00004973
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004974static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004975 struct rpc_message *msg,
4976 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004978 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004979
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004980 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4981 hdr->args.bitmask = NULL;
4982 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004983 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004984 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004985
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004986 if (!hdr->pgio_done_cb)
4987 hdr->pgio_done_cb = nfs4_write_done_cb;
4988 hdr->res.server = server;
4989 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004991 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004992 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004993 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994}
4995
Fred Isaman0b7c0152012-04-20 14:47:39 -04004996static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4997{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004998 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004999 &data->args.seq_args,
5000 &data->res.seq_res,
5001 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005002}
5003
Fred Isaman0b7c0152012-04-20 14:47:39 -04005004static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005007
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005008 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005009 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5010 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005011 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005012 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005014 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015}
5016
Fred Isaman0b7c0152012-04-20 14:47:39 -04005017static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005018{
5019 if (!nfs4_sequence_done(task, &data->res.seq_res))
5020 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005021 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005022}
5023
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005024static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5025 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005026{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005027 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005028
Fred Isaman0b7c0152012-04-20 14:47:39 -04005029 if (data->commit_done_cb == NULL)
5030 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005031 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005032 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005033 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005034 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035}
5036
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005037struct nfs4_renewdata {
5038 struct nfs_client *client;
5039 unsigned long timestamp;
5040};
5041
Linus Torvalds1da177e2005-04-16 15:20:36 -07005042/*
5043 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5044 * standalone procedure for queueing an asynchronous RENEW.
5045 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005046static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005047{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005048 struct nfs4_renewdata *data = calldata;
5049 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005050
Elena Reshetova212bf412017-10-20 12:53:38 +03005051 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005052 nfs4_schedule_state_renewal(clp);
5053 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005054 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005055}
5056
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005057static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005059 struct nfs4_renewdata *data = calldata;
5060 struct nfs_client *clp = data->client;
5061 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005063 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005064 switch (task->tk_status) {
5065 case 0:
5066 break;
5067 case -NFS4ERR_LEASE_MOVED:
5068 nfs4_schedule_lease_moved_recovery(clp);
5069 break;
5070 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005071 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005072 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5073 return;
5074 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005075 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005076 return;
5077 }
5078 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005080 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081}
5082
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005083static const struct rpc_call_ops nfs4_renew_ops = {
5084 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005085 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005086};
5087
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005088static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089{
5090 struct rpc_message msg = {
5091 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5092 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005093 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005095 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005097 if (renew_flags == 0)
5098 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005099 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005100 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005101 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005102 if (data == NULL) {
5103 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005104 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005105 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005106 data->client = clp;
5107 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005108 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005109 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005110}
5111
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005112static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005113{
5114 struct rpc_message msg = {
5115 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5116 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005117 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118 };
5119 unsigned long now = jiffies;
5120 int status;
5121
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005122 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 if (status < 0)
5124 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005125 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005126 return 0;
5127}
5128
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005129static inline int nfs4_server_supports_acls(struct nfs_server *server)
5130{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005131 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005132}
5133
Trond Myklebust21f498c2012-08-24 10:59:25 -04005134/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5135 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005136 * the stack.
5137 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005138#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005139
Neil Hormane9e3d722011-03-04 19:26:03 -05005140static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005141 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005142{
5143 struct page *newpage, **spages;
5144 int rc = 0;
5145 size_t len;
5146 spages = pages;
5147
5148 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005149 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005150 newpage = alloc_page(GFP_KERNEL);
5151
5152 if (newpage == NULL)
5153 goto unwind;
5154 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005155 buf += len;
5156 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005157 *pages++ = newpage;
5158 rc++;
5159 } while (buflen != 0);
5160
5161 return rc;
5162
5163unwind:
5164 for(; rc > 0; rc--)
5165 __free_page(spages[rc-1]);
5166 return -ENOMEM;
5167}
5168
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005169struct nfs4_cached_acl {
5170 int cached;
5171 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005172 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005173};
5174
5175static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005176{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005177 struct nfs_inode *nfsi = NFS_I(inode);
5178
5179 spin_lock(&inode->i_lock);
5180 kfree(nfsi->nfs4_acl);
5181 nfsi->nfs4_acl = acl;
5182 spin_unlock(&inode->i_lock);
5183}
5184
5185static void nfs4_zap_acl_attr(struct inode *inode)
5186{
5187 nfs4_set_cached_acl(inode, NULL);
5188}
5189
5190static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5191{
5192 struct nfs_inode *nfsi = NFS_I(inode);
5193 struct nfs4_cached_acl *acl;
5194 int ret = -ENOENT;
5195
5196 spin_lock(&inode->i_lock);
5197 acl = nfsi->nfs4_acl;
5198 if (acl == NULL)
5199 goto out;
5200 if (buf == NULL) /* user is just asking for length */
5201 goto out_len;
5202 if (acl->cached == 0)
5203 goto out;
5204 ret = -ERANGE; /* see getxattr(2) man page */
5205 if (acl->len > buflen)
5206 goto out;
5207 memcpy(buf, acl->data, acl->len);
5208out_len:
5209 ret = acl->len;
5210out:
5211 spin_unlock(&inode->i_lock);
5212 return ret;
5213}
5214
Sachin Prabhu5794d212012-04-17 14:36:40 +01005215static 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 +00005216{
5217 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005218 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005219
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005220 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005221 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005222 if (acl == NULL)
5223 goto out;
5224 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005225 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005226 } else {
5227 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5228 if (acl == NULL)
5229 goto out;
5230 acl->cached = 0;
5231 }
5232 acl->len = acl_len;
5233out:
5234 nfs4_set_cached_acl(inode, acl);
5235}
5236
Andy Adamsonbf118a32011-12-07 11:55:27 -05005237/*
5238 * The getxattr API returns the required buffer length when called with a
5239 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5240 * the required buf. On a NULL buf, we send a page of data to the server
5241 * guessing that the ACL request can be serviced by a page. If so, we cache
5242 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5243 * the cache. If not so, we throw away the page, and cache the required
5244 * length. The next getxattr call will then produce another round trip to
5245 * the server, this time with the input buf of the required size.
5246 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005247static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005248{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005249 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005250 struct nfs_getaclargs args = {
5251 .fh = NFS_FH(inode),
5252 .acl_pages = pages,
5253 .acl_len = buflen,
5254 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005255 struct nfs_getaclres res = {
5256 .acl_len = buflen,
5257 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005258 struct rpc_message msg = {
5259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5260 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005261 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005262 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005263 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005264 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005265
Trond Myklebust21f498c2012-08-24 10:59:25 -04005266 if (npages > ARRAY_SIZE(pages))
5267 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005268
Andy Adamsonbf118a32011-12-07 11:55:27 -05005269 for (i = 0; i < npages; i++) {
5270 pages[i] = alloc_page(GFP_KERNEL);
5271 if (!pages[i])
5272 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005273 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005274
5275 /* for decoding across pages */
5276 res.acl_scratch = alloc_page(GFP_KERNEL);
5277 if (!res.acl_scratch)
5278 goto out_free;
5279
Andy Adamsonbf118a32011-12-07 11:55:27 -05005280 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005281
Peng Taode040be2012-01-10 22:42:47 +08005282 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005283 __func__, buf, buflen, npages, args.acl_len);
5284 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5285 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005286 if (ret)
5287 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005288
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005289 /* Handle the case where the passed-in buffer is too short */
5290 if (res.acl_flags & NFS4_ACL_TRUNC) {
5291 /* Did the user only issue a request for the acl length? */
5292 if (buf == NULL)
5293 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005294 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005295 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005296 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005297 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005298 if (buf) {
5299 if (res.acl_len > buflen) {
5300 ret = -ERANGE;
5301 goto out_free;
5302 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005303 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005304 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005305out_ok:
5306 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005307out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005308 for (i = 0; i < npages; i++)
5309 if (pages[i])
5310 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005311 if (res.acl_scratch)
5312 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005313 return ret;
5314}
5315
Trond Myklebust16b42892006-08-24 12:27:15 -04005316static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5317{
5318 struct nfs4_exception exception = { };
5319 ssize_t ret;
5320 do {
5321 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005322 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005323 if (ret >= 0)
5324 break;
5325 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5326 } while (exception.retry);
5327 return ret;
5328}
5329
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005330static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5331{
5332 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005333 int ret;
5334
5335 if (!nfs4_server_supports_acls(server))
5336 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005337 ret = nfs_revalidate_inode(server, inode);
5338 if (ret < 0)
5339 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005340 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5341 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005342 ret = nfs4_read_cached_acl(inode, buf, buflen);
5343 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005344 /* -ENOENT is returned if there is no ACL or if there is an ACL
5345 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005346 return ret;
5347 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005348}
5349
Trond Myklebust16b42892006-08-24 12:27:15 -04005350static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005351{
5352 struct nfs_server *server = NFS_SERVER(inode);
5353 struct page *pages[NFS4ACL_MAXPAGES];
5354 struct nfs_setaclargs arg = {
5355 .fh = NFS_FH(inode),
5356 .acl_pages = pages,
5357 .acl_len = buflen,
5358 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005359 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005360 struct rpc_message msg = {
5361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5362 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005363 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005364 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005365 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005366 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005367
5368 if (!nfs4_server_supports_acls(server))
5369 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005370 if (npages > ARRAY_SIZE(pages))
5371 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005372 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005373 if (i < 0)
5374 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005375 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005376 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005377
5378 /*
5379 * Free each page after tx, so the only ref left is
5380 * held by the network stack
5381 */
5382 for (; i > 0; i--)
5383 put_page(pages[i-1]);
5384
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005385 /*
5386 * Acl update can result in inode attribute update.
5387 * so mark the attribute cache invalid.
5388 */
5389 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005390 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5391 | NFS_INO_INVALID_CTIME;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005392 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005393 nfs_access_zap_cache(inode);
5394 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005395 return ret;
5396}
5397
Trond Myklebust16b42892006-08-24 12:27:15 -04005398static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5399{
5400 struct nfs4_exception exception = { };
5401 int err;
5402 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005403 err = __nfs4_proc_set_acl(inode, buf, buflen);
5404 trace_nfs4_set_acl(inode, err);
5405 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005406 &exception);
5407 } while (exception.retry);
5408 return err;
5409}
5410
David Quigleyaa9c2662013-05-22 12:50:44 -04005411#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5412static int _nfs4_get_security_label(struct inode *inode, void *buf,
5413 size_t buflen)
5414{
5415 struct nfs_server *server = NFS_SERVER(inode);
5416 struct nfs_fattr fattr;
5417 struct nfs4_label label = {0, 0, buflen, buf};
5418
5419 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005420 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005421 .fh = NFS_FH(inode),
5422 .bitmask = bitmask,
5423 };
5424 struct nfs4_getattr_res res = {
5425 .fattr = &fattr,
5426 .label = &label,
5427 .server = server,
5428 };
5429 struct rpc_message msg = {
5430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005431 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005432 .rpc_resp = &res,
5433 };
5434 int ret;
5435
5436 nfs_fattr_init(&fattr);
5437
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005438 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005439 if (ret)
5440 return ret;
5441 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5442 return -ENOENT;
5443 if (buflen < label.len)
5444 return -ERANGE;
5445 return 0;
5446}
5447
5448static int nfs4_get_security_label(struct inode *inode, void *buf,
5449 size_t buflen)
5450{
5451 struct nfs4_exception exception = { };
5452 int err;
5453
5454 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5455 return -EOPNOTSUPP;
5456
5457 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005458 err = _nfs4_get_security_label(inode, buf, buflen);
5459 trace_nfs4_get_security_label(inode, err);
5460 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005461 &exception);
5462 } while (exception.retry);
5463 return err;
5464}
5465
5466static int _nfs4_do_set_security_label(struct inode *inode,
5467 struct nfs4_label *ilabel,
5468 struct nfs_fattr *fattr,
5469 struct nfs4_label *olabel)
5470{
5471
5472 struct iattr sattr = {0};
5473 struct nfs_server *server = NFS_SERVER(inode);
5474 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005475 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005476 .fh = NFS_FH(inode),
5477 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005478 .server = server,
5479 .bitmask = bitmask,
5480 .label = ilabel,
5481 };
5482 struct nfs_setattrres res = {
5483 .fattr = fattr,
5484 .label = olabel,
5485 .server = server,
5486 };
5487 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5489 .rpc_argp = &arg,
5490 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005491 };
5492 int status;
5493
Jeff Layton12207f62013-11-01 10:49:32 -04005494 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005495
Jeff Layton12207f62013-11-01 10:49:32 -04005496 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005497 if (status)
5498 dprintk("%s failed: %d\n", __func__, status);
5499
5500 return status;
5501}
5502
5503static int nfs4_do_set_security_label(struct inode *inode,
5504 struct nfs4_label *ilabel,
5505 struct nfs_fattr *fattr,
5506 struct nfs4_label *olabel)
5507{
5508 struct nfs4_exception exception = { };
5509 int err;
5510
5511 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005512 err = _nfs4_do_set_security_label(inode, ilabel,
5513 fattr, olabel);
5514 trace_nfs4_set_security_label(inode, err);
5515 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005516 &exception);
5517 } while (exception.retry);
5518 return err;
5519}
5520
5521static int
Al Viro59301222016-05-27 10:19:30 -04005522nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005523{
5524 struct nfs4_label ilabel, *olabel = NULL;
5525 struct nfs_fattr fattr;
5526 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005527 int status;
5528
5529 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5530 return -EOPNOTSUPP;
5531
5532 nfs_fattr_init(&fattr);
5533
5534 ilabel.pi = 0;
5535 ilabel.lfs = 0;
5536 ilabel.label = (char *)buf;
5537 ilabel.len = buflen;
5538
5539 cred = rpc_lookup_cred();
5540 if (IS_ERR(cred))
5541 return PTR_ERR(cred);
5542
5543 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5544 if (IS_ERR(olabel)) {
5545 status = -PTR_ERR(olabel);
5546 goto out;
5547 }
5548
5549 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5550 if (status == 0)
5551 nfs_setsecurity(inode, &fattr, olabel);
5552
5553 nfs4_label_free(olabel);
5554out:
5555 put_rpccred(cred);
5556 return status;
5557}
5558#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5559
5560
Chuck Leverf0920752012-05-21 22:45:41 -04005561static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5562 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005563{
5564 __be32 verf[2];
5565
Chuck Lever2c820d92012-05-21 22:45:33 -04005566 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5567 /* An impossible timestamp guarantees this value
5568 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005569 verf[0] = cpu_to_be32(U32_MAX);
5570 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005571 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005572 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005573 u64 ns = ktime_to_ns(nn->boot_time);
5574
5575 verf[0] = cpu_to_be32(ns >> 32);
5576 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005577 }
Chuck Levercd937102012-03-02 17:14:31 -05005578 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5579}
5580
Jeff Laytona3192682015-06-09 19:43:59 -04005581static int
5582nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005583{
Jeff Laytona3192682015-06-09 19:43:59 -04005584 size_t len;
5585 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005586
Trond Myklebustceb3a162015-01-03 15:16:04 -05005587 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005588 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005589
Jeff Laytona3192682015-06-09 19:43:59 -04005590 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005591 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005592 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5593 1 +
5594 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5595 1;
5596 rcu_read_unlock();
5597
5598 if (len > NFS4_OPAQUE_LIMIT + 1)
5599 return -EINVAL;
5600
5601 /*
5602 * Since this string is allocated at mount time, and held until the
5603 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5604 * about a memory-reclaim deadlock.
5605 */
5606 str = kmalloc(len, GFP_KERNEL);
5607 if (!str)
5608 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005609
Chuck Levere984a552012-09-14 17:24:21 -04005610 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005611 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005612 clp->cl_ipaddr,
5613 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5614 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005615 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005616
Jeff Laytona3192682015-06-09 19:43:59 -04005617 clp->cl_owner_id = str;
5618 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005619}
5620
Jeff Layton873e3852015-06-09 19:44:00 -04005621static int
5622nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005623{
Jeff Layton873e3852015-06-09 19:44:00 -04005624 size_t len;
5625 char *str;
5626
5627 len = 10 + 10 + 1 + 10 + 1 +
5628 strlen(nfs4_client_id_uniquifier) + 1 +
5629 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5630
5631 if (len > NFS4_OPAQUE_LIMIT + 1)
5632 return -EINVAL;
5633
5634 /*
5635 * Since this string is allocated at mount time, and held until the
5636 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5637 * about a memory-reclaim deadlock.
5638 */
5639 str = kmalloc(len, GFP_KERNEL);
5640 if (!str)
5641 return -ENOMEM;
5642
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005643 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005644 clp->rpc_ops->version, clp->cl_minorversion,
5645 nfs4_client_id_uniquifier,
5646 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005647 clp->cl_owner_id = str;
5648 return 0;
5649}
5650
5651static int
5652nfs4_init_uniform_client_string(struct nfs_client *clp)
5653{
Jeff Layton873e3852015-06-09 19:44:00 -04005654 size_t len;
5655 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005656
5657 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005658 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005659
5660 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005661 return nfs4_init_uniquifier_client_string(clp);
5662
5663 len = 10 + 10 + 1 + 10 + 1 +
5664 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5665
5666 if (len > NFS4_OPAQUE_LIMIT + 1)
5667 return -EINVAL;
5668
5669 /*
5670 * Since this string is allocated at mount time, and held until the
5671 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5672 * about a memory-reclaim deadlock.
5673 */
5674 str = kmalloc(len, GFP_KERNEL);
5675 if (!str)
5676 return -ENOMEM;
5677
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005678 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005679 clp->rpc_ops->version, clp->cl_minorversion,
5680 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005681 clp->cl_owner_id = str;
5682 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005683}
5684
Chuck Lever706cb8d2014-03-12 12:51:47 -04005685/*
5686 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5687 * services. Advertise one based on the address family of the
5688 * clientaddr.
5689 */
5690static unsigned int
5691nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5692{
5693 if (strchr(clp->cl_ipaddr, ':') != NULL)
5694 return scnprintf(buf, len, "tcp6");
5695 else
5696 return scnprintf(buf, len, "tcp");
5697}
5698
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005699static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5700{
5701 struct nfs4_setclientid *sc = calldata;
5702
5703 if (task->tk_status == 0)
5704 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5705}
5706
5707static const struct rpc_call_ops nfs4_setclientid_ops = {
5708 .rpc_call_done = nfs4_setclientid_done,
5709};
5710
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005711/**
5712 * nfs4_proc_setclientid - Negotiate client ID
5713 * @clp: state data structure
5714 * @program: RPC program for NFSv4 callback service
5715 * @port: IP port number for NFS4 callback service
5716 * @cred: RPC credential to use for this call
5717 * @res: where to place the result
5718 *
5719 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5720 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005721int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5722 unsigned short port, struct rpc_cred *cred,
5723 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
5725 nfs4_verifier sc_verifier;
5726 struct nfs4_setclientid setclientid = {
5727 .sc_verifier = &sc_verifier,
5728 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005729 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 };
5731 struct rpc_message msg = {
5732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5733 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005734 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005735 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005737 struct rpc_task *task;
5738 struct rpc_task_setup task_setup_data = {
5739 .rpc_client = clp->cl_rpcclient,
5740 .rpc_message = &msg,
5741 .callback_ops = &nfs4_setclientid_ops,
5742 .callback_data = &setclientid,
5743 .flags = RPC_TASK_TIMEOUT,
5744 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005745 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Chuck Leverde734832012-07-11 16:30:50 -04005747 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005748 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005749
5750 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5751 status = nfs4_init_uniform_client_string(clp);
5752 else
Jeff Laytona3192682015-06-09 19:43:59 -04005753 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005754
5755 if (status)
5756 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005757
Chuck Leverde734832012-07-11 16:30:50 -04005758 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005759 setclientid.sc_netid_len =
5760 nfs4_init_callback_netid(clp,
5761 setclientid.sc_netid,
5762 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005763 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005764 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 clp->cl_ipaddr, port >> 8, port & 255);
5766
Jeff Layton3a6bb732015-06-09 19:43:57 -04005767 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005768 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005769 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005770 task = rpc_run_task(&task_setup_data);
5771 if (IS_ERR(task)) {
5772 status = PTR_ERR(task);
5773 goto out;
5774 }
5775 status = task->tk_status;
5776 if (setclientid.sc_cred) {
5777 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5778 put_rpccred(setclientid.sc_cred);
5779 }
5780 rpc_put_task(task);
5781out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005782 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005783 dprintk("NFS reply setclientid: %d\n", status);
5784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785}
5786
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005787/**
5788 * nfs4_proc_setclientid_confirm - Confirm client ID
5789 * @clp: state data structure
5790 * @res: result of a previous SETCLIENTID
5791 * @cred: RPC credential to use for this call
5792 *
5793 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5794 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005795int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005796 struct nfs4_setclientid_res *arg,
5797 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 struct rpc_message msg = {
5800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005801 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005802 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 int status;
5805
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005806 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5807 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5808 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005809 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005810 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005811 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 return status;
5813}
5814
Trond Myklebustfe650402006-01-03 09:55:18 +01005815struct nfs4_delegreturndata {
5816 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005817 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005818 struct nfs_fh fh;
5819 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005820 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005821 struct {
5822 struct nfs4_layoutreturn_args arg;
5823 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005824 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005825 u32 roc_barrier;
5826 bool roc;
5827 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005828 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005829 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005830 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005831};
5832
Trond Myklebustfe650402006-01-03 09:55:18 +01005833static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5834{
5835 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005836 struct nfs4_exception exception = {
5837 .inode = data->inode,
5838 .stateid = &data->stateid,
5839 };
Andy Adamson938e1012009-04-01 09:22:28 -04005840
Trond Myklebust14516c32010-07-31 14:29:06 -04005841 if (!nfs4_sequence_done(task, &data->res.seq_res))
5842 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005843
Trond Myklebustca8acf82013-08-13 10:36:56 -04005844 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005845
5846 /* Handle Layoutreturn errors */
5847 if (data->args.lr_args && task->tk_status != 0) {
5848 switch(data->res.lr_ret) {
5849 default:
5850 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5851 break;
5852 case 0:
5853 data->args.lr_args = NULL;
5854 data->res.lr_res = NULL;
5855 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005856 case -NFS4ERR_OLD_STATEID:
5857 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005858 data->inode))
5859 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005860 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005861 case -NFS4ERR_ADMIN_REVOKED:
5862 case -NFS4ERR_DELEG_REVOKED:
5863 case -NFS4ERR_EXPIRED:
5864 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005865 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5866 case -NFS4ERR_WRONG_CRED:
5867 data->args.lr_args = NULL;
5868 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005869 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005870 }
5871 }
5872
Ricardo Labiaga79708862009-12-07 09:23:21 -05005873 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005874 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005875 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005876 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005877 case -NFS4ERR_ADMIN_REVOKED:
5878 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005879 case -NFS4ERR_EXPIRED:
5880 nfs4_free_revoked_stateid(data->res.server,
5881 data->args.stateid,
5882 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005883 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005884 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005885 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005886 task->tk_status = 0;
5887 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005888 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005889 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5890 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005891 task->tk_status = 0;
5892 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005893 case -NFS4ERR_ACCESS:
5894 if (data->args.bitmask) {
5895 data->args.bitmask = NULL;
5896 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005897 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005898 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005899 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005900 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005901 task->tk_status = nfs4_async_handle_exception(task,
5902 data->res.server, task->tk_status,
5903 &exception);
5904 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005905 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005906 }
5907 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005908 return;
5909lr_restart:
5910 data->res.lr_ret = 0;
5911out_restart:
5912 task->tk_status = 0;
5913 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005914}
5915
5916static void nfs4_delegreturn_release(void *calldata)
5917{
Peng Tao039b7562014-07-03 13:05:02 +08005918 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005919 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005920
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005921 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005922 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005923 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5924 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005925 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005926 nfs_iput_and_deactive(inode);
5927 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005928 kfree(calldata);
5929}
5930
Andy Adamson938e1012009-04-01 09:22:28 -04005931static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5932{
5933 struct nfs4_delegreturndata *d_data;
5934
5935 d_data = (struct nfs4_delegreturndata *)data;
5936
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005937 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005938 return;
5939
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005940 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005941 &d_data->args.seq_args,
5942 &d_data->res.seq_res,
5943 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005944}
Andy Adamson938e1012009-04-01 09:22:28 -04005945
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005946static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005947 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005948 .rpc_call_done = nfs4_delegreturn_done,
5949 .rpc_release = nfs4_delegreturn_release,
5950};
5951
Trond Myklebuste6f81072008-01-24 18:14:34 -05005952static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005953{
5954 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005955 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005956 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005957 struct rpc_message msg = {
5958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5959 .rpc_cred = cred,
5960 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005961 struct rpc_task_setup task_setup_data = {
5962 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005963 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005964 .callback_ops = &nfs4_delegreturn_ops,
5965 .flags = RPC_TASK_ASYNC,
5966 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005967 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005968
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005969 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005970 if (data == NULL)
5971 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04005972 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005973
5974 nfs4_state_protect(server->nfs_client,
5975 NFS_SP4_MACH_CRED_CLEANUP,
5976 &task_setup_data.rpc_client, &msg);
5977
Trond Myklebustfe650402006-01-03 09:55:18 +01005978 data->args.fhandle = &data->fh;
5979 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005980 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005981 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005982 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005983 data->res.fattr = &data->fattr;
5984 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005985 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005986 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005987 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005988 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005989 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005990 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005991 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005992 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005993 if (data->lr.roc) {
5994 data->args.lr_args = &data->lr.arg;
5995 data->res.lr_res = &data->lr.res;
5996 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005997 } else if (data->lr.roc) {
5998 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5999 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006000 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006001
Trond Myklebustc970aa82007-07-14 15:39:59 -04006002 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006003 msg.rpc_argp = &data->args;
6004 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006005 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006006 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006007 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006008 if (!issync)
6009 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006010 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006011 if (status != 0)
6012 goto out;
6013 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006014out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006015 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006016 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
6018
Trond Myklebuste6f81072008-01-24 18:14:34 -05006019int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
6021 struct nfs_server *server = NFS_SERVER(inode);
6022 struct nfs4_exception exception = { };
6023 int err;
6024 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006025 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006026 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 switch (err) {
6028 case -NFS4ERR_STALE_STATEID:
6029 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 case 0:
6031 return 0;
6032 }
6033 err = nfs4_handle_exception(server, err, &exception);
6034 } while (exception.retry);
6035 return err;
6036}
6037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6039{
6040 struct inode *inode = state->inode;
6041 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006042 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006043 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006045 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006047 struct nfs_lockt_res res = {
6048 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 };
6050 struct rpc_message msg = {
6051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006052 .rpc_argp = &arg,
6053 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 .rpc_cred = state->owner->so_cred,
6055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 struct nfs4_lock_state *lsp;
6057 int status;
6058
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006059 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006060 status = nfs4_set_lock_state(state, request);
6061 if (status != 0)
6062 goto out;
6063 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006064 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006065 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006066 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006067 switch (status) {
6068 case 0:
6069 request->fl_type = F_UNLCK;
6070 break;
6071 case -NFS4ERR_DENIED:
6072 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006074 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006075 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006076out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 return status;
6078}
6079
6080static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6081{
6082 struct nfs4_exception exception = { };
6083 int err;
6084
6085 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006086 err = _nfs4_proc_getlk(state, cmd, request);
6087 trace_nfs4_get_lock(request, state, cmd, err);
6088 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 &exception);
6090 } while (exception.retry);
6091 return err;
6092}
6093
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006094struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006095 struct nfs_locku_args arg;
6096 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006097 struct nfs4_lock_state *lsp;
6098 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006099 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006101 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006102 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006103};
6104
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006105static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6106 struct nfs_open_context *ctx,
6107 struct nfs4_lock_state *lsp,
6108 struct nfs_seqid *seqid)
6109{
6110 struct nfs4_unlockdata *p;
6111 struct inode *inode = lsp->ls_state->inode;
6112
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006113 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006114 if (p == NULL)
6115 return NULL;
6116 p->arg.fh = NFS_FH(inode);
6117 p->arg.fl = &p->fl;
6118 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006119 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006120 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006121 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006122 /* Ensure we don't close file until we're done freeing locks! */
6123 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006124 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006125 memcpy(&p->fl, fl, sizeof(p->fl));
6126 p->server = NFS_SERVER(inode);
6127 return p;
6128}
6129
Trond Myklebust06f814a2006-01-03 09:55:07 +01006130static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006131{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006132 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006133 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006134 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006135 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006136 put_nfs_open_context(calldata->ctx);
6137 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006138}
6139
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006140static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006141{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006142 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006143 struct nfs4_exception exception = {
6144 .inode = calldata->lsp->ls_state->inode,
6145 .stateid = &calldata->arg.stateid,
6146 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006147
Trond Myklebust14516c32010-07-31 14:29:06 -04006148 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6149 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006150 switch (task->tk_status) {
6151 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006152 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006153 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006154 if (nfs4_update_lock_stateid(calldata->lsp,
6155 &calldata->res.stateid))
6156 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006157 case -NFS4ERR_ADMIN_REVOKED:
6158 case -NFS4ERR_EXPIRED:
6159 nfs4_free_revoked_stateid(calldata->server,
6160 &calldata->arg.stateid,
6161 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006162 case -NFS4ERR_BAD_STATEID:
6163 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006164 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006165 if (!nfs4_stateid_match(&calldata->arg.stateid,
6166 &calldata->lsp->ls_stateid))
6167 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006168 break;
6169 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006170 task->tk_status = nfs4_async_handle_exception(task,
6171 calldata->server, task->tk_status,
6172 &exception);
6173 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006174 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006175 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006176 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006177}
6178
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006179static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006180{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006181 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006183 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6184 nfs_async_iocounter_wait(task, calldata->l_ctx))
6185 return;
6186
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006187 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006188 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006189 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006190 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006191 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006192 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006193 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006194 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006195 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006196 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006197 &calldata->res.seq_res,
6198 task) != 0)
6199 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006200 return;
6201out_no_action:
6202 task->tk_action = NULL;
6203out_wait:
6204 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006207static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006208 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006209 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006210 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006211};
6212
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006213static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6214 struct nfs_open_context *ctx,
6215 struct nfs4_lock_state *lsp,
6216 struct nfs_seqid *seqid)
6217{
6218 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006219 struct rpc_message msg = {
6220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6221 .rpc_cred = ctx->cred,
6222 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006223 struct rpc_task_setup task_setup_data = {
6224 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006225 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006226 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006227 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006228 .flags = RPC_TASK_ASYNC,
6229 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006230
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006231 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6232 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6233
Frank Filz137d6ac2007-07-09 15:32:29 -07006234 /* Ensure this is an unlock - when canceling a lock, the
6235 * canceled lock is passed in, and it won't be an unlock.
6236 */
6237 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006238 if (fl->fl_flags & FL_CLOSE)
6239 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006240
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6242 if (data == NULL) {
6243 nfs_free_seqid(seqid);
6244 return ERR_PTR(-ENOMEM);
6245 }
6246
Anna Schumakerfba83f32018-05-04 16:22:50 -04006247 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006248 msg.rpc_argp = &data->arg;
6249 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006250 task_setup_data.callback_data = data;
6251 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006252}
6253
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6255{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006256 struct inode *inode = state->inode;
6257 struct nfs4_state_owner *sp = state->owner;
6258 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006259 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006260 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006261 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006262 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006263 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006264 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
Trond Myklebust9b073572006-06-29 16:38:34 -04006266 status = nfs4_set_lock_state(state, request);
6267 /* Unlock _before_ we do the RPC call */
6268 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006269 /* Exclude nfs_delegation_claim_locks() */
6270 mutex_lock(&sp->so_delegreturn_mutex);
6271 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006272 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006273 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006274 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006275 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006276 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006277 }
6278 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006279 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006280 if (status != 0)
6281 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006282 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006283 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006284 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6285 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006286 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6287 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006288 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006289 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006290 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006291 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006292 status = PTR_ERR(task);
6293 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006294 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006295 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006296 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006297out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006298 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006299 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006300 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301}
6302
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006303struct nfs4_lockdata {
6304 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006305 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006306 struct nfs4_lock_state *lsp;
6307 struct nfs_open_context *ctx;
6308 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006309 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006310 int rpc_status;
6311 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006312 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006313};
6314
6315static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006316 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6317 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006318{
6319 struct nfs4_lockdata *p;
6320 struct inode *inode = lsp->ls_state->inode;
6321 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006322 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006323
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006324 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006325 if (p == NULL)
6326 return NULL;
6327
6328 p->arg.fh = NFS_FH(inode);
6329 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006330 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006331 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006332 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006333 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6334 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006335 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006336 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006337 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006338 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006339 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006340 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006341 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006342 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006343 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006344 p->ctx = get_nfs_open_context(ctx);
6345 memcpy(&p->fl, fl, sizeof(p->fl));
6346 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006347out_free_seqid:
6348 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006349out_free:
6350 kfree(p);
6351 return NULL;
6352}
6353
6354static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6355{
6356 struct nfs4_lockdata *data = calldata;
6357 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Harvey Harrison3110ff82008-05-02 13:42:44 -07006359 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006360 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006361 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006362 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006363 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006364 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006365 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006366 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006367 nfs4_stateid_copy(&data->arg.open_stateid,
6368 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006369 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006370 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006371 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006372 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006373 nfs4_stateid_copy(&data->arg.lock_stateid,
6374 &data->lsp->ls_stateid);
6375 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006376 if (!nfs4_valid_open_stateid(state)) {
6377 data->rpc_status = -EBADF;
6378 task->tk_action = NULL;
6379 goto out_release_open_seqid;
6380 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006381 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006382 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006383 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006384 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006385 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006386 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006387out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006388 nfs_release_seqid(data->arg.open_seqid);
6389out_release_lock_seqid:
6390 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006391out_wait:
6392 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006393 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006394}
6395
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006396static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6397{
6398 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006399 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006400
Harvey Harrison3110ff82008-05-02 13:42:44 -07006401 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006402
Trond Myklebust14516c32010-07-31 14:29:06 -04006403 if (!nfs4_sequence_done(task, &data->res.seq_res))
6404 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006405
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006406 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006407 switch (task->tk_status) {
6408 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006409 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006410 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006411 if (data->arg.new_lock) {
6412 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006413 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006414 rpc_restart_call_prepare(task);
6415 break;
6416 }
6417 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006418 if (data->arg.new_lock_owner != 0) {
6419 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6420 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6421 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6422 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6423 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006424 break;
6425 case -NFS4ERR_BAD_STATEID:
6426 case -NFS4ERR_OLD_STATEID:
6427 case -NFS4ERR_STALE_STATEID:
6428 case -NFS4ERR_EXPIRED:
6429 if (data->arg.new_lock_owner != 0) {
6430 if (!nfs4_stateid_match(&data->arg.open_stateid,
6431 &lsp->ls_state->open_stateid))
6432 rpc_restart_call_prepare(task);
6433 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6434 &lsp->ls_stateid))
6435 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006436 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006437 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006438}
6439
6440static void nfs4_lock_release(void *calldata)
6441{
6442 struct nfs4_lockdata *data = calldata;
6443
Harvey Harrison3110ff82008-05-02 13:42:44 -07006444 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006445 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006446 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006447 struct rpc_task *task;
6448 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6449 data->arg.lock_seqid);
6450 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006451 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006452 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006453 } else
6454 nfs_free_seqid(data->arg.lock_seqid);
6455 nfs4_put_lock_state(data->lsp);
6456 put_nfs_open_context(data->ctx);
6457 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006458 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006459}
6460
6461static const struct rpc_call_ops nfs4_lock_ops = {
6462 .rpc_call_prepare = nfs4_lock_prepare,
6463 .rpc_call_done = nfs4_lock_done,
6464 .rpc_release = nfs4_lock_release,
6465};
6466
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006467static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6468{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006469 switch (error) {
6470 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006471 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006472 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006473 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006474 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006475 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006476 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006477 break;
6478 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006479 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006480 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006481 };
6482}
6483
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006484static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006485{
6486 struct nfs4_lockdata *data;
6487 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006488 struct rpc_message msg = {
6489 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6490 .rpc_cred = state->owner->so_cred,
6491 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006492 struct rpc_task_setup task_setup_data = {
6493 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006494 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006495 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006496 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006497 .flags = RPC_TASK_ASYNC,
6498 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006499 int ret;
6500
Harvey Harrison3110ff82008-05-02 13:42:44 -07006501 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006502 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006503 fl->fl_u.nfs4_fl.owner,
6504 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006505 if (data == NULL)
6506 return -ENOMEM;
6507 if (IS_SETLKW(cmd))
6508 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006509 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6510 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006511 msg.rpc_argp = &data->arg;
6512 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006513 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006514 if (recovery_type > NFS_LOCK_NEW) {
6515 if (recovery_type == NFS_LOCK_RECLAIM)
6516 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006517 } else
6518 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006519 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006520 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006521 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006522 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006523 if (ret == 0) {
6524 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006525 if (ret)
6526 nfs4_handle_setlk_error(data->server, data->lsp,
6527 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006528 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006529 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006530 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006531 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006532 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006533 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006534}
6535
6536static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6537{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006538 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006539 struct nfs4_exception exception = {
6540 .inode = state->inode,
6541 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006542 int err;
6543
6544 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006545 /* Cache the lock if possible... */
6546 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6547 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006548 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006549 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006550 break;
6551 nfs4_handle_exception(server, err, &exception);
6552 } while (exception.retry);
6553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554}
6555
6556static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6557{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006558 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006559 struct nfs4_exception exception = {
6560 .inode = state->inode,
6561 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006562 int err;
6563
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006564 err = nfs4_set_lock_state(state, request);
6565 if (err != 0)
6566 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006567 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006568 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6569 return 0;
6570 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006571 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006572 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6573 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006574 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006575 switch (err) {
6576 default:
6577 goto out;
6578 case -NFS4ERR_GRACE:
6579 case -NFS4ERR_DELAY:
6580 nfs4_handle_exception(server, err, &exception);
6581 err = 0;
6582 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006583 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006584out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006585 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006586}
6587
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006588#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006589static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6590{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006591 struct nfs4_lock_state *lsp;
6592 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006593
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006594 status = nfs4_set_lock_state(state, request);
6595 if (status != 0)
6596 return status;
6597 lsp = request->fl_u.nfs4_fl.owner;
6598 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6599 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6600 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006601 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006602}
6603#endif
6604
Linus Torvalds1da177e2005-04-16 15:20:36 -07006605static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6606{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006607 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006608 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006609 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006610 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611
Trond Myklebust01c3b862006-06-29 16:38:39 -04006612 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006613 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006614 if (status < 0)
6615 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006616 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006617 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006618 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006619 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006620 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006621 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006622 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006623 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006624 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006625 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006626 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006627 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006628 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006629 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006630out:
6631 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006632 return status;
6633}
6634
6635static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6636{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006637 struct nfs4_exception exception = {
6638 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006639 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006640 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006641 int err;
6642
6643 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006644 err = _nfs4_proc_setlk(state, cmd, request);
6645 if (err == -NFS4ERR_DENIED)
6646 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006647 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006648 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 } while (exception.retry);
6650 return err;
6651}
6652
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006653#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6654#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6655
6656static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006657nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6658 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006659{
6660 int status = -ERESTARTSYS;
6661 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6662
6663 while(!signalled()) {
6664 status = nfs4_proc_setlk(state, cmd, request);
6665 if ((status != -EAGAIN) || IS_SETLK(cmd))
6666 break;
6667 freezable_schedule_timeout_interruptible(timeout);
6668 timeout *= 2;
6669 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6670 status = -ERESTARTSYS;
6671 }
6672 return status;
6673}
6674
Jeff Laytona1d617d82016-09-17 18:17:39 -04006675#ifdef CONFIG_NFS_V4_1
6676struct nfs4_lock_waiter {
6677 struct task_struct *task;
6678 struct inode *inode;
6679 struct nfs_lowner *owner;
6680 bool notified;
6681};
6682
6683static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006684nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006685{
6686 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006687 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006688
Jeff Layton57174592018-03-18 08:37:03 -04006689 /* NULL key means to wake up everyone */
6690 if (key) {
6691 struct cb_notify_lock_args *cbnl = key;
6692 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6693 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006694
Jeff Layton57174592018-03-18 08:37:03 -04006695 /* Only wake if the callback was for the same owner. */
6696 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6697 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006698
Jeff Layton57174592018-03-18 08:37:03 -04006699 /* Make sure it's for the right inode */
6700 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6701 return 0;
6702
6703 waiter->notified = true;
6704 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006705
6706 /* override "private" so we can use default_wake_function */
6707 wait->private = waiter->task;
6708 ret = autoremove_wake_function(wait, mode, flags, key);
6709 wait->private = waiter;
6710 return ret;
6711}
6712
6713static int
6714nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6715{
6716 int status = -ERESTARTSYS;
6717 unsigned long flags;
6718 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6719 struct nfs_server *server = NFS_SERVER(state->inode);
6720 struct nfs_client *clp = server->nfs_client;
6721 wait_queue_head_t *q = &clp->cl_lock_waitq;
6722 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6723 .id = lsp->ls_seqid.owner_id,
6724 .s_dev = server->s_dev };
6725 struct nfs4_lock_waiter waiter = { .task = current,
6726 .inode = state->inode,
6727 .owner = &owner,
6728 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006729 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006730
6731 /* Don't bother with waitqueue if we don't expect a callback */
6732 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6733 return nfs4_retry_setlk_simple(state, cmd, request);
6734
6735 init_wait(&wait);
6736 wait.private = &waiter;
6737 wait.func = nfs4_wake_lock_waiter;
6738 add_wait_queue(q, &wait);
6739
6740 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006741 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006742 status = nfs4_proc_setlk(state, cmd, request);
6743 if ((status != -EAGAIN) || IS_SETLK(cmd))
6744 break;
6745
6746 status = -ERESTARTSYS;
6747 spin_lock_irqsave(&q->lock, flags);
6748 if (waiter.notified) {
6749 spin_unlock_irqrestore(&q->lock, flags);
6750 continue;
6751 }
6752 set_current_state(TASK_INTERRUPTIBLE);
6753 spin_unlock_irqrestore(&q->lock, flags);
6754
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006755 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006756 }
6757
6758 finish_wait(q, &wait);
6759 return status;
6760}
6761#else /* !CONFIG_NFS_V4_1 */
6762static inline int
6763nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6764{
6765 return nfs4_retry_setlk_simple(state, cmd, request);
6766}
6767#endif
6768
Linus Torvalds1da177e2005-04-16 15:20:36 -07006769static int
6770nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6771{
6772 struct nfs_open_context *ctx;
6773 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774 int status;
6775
6776 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006777 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 state = ctx->state;
6779
Trond Myklebustd9531262009-07-21 19:22:38 -04006780 if (IS_GETLK(cmd)) {
6781 if (state != NULL)
6782 return nfs4_proc_getlk(state, F_GETLK, request);
6783 return 0;
6784 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006785
6786 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6787 return -EINVAL;
6788
Trond Myklebustd9531262009-07-21 19:22:38 -04006789 if (request->fl_type == F_UNLCK) {
6790 if (state != NULL)
6791 return nfs4_proc_unlck(state, cmd, request);
6792 return 0;
6793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006794
Trond Myklebustd9531262009-07-21 19:22:38 -04006795 if (state == NULL)
6796 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006797
6798 if ((request->fl_flags & FL_POSIX) &&
6799 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6800 return -ENOLCK;
6801
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006802 /*
6803 * Don't rely on the VFS having checked the file open mode,
6804 * since it won't do this for flock() locks.
6805 */
6806 switch (request->fl_type) {
6807 case F_RDLCK:
6808 if (!(filp->f_mode & FMODE_READ))
6809 return -EBADF;
6810 break;
6811 case F_WRLCK:
6812 if (!(filp->f_mode & FMODE_WRITE))
6813 return -EBADF;
6814 }
6815
Jeff Layton1ea67db2016-09-17 18:17:37 -04006816 status = nfs4_set_lock_state(state, request);
6817 if (status != 0)
6818 return status;
6819
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006820 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006821}
6822
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006823int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006824{
6825 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006826 int err;
6827
6828 err = nfs4_set_lock_state(state, fl);
6829 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006830 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006831 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006832 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006833}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006834
Trond Myklebustcf470c32012-03-07 13:49:12 -05006835struct nfs_release_lockowner_data {
6836 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006837 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006838 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006839 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006840 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006841};
6842
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006843static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6844{
6845 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006846 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006847 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6848 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006849 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006850 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006851}
6852
6853static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6854{
6855 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006856 struct nfs_server *server = data->server;
6857
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006858 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006859
6860 switch (task->tk_status) {
6861 case 0:
6862 renew_lease(server, data->timestamp);
6863 break;
6864 case -NFS4ERR_STALE_CLIENTID:
6865 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006866 nfs4_schedule_lease_recovery(server->nfs_client);
6867 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006868 case -NFS4ERR_LEASE_MOVED:
6869 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006870 if (nfs4_async_handle_error(task, server,
6871 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006872 rpc_restart_call_prepare(task);
6873 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006874}
6875
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006876static void nfs4_release_lockowner_release(void *calldata)
6877{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006878 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006879 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006880 kfree(calldata);
6881}
6882
Trond Myklebust17280172012-03-11 13:11:00 -04006883static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006884 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6885 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006886 .rpc_release = nfs4_release_lockowner_release,
6887};
6888
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006889static void
6890nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006891{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006892 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006893 struct rpc_message msg = {
6894 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6895 };
6896
6897 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006898 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006899
Trond Myklebustcf470c32012-03-07 13:49:12 -05006900 data = kmalloc(sizeof(*data), GFP_NOFS);
6901 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006902 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006903 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006904 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006905 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6906 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6907 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006908
Trond Myklebustcf470c32012-03-07 13:49:12 -05006909 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006910 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006911 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006912 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006913}
6914
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006915#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6916
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006917static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006918 struct dentry *unused, struct inode *inode,
6919 const char *key, const void *buf,
6920 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006921{
Al Viro59301222016-05-27 10:19:30 -04006922 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006923}
6924
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006925static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006926 struct dentry *unused, struct inode *inode,
6927 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006928{
Al Virob2968212016-04-10 20:48:24 -04006929 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006930}
6931
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006932static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006933{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006934 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006935}
6936
David Quigleyc9bccef2013-05-22 12:50:45 -04006937#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006938
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006939static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006940 struct dentry *unused, struct inode *inode,
6941 const char *key, const void *buf,
6942 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006943{
6944 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006945 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006946
6947 return -EOPNOTSUPP;
6948}
6949
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006950static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006951 struct dentry *unused, struct inode *inode,
6952 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006953{
6954 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006955 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006956 return -EOPNOTSUPP;
6957}
6958
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006959static ssize_t
6960nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006961{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006962 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006963
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006964 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6965 len = security_inode_listsecurity(inode, list, list_len);
6966 if (list_len && len > list_len)
6967 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006968 }
6969 return len;
6970}
6971
6972static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6973 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006974 .get = nfs4_xattr_get_nfs4_label,
6975 .set = nfs4_xattr_set_nfs4_label,
6976};
David Quigleyc9bccef2013-05-22 12:50:45 -04006977
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006978#else
6979
6980static ssize_t
6981nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6982{
6983 return 0;
6984}
6985
6986#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006987
Andy Adamson533eb462011-06-13 18:25:56 -04006988/*
6989 * nfs_fhget will use either the mounted_on_fileid or the fileid
6990 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006991static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6992{
Andy Adamson533eb462011-06-13 18:25:56 -04006993 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6994 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6995 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006996 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006997 return;
6998
6999 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007000 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007001 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7002 fattr->nlink = 2;
7003}
7004
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007005static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7006 const struct qstr *name,
7007 struct nfs4_fs_locations *fs_locations,
7008 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007009{
7010 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007011 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007012 struct nfs4_fs_locations_arg args = {
7013 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007014 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007015 .page = page,
7016 .bitmask = bitmask,
7017 };
Benny Halevy22958462009-04-01 09:22:02 -04007018 struct nfs4_fs_locations_res res = {
7019 .fs_locations = fs_locations,
7020 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007021 struct rpc_message msg = {
7022 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7023 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007024 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007025 };
7026 int status;
7027
Harvey Harrison3110ff82008-05-02 13:42:44 -07007028 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007029
Chuck Leverc05cefc2017-11-05 15:45:22 -05007030 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7031 bitmask[1] = nfs4_fattr_bitmap[1];
7032
Andy Adamson533eb462011-06-13 18:25:56 -04007033 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7034 * is not supported */
7035 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007036 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007037 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007038 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007039
Trond Myklebustc228fd32007-01-13 02:28:11 -05007040 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007041 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007042 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007043 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007044 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007045 return status;
7046}
7047
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007048int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7049 const struct qstr *name,
7050 struct nfs4_fs_locations *fs_locations,
7051 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007052{
7053 struct nfs4_exception exception = { };
7054 int err;
7055 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007056 err = _nfs4_proc_fs_locations(client, dir, name,
7057 fs_locations, page);
7058 trace_nfs4_get_fs_locations(dir, name, err);
7059 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007060 &exception);
7061 } while (exception.retry);
7062 return err;
7063}
7064
Chuck Leverb03d7352013-10-17 14:12:50 -04007065/*
7066 * This operation also signals the server that this client is
7067 * performing migration recovery. The server can stop returning
7068 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7069 * appended to this compound to identify the client ID which is
7070 * performing recovery.
7071 */
7072static int _nfs40_proc_get_locations(struct inode *inode,
7073 struct nfs4_fs_locations *locations,
7074 struct page *page, struct rpc_cred *cred)
7075{
7076 struct nfs_server *server = NFS_SERVER(inode);
7077 struct rpc_clnt *clnt = server->client;
7078 u32 bitmask[2] = {
7079 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7080 };
7081 struct nfs4_fs_locations_arg args = {
7082 .clientid = server->nfs_client->cl_clientid,
7083 .fh = NFS_FH(inode),
7084 .page = page,
7085 .bitmask = bitmask,
7086 .migration = 1, /* skip LOOKUP */
7087 .renew = 1, /* append RENEW */
7088 };
7089 struct nfs4_fs_locations_res res = {
7090 .fs_locations = locations,
7091 .migration = 1,
7092 .renew = 1,
7093 };
7094 struct rpc_message msg = {
7095 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7096 .rpc_argp = &args,
7097 .rpc_resp = &res,
7098 .rpc_cred = cred,
7099 };
7100 unsigned long now = jiffies;
7101 int status;
7102
7103 nfs_fattr_init(&locations->fattr);
7104 locations->server = server;
7105 locations->nlocations = 0;
7106
Anna Schumakerfba83f32018-05-04 16:22:50 -04007107 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007108 status = nfs4_call_sync_sequence(clnt, server, &msg,
7109 &args.seq_args, &res.seq_res);
7110 if (status)
7111 return status;
7112
7113 renew_lease(server, now);
7114 return 0;
7115}
7116
7117#ifdef CONFIG_NFS_V4_1
7118
7119/*
7120 * This operation also signals the server that this client is
7121 * performing migration recovery. The server can stop asserting
7122 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7123 * performing this operation is identified in the SEQUENCE
7124 * operation in this compound.
7125 *
7126 * When the client supports GETATTR(fs_locations_info), it can
7127 * be plumbed in here.
7128 */
7129static int _nfs41_proc_get_locations(struct inode *inode,
7130 struct nfs4_fs_locations *locations,
7131 struct page *page, struct rpc_cred *cred)
7132{
7133 struct nfs_server *server = NFS_SERVER(inode);
7134 struct rpc_clnt *clnt = server->client;
7135 u32 bitmask[2] = {
7136 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7137 };
7138 struct nfs4_fs_locations_arg args = {
7139 .fh = NFS_FH(inode),
7140 .page = page,
7141 .bitmask = bitmask,
7142 .migration = 1, /* skip LOOKUP */
7143 };
7144 struct nfs4_fs_locations_res res = {
7145 .fs_locations = locations,
7146 .migration = 1,
7147 };
7148 struct rpc_message msg = {
7149 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7150 .rpc_argp = &args,
7151 .rpc_resp = &res,
7152 .rpc_cred = cred,
7153 };
7154 int status;
7155
7156 nfs_fattr_init(&locations->fattr);
7157 locations->server = server;
7158 locations->nlocations = 0;
7159
Anna Schumakerfba83f32018-05-04 16:22:50 -04007160 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007161 status = nfs4_call_sync_sequence(clnt, server, &msg,
7162 &args.seq_args, &res.seq_res);
7163 if (status == NFS4_OK &&
7164 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7165 status = -NFS4ERR_LEASE_MOVED;
7166 return status;
7167}
7168
7169#endif /* CONFIG_NFS_V4_1 */
7170
7171/**
7172 * nfs4_proc_get_locations - discover locations for a migrated FSID
7173 * @inode: inode on FSID that is migrating
7174 * @locations: result of query
7175 * @page: buffer
7176 * @cred: credential to use for this operation
7177 *
7178 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7179 * operation failed, or a negative errno if a local error occurred.
7180 *
7181 * On success, "locations" is filled in, but if the server has
7182 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7183 * asserted.
7184 *
7185 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7186 * from this client that require migration recovery.
7187 */
7188int nfs4_proc_get_locations(struct inode *inode,
7189 struct nfs4_fs_locations *locations,
7190 struct page *page, struct rpc_cred *cred)
7191{
7192 struct nfs_server *server = NFS_SERVER(inode);
7193 struct nfs_client *clp = server->nfs_client;
7194 const struct nfs4_mig_recovery_ops *ops =
7195 clp->cl_mvops->mig_recovery_ops;
7196 struct nfs4_exception exception = { };
7197 int status;
7198
7199 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7200 (unsigned long long)server->fsid.major,
7201 (unsigned long long)server->fsid.minor,
7202 clp->cl_hostname);
7203 nfs_display_fhandle(NFS_FH(inode), __func__);
7204
7205 do {
7206 status = ops->get_locations(inode, locations, page, cred);
7207 if (status != -NFS4ERR_DELAY)
7208 break;
7209 nfs4_handle_exception(server, status, &exception);
7210 } while (exception.retry);
7211 return status;
7212}
7213
Chuck Lever44c99932013-10-17 14:13:30 -04007214/*
7215 * This operation also signals the server that this client is
7216 * performing "lease moved" recovery. The server can stop
7217 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7218 * is appended to this compound to identify the client ID which is
7219 * performing recovery.
7220 */
7221static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7222{
7223 struct nfs_server *server = NFS_SERVER(inode);
7224 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7225 struct rpc_clnt *clnt = server->client;
7226 struct nfs4_fsid_present_arg args = {
7227 .fh = NFS_FH(inode),
7228 .clientid = clp->cl_clientid,
7229 .renew = 1, /* append RENEW */
7230 };
7231 struct nfs4_fsid_present_res res = {
7232 .renew = 1,
7233 };
7234 struct rpc_message msg = {
7235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7236 .rpc_argp = &args,
7237 .rpc_resp = &res,
7238 .rpc_cred = cred,
7239 };
7240 unsigned long now = jiffies;
7241 int status;
7242
7243 res.fh = nfs_alloc_fhandle();
7244 if (res.fh == NULL)
7245 return -ENOMEM;
7246
Anna Schumakerfba83f32018-05-04 16:22:50 -04007247 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007248 status = nfs4_call_sync_sequence(clnt, server, &msg,
7249 &args.seq_args, &res.seq_res);
7250 nfs_free_fhandle(res.fh);
7251 if (status)
7252 return status;
7253
7254 do_renew_lease(clp, now);
7255 return 0;
7256}
7257
7258#ifdef CONFIG_NFS_V4_1
7259
7260/*
7261 * This operation also signals the server that this client is
7262 * performing "lease moved" recovery. The server can stop asserting
7263 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7264 * this operation is identified in the SEQUENCE operation in this
7265 * compound.
7266 */
7267static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7268{
7269 struct nfs_server *server = NFS_SERVER(inode);
7270 struct rpc_clnt *clnt = server->client;
7271 struct nfs4_fsid_present_arg args = {
7272 .fh = NFS_FH(inode),
7273 };
7274 struct nfs4_fsid_present_res res = {
7275 };
7276 struct rpc_message msg = {
7277 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7278 .rpc_argp = &args,
7279 .rpc_resp = &res,
7280 .rpc_cred = cred,
7281 };
7282 int status;
7283
7284 res.fh = nfs_alloc_fhandle();
7285 if (res.fh == NULL)
7286 return -ENOMEM;
7287
Anna Schumakerfba83f32018-05-04 16:22:50 -04007288 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007289 status = nfs4_call_sync_sequence(clnt, server, &msg,
7290 &args.seq_args, &res.seq_res);
7291 nfs_free_fhandle(res.fh);
7292 if (status == NFS4_OK &&
7293 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7294 status = -NFS4ERR_LEASE_MOVED;
7295 return status;
7296}
7297
7298#endif /* CONFIG_NFS_V4_1 */
7299
7300/**
7301 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7302 * @inode: inode on FSID to check
7303 * @cred: credential to use for this operation
7304 *
7305 * Server indicates whether the FSID is present, moved, or not
7306 * recognized. This operation is necessary to clear a LEASE_MOVED
7307 * condition for this client ID.
7308 *
7309 * Returns NFS4_OK if the FSID is present on this server,
7310 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7311 * NFS4ERR code if some error occurred on the server, or a
7312 * negative errno if a local failure occurred.
7313 */
7314int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7315{
7316 struct nfs_server *server = NFS_SERVER(inode);
7317 struct nfs_client *clp = server->nfs_client;
7318 const struct nfs4_mig_recovery_ops *ops =
7319 clp->cl_mvops->mig_recovery_ops;
7320 struct nfs4_exception exception = { };
7321 int status;
7322
7323 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7324 (unsigned long long)server->fsid.major,
7325 (unsigned long long)server->fsid.minor,
7326 clp->cl_hostname);
7327 nfs_display_fhandle(NFS_FH(inode), __func__);
7328
7329 do {
7330 status = ops->fsid_present(inode, cred);
7331 if (status != -NFS4ERR_DELAY)
7332 break;
7333 nfs4_handle_exception(server, status, &exception);
7334 } while (exception.retry);
7335 return status;
7336}
7337
Andy Adamson5ec16a82013-08-08 10:57:55 -04007338/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007339 * If 'use_integrity' is true and the state managment nfs_client
7340 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7341 * and the machine credential as per RFC3530bis and RFC5661 Security
7342 * Considerations sections. Otherwise, just use the user cred with the
7343 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007344 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007345static 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 +00007346{
7347 int status;
7348 struct nfs4_secinfo_arg args = {
7349 .dir_fh = NFS_FH(dir),
7350 .name = name,
7351 };
7352 struct nfs4_secinfo_res res = {
7353 .flavors = flavors,
7354 };
7355 struct rpc_message msg = {
7356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7357 .rpc_argp = &args,
7358 .rpc_resp = &res,
7359 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007360 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007361 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007362
7363 if (use_integrity) {
7364 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007365 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7366 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007367 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007368
7369 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007370
7371 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7372 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7373
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007374 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7375 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007376 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007377
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007378 if (cred)
7379 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007380
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007381 return status;
7382}
7383
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007384int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7385 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007386{
7387 struct nfs4_exception exception = { };
7388 int err;
7389 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007390 err = -NFS4ERR_WRONGSEC;
7391
7392 /* try to use integrity protection with machine cred */
7393 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7394 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7395
7396 /*
7397 * if unable to use integrity protection, or SECINFO with
7398 * integrity protection returns NFS4ERR_WRONGSEC (which is
7399 * disallowed by spec, but exists in deployed servers) use
7400 * the current filesystem's rpc_client and the user cred.
7401 */
7402 if (err == -NFS4ERR_WRONGSEC)
7403 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7404
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007405 trace_nfs4_secinfo(dir, name, err);
7406 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007407 &exception);
7408 } while (exception.retry);
7409 return err;
7410}
7411
Andy Adamson557134a2009-04-01 09:21:53 -04007412#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007413/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007414 * Check the exchange flags returned by the server for invalid flags, having
7415 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7416 * DS flags set.
7417 */
7418static int nfs4_check_cl_exchange_flags(u32 flags)
7419{
7420 if (flags & ~EXCHGID4_FLAG_MASK_R)
7421 goto out_inval;
7422 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7423 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7424 goto out_inval;
7425 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7426 goto out_inval;
7427 return NFS_OK;
7428out_inval:
7429 return -NFS4ERR_INVAL;
7430}
7431
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007432static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007433nfs41_same_server_scope(struct nfs41_server_scope *a,
7434 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007435{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007436 if (a->server_scope_sz != b->server_scope_sz)
7437 return false;
7438 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007439}
7440
Andy Adamson02a95de2016-02-05 16:08:37 -05007441static void
7442nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7443{
7444}
7445
7446static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7447 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7448};
7449
Andy Adamson357f54d2010-12-14 10:11:57 -05007450/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007451 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007452 *
7453 * The 4.1 client currently uses the same TCP connection for the
7454 * fore and backchannel.
7455 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007456static
7457int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7458 struct rpc_xprt *xprt,
7459 struct nfs_client *clp,
7460 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007461{
7462 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007463 struct nfs41_bind_conn_to_session_args args = {
7464 .client = clp,
7465 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7466 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007467 struct nfs41_bind_conn_to_session_res res;
7468 struct rpc_message msg = {
7469 .rpc_proc =
7470 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007471 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007472 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007473 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007474 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007475 struct rpc_task_setup task_setup_data = {
7476 .rpc_client = clnt,
7477 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007478 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007479 .rpc_message = &msg,
7480 .flags = RPC_TASK_TIMEOUT,
7481 };
7482 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007483
Trond Myklebust71a097c2015-02-18 09:27:18 -08007484 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7485 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7486 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007487
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007488 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7489 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7490 args.dir = NFS4_CDFC4_FORE;
7491
7492 task = rpc_run_task(&task_setup_data);
7493 if (!IS_ERR(task)) {
7494 status = task->tk_status;
7495 rpc_put_task(task);
7496 } else
7497 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007498 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007499 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007500 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007501 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7502 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007503 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007504 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007505 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007506 dprintk("NFS: %s: Unexpected direction from server\n",
7507 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007508 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007509 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007510 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007511 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7512 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007513 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007514 }
7515 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007516
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007517 return status;
7518}
7519
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007520struct rpc_bind_conn_calldata {
7521 struct nfs_client *clp;
7522 struct rpc_cred *cred;
7523};
7524
7525static int
7526nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7527 struct rpc_xprt *xprt,
7528 void *calldata)
7529{
7530 struct rpc_bind_conn_calldata *p = calldata;
7531
7532 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7533}
7534
7535int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7536{
7537 struct rpc_bind_conn_calldata data = {
7538 .clp = clp,
7539 .cred = cred,
7540 };
7541 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7542 nfs4_proc_bind_conn_to_session_callback, &data);
7543}
7544
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007545/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007546 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7547 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007548 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007549static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7550 .how = SP4_MACH_CRED,
7551 .enforce.u.words = {
7552 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7553 1 << (OP_EXCHANGE_ID - 32) |
7554 1 << (OP_CREATE_SESSION - 32) |
7555 1 << (OP_DESTROY_SESSION - 32) |
7556 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007557 },
7558 .allow.u.words = {
7559 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007560 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007561 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007562 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007563 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007564 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007565 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007566 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007567 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007568 1 << (OP_FREE_STATEID - 32) |
7569 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007570 }
7571};
7572
7573/*
7574 * Select the state protection mode for client `clp' given the server results
7575 * from exchange_id in `sp'.
7576 *
7577 * Returns 0 on success, negative errno otherwise.
7578 */
7579static int nfs4_sp4_select_mode(struct nfs_client *clp,
7580 struct nfs41_state_protection *sp)
7581{
7582 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7583 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7584 1 << (OP_EXCHANGE_ID - 32) |
7585 1 << (OP_CREATE_SESSION - 32) |
7586 1 << (OP_DESTROY_SESSION - 32) |
7587 1 << (OP_DESTROY_CLIENTID - 32)
7588 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007589 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007590 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007591 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007592
7593 if (sp->how == SP4_MACH_CRED) {
7594 /* Print state protect result */
7595 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7596 for (i = 0; i <= LAST_NFS4_OP; i++) {
7597 if (test_bit(i, sp->enforce.u.longs))
7598 dfprintk(MOUNT, " enforce op %d\n", i);
7599 if (test_bit(i, sp->allow.u.longs))
7600 dfprintk(MOUNT, " allow op %d\n", i);
7601 }
7602
7603 /* make sure nothing is on enforce list that isn't supported */
7604 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7605 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7606 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007607 ret = -EINVAL;
7608 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007609 }
7610 }
7611
7612 /*
7613 * Minimal mode - state operations are allowed to use machine
7614 * credential. Note this already happens by default, so the
7615 * client doesn't have to do anything more than the negotiation.
7616 *
7617 * NOTE: we don't care if EXCHANGE_ID is in the list -
7618 * we're already using the machine cred for exchange_id
7619 * and will never use a different cred.
7620 */
7621 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7622 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7623 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7624 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7625 dfprintk(MOUNT, "sp4_mach_cred:\n");
7626 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007627 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007628 } else {
7629 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007630 ret = -EINVAL;
7631 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007632 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007633
7634 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007635 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7636 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007637 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7638 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007639 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007640 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007641
Andrew Elble99ade3c2015-12-02 09:39:51 -05007642 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7643 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007644 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007645 }
7646
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007647 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7648 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7649 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007650 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007651 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007652
7653 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7654 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7655 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007656 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007657 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007658
7659 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7660 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007661 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007662 }
7663
7664 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7665 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007666 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007667 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007668 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007669out:
7670 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007671 return 0;
7672}
7673
Andy Adamson8d89bd72016-09-09 09:22:18 -04007674struct nfs41_exchange_id_data {
7675 struct nfs41_exchange_id_res res;
7676 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007677};
7678
Andy Adamson8d89bd72016-09-09 09:22:18 -04007679static void nfs4_exchange_id_release(void *data)
7680{
7681 struct nfs41_exchange_id_data *cdata =
7682 (struct nfs41_exchange_id_data *)data;
7683
Olga Kornievskaia63513232017-03-13 10:36:19 -04007684 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007685 kfree(cdata->res.impl_id);
7686 kfree(cdata->res.server_scope);
7687 kfree(cdata->res.server_owner);
7688 kfree(cdata);
7689}
7690
7691static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007692 .rpc_release = nfs4_exchange_id_release,
7693};
7694
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007695/*
7696 * _nfs4_proc_exchange_id()
7697 *
7698 * Wrapper for EXCHANGE_ID operation.
7699 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007700static struct rpc_task *
7701nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007702 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007703{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007704 struct rpc_message msg = {
7705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007706 .rpc_cred = cred,
7707 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007708 struct rpc_task_setup task_setup_data = {
7709 .rpc_client = clp->cl_rpcclient,
7710 .callback_ops = &nfs4_exchange_id_call_ops,
7711 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007712 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007713 };
7714 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007715 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007716
Elena Reshetova212bf412017-10-20 12:53:38 +03007717 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007718 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007719
Trond Myklebust9c760d12017-07-31 18:38:50 -04007720 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007721 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007722 if (!calldata)
7723 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007724
Trond Myklebustfd405592017-08-01 16:02:47 -04007725 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007726
7727 status = nfs4_init_uniform_client_string(clp);
7728 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007729 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007730
Andy Adamson8d89bd72016-09-09 09:22:18 -04007731 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7732 GFP_NOFS);
7733 status = -ENOMEM;
7734 if (unlikely(calldata->res.server_owner == NULL))
7735 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007736
Andy Adamson8d89bd72016-09-09 09:22:18 -04007737 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007738 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007739 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007740 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007741
Andy Adamson8d89bd72016-09-09 09:22:18 -04007742 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7743 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007744 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007745
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007746 switch (sp4_how) {
7747 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007748 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007749 break;
7750
7751 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007752 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007753 break;
7754
7755 default:
7756 /* unsupported! */
7757 WARN_ON_ONCE(1);
7758 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007759 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007760 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007761 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007762 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007763 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007764 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7765 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007766 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007767 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007768 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7769 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007770#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007771 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007772#endif
7773 msg.rpc_argp = &calldata->args;
7774 msg.rpc_resp = &calldata->res;
7775 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007776
Trond Myklebust9c760d12017-07-31 18:38:50 -04007777 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007778
7779out_impl_id:
7780 kfree(calldata->res.impl_id);
7781out_server_scope:
7782 kfree(calldata->res.server_scope);
7783out_server_owner:
7784 kfree(calldata->res.server_owner);
7785out_calldata:
7786 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007787out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007788 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007789 return ERR_PTR(status);
7790}
7791
7792/*
7793 * _nfs4_proc_exchange_id()
7794 *
7795 * Wrapper for EXCHANGE_ID operation.
7796 */
7797static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7798 u32 sp4_how)
7799{
7800 struct rpc_task *task;
7801 struct nfs41_exchange_id_args *argp;
7802 struct nfs41_exchange_id_res *resp;
7803 int status;
7804
7805 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7806 if (IS_ERR(task))
7807 return PTR_ERR(task);
7808
7809 argp = task->tk_msg.rpc_argp;
7810 resp = task->tk_msg.rpc_resp;
7811 status = task->tk_status;
7812 if (status != 0)
7813 goto out;
7814
7815 status = nfs4_check_cl_exchange_flags(resp->flags);
7816 if (status != 0)
7817 goto out;
7818
7819 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7820 if (status != 0)
7821 goto out;
7822
7823 clp->cl_clientid = resp->clientid;
7824 clp->cl_exchange_flags = resp->flags;
7825 clp->cl_seqid = resp->seqid;
7826 /* Client ID is not confirmed */
7827 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7828 clear_bit(NFS4_SESSION_ESTABLISHED,
7829 &clp->cl_session->session_state);
7830
7831 if (clp->cl_serverscope != NULL &&
7832 !nfs41_same_server_scope(clp->cl_serverscope,
7833 resp->server_scope)) {
7834 dprintk("%s: server_scope mismatch detected\n",
7835 __func__);
7836 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7837 }
7838
7839 swap(clp->cl_serverowner, resp->server_owner);
7840 swap(clp->cl_serverscope, resp->server_scope);
7841 swap(clp->cl_implid, resp->impl_id);
7842
7843 /* Save the EXCHANGE_ID verifier session trunk tests */
7844 memcpy(clp->cl_confirm.data, argp->verifier.data,
7845 sizeof(clp->cl_confirm.data));
7846out:
7847 trace_nfs4_exchange_id(clp, status);
7848 rpc_put_task(task);
7849 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007850}
7851
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007852/*
7853 * nfs4_proc_exchange_id()
7854 *
7855 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7856 *
7857 * Since the clientid has expired, all compounds using sessions
7858 * associated with the stale clientid will be returning
7859 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7860 * be in some phase of session reset.
7861 *
7862 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7863 */
7864int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7865{
7866 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7867 int status;
7868
7869 /* try SP4_MACH_CRED if krb5i/p */
7870 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7871 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007872 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007873 if (!status)
7874 return 0;
7875 }
7876
7877 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007878 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007879}
7880
Andy Adamson04fa2c62016-09-09 09:22:29 -04007881/**
7882 * nfs4_test_session_trunk
7883 *
7884 * This is an add_xprt_test() test function called from
7885 * rpc_clnt_setup_test_and_add_xprt.
7886 *
7887 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7888 * and is dereferrenced in nfs4_exchange_id_release
7889 *
7890 * Upon success, add the new transport to the rpc_clnt
7891 *
7892 * @clnt: struct rpc_clnt to get new transport
7893 * @xprt: the rpc_xprt to test
7894 * @data: call data for _nfs4_proc_exchange_id.
7895 */
7896int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7897 void *data)
7898{
7899 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007900 struct rpc_task *task;
7901 int status;
7902
Andy Adamson04fa2c62016-09-09 09:22:29 -04007903 u32 sp4_how;
7904
7905 dprintk("--> %s try %s\n", __func__,
7906 xprt->address_strings[RPC_DISPLAY_ADDR]);
7907
7908 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7909
7910 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007911 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7912 if (IS_ERR(task))
7913 return PTR_ERR(task);
7914
7915 status = task->tk_status;
7916 if (status == 0)
7917 status = nfs4_detect_session_trunking(adata->clp,
7918 task->tk_msg.rpc_resp, xprt);
7919
7920 rpc_put_task(task);
7921 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007922}
7923EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7924
Trond Myklebust66245532012-05-25 17:18:09 -04007925static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7926 struct rpc_cred *cred)
7927{
7928 struct rpc_message msg = {
7929 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7930 .rpc_argp = clp,
7931 .rpc_cred = cred,
7932 };
7933 int status;
7934
7935 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007936 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007937 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007938 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007939 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7940 return status;
7941}
7942
7943static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7944 struct rpc_cred *cred)
7945{
7946 unsigned int loop;
7947 int ret;
7948
7949 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7950 ret = _nfs4_proc_destroy_clientid(clp, cred);
7951 switch (ret) {
7952 case -NFS4ERR_DELAY:
7953 case -NFS4ERR_CLIENTID_BUSY:
7954 ssleep(1);
7955 break;
7956 default:
7957 return ret;
7958 }
7959 }
7960 return 0;
7961}
7962
7963int nfs4_destroy_clientid(struct nfs_client *clp)
7964{
7965 struct rpc_cred *cred;
7966 int ret = 0;
7967
7968 if (clp->cl_mvops->minor_version < 1)
7969 goto out;
7970 if (clp->cl_exchange_flags == 0)
7971 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007972 if (clp->cl_preserve_clid)
7973 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007974 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007975 ret = nfs4_proc_destroy_clientid(clp, cred);
7976 if (cred)
7977 put_rpccred(cred);
7978 switch (ret) {
7979 case 0:
7980 case -NFS4ERR_STALE_CLIENTID:
7981 clp->cl_exchange_flags = 0;
7982 }
7983out:
7984 return ret;
7985}
7986
Andy Adamson2050f0c2009-04-01 09:22:30 -04007987struct nfs4_get_lease_time_data {
7988 struct nfs4_get_lease_time_args *args;
7989 struct nfs4_get_lease_time_res *res;
7990 struct nfs_client *clp;
7991};
7992
7993static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7994 void *calldata)
7995{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007996 struct nfs4_get_lease_time_data *data =
7997 (struct nfs4_get_lease_time_data *)calldata;
7998
7999 dprintk("--> %s\n", __func__);
8000 /* just setup sequence, do not trigger session recovery
8001 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008002 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008003 &data->args->la_seq_args,
8004 &data->res->lr_seq_res,
8005 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008006 dprintk("<-- %s\n", __func__);
8007}
8008
8009/*
8010 * Called from nfs4_state_manager thread for session setup, so don't recover
8011 * from sequence operation or clientid errors.
8012 */
8013static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8014{
8015 struct nfs4_get_lease_time_data *data =
8016 (struct nfs4_get_lease_time_data *)calldata;
8017
8018 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008019 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8020 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008021 switch (task->tk_status) {
8022 case -NFS4ERR_DELAY:
8023 case -NFS4ERR_GRACE:
8024 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8025 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8026 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008027 /* fall through */
8028 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008029 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008030 return;
8031 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008032 dprintk("<-- %s\n", __func__);
8033}
8034
Trond Myklebust17280172012-03-11 13:11:00 -04008035static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008036 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8037 .rpc_call_done = nfs4_get_lease_time_done,
8038};
8039
8040int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8041{
8042 struct rpc_task *task;
8043 struct nfs4_get_lease_time_args args;
8044 struct nfs4_get_lease_time_res res = {
8045 .lr_fsinfo = fsinfo,
8046 };
8047 struct nfs4_get_lease_time_data data = {
8048 .args = &args,
8049 .res = &res,
8050 .clp = clp,
8051 };
8052 struct rpc_message msg = {
8053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8054 .rpc_argp = &args,
8055 .rpc_resp = &res,
8056 };
8057 struct rpc_task_setup task_setup = {
8058 .rpc_client = clp->cl_rpcclient,
8059 .rpc_message = &msg,
8060 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008061 .callback_data = &data,
8062 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008063 };
8064 int status;
8065
Anna Schumakerfba83f32018-05-04 16:22:50 -04008066 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008067 task = rpc_run_task(&task_setup);
8068
8069 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008070 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008071
Anna Schumakerf6148712017-04-07 14:15:23 -04008072 status = task->tk_status;
8073 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008074 return status;
8075}
8076
Andy Adamsonfc931582009-04-01 09:22:31 -04008077/*
8078 * Initialize the values to be used by the client in CREATE_SESSION
8079 * If nfs4_init_session set the fore channel request and response sizes,
8080 * use them.
8081 *
8082 * Set the back channel max_resp_sz_cached to zero to force the client to
8083 * always set csa_cachethis to FALSE because the current implementation
8084 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8085 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008086static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8087 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008088{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008089 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008090 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008091
Andy Adamson18aad3d2013-06-26 12:21:49 -04008092 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8093 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8094
Andy Adamsonfc931582009-04-01 09:22:31 -04008095 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008096 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8097 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008098 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008099 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008100
8101 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008102 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008103 __func__,
8104 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008105 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008106
8107 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008108 args->bc_attrs.max_rqst_sz = max_bc_payload;
8109 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008110 args->bc_attrs.max_resp_sz_cached = 0;
8111 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008112 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008113
8114 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8115 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8116 __func__,
8117 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8118 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8119 args->bc_attrs.max_reqs);
8120}
8121
Trond Myklebust79969dd2015-02-18 11:30:18 -08008122static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8123 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008124{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008125 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008126 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008127
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008128 if (rcvd->max_resp_sz > sent->max_resp_sz)
8129 return -EINVAL;
8130 /*
8131 * Our requested max_ops is the minimum we need; we're not
8132 * prepared to break up compounds into smaller pieces than that.
8133 * So, no point even trying to continue if the server won't
8134 * cooperate:
8135 */
8136 if (rcvd->max_ops < sent->max_ops)
8137 return -EINVAL;
8138 if (rcvd->max_reqs == 0)
8139 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008140 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8141 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008142 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008143}
8144
Trond Myklebust79969dd2015-02-18 11:30:18 -08008145static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8146 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008147{
8148 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008149 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008150
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008151 if (!(res->flags & SESSION4_BACK_CHAN))
8152 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008153 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8154 return -EINVAL;
8155 if (rcvd->max_resp_sz < sent->max_resp_sz)
8156 return -EINVAL;
8157 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8158 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008159 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008160 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008161 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008162 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008163out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008164 return 0;
8165}
Andy Adamson8d353012009-04-01 09:22:32 -04008166
Andy Adamson8d353012009-04-01 09:22:32 -04008167static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008168 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008169{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008170 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008171
Trond Myklebust79969dd2015-02-18 11:30:18 -08008172 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008173 if (ret)
8174 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008175 return nfs4_verify_back_channel_attrs(args, res);
8176}
8177
8178static void nfs4_update_session(struct nfs4_session *session,
8179 struct nfs41_create_session_res *res)
8180{
8181 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008182 /* Mark client id and session as being confirmed */
8183 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8184 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008185 session->flags = res->flags;
8186 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008187 if (res->flags & SESSION4_BACK_CHAN)
8188 memcpy(&session->bc_attrs, &res->bc_attrs,
8189 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008190}
8191
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008192static int _nfs4_proc_create_session(struct nfs_client *clp,
8193 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008194{
8195 struct nfs4_session *session = clp->cl_session;
8196 struct nfs41_create_session_args args = {
8197 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008198 .clientid = clp->cl_clientid,
8199 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008200 .cb_program = NFS4_CALLBACK,
8201 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008202 struct nfs41_create_session_res res;
8203
Andy Adamsonfc931582009-04-01 09:22:31 -04008204 struct rpc_message msg = {
8205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8206 .rpc_argp = &args,
8207 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008208 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008209 };
8210 int status;
8211
Chuck Lever6b26cc82016-05-02 14:40:40 -04008212 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008213 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008214
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008215 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008216 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008217
Trond Myklebustb519d402016-09-11 14:50:01 -04008218 switch (status) {
8219 case -NFS4ERR_STALE_CLIENTID:
8220 case -NFS4ERR_DELAY:
8221 case -ETIMEDOUT:
8222 case -EACCES:
8223 case -EAGAIN:
8224 goto out;
8225 };
8226
8227 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008228 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008229 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008230 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008231 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008232 if (status)
8233 goto out;
8234 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008235 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008236out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008237 return status;
8238}
8239
8240/*
8241 * Issues a CREATE_SESSION operation to the server.
8242 * It is the responsibility of the caller to verify the session is
8243 * expired before calling this routine.
8244 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008245int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008246{
8247 int status;
8248 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008249 struct nfs4_session *session = clp->cl_session;
8250
8251 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8252
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008253 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008254 if (status)
8255 goto out;
8256
Andy Adamsonaacd5532011-11-09 13:58:21 -05008257 /* Init or reset the session slot tables */
8258 status = nfs4_setup_session_slot_tables(session);
8259 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008260 if (status)
8261 goto out;
8262
8263 ptr = (unsigned *)&session->sess_id.data[0];
8264 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8265 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008266out:
8267 dprintk("<-- %s\n", __func__);
8268 return status;
8269}
8270
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008271/*
8272 * Issue the over-the-wire RPC DESTROY_SESSION.
8273 * The caller must serialize access to this routine.
8274 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008275int nfs4_proc_destroy_session(struct nfs4_session *session,
8276 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008277{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008278 struct rpc_message msg = {
8279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8280 .rpc_argp = session,
8281 .rpc_cred = cred,
8282 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008283 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008284
8285 dprintk("--> nfs4_proc_destroy_session\n");
8286
8287 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008288 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8289 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008290
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008291 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008292 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008293
8294 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008295 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008296 "Session has been destroyed regardless...\n", status);
8297
8298 dprintk("<-- nfs4_proc_destroy_session\n");
8299 return status;
8300}
8301
Trond Myklebust7b38c362012-05-23 13:23:31 -04008302/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008303 * Renew the cl_session lease.
8304 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008305struct nfs4_sequence_data {
8306 struct nfs_client *clp;
8307 struct nfs4_sequence_args args;
8308 struct nfs4_sequence_res res;
8309};
8310
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008311static void nfs41_sequence_release(void *data)
8312{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008313 struct nfs4_sequence_data *calldata = data;
8314 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008315
Elena Reshetova212bf412017-10-20 12:53:38 +03008316 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008317 nfs4_schedule_state_renewal(clp);
8318 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008319 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008320}
8321
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008322static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8323{
8324 switch(task->tk_status) {
8325 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008326 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8327 return -EAGAIN;
8328 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008329 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008330 }
8331 return 0;
8332}
8333
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008334static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008335{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008336 struct nfs4_sequence_data *calldata = data;
8337 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008338
Trond Myklebust14516c32010-07-31 14:29:06 -04008339 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8340 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008341
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008342 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008343 if (task->tk_status < 0) {
8344 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008345 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008346 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008347
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008348 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8349 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008350 return;
8351 }
8352 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008353 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008354out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008355 dprintk("<-- %s\n", __func__);
8356}
8357
8358static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8359{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008360 struct nfs4_sequence_data *calldata = data;
8361 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008362 struct nfs4_sequence_args *args;
8363 struct nfs4_sequence_res *res;
8364
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008365 args = task->tk_msg.rpc_argp;
8366 res = task->tk_msg.rpc_resp;
8367
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008368 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008369}
8370
8371static const struct rpc_call_ops nfs41_sequence_ops = {
8372 .rpc_call_done = nfs41_sequence_call_done,
8373 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008374 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008375};
8376
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008377static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8378 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008379 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008380 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008381{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008382 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008383 struct rpc_message msg = {
8384 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8385 .rpc_cred = cred,
8386 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008387 struct rpc_task_setup task_setup_data = {
8388 .rpc_client = clp->cl_rpcclient,
8389 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008390 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008391 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008392 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008393 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008394
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008395 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008396 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008397 goto out_err;
8398
8399 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008400 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008401 if (calldata == NULL)
8402 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008403 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008404 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008405 msg.rpc_argp = &calldata->args;
8406 msg.rpc_resp = &calldata->res;
8407 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008408 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008409
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008410 ret = rpc_run_task(&task_setup_data);
8411 if (IS_ERR(ret))
8412 goto out_err;
8413 return ret;
8414out_put_clp:
8415 nfs_put_client(clp);
8416out_err:
8417 nfs41_release_slot(slot);
8418 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008419}
8420
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008421static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008422{
8423 struct rpc_task *task;
8424 int ret = 0;
8425
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008426 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008427 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008428 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008429 if (IS_ERR(task))
8430 ret = PTR_ERR(task);
8431 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008432 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008433 dprintk("<-- %s status=%d\n", __func__, ret);
8434 return ret;
8435}
8436
8437static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8438{
8439 struct rpc_task *task;
8440 int ret;
8441
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008442 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008443 if (IS_ERR(task)) {
8444 ret = PTR_ERR(task);
8445 goto out;
8446 }
8447 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008448 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008449 ret = task->tk_status;
8450 rpc_put_task(task);
8451out:
8452 dprintk("<-- %s status=%d\n", __func__, ret);
8453 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008454}
8455
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008456struct nfs4_reclaim_complete_data {
8457 struct nfs_client *clp;
8458 struct nfs41_reclaim_complete_args arg;
8459 struct nfs41_reclaim_complete_res res;
8460};
8461
8462static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8463{
8464 struct nfs4_reclaim_complete_data *calldata = data;
8465
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008466 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008467 &calldata->arg.seq_args,
8468 &calldata->res.seq_res,
8469 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008470}
8471
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008472static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8473{
8474 switch(task->tk_status) {
8475 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008476 wake_up_all(&clp->cl_lock_waitq);
8477 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008478 case -NFS4ERR_COMPLETE_ALREADY:
8479 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8480 break;
8481 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008482 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008483 /* fall through */
8484 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008485 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008486 case -NFS4ERR_BADSESSION:
8487 case -NFS4ERR_DEADSESSION:
8488 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8489 nfs4_schedule_session_recovery(clp->cl_session,
8490 task->tk_status);
8491 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008492 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008493 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008494 }
8495 return 0;
8496}
8497
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008498static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8499{
8500 struct nfs4_reclaim_complete_data *calldata = data;
8501 struct nfs_client *clp = calldata->clp;
8502 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8503
8504 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008505 if (!nfs41_sequence_done(task, res))
8506 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008507
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008508 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008509 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8510 rpc_restart_call_prepare(task);
8511 return;
8512 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008513 dprintk("<-- %s\n", __func__);
8514}
8515
8516static void nfs4_free_reclaim_complete_data(void *data)
8517{
8518 struct nfs4_reclaim_complete_data *calldata = data;
8519
8520 kfree(calldata);
8521}
8522
8523static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8524 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8525 .rpc_call_done = nfs4_reclaim_complete_done,
8526 .rpc_release = nfs4_free_reclaim_complete_data,
8527};
8528
8529/*
8530 * Issue a global reclaim complete.
8531 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008532static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8533 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008534{
8535 struct nfs4_reclaim_complete_data *calldata;
8536 struct rpc_task *task;
8537 struct rpc_message msg = {
8538 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008539 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008540 };
8541 struct rpc_task_setup task_setup_data = {
8542 .rpc_client = clp->cl_rpcclient,
8543 .rpc_message = &msg,
8544 .callback_ops = &nfs4_reclaim_complete_call_ops,
8545 .flags = RPC_TASK_ASYNC,
8546 };
8547 int status = -ENOMEM;
8548
8549 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008550 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008551 if (calldata == NULL)
8552 goto out;
8553 calldata->clp = clp;
8554 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008555
Anna Schumakerfba83f32018-05-04 16:22:50 -04008556 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008557 msg.rpc_argp = &calldata->arg;
8558 msg.rpc_resp = &calldata->res;
8559 task_setup_data.callback_data = calldata;
8560 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008561 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008562 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008563 goto out;
8564 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008565 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008566 if (status == 0)
8567 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008568 rpc_put_task(task);
8569out:
8570 dprintk("<-- %s status=%d\n", __func__, status);
8571 return status;
8572}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008573
8574static void
8575nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8576{
8577 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008578 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008579
8580 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008581 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008582 &lgp->res.seq_res, task);
8583 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008584}
8585
8586static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8587{
8588 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008589
8590 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008591 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008592 dprintk("<-- %s\n", __func__);
8593}
8594
8595static int
8596nfs4_layoutget_handle_exception(struct rpc_task *task,
8597 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8598{
Trond Myklebustee314c22012-10-01 17:25:48 -07008599 struct inode *inode = lgp->args.inode;
8600 struct nfs_server *server = NFS_SERVER(inode);
8601 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008602 int nfs4err = task->tk_status;
8603 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008604 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008605
Boaz Harroshed7e5422014-01-22 20:34:54 +02008606 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008607
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008608 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008609 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008610 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008611
8612 /*
8613 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8614 * on the file. set tk_status to -ENODATA to tell upper layer to
8615 * retry go inband.
8616 */
8617 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008618 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008619 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008620 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008621 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8622 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8623 */
8624 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008625 status = -EOVERFLOW;
8626 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008627 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008628 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008629 * (or clients) writing to the same RAID stripe except when
8630 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008631 *
8632 * Treat it like we would RECALLCONFLICT -- we retry for a little
8633 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008634 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008635 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008636 if (lgp->args.minlength == 0) {
8637 status = -EOVERFLOW;
8638 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008639 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008640 status = -EBUSY;
8641 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008642 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008643 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008644 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008645 case -NFS4ERR_DELEG_REVOKED:
8646 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008647 case -NFS4ERR_EXPIRED:
8648 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008649 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008650 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008651 lo = NFS_I(inode)->layout;
8652 /* If the open stateid was bad, then recover it. */
8653 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008654 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008655 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008656 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008657 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008658 break;
8659 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008660
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008661 /*
8662 * Mark the bad layout state as invalid, then retry
8663 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008664 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008665 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008666 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008667 pnfs_free_lseg_list(&head);
8668 status = -EAGAIN;
8669 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008670 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008671
Trond Myklebust8ac09252017-01-23 22:44:12 -05008672 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008673 err = nfs4_handle_exception(server, nfs4err, exception);
8674 if (!status) {
8675 if (exception->retry)
8676 status = -EAGAIN;
8677 else
8678 status = err;
8679 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008680out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008681 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008682 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683}
8684
Idan Kedar85541162012-08-02 11:47:10 +03008685static size_t max_response_pages(struct nfs_server *server)
8686{
8687 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8688 return nfs_page_array_len(0, max_resp_sz);
8689}
8690
8691static void nfs4_free_pages(struct page **pages, size_t size)
8692{
8693 int i;
8694
8695 if (!pages)
8696 return;
8697
8698 for (i = 0; i < size; i++) {
8699 if (!pages[i])
8700 break;
8701 __free_page(pages[i]);
8702 }
8703 kfree(pages);
8704}
8705
8706static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8707{
8708 struct page **pages;
8709 int i;
8710
8711 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8712 if (!pages) {
8713 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8714 return NULL;
8715 }
8716
8717 for (i = 0; i < size; i++) {
8718 pages[i] = alloc_page(gfp_flags);
8719 if (!pages[i]) {
8720 dprintk("%s: failed to allocate page\n", __func__);
8721 nfs4_free_pages(pages, size);
8722 return NULL;
8723 }
8724 }
8725
8726 return pages;
8727}
8728
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008729static void nfs4_layoutget_release(void *calldata)
8730{
8731 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008732 struct inode *inode = lgp->args.inode;
8733 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008734 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008735
8736 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008737 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008738 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008739 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008740 put_nfs_open_context(lgp->args.ctx);
8741 kfree(calldata);
8742 dprintk("<-- %s\n", __func__);
8743}
8744
8745static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8746 .rpc_call_prepare = nfs4_layoutget_prepare,
8747 .rpc_call_done = nfs4_layoutget_done,
8748 .rpc_release = nfs4_layoutget_release,
8749};
8750
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008751struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008752nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008753{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008754 struct inode *inode = lgp->args.inode;
8755 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008756 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008757 struct rpc_task *task;
8758 struct rpc_message msg = {
8759 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8760 .rpc_argp = &lgp->args,
8761 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008762 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008763 };
8764 struct rpc_task_setup task_setup_data = {
8765 .rpc_client = server->client,
8766 .rpc_message = &msg,
8767 .callback_ops = &nfs4_layoutget_call_ops,
8768 .callback_data = lgp,
8769 .flags = RPC_TASK_ASYNC,
8770 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008771 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008772 struct nfs4_exception exception = {
8773 .inode = inode,
8774 .timeout = *timeout,
8775 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008776 int status = 0;
8777
8778 dprintk("--> %s\n", __func__);
8779
Peng Tao4bd5a982014-11-17 11:05:17 +08008780 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8781 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8782
Idan Kedar85541162012-08-02 11:47:10 +03008783 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8784 if (!lgp->args.layout.pages) {
8785 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008786 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008787 }
8788 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8789
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008790 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008791 lgp->res.seq_res.sr_slot = NULL;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008792 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008793
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008794 task = rpc_run_task(&task_setup_data);
8795 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008796 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008797 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008798 if (status == 0) {
8799 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8800 *timeout = exception.timeout;
8801 }
8802
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008803 trace_nfs4_layoutget(lgp->args.ctx,
8804 &lgp->args.range,
8805 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008806 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008807 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008808
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008809 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8810 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008811 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008812 rpc_put_task(task);
8813 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008814 if (status)
8815 return ERR_PTR(status);
8816 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008817}
8818
Benny Halevycbe82602011-05-22 19:52:37 +03008819static void
8820nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8821{
8822 struct nfs4_layoutreturn *lrp = calldata;
8823
8824 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008825 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008826 &lrp->args.seq_args,
8827 &lrp->res.seq_res,
8828 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008829}
8830
8831static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8832{
8833 struct nfs4_layoutreturn *lrp = calldata;
8834 struct nfs_server *server;
8835
8836 dprintk("--> %s\n", __func__);
8837
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008838 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008839 return;
8840
8841 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008842 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008843 case -NFS4ERR_OLD_STATEID:
8844 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8845 lrp->args.inode))
8846 goto out_restart;
8847 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008848 default:
8849 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008850 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008851 case 0:
8852 break;
8853 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008854 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008855 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008856 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008857 }
Benny Halevycbe82602011-05-22 19:52:37 +03008858 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008859 return;
8860out_restart:
8861 task->tk_status = 0;
8862 nfs4_sequence_free_slot(&lrp->res.seq_res);
8863 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008864}
8865
8866static void nfs4_layoutreturn_release(void *calldata)
8867{
8868 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008869 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008870
8871 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008872 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008873 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008874 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008875 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8876 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008877 pnfs_put_layout_hdr(lrp->args.layout);
8878 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008879 kfree(calldata);
8880 dprintk("<-- %s\n", __func__);
8881}
8882
8883static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8884 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8885 .rpc_call_done = nfs4_layoutreturn_done,
8886 .rpc_release = nfs4_layoutreturn_release,
8887};
8888
Peng Tao6c166052014-11-17 09:30:40 +08008889int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008890{
8891 struct rpc_task *task;
8892 struct rpc_message msg = {
8893 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8894 .rpc_argp = &lrp->args,
8895 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008896 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008897 };
8898 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008899 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008900 .rpc_message = &msg,
8901 .callback_ops = &nfs4_layoutreturn_call_ops,
8902 .callback_data = lrp,
8903 };
Peng Tao6c166052014-11-17 09:30:40 +08008904 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008905
Andrew Elble99ade3c2015-12-02 09:39:51 -05008906 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8907 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8908 &task_setup_data.rpc_client, &msg);
8909
Benny Halevycbe82602011-05-22 19:52:37 +03008910 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008911 if (!sync) {
8912 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8913 if (!lrp->inode) {
8914 nfs4_layoutreturn_release(lrp);
8915 return -EAGAIN;
8916 }
8917 task_setup_data.flags |= RPC_TASK_ASYNC;
8918 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008919 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008920 task = rpc_run_task(&task_setup_data);
8921 if (IS_ERR(task))
8922 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008923 if (sync)
8924 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008925 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008926 dprintk("<-- %s status=%d\n", __func__, status);
8927 rpc_put_task(task);
8928 return status;
8929}
8930
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008931static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008932_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8933 struct pnfs_device *pdev,
8934 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008935{
8936 struct nfs4_getdeviceinfo_args args = {
8937 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008938 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8939 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008940 };
8941 struct nfs4_getdeviceinfo_res res = {
8942 .pdev = pdev,
8943 };
8944 struct rpc_message msg = {
8945 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8946 .rpc_argp = &args,
8947 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008948 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008949 };
8950 int status;
8951
8952 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008953 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008954 if (res.notification & ~args.notify_types)
8955 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008956 if (res.notification != args.notify_types)
8957 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008958
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008959 dprintk("<-- %s status=%d\n", __func__, status);
8960
8961 return status;
8962}
8963
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008964int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8965 struct pnfs_device *pdev,
8966 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008967{
8968 struct nfs4_exception exception = { };
8969 int err;
8970
8971 do {
8972 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008973 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008974 &exception);
8975 } while (exception.retry);
8976 return err;
8977}
8978EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8979
Andy Adamson863a3c62011-03-23 13:27:54 +00008980static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8981{
8982 struct nfs4_layoutcommit_data *data = calldata;
8983 struct nfs_server *server = NFS_SERVER(data->args.inode);
8984
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008985 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008986 &data->args.seq_args,
8987 &data->res.seq_res,
8988 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008989}
8990
8991static void
8992nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8993{
8994 struct nfs4_layoutcommit_data *data = calldata;
8995 struct nfs_server *server = NFS_SERVER(data->args.inode);
8996
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008997 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008998 return;
8999
9000 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009001 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9002 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9003 case -NFS4ERR_BADLAYOUT: /* no layout */
9004 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009005 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009006 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009007 break;
9008 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009009 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009010 rpc_restart_call_prepare(task);
9011 return;
9012 }
9013 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009014}
9015
9016static void nfs4_layoutcommit_release(void *calldata)
9017{
9018 struct nfs4_layoutcommit_data *data = calldata;
9019
Andy Adamsondb29c082011-07-30 20:52:38 -04009020 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009021 nfs_post_op_update_inode_force_wcc(data->args.inode,
9022 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009023 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009024 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009025 kfree(data);
9026}
9027
9028static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9029 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9030 .rpc_call_done = nfs4_layoutcommit_done,
9031 .rpc_release = nfs4_layoutcommit_release,
9032};
9033
9034int
Andy Adamsonef311532011-03-12 02:58:10 -05009035nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009036{
9037 struct rpc_message msg = {
9038 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9039 .rpc_argp = &data->args,
9040 .rpc_resp = &data->res,
9041 .rpc_cred = data->cred,
9042 };
9043 struct rpc_task_setup task_setup_data = {
9044 .task = &data->task,
9045 .rpc_client = NFS_CLIENT(data->args.inode),
9046 .rpc_message = &msg,
9047 .callback_ops = &nfs4_layoutcommit_ops,
9048 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009049 };
9050 struct rpc_task *task;
9051 int status = 0;
9052
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009053 dprintk("NFS: initiating layoutcommit call. sync %d "
9054 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009055 data->args.lastbytewritten,
9056 data->args.inode->i_ino);
9057
Trond Myklebust472e2592015-02-05 16:50:30 -05009058 if (!sync) {
9059 data->inode = nfs_igrab_and_active(data->args.inode);
9060 if (data->inode == NULL) {
9061 nfs4_layoutcommit_release(data);
9062 return -EAGAIN;
9063 }
9064 task_setup_data.flags = RPC_TASK_ASYNC;
9065 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009066 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009067 task = rpc_run_task(&task_setup_data);
9068 if (IS_ERR(task))
9069 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009070 if (sync)
9071 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009072 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009073 dprintk("%s: status %d\n", __func__, status);
9074 rpc_put_task(task);
9075 return status;
9076}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009077
Andy Adamson97431202013-08-08 10:57:56 -04009078/**
9079 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9080 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9081 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009082static int
9083_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009084 struct nfs_fsinfo *info,
9085 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009086{
9087 struct nfs41_secinfo_no_name_args args = {
9088 .style = SECINFO_STYLE_CURRENT_FH,
9089 };
9090 struct nfs4_secinfo_res res = {
9091 .flavors = flavors,
9092 };
9093 struct rpc_message msg = {
9094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9095 .rpc_argp = &args,
9096 .rpc_resp = &res,
9097 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009098 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009099 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009100 int status;
9101
9102 if (use_integrity) {
9103 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009104 cred = nfs4_get_clid_cred(server->nfs_client);
9105 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009106 }
9107
9108 dprintk("--> %s\n", __func__);
9109 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9110 &res.seq_res, 0);
9111 dprintk("<-- %s status=%d\n", __func__, status);
9112
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009113 if (cred)
9114 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009115
9116 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009117}
9118
9119static int
9120nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9121 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9122{
9123 struct nfs4_exception exception = { };
9124 int err;
9125 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009126 /* first try using integrity protection */
9127 err = -NFS4ERR_WRONGSEC;
9128
9129 /* try to use integrity protection with machine cred */
9130 if (_nfs4_is_integrity_protected(server->nfs_client))
9131 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9132 flavors, true);
9133
9134 /*
9135 * if unable to use integrity protection, or SECINFO with
9136 * integrity protection returns NFS4ERR_WRONGSEC (which is
9137 * disallowed by spec, but exists in deployed servers) use
9138 * the current filesystem's rpc_client and the user cred.
9139 */
9140 if (err == -NFS4ERR_WRONGSEC)
9141 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9142 flavors, false);
9143
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009144 switch (err) {
9145 case 0:
9146 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009147 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009148 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009149 default:
9150 err = nfs4_handle_exception(server, err, &exception);
9151 }
9152 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009153out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009154 return err;
9155}
9156
9157static int
9158nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9159 struct nfs_fsinfo *info)
9160{
9161 int err;
9162 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009163 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009164 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009165 struct nfs4_secinfo4 *secinfo;
9166 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009167
9168 page = alloc_page(GFP_KERNEL);
9169 if (!page) {
9170 err = -ENOMEM;
9171 goto out;
9172 }
9173
9174 flavors = page_address(page);
9175 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9176
9177 /*
9178 * Fall back on "guess and check" method if
9179 * the server doesn't support SECINFO_NO_NAME
9180 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009181 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009182 err = nfs4_find_root_sec(server, fhandle, info);
9183 goto out_freepage;
9184 }
9185 if (err)
9186 goto out_freepage;
9187
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009188 for (i = 0; i < flavors->num_flavors; i++) {
9189 secinfo = &flavors->flavors[i];
9190
9191 switch (secinfo->flavor) {
9192 case RPC_AUTH_NULL:
9193 case RPC_AUTH_UNIX:
9194 case RPC_AUTH_GSS:
9195 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9196 &secinfo->flavor_info);
9197 break;
9198 default:
9199 flavor = RPC_AUTH_MAXFLAVOR;
9200 break;
9201 }
9202
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009203 if (!nfs_auth_info_match(&server->auth_info, flavor))
9204 flavor = RPC_AUTH_MAXFLAVOR;
9205
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009206 if (flavor != RPC_AUTH_MAXFLAVOR) {
9207 err = nfs4_lookup_root_sec(server, fhandle,
9208 info, flavor);
9209 if (!err)
9210 break;
9211 }
9212 }
9213
9214 if (flavor == RPC_AUTH_MAXFLAVOR)
9215 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009216
9217out_freepage:
9218 put_page(page);
9219 if (err == -EACCES)
9220 return -EPERM;
9221out:
9222 return err;
9223}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009224
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009225static int _nfs41_test_stateid(struct nfs_server *server,
9226 nfs4_stateid *stateid,
9227 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009228{
9229 int status;
9230 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009231 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009232 };
9233 struct nfs41_test_stateid_res res;
9234 struct rpc_message msg = {
9235 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9236 .rpc_argp = &args,
9237 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009238 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009239 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009240 struct rpc_clnt *rpc_client = server->client;
9241
9242 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9243 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009244
Chuck Lever38527b12012-07-11 16:30:23 -04009245 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009246 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009247 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009248 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009249 if (status != NFS_OK) {
9250 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009251 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009252 }
9253 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009254 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009255}
9256
Trond Myklebust43912bb2016-09-22 13:38:56 -04009257static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9258 int err, struct nfs4_exception *exception)
9259{
9260 exception->retry = 0;
9261 switch(err) {
9262 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009263 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009264 nfs4_handle_exception(server, err, exception);
9265 break;
9266 case -NFS4ERR_BADSESSION:
9267 case -NFS4ERR_BADSLOT:
9268 case -NFS4ERR_BAD_HIGH_SLOT:
9269 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9270 case -NFS4ERR_DEADSESSION:
9271 nfs4_do_handle_exception(server, err, exception);
9272 }
9273}
9274
Chuck Lever38527b12012-07-11 16:30:23 -04009275/**
9276 * nfs41_test_stateid - perform a TEST_STATEID operation
9277 *
9278 * @server: server / transport on which to perform the operation
9279 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009280 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009281 *
9282 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9283 * Otherwise a negative NFS4ERR value is returned if the operation
9284 * failed or the state ID is not currently valid.
9285 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009286static int nfs41_test_stateid(struct nfs_server *server,
9287 nfs4_stateid *stateid,
9288 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009289{
9290 struct nfs4_exception exception = { };
9291 int err;
9292 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009293 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009294 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009295 } while (exception.retry);
9296 return err;
9297}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009298
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009299struct nfs_free_stateid_data {
9300 struct nfs_server *server;
9301 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009302 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009303};
9304
9305static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9306{
9307 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009308 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009309 &data->args.seq_args,
9310 &data->res.seq_res,
9311 task);
9312}
9313
9314static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9315{
9316 struct nfs_free_stateid_data *data = calldata;
9317
9318 nfs41_sequence_done(task, &data->res.seq_res);
9319
9320 switch (task->tk_status) {
9321 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009322 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009323 rpc_restart_call_prepare(task);
9324 }
9325}
9326
9327static void nfs41_free_stateid_release(void *calldata)
9328{
9329 kfree(calldata);
9330}
9331
Trond Myklebust17f26b12013-08-21 15:48:42 -04009332static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009333 .rpc_call_prepare = nfs41_free_stateid_prepare,
9334 .rpc_call_done = nfs41_free_stateid_done,
9335 .rpc_release = nfs41_free_stateid_release,
9336};
9337
Anna Schumaker2f261022018-05-15 13:03:39 -04009338/**
9339 * nfs41_free_stateid - perform a FREE_STATEID operation
9340 *
9341 * @server: server / transport on which to perform the operation
9342 * @stateid: state ID to release
9343 * @cred: credential
9344 * @is_recovery: set to true if this call needs to be privileged
9345 *
9346 * Note: this function is always asynchronous.
9347 */
9348static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009349 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009350 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009351 bool privileged)
9352{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009353 struct rpc_message msg = {
9354 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009355 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009356 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009357 struct rpc_task_setup task_setup = {
9358 .rpc_client = server->client,
9359 .rpc_message = &msg,
9360 .callback_ops = &nfs41_free_stateid_ops,
9361 .flags = RPC_TASK_ASYNC,
9362 };
9363 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009364 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009365
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009366 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9367 &task_setup.rpc_client, &msg);
9368
Chuck Lever38527b12012-07-11 16:30:23 -04009369 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009370 data = kmalloc(sizeof(*data), GFP_NOFS);
9371 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009372 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009373 data->server = server;
9374 nfs4_stateid_copy(&data->args.stateid, stateid);
9375
9376 task_setup.callback_data = data;
9377
9378 msg.rpc_argp = &data->args;
9379 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009380 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009381 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009382 if (IS_ERR(task))
9383 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009384 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009385 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009386}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009387
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009388static void
9389nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009390{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009391 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009392
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009393 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009394 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009395}
9396
Trond Myklebust36281ca2012-03-04 18:13:56 -05009397static bool nfs41_match_stateid(const nfs4_stateid *s1,
9398 const nfs4_stateid *s2)
9399{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009400 if (s1->type != s2->type)
9401 return false;
9402
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009403 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009404 return false;
9405
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009406 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009407 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009408
Anna Schumaker045c5512017-01-11 16:59:48 -05009409 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009410}
9411
Andy Adamson557134a2009-04-01 09:21:53 -04009412#endif /* CONFIG_NFS_V4_1 */
9413
Trond Myklebust36281ca2012-03-04 18:13:56 -05009414static bool nfs4_match_stateid(const nfs4_stateid *s1,
9415 const nfs4_stateid *s2)
9416{
Trond Myklebustf597c532012-03-04 18:13:56 -05009417 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009418}
9419
9420
Trond Myklebust17280172012-03-11 13:11:00 -04009421static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009422 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009423 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 .recover_open = nfs4_open_reclaim,
9425 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009426 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009427 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428};
9429
Andy Adamson591d71c2009-04-01 09:22:47 -04009430#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009431static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009432 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9433 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9434 .recover_open = nfs4_open_reclaim,
9435 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009436 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009437 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009438 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009439};
9440#endif /* CONFIG_NFS_V4_1 */
9441
Trond Myklebust17280172012-03-11 13:11:00 -04009442static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009443 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009444 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009445 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009447 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448};
9449
Andy Adamson591d71c2009-04-01 09:22:47 -04009450#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009451static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009452 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9453 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009454 .recover_open = nfs41_open_expired,
9455 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009456 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009457};
9458#endif /* CONFIG_NFS_V4_1 */
9459
Trond Myklebust17280172012-03-11 13:11:00 -04009460static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009461 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009462 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009463 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009464};
9465
9466#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009467static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009468 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009469 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009470 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009471};
9472#endif
9473
Chuck Leverec011fe2013-10-17 14:12:39 -04009474static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009475 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009476 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009477};
9478
9479#if defined(CONFIG_NFS_V4_1)
9480static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009481 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009482 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009483};
9484#endif /* CONFIG_NFS_V4_1 */
9485
Trond Myklebust97dc1352010-06-16 09:52:26 -04009486static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9487 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009488 .init_caps = NFS_CAP_READDIRPLUS
9489 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009490 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009491 .init_client = nfs40_init_client,
9492 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009493 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009494 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009495 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009496 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009497 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009498 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009499 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9500 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9501 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009502 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009503};
9504
9505#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009506static struct nfs_seqid *
9507nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9508{
9509 return NULL;
9510}
9511
Trond Myklebust97dc1352010-06-16 09:52:26 -04009512static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9513 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009514 .init_caps = NFS_CAP_READDIRPLUS
9515 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009516 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009517 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009518 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009519 .init_client = nfs41_init_client,
9520 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009521 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009522 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009523 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009524 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009525 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009526 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009527 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009528 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9529 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9530 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009531 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009532};
9533#endif
9534
Steve Dickson42c2c422013-05-22 12:50:38 -04009535#if defined(CONFIG_NFS_V4_2)
9536static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9537 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009538 .init_caps = NFS_CAP_READDIRPLUS
9539 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009540 | NFS_CAP_POSIX_LOCK
9541 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009542 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009543 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009544 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009545 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009546 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009547 | NFS_CAP_LAYOUTSTATS
9548 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009549 .init_client = nfs41_init_client,
9550 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009551 .match_stateid = nfs41_match_stateid,
9552 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009553 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009554 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009555 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009556 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009557 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009558 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9559 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9560 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009561 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009562};
9563#endif
9564
Trond Myklebust97dc1352010-06-16 09:52:26 -04009565const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9566 [0] = &nfs_v4_0_minor_ops,
9567#if defined(CONFIG_NFS_V4_1)
9568 [1] = &nfs_v4_1_minor_ops,
9569#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009570#if defined(CONFIG_NFS_V4_2)
9571 [2] = &nfs_v4_2_minor_ops,
9572#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009573};
9574
Trond Myklebust13997822016-07-24 17:10:52 -04009575static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009576{
9577 ssize_t error, error2;
9578
9579 error = generic_listxattr(dentry, list, size);
9580 if (error < 0)
9581 return error;
9582 if (list) {
9583 list += error;
9584 size -= error;
9585 }
9586
9587 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9588 if (error2 < 0)
9589 return error2;
9590 return error + error2;
9591}
9592
Trond Myklebust17f26b12013-08-21 15:48:42 -04009593static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009594 .create = nfs_create,
9595 .lookup = nfs_lookup,
9596 .atomic_open = nfs_atomic_open,
9597 .link = nfs_link,
9598 .unlink = nfs_unlink,
9599 .symlink = nfs_symlink,
9600 .mkdir = nfs_mkdir,
9601 .rmdir = nfs_rmdir,
9602 .mknod = nfs_mknod,
9603 .rename = nfs_rename,
9604 .permission = nfs_permission,
9605 .getattr = nfs_getattr,
9606 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009607 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009608};
9609
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009610static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009611 .permission = nfs_permission,
9612 .getattr = nfs_getattr,
9613 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009614 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009615};
9616
David Howells509de812006-08-22 20:06:11 -04009617const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618 .version = 4, /* protocol version */
9619 .dentry_ops = &nfs4_dentry_operations,
9620 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009621 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009622 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009624 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009625 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009626 .getattr = nfs4_proc_getattr,
9627 .setattr = nfs4_proc_setattr,
9628 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009629 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630 .access = nfs4_proc_access,
9631 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 .create = nfs4_proc_create,
9633 .remove = nfs4_proc_remove,
9634 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009635 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009637 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009638 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009639 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640 .link = nfs4_proc_link,
9641 .symlink = nfs4_proc_symlink,
9642 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009643 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644 .readdir = nfs4_proc_readdir,
9645 .mknod = nfs4_proc_mknod,
9646 .statfs = nfs4_proc_statfs,
9647 .fsinfo = nfs4_proc_fsinfo,
9648 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009649 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009650 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009651 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009653 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009655 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009657 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009658 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009660 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009661 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009662 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009663 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009664 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009665 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009666 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009667 .create_server = nfs4_create_server,
9668 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009669};
9670
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009671static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009672 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009673 .list = nfs4_xattr_list_nfs4_acl,
9674 .get = nfs4_xattr_get_nfs4_acl,
9675 .set = nfs4_xattr_set_nfs4_acl,
9676};
9677
9678const struct xattr_handler *nfs4_xattr_handlers[] = {
9679 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009680#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9681 &nfs4_xattr_nfs4_label_handler,
9682#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009683 NULL
9684};
9685
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686/*
9687 * Local variables:
9688 * c-basic-offset: 8
9689 * End:
9690 */