blob: 844eafbf1564cfacce1b7329b71eefbe28a49407 [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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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;
Trond Myklebust8487c472016-06-26 08:44:35 -04003035 bool truncate;
3036 int status;
3037
3038 nfs_fattr_init(res->fattr);
3039
3040 /* Servers should only apply open mode checks for file size changes */
3041 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003042 if (!truncate)
3043 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003044
Trond Myklebust991eedb2018-04-09 11:15:30 -04003045 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003046 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003047 } else if (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;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003059 } else {
3060zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003061 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003062 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003063 if (delegation_cred)
3064 msg.rpc_cred = delegation_cred;
3065
3066 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3067
3068 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003069 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003070 renew_lease(server, timestamp);
3071 trace_nfs4_setattr(inode, &arg->stateid, status);
3072 return status;
3073}
3074
3075static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3076 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003077 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003078 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003079{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003080 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003081 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003082 struct nfs_setattrargs arg = {
3083 .fh = NFS_FH(inode),
3084 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003085 .server = server,
3086 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003087 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003088 };
3089 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003091 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003092 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003093 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003094 struct nfs4_exception exception = {
3095 .state = state,
3096 .inode = inode,
3097 .stateid = &arg.stateid,
3098 };
3099 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003100
David Quigleyaa9c2662013-05-22 12:50:44 -04003101 arg.bitmask = nfs4_bitmask(server, ilabel);
3102 if (ilabel)
3103 arg.bitmask = nfs4_bitmask(server, olabel);
3104
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003106 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003107 switch (err) {
3108 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003109 if (!(sattr->ia_valid & ATTR_SIZE)) {
3110 pr_warn_once("NFSv4: server %s is incorrectly "
3111 "applying open mode checks to "
3112 "a SETATTR that is not "
3113 "changing file size.\n",
3114 server->nfs_client->cl_hostname);
3115 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003116 if (state && !(state->state & FMODE_WRITE)) {
3117 err = -EBADF;
3118 if (sattr->ia_valid & ATTR_OPEN)
3119 err = -EACCES;
3120 goto out;
3121 }
3122 }
3123 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003124 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003125out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003126 return err;
3127}
3128
Peng Tao500d7012015-09-22 11:35:22 +08003129static bool
3130nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3131{
3132 if (inode == NULL || !nfs_have_layout(inode))
3133 return false;
3134
3135 return pnfs_wait_on_layoutreturn(inode, task);
3136}
3137
Linus Torvalds1da177e2005-04-16 15:20:36 -07003138struct nfs4_closedata {
3139 struct inode *inode;
3140 struct nfs4_state *state;
3141 struct nfs_closeargs arg;
3142 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003143 struct {
3144 struct nfs4_layoutreturn_args arg;
3145 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003146 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003147 u32 roc_barrier;
3148 bool roc;
3149 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003150 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003151 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003152};
3153
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003154static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003155{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003156 struct nfs4_closedata *calldata = data;
3157 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003158 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003159
Trond Myklebustcf805162016-11-15 14:56:07 -05003160 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003161 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3162 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003163 nfs4_put_open_state(calldata->state);
3164 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003165 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003166 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003167 kfree(calldata);
3168}
3169
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003170static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003171{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003172 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003173 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003174 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003175 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003176 struct nfs4_exception exception = {
3177 .state = state,
3178 .inode = calldata->inode,
3179 .stateid = &calldata->arg.stateid,
3180 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003181
Chuck Levera3ca5652012-03-01 17:00:40 -05003182 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003183 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3184 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003185 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003186
3187 /* Handle Layoutreturn errors */
3188 if (calldata->arg.lr_args && task->tk_status != 0) {
3189 switch (calldata->res.lr_ret) {
3190 default:
3191 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3192 break;
3193 case 0:
3194 calldata->arg.lr_args = NULL;
3195 calldata->res.lr_res = NULL;
3196 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003197 case -NFS4ERR_OLD_STATEID:
3198 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003199 calldata->inode))
3200 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003201 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003202 case -NFS4ERR_ADMIN_REVOKED:
3203 case -NFS4ERR_DELEG_REVOKED:
3204 case -NFS4ERR_EXPIRED:
3205 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003206 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3207 case -NFS4ERR_WRONG_CRED:
3208 calldata->arg.lr_args = NULL;
3209 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003210 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003211 }
3212 }
3213
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003214 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003215 * the state_owner. we keep this around to process errors
3216 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003217 switch (task->tk_status) {
3218 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003219 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003220 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003221 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003222 case -NFS4ERR_ACCESS:
3223 if (calldata->arg.bitmask != NULL) {
3224 calldata->arg.bitmask = NULL;
3225 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003226 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003227
3228 }
3229 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003230 case -NFS4ERR_OLD_STATEID:
3231 /* Did we race with OPEN? */
3232 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003233 state))
3234 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003235 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003236 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003237 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003238 case -NFS4ERR_EXPIRED:
3239 nfs4_free_revoked_stateid(server,
3240 &calldata->arg.stateid,
3241 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003242 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003243 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003244 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003245 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003246 task->tk_status = nfs4_async_handle_exception(task,
3247 server, task->tk_status, &exception);
3248 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003249 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003251 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3252 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003253out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003254 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003255 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003256 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003257 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003258 return;
3259lr_restart:
3260 calldata->res.lr_ret = 0;
3261out_restart:
3262 task->tk_status = 0;
3263 rpc_restart_call_prepare(task);
3264 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003265}
3266
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003267static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003268{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003269 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003270 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003271 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003272 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003273 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003274
Chuck Levera3ca5652012-03-01 17:00:40 -05003275 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003276 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003277 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003278
Trond Myklebust88069f72009-12-08 08:33:16 -05003279 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003280 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003281 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3282 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3283 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003284 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003285 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003286 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003287 if (state->n_rdonly == 0)
3288 call_close |= is_rdonly;
3289 else if (is_rdonly)
3290 calldata->arg.fmode |= FMODE_READ;
3291 if (state->n_wronly == 0)
3292 call_close |= is_wronly;
3293 else if (is_wronly)
3294 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003295 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3296 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003297 } else if (is_rdwr)
3298 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3299
Trond Myklebust5cc78612016-11-14 11:19:56 -05003300 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003301 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003302 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003303 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003304
3305 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003306 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003307 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003308 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003309
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003310 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003311 nfs_release_seqid(calldata->arg.seqid);
3312 goto out_wait;
3313 }
3314
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003315 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003316 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003317
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003318 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003319 /* Close-to-open cache consistency revalidation */
3320 if (!nfs4_have_delegation(inode, FMODE_READ))
3321 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3322 else
3323 calldata->arg.bitmask = NULL;
3324 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003325
Trond Myklebust6ae37332015-01-30 14:21:14 -05003326 calldata->arg.share_access =
3327 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3328 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003329
Trond Myklebustd8d84982016-12-19 12:14:44 -05003330 if (calldata->res.fattr == NULL)
3331 calldata->arg.bitmask = NULL;
3332 else if (calldata->arg.bitmask == NULL)
3333 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003334 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003335 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003336 &calldata->arg.seq_args,
3337 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003338 task) != 0)
3339 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003340 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003341 return;
3342out_no_action:
3343 task->tk_action = NULL;
3344out_wait:
3345 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003346}
3347
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003348static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003349 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003350 .rpc_call_done = nfs4_close_done,
3351 .rpc_release = nfs4_free_closedata,
3352};
3353
Linus Torvalds1da177e2005-04-16 15:20:36 -07003354/*
3355 * It is possible for data to be read/written from a mem-mapped file
3356 * after the sys_close call (which hits the vfs layer as a flush).
3357 * This means that we can't safely call nfsv4 close on a file until
3358 * the inode is cleared. This in turn means that we are not good
3359 * NFSv4 citizens - we do not indicate to the server to update the file's
3360 * share state even when we are done with one of the three share
3361 * stateid's in the inode.
3362 *
3363 * NOTE: Caller must be holding the sp->so_owner semaphore!
3364 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003365int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003367 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003368 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003369 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003370 struct nfs4_state_owner *sp = state->owner;
3371 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003372 struct rpc_message msg = {
3373 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3374 .rpc_cred = state->owner->so_cred,
3375 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003376 struct rpc_task_setup task_setup_data = {
3377 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003378 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003379 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003380 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003381 .flags = RPC_TASK_ASYNC,
3382 };
Trond Myklebust95121352005-10-18 14:20:12 -07003383 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003384
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003385 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3386 &task_setup_data.rpc_client, &msg);
3387
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003388 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003390 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003391 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003392 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003394 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003395 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3396 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003397 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003398 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3399 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003400 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003401 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003402 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003403 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003404 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003405 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003406 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003407 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003408 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003409 calldata->lr.roc = pnfs_roc(state->inode,
3410 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3411 if (calldata->lr.roc) {
3412 calldata->arg.lr_args = &calldata->lr.arg;
3413 calldata->res.lr_res = &calldata->lr.res;
3414 }
Al Viro643168c2011-06-22 18:20:23 -04003415 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003416
Trond Myklebust1174dd12010-12-21 10:52:24 -05003417 msg.rpc_argp = &calldata->arg;
3418 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003419 task_setup_data.callback_data = calldata;
3420 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003421 if (IS_ERR(task))
3422 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003423 status = 0;
3424 if (wait)
3425 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003426 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003427 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003428out_free_calldata:
3429 kfree(calldata);
3430out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003431 nfs4_put_open_state(state);
3432 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003433 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003434}
3435
Trond Myklebust2b484292010-09-17 10:56:51 -04003436static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003437nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3438 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003441 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3442
3443 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444
Trond Myklebust565277f2007-10-15 18:17:53 -04003445 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003446 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003447
3448 nfs4_label_release_security(label);
3449
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003450 if (IS_ERR(state))
3451 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003452 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453}
3454
Trond Myklebust1185a552009-12-03 15:54:02 -05003455static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003456{
3457 if (ctx->state == NULL)
3458 return;
3459 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003460 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003461 else
Al Viro643168c2011-06-22 18:20:23 -04003462 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003463}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464
Trond Myklebustb944dba2013-11-04 15:20:20 -05003465#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3466#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003467#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003468
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3470{
Kinglong Mee8c612822015-08-26 21:12:58 +08003471 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003472 struct nfs4_server_caps_arg args = {
3473 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003474 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003475 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 struct nfs4_server_caps_res res = {};
3477 struct rpc_message msg = {
3478 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003479 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480 .rpc_resp = &res,
3481 };
3482 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003483 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003484
Kinglong Mee8c612822015-08-26 21:12:58 +08003485 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3486 FATTR4_WORD0_FH_EXPIRE_TYPE |
3487 FATTR4_WORD0_LINK_SUPPORT |
3488 FATTR4_WORD0_SYMLINK_SUPPORT |
3489 FATTR4_WORD0_ACLSUPPORT;
3490 if (minorversion)
3491 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3492
Bryan Schumaker7c513052011-03-24 17:12:24 +00003493 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003494 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003495 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003496 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003497 case 0:
3498 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3499 res.attr_bitmask[2] = 0;
3500 break;
3501 case 1:
3502 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3503 break;
3504 case 2:
3505 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3506 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003507 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003508 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3509 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3510 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3511 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003512 NFS_CAP_CTIME|NFS_CAP_MTIME|
3513 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003514 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3515 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003516 server->caps |= NFS_CAP_ACLS;
3517 if (res.has_links != 0)
3518 server->caps |= NFS_CAP_HARDLINKS;
3519 if (res.has_symlinks != 0)
3520 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003521 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3522 server->caps |= NFS_CAP_FILEID;
3523 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3524 server->caps |= NFS_CAP_MODE;
3525 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3526 server->caps |= NFS_CAP_NLINK;
3527 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3528 server->caps |= NFS_CAP_OWNER;
3529 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3530 server->caps |= NFS_CAP_OWNER_GROUP;
3531 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3532 server->caps |= NFS_CAP_ATIME;
3533 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3534 server->caps |= NFS_CAP_CTIME;
3535 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3536 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003537#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3538 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3539 server->caps |= NFS_CAP_SECURITY_LABEL;
3540#endif
3541 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3542 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003543 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab460c2009-08-09 15:06:19 -04003544
Trond Myklebusta65318b2009-03-11 14:10:28 -04003545 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3546 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3547 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003548 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003549
3550 /* Avoid a regression due to buggy server */
3551 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3552 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003553 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3554 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003557 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003558 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003559
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560 return status;
3561}
3562
Trond Myklebust55a97592006-06-09 09:34:19 -04003563int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003564{
3565 struct nfs4_exception exception = { };
3566 int err;
3567 do {
3568 err = nfs4_handle_exception(server,
3569 _nfs4_server_capabilities(server, fhandle),
3570 &exception);
3571 } while (exception.retry);
3572 return err;
3573}
3574
3575static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3576 struct nfs_fsinfo *info)
3577{
David Quigleyaa9c2662013-05-22 12:50:44 -04003578 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003579 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003580 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 };
3582 struct nfs4_lookup_res res = {
3583 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003584 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003585 .fh = fhandle,
3586 };
3587 struct rpc_message msg = {
3588 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3589 .rpc_argp = &args,
3590 .rpc_resp = &res,
3591 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003592
David Quigleyaa9c2662013-05-22 12:50:44 -04003593 bitmask[0] = nfs4_fattr_bitmap[0];
3594 bitmask[1] = nfs4_fattr_bitmap[1];
3595 /*
3596 * Process the label in the upcoming getfattr
3597 */
3598 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3599
Trond Myklebust0e574af2005-10-27 22:12:38 -04003600 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003601 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003602}
3603
3604static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3605 struct nfs_fsinfo *info)
3606{
3607 struct nfs4_exception exception = { };
3608 int err;
3609 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003610 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003611 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003612 switch (err) {
3613 case 0:
3614 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003615 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003616 default:
3617 err = nfs4_handle_exception(server, err, &exception);
3618 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003619 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003620out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003621 return err;
3622}
3623
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003624static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3625 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3626{
Trond Myklebustc2190662013-08-26 19:23:04 -04003627 struct rpc_auth_create_args auth_args = {
3628 .pseudoflavor = flavor,
3629 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003630 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003631
Trond Myklebustc2190662013-08-26 19:23:04 -04003632 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003633 if (IS_ERR(auth))
3634 return -EACCES;
3635 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003636}
3637
Chuck Lever9a744ba2013-03-16 15:56:02 -04003638/*
3639 * Retry pseudoroot lookup with various security flavors. We do this when:
3640 *
3641 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3642 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3643 *
3644 * Returns zero on success, or a negative NFS4ERR value, or a
3645 * negative errno value.
3646 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003647static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003648 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003649{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003650 /* Per 3530bis 15.33.5 */
3651 static const rpc_authflavor_t flav_array[] = {
3652 RPC_AUTH_GSS_KRB5P,
3653 RPC_AUTH_GSS_KRB5I,
3654 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003655 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003656 RPC_AUTH_NULL,
3657 };
3658 int status = -EPERM;
3659 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003660
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003661 if (server->auth_info.flavor_len > 0) {
3662 /* try each flavor specified by user */
3663 for (i = 0; i < server->auth_info.flavor_len; i++) {
3664 status = nfs4_lookup_root_sec(server, fhandle, info,
3665 server->auth_info.flavors[i]);
3666 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3667 continue;
3668 break;
3669 }
3670 } else {
3671 /* no flavors specified by user, try default list */
3672 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3673 status = nfs4_lookup_root_sec(server, fhandle, info,
3674 flav_array[i]);
3675 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3676 continue;
3677 break;
3678 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003679 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003680
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003681 /*
3682 * -EACCESS could mean that the user doesn't have correct permissions
3683 * to access the mount. It could also mean that we tried to mount
3684 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3685 * existing mount programs don't handle -EACCES very well so it should
3686 * be mapped to -EPERM instead.
3687 */
3688 if (status == -EACCES)
3689 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003690 return status;
3691}
3692
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003693/**
3694 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3695 * @server: initialized nfs_server handle
3696 * @fhandle: we fill in the pseudo-fs root file handle
3697 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003698 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003699 *
3700 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003701 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003702int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003703 struct nfs_fsinfo *info,
3704 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003705{
Andre Przywarac7757072015-04-23 17:17:40 +01003706 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003707
Andre Przywarac7757072015-04-23 17:17:40 +01003708 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003709 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003710
3711 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003712 status = server->nfs_client->cl_mvops->find_root_sec(server,
3713 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003714
Linus Torvalds1da177e2005-04-16 15:20:36 -07003715 if (status == 0)
3716 status = nfs4_server_capabilities(server, fhandle);
3717 if (status == 0)
3718 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003719
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003720 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003721}
3722
Bryan Schumakerbae36242012-05-10 15:07:31 -04003723static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3724 struct nfs_fsinfo *info)
3725{
3726 int error;
3727 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003728 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003729
3730 error = nfs4_server_capabilities(server, mntfh);
3731 if (error < 0) {
3732 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3733 return error;
3734 }
3735
David Quigley14c43f72013-05-22 12:50:43 -04003736 label = nfs4_label_alloc(server, GFP_KERNEL);
3737 if (IS_ERR(label))
3738 return PTR_ERR(label);
3739
David Quigley1775fd32013-05-22 12:50:42 -04003740 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003741 if (error < 0) {
3742 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003743 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003744 }
3745
3746 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3747 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3748 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3749
David Quigley14c43f72013-05-22 12:50:43 -04003750err_free_label:
3751 nfs4_label_free(label);
3752
Bryan Schumakerbae36242012-05-10 15:07:31 -04003753 return error;
3754}
3755
Manoj Naik6b97fd32006-06-09 09:34:29 -04003756/*
3757 * Get locations and (maybe) other attributes of a referral.
3758 * Note that we'll actually follow the referral later when
3759 * we detect fsid mismatch in inode revalidation
3760 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003761static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3762 const struct qstr *name, struct nfs_fattr *fattr,
3763 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003764{
3765 int status = -ENOMEM;
3766 struct page *page = NULL;
3767 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003768
3769 page = alloc_page(GFP_KERNEL);
3770 if (page == NULL)
3771 goto out;
3772 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3773 if (locations == NULL)
3774 goto out;
3775
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003776 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003777 if (status != 0)
3778 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003779
3780 /*
3781 * If the fsid didn't change, this is a migration event, not a
3782 * referral. Cause us to drop into the exception handler, which
3783 * will kick off migration recovery.
3784 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003785 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003786 dprintk("%s: server did not return a different fsid for"
3787 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003788 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003789 goto out;
3790 }
Andy Adamson533eb462011-06-13 18:25:56 -04003791 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3792 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003793
Andy Adamson533eb462011-06-13 18:25:56 -04003794 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003795 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003796 memset(fhandle, 0, sizeof(struct nfs_fh));
3797out:
3798 if (page)
3799 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003800 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003801 return status;
3802}
3803
David Quigley1775fd32013-05-22 12:50:42 -04003804static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3805 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003806{
3807 struct nfs4_getattr_arg args = {
3808 .fh = fhandle,
3809 .bitmask = server->attr_bitmask,
3810 };
3811 struct nfs4_getattr_res res = {
3812 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003813 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814 .server = server,
3815 };
3816 struct rpc_message msg = {
3817 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3818 .rpc_argp = &args,
3819 .rpc_resp = &res,
3820 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003821
3822 args.bitmask = nfs4_bitmask(server, label);
3823
Trond Myklebust0e574af2005-10-27 22:12:38 -04003824 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003825 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826}
3827
David Quigley1775fd32013-05-22 12:50:42 -04003828static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3829 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003830{
3831 struct nfs4_exception exception = { };
3832 int err;
3833 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003834 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3835 trace_nfs4_getattr(server, fhandle, fattr, err);
3836 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003837 &exception);
3838 } while (exception.retry);
3839 return err;
3840}
3841
3842/*
3843 * The file is not closed if it is opened due to the a request to change
3844 * the size of the file. The open call will not be needed once the
3845 * VFS layer lookup-intents are implemented.
3846 *
3847 * Close is called when the inode is destroyed.
3848 * If we haven't opened the file for O_WRONLY, we
3849 * need to in the size_change case to obtain a stateid.
3850 *
3851 * Got race?
3852 * Because OPEN is always done by name in nfsv4, it is
3853 * possible that we opened a different file by the same
3854 * name. We can recognize this race condition, but we
3855 * can't do anything about it besides returning an error.
3856 *
3857 * This will be fixed with VFS changes (lookup-intent).
3858 */
3859static int
3860nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3861 struct iattr *sattr)
3862{
David Howells2b0143b2015-03-17 22:25:59 +00003863 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003864 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003865 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003866 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003867 int status;
3868
Peng Tao88ac8152014-09-12 11:04:10 +08003869 if (pnfs_ld_layoutret_on_setattr(inode) &&
3870 sattr->ia_valid & ATTR_SIZE &&
3871 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003872 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003873
Trond Myklebust0e574af2005-10-27 22:12:38 -04003874 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003875
Andy Adamson26699402012-05-30 16:12:24 -04003876 /* Deal with open(O_TRUNC) */
3877 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003878 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003879
3880 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003881 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003882 return 0;
3883
Trond Myklebustd5308382005-11-04 15:33:38 -05003884 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003885 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003886
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003887 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003888 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003889 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003890 }
3891
David Quigley14c43f72013-05-22 12:50:43 -04003892 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3893 if (IS_ERR(label))
3894 return PTR_ERR(label);
3895
Trond Myklebust199366f2018-03-20 16:43:18 -04003896 /* Return any delegations if we're going to change ACLs */
3897 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003898 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003899
NeilBrown29b59f92016-10-13 15:26:47 +11003900 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003901 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003902 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003903 nfs_setsecurity(inode, fattr, label);
3904 }
David Quigley14c43f72013-05-22 12:50:43 -04003905 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003906 return status;
3907}
3908
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003909static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3910 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003911 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003912{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003913 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003914 int status;
3915 struct nfs4_lookup_arg args = {
3916 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003917 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003918 .name = name,
3919 };
3920 struct nfs4_lookup_res res = {
3921 .server = server,
3922 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003923 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003924 .fh = fhandle,
3925 };
3926 struct rpc_message msg = {
3927 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3928 .rpc_argp = &args,
3929 .rpc_resp = &res,
3930 };
3931
David Quigleyaa9c2662013-05-22 12:50:44 -04003932 args.bitmask = nfs4_bitmask(server, label);
3933
David Howells2b3de442006-08-22 20:06:09 -04003934 nfs_fattr_init(fattr);
3935
Linus Torvalds1da177e2005-04-16 15:20:36 -07003936 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003937 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003938 dprintk("NFS reply lookup: %d\n", status);
3939 return status;
3940}
3941
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003942static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003943{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003944 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003945 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003946 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3947 fattr->nlink = 2;
3948}
3949
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003950static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003951 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003952 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003953{
3954 struct nfs4_exception exception = { };
3955 struct rpc_clnt *client = *clnt;
3956 int err;
3957 do {
David Quigley1775fd32013-05-22 12:50:42 -04003958 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003959 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003960 switch (err) {
3961 case -NFS4ERR_BADNAME:
3962 err = -ENOENT;
3963 goto out;
3964 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003965 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003966 if (err == -NFS4ERR_MOVED)
3967 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003968 goto out;
3969 case -NFS4ERR_WRONGSEC:
3970 err = -EPERM;
3971 if (client != *clnt)
3972 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003973 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003974 if (IS_ERR(client))
3975 return PTR_ERR(client);
3976
3977 exception.retry = 1;
3978 break;
3979 default:
3980 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3981 }
3982 } while (exception.retry);
3983
3984out:
3985 if (err == 0)
3986 *clnt = client;
3987 else if (client != *clnt)
3988 rpc_shutdown_client(client);
3989
3990 return err;
3991}
3992
Al Virobeffb8f2016-07-20 16:34:42 -04003993static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003994 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3995 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003996{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003997 int status;
3998 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003999
David Quigley1775fd32013-05-22 12:50:42 -04004000 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004001 if (client != NFS_CLIENT(dir)) {
4002 rpc_shutdown_client(client);
4003 nfs_fixup_secinfo_attributes(fattr);
4004 }
4005 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004006}
4007
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004008struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004009nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004010 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4011{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004012 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004013 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004014
David Quigley1775fd32013-05-22 12:50:42 -04004015 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004016 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004017 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004018 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004019}
4020
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004021static int _nfs4_proc_lookupp(struct inode *inode,
4022 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4023 struct nfs4_label *label)
4024{
4025 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4026 struct nfs_server *server = NFS_SERVER(inode);
4027 int status;
4028 struct nfs4_lookupp_arg args = {
4029 .bitmask = server->attr_bitmask,
4030 .fh = NFS_FH(inode),
4031 };
4032 struct nfs4_lookupp_res res = {
4033 .server = server,
4034 .fattr = fattr,
4035 .label = label,
4036 .fh = fhandle,
4037 };
4038 struct rpc_message msg = {
4039 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4040 .rpc_argp = &args,
4041 .rpc_resp = &res,
4042 };
4043
4044 args.bitmask = nfs4_bitmask(server, label);
4045
4046 nfs_fattr_init(fattr);
4047
4048 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4049 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4050 &res.seq_res, 0);
4051 dprintk("NFS reply lookupp: %d\n", status);
4052 return status;
4053}
4054
4055static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4056 struct nfs_fattr *fattr, struct nfs4_label *label)
4057{
4058 struct nfs4_exception exception = { };
4059 int err;
4060 do {
4061 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4062 trace_nfs4_lookupp(inode, err);
4063 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4064 &exception);
4065 } while (exception.retry);
4066 return err;
4067}
4068
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4070{
Trond Myklebust76b32992007-08-10 17:45:11 -04004071 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004072 struct nfs4_accessargs args = {
4073 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004074 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004076 struct nfs4_accessres res = {
4077 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004078 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004079 struct rpc_message msg = {
4080 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4081 .rpc_argp = &args,
4082 .rpc_resp = &res,
4083 .rpc_cred = entry->cred,
4084 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004085 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004086
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004087 if (!nfs_have_delegated_attributes(inode)) {
4088 res.fattr = nfs_alloc_fattr();
4089 if (res.fattr == NULL)
4090 return -ENOMEM;
4091 args.bitmask = server->cache_consistency_bitmask;
4092 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004093
Bryan Schumaker7c513052011-03-24 17:12:24 +00004094 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004096 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004097 if (res.fattr)
4098 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004099 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004100 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004101 return status;
4102}
4103
4104static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4105{
4106 struct nfs4_exception exception = { };
4107 int err;
4108 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004109 err = _nfs4_proc_access(inode, entry);
4110 trace_nfs4_access(inode, err);
4111 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004112 &exception);
4113 } while (exception.retry);
4114 return err;
4115}
4116
4117/*
4118 * TODO: For the time being, we don't try to get any attributes
4119 * along with any of the zero-copy operations READ, READDIR,
4120 * READLINK, WRITE.
4121 *
4122 * In the case of the first three, we want to put the GETATTR
4123 * after the read-type operation -- this is because it is hard
4124 * to predict the length of a GETATTR response in v4, and thus
4125 * align the READ data correctly. This means that the GETATTR
4126 * may end up partially falling into the page cache, and we should
4127 * shift it into the 'tail' of the xdr_buf before processing.
4128 * To do this efficiently, we need to know the total length
4129 * of data received, which doesn't seem to be available outside
4130 * of the RPC layer.
4131 *
4132 * In the case of WRITE, we also want to put the GETATTR after
4133 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004134 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004135 *
4136 * Both of these changes to the XDR layer would in fact be quite
4137 * minor, but I decided to leave them for a subsequent patch.
4138 */
4139static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4140 unsigned int pgbase, unsigned int pglen)
4141{
4142 struct nfs4_readlink args = {
4143 .fh = NFS_FH(inode),
4144 .pgbase = pgbase,
4145 .pglen = pglen,
4146 .pages = &page,
4147 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004148 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 struct rpc_message msg = {
4150 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4151 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004152 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 };
4154
Bryan Schumaker7c513052011-03-24 17:12:24 +00004155 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 -07004156}
4157
4158static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4159 unsigned int pgbase, unsigned int pglen)
4160{
4161 struct nfs4_exception exception = { };
4162 int err;
4163 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004164 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4165 trace_nfs4_readlink(inode, err);
4166 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004167 &exception);
4168 } while (exception.retry);
4169 return err;
4170}
4171
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004173 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175static int
4176nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004177 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004179 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004180 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004181 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 int status = 0;
4184
NeilBrown532d4de2016-10-13 15:26:47 +11004185 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004186 if (IS_ERR(ctx))
4187 return PTR_ERR(ctx);
4188
David Quigleyaa9c2662013-05-22 12:50:44 -04004189 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4190
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004191 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4192 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004193 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194 if (IS_ERR(state)) {
4195 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004196 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004198out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004199 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004200 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201 return status;
4202}
4203
Al Virobeffb8f2016-07-20 16:34:42 -04004204static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004205{
Trond Myklebust16e42952005-10-27 22:12:44 -04004206 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004207 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004208 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004209 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004211 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004212 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004213 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4216 .rpc_argp = &args,
4217 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004218 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004219 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004220 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004221
Bryan Schumaker7c513052011-03-24 17:12:24 +00004222 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004223 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004224 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 return status;
4226}
4227
Trond Myklebust912678d2018-03-20 16:43:15 -04004228static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4229{
4230 struct nfs4_exception exception = { };
4231 struct inode *inode = d_inode(dentry);
4232 int err;
4233
Trond Myklebustc01d3642018-03-20 16:43:20 -04004234 if (inode) {
4235 if (inode->i_nlink == 1)
4236 nfs4_inode_return_delegation(inode);
4237 else
4238 nfs4_inode_make_writeable(inode);
4239 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004240 do {
4241 err = _nfs4_proc_remove(dir, &dentry->d_name);
4242 trace_nfs4_remove(dir, &dentry->d_name, err);
4243 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4244 &exception);
4245 } while (exception.retry);
4246 return err;
4247}
4248
4249static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250{
4251 struct nfs4_exception exception = { };
4252 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004253
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004255 err = _nfs4_proc_remove(dir, name);
4256 trace_nfs4_remove(dir, name, err);
4257 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258 &exception);
4259 } while (exception.retry);
4260 return err;
4261}
4262
Trond Myklebust977fcc22018-03-20 16:43:17 -04004263static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004264{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004265 struct nfs_removeargs *args = msg->rpc_argp;
4266 struct nfs_removeres *res = msg->rpc_resp;
Trond Myklebust977fcc22018-03-20 16:43:17 -04004267 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004268
Trond Myklebust977fcc22018-03-20 16:43:17 -04004269 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004270 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004271 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004272
4273 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004274
4275 if (inode)
4276 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004277}
4278
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004279static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4280{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004281 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004282 &data->args.seq_args,
4283 &data->res.seq_res,
4284 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285}
4286
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004287static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004288{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004289 struct nfs_unlinkdata *data = task->tk_calldata;
4290 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004291
Trond Myklebust14516c32010-07-31 14:29:06 -04004292 if (!nfs4_sequence_done(task, &res->seq_res))
4293 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004294 if (nfs4_async_handle_error(task, res->server, NULL,
4295 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004296 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004297 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004298 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004299 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004300}
4301
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004302static void nfs4_proc_rename_setup(struct rpc_message *msg,
4303 struct dentry *old_dentry,
4304 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004305{
Jeff Laytond3d41522010-09-17 17:31:57 -04004306 struct nfs_renameargs *arg = msg->rpc_argp;
4307 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004308 struct inode *old_inode = d_inode(old_dentry);
4309 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004310
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004311 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004312 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004313 if (new_inode)
4314 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004315 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004316 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004317 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004318}
4319
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004320static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4321{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004322 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004323 &data->args.seq_args,
4324 &data->res.seq_res,
4325 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004326}
4327
4328static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4329 struct inode *new_dir)
4330{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004331 struct nfs_renamedata *data = task->tk_calldata;
4332 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004333
4334 if (!nfs4_sequence_done(task, &res->seq_res))
4335 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004336 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004337 return 0;
4338
Trond Myklebustc733c492017-01-11 12:32:26 -05004339 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004340 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004341 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004342 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004343 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004344 return 1;
4345}
4346
Al Virobeffb8f2016-07-20 16:34:42 -04004347static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004348{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004349 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004350 struct nfs4_link_arg arg = {
4351 .fh = NFS_FH(inode),
4352 .dir_fh = NFS_FH(dir),
4353 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004354 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004356 struct nfs4_link_res res = {
4357 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004358 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004359 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 struct rpc_message msg = {
4361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4362 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004363 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004365 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004366
Trond Myklebust136f2622010-04-16 16:22:49 -04004367 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004368 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004369 goto out;
4370
David Quigley14c43f72013-05-22 12:50:43 -04004371 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4372 if (IS_ERR(res.label)) {
4373 status = PTR_ERR(res.label);
4374 goto out;
4375 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004376 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004377
Trond Myklebustc01d3642018-03-20 16:43:20 -04004378 nfs4_inode_make_writeable(inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004379
Bryan Schumaker7c513052011-03-24 17:12:24 +00004380 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004381 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004382 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004383 status = nfs_post_op_update_inode(inode, res.fattr);
4384 if (!status)
4385 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004386 }
David Quigley14c43f72013-05-22 12:50:43 -04004387
4388
4389 nfs4_label_free(res.label);
4390
Trond Myklebust136f2622010-04-16 16:22:49 -04004391out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004392 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004393 return status;
4394}
4395
Al Virobeffb8f2016-07-20 16:34:42 -04004396static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004397{
4398 struct nfs4_exception exception = { };
4399 int err;
4400 do {
4401 err = nfs4_handle_exception(NFS_SERVER(inode),
4402 _nfs4_proc_link(inode, dir, name),
4403 &exception);
4404 } while (exception.retry);
4405 return err;
4406}
4407
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004408struct nfs4_createdata {
4409 struct rpc_message msg;
4410 struct nfs4_create_arg arg;
4411 struct nfs4_create_res res;
4412 struct nfs_fh fh;
4413 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004414 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004415};
4416
4417static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004418 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419{
4420 struct nfs4_createdata *data;
4421
4422 data = kzalloc(sizeof(*data), GFP_KERNEL);
4423 if (data != NULL) {
4424 struct nfs_server *server = NFS_SERVER(dir);
4425
David Quigley14c43f72013-05-22 12:50:43 -04004426 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4427 if (IS_ERR(data->label))
4428 goto out_free;
4429
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004430 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4431 data->msg.rpc_argp = &data->arg;
4432 data->msg.rpc_resp = &data->res;
4433 data->arg.dir_fh = NFS_FH(dir);
4434 data->arg.server = server;
4435 data->arg.name = name;
4436 data->arg.attrs = sattr;
4437 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004438 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004439 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004440 data->res.server = server;
4441 data->res.fh = &data->fh;
4442 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004443 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004444 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445 }
4446 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004447out_free:
4448 kfree(data);
4449 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450}
4451
4452static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4453{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004454 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004455 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004456 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004457 update_changeattr(dir, &data->res.dir_cinfo,
4458 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004459 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004460 }
4461 return status;
4462}
4463
4464static void nfs4_free_createdata(struct nfs4_createdata *data)
4465{
David Quigley14c43f72013-05-22 12:50:43 -04004466 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004467 kfree(data);
4468}
4469
Chuck Lever4f390c12006-08-22 20:06:22 -04004470static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004471 struct page *page, unsigned int len, struct iattr *sattr,
4472 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004474 struct nfs4_createdata *data;
4475 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004476
Chuck Lever94a6d752006-08-22 20:06:23 -04004477 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004478 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004479
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004480 status = -ENOMEM;
4481 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4482 if (data == NULL)
4483 goto out;
4484
4485 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4486 data->arg.u.symlink.pages = &page;
4487 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004488 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004490 status = nfs4_do_create(dir, dentry, data);
4491
4492 nfs4_free_createdata(data);
4493out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494 return status;
4495}
4496
Chuck Lever4f390c12006-08-22 20:06:22 -04004497static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004498 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499{
4500 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004501 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004502 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004503
4504 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4505
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004507 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4508 trace_nfs4_symlink(dir, &dentry->d_name, err);
4509 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 &exception);
4511 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004512
4513 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 return err;
4515}
4516
4517static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004518 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004520 struct nfs4_createdata *data;
4521 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004523 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4524 if (data == NULL)
4525 goto out;
4526
David Quigley1775fd32013-05-22 12:50:42 -04004527 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004528 status = nfs4_do_create(dir, dentry, data);
4529
4530 nfs4_free_createdata(data);
4531out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 return status;
4533}
4534
4535static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4536 struct iattr *sattr)
4537{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004538 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004540 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004542
David Quigleyaa9c2662013-05-22 12:50:44 -04004543 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4544
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004545 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4546 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004548 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4549 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4550 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004551 &exception);
4552 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004553 nfs4_label_release_security(label);
4554
Linus Torvalds1da177e2005-04-16 15:20:36 -07004555 return err;
4556}
4557
4558static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004559 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560{
David Howells2b0143b2015-03-17 22:25:59 +00004561 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 struct nfs4_readdir_arg args = {
4563 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004564 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 .pgbase = 0,
4566 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004567 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004568 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 };
4570 struct nfs4_readdir_res res;
4571 struct rpc_message msg = {
4572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4573 .rpc_argp = &args,
4574 .rpc_resp = &res,
4575 .rpc_cred = cred,
4576 };
4577 int status;
4578
Al Viro6de14722013-09-16 10:53:17 -04004579 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4580 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004581 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004582 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004583 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004584 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 -05004585 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004586 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004587 status += args.pgbase;
4588 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004589
4590 nfs_invalidate_atime(dir);
4591
Harvey Harrison3110ff82008-05-02 13:42:44 -07004592 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 return status;
4594}
4595
4596static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004597 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598{
4599 struct nfs4_exception exception = { };
4600 int err;
4601 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004602 err = _nfs4_proc_readdir(dentry, cred, cookie,
4603 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004604 trace_nfs4_readdir(d_inode(dentry), err);
4605 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004606 &exception);
4607 } while (exception.retry);
4608 return err;
4609}
4610
4611static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004612 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004613{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004614 struct nfs4_createdata *data;
4615 int mode = sattr->ia_mode;
4616 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004618 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4619 if (data == NULL)
4620 goto out;
4621
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004623 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004625 data->arg.ftype = NF4BLK;
4626 data->arg.u.device.specdata1 = MAJOR(rdev);
4627 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004628 }
4629 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004630 data->arg.ftype = NF4CHR;
4631 data->arg.u.device.specdata1 = MAJOR(rdev);
4632 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004633 } else if (!S_ISSOCK(mode)) {
4634 status = -EINVAL;
4635 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004636 }
David Quigley1775fd32013-05-22 12:50:42 -04004637
David Quigleyaa9c2662013-05-22 12:50:44 -04004638 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004639 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004640out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004641 nfs4_free_createdata(data);
4642out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 return status;
4644}
4645
4646static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4647 struct iattr *sattr, dev_t rdev)
4648{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004649 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004650 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004651 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004652 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004653
David Quigleyaa9c2662013-05-22 12:50:44 -04004654 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4655
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004656 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4657 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004658 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004659 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4660 trace_nfs4_mknod(dir, &dentry->d_name, err);
4661 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004662 &exception);
4663 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004664
4665 nfs4_label_release_security(label);
4666
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 return err;
4668}
4669
4670static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4671 struct nfs_fsstat *fsstat)
4672{
4673 struct nfs4_statfs_arg args = {
4674 .fh = fhandle,
4675 .bitmask = server->attr_bitmask,
4676 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004677 struct nfs4_statfs_res res = {
4678 .fsstat = fsstat,
4679 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004680 struct rpc_message msg = {
4681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4682 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004683 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004684 };
4685
Trond Myklebust0e574af2005-10-27 22:12:38 -04004686 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004687 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004688}
4689
4690static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4691{
4692 struct nfs4_exception exception = { };
4693 int err;
4694 do {
4695 err = nfs4_handle_exception(server,
4696 _nfs4_proc_statfs(server, fhandle, fsstat),
4697 &exception);
4698 } while (exception.retry);
4699 return err;
4700}
4701
4702static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4703 struct nfs_fsinfo *fsinfo)
4704{
4705 struct nfs4_fsinfo_arg args = {
4706 .fh = fhandle,
4707 .bitmask = server->attr_bitmask,
4708 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004709 struct nfs4_fsinfo_res res = {
4710 .fsinfo = fsinfo,
4711 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 struct rpc_message msg = {
4713 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4714 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004715 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716 };
4717
Bryan Schumaker7c513052011-03-24 17:12:24 +00004718 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004719}
4720
4721static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4722{
4723 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004724 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725 int err;
4726
4727 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004728 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004729 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004730 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004731 nfs4_set_lease_period(server->nfs_client,
4732 fsinfo->lease_time * HZ,
4733 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004734 break;
4735 }
4736 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 } while (exception.retry);
4738 return err;
4739}
4740
4741static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4742{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004743 int error;
4744
Trond Myklebust0e574af2005-10-27 22:12:38 -04004745 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004746 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004747 if (error == 0) {
4748 /* block layout checks this! */
4749 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004750 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004751 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004752
4753 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004754}
4755
4756static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4757 struct nfs_pathconf *pathconf)
4758{
4759 struct nfs4_pathconf_arg args = {
4760 .fh = fhandle,
4761 .bitmask = server->attr_bitmask,
4762 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004763 struct nfs4_pathconf_res res = {
4764 .pathconf = pathconf,
4765 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004766 struct rpc_message msg = {
4767 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4768 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004769 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004770 };
4771
4772 /* None of the pathconf attributes are mandatory to implement */
4773 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4774 memset(pathconf, 0, sizeof(*pathconf));
4775 return 0;
4776 }
4777
Trond Myklebust0e574af2005-10-27 22:12:38 -04004778 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004779 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004780}
4781
4782static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4783 struct nfs_pathconf *pathconf)
4784{
4785 struct nfs4_exception exception = { };
4786 int err;
4787
4788 do {
4789 err = nfs4_handle_exception(server,
4790 _nfs4_proc_pathconf(server, fhandle, pathconf),
4791 &exception);
4792 } while (exception.retry);
4793 return err;
4794}
4795
Trond Myklebust5521abf2013-03-16 20:54:34 -04004796int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004797 const struct nfs_open_context *ctx,
4798 const struct nfs_lock_context *l_ctx,
4799 fmode_t fmode)
4800{
NeilBrown17393472016-10-13 15:26:47 +11004801 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004802}
4803EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4804
Trond Myklebust5521abf2013-03-16 20:54:34 -04004805static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4806 const struct nfs_open_context *ctx,
4807 const struct nfs_lock_context *l_ctx,
4808 fmode_t fmode)
4809{
4810 nfs4_stateid current_stateid;
4811
Trond Myklebuste1253be2014-03-05 08:44:23 -05004812 /* If the current stateid represents a lost lock, then exit */
4813 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4814 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004815 return nfs4_stateid_match(stateid, &current_stateid);
4816}
4817
4818static bool nfs4_error_stateid_expired(int err)
4819{
4820 switch (err) {
4821 case -NFS4ERR_DELEG_REVOKED:
4822 case -NFS4ERR_ADMIN_REVOKED:
4823 case -NFS4ERR_BAD_STATEID:
4824 case -NFS4ERR_STALE_STATEID:
4825 case -NFS4ERR_OLD_STATEID:
4826 case -NFS4ERR_OPENMODE:
4827 case -NFS4ERR_EXPIRED:
4828 return true;
4829 }
4830 return false;
4831}
4832
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004833static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004834{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004835 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004837 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004838 if (task->tk_status < 0) {
4839 struct nfs4_exception exception = {
4840 .inode = hdr->inode,
4841 .state = hdr->args.context->state,
4842 .stateid = &hdr->args.stateid,
4843 };
4844 task->tk_status = nfs4_async_handle_exception(task,
4845 server, task->tk_status, &exception);
4846 if (exception.retry) {
4847 rpc_restart_call_prepare(task);
4848 return -EAGAIN;
4849 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004850 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004851
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004853 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004854 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855}
4856
Trond Myklebust5521abf2013-03-16 20:54:34 -04004857static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004858 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004859{
4860
4861 if (!nfs4_error_stateid_expired(task->tk_status) ||
4862 nfs4_stateid_is_current(&args->stateid,
4863 args->context,
4864 args->lock_context,
4865 FMODE_READ))
4866 return false;
4867 rpc_restart_call_prepare(task);
4868 return true;
4869}
4870
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004871static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004872{
4873
4874 dprintk("--> %s\n", __func__);
4875
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004876 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004877 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004878 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004879 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004880 if (task->tk_status > 0)
4881 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004882 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4883 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004884}
4885
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004886static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4887 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004888{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004889 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004890 if (!hdr->pgio_done_cb)
4891 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004892 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004893 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004894}
4895
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004896static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4897 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004898{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004899 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004900 &hdr->args.seq_args,
4901 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004902 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004903 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004904 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4905 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004906 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004907 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004908 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004909 return -EIO;
4910 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004911}
4912
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004913static int nfs4_write_done_cb(struct rpc_task *task,
4914 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004915{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004916 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004917
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004918 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004919 if (task->tk_status < 0) {
4920 struct nfs4_exception exception = {
4921 .inode = hdr->inode,
4922 .state = hdr->args.context->state,
4923 .stateid = &hdr->args.stateid,
4924 };
4925 task->tk_status = nfs4_async_handle_exception(task,
4926 NFS_SERVER(inode), task->tk_status,
4927 &exception);
4928 if (exception.retry) {
4929 rpc_restart_call_prepare(task);
4930 return -EAGAIN;
4931 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004933 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004934 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004935 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004936 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004937 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004938}
4939
Trond Myklebust5521abf2013-03-16 20:54:34 -04004940static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004941 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004942{
4943
4944 if (!nfs4_error_stateid_expired(task->tk_status) ||
4945 nfs4_stateid_is_current(&args->stateid,
4946 args->context,
4947 args->lock_context,
4948 FMODE_WRITE))
4949 return false;
4950 rpc_restart_call_prepare(task);
4951 return true;
4952}
4953
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004954static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004955{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004956 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004957 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004958 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004959 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004960 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4961 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004962}
4963
Trond Myklebust5a37f852012-04-28 14:55:16 -04004964static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004965bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004966{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004967 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004968 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004969 return false;
4970 /* Otherwise, request attributes if and only if we don't hold
4971 * a delegation
4972 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004973 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004974}
Fred Isamana69aef12011-03-03 15:13:47 +00004975
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004976static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004977 struct rpc_message *msg,
4978 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004979{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004980 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004981
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004982 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4983 hdr->args.bitmask = NULL;
4984 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004985 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004986 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004987
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004988 if (!hdr->pgio_done_cb)
4989 hdr->pgio_done_cb = nfs4_write_done_cb;
4990 hdr->res.server = server;
4991 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004992
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004993 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004994 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004995 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
Fred Isaman0b7c0152012-04-20 14:47:39 -04004998static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4999{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005000 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005001 &data->args.seq_args,
5002 &data->res.seq_res,
5003 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004}
5005
Fred Isaman0b7c0152012-04-20 14:47:39 -04005006static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005007{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005009
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005010 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005011 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5012 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005013 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005014 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005016 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005017}
5018
Fred Isaman0b7c0152012-04-20 14:47:39 -04005019static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005020{
5021 if (!nfs4_sequence_done(task, &data->res.seq_res))
5022 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005023 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005024}
5025
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005026static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5027 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005029 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005030
Fred Isaman0b7c0152012-04-20 14:47:39 -04005031 if (data->commit_done_cb == NULL)
5032 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005033 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005034 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005035 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005036 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037}
5038
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005039struct nfs4_renewdata {
5040 struct nfs_client *client;
5041 unsigned long timestamp;
5042};
5043
Linus Torvalds1da177e2005-04-16 15:20:36 -07005044/*
5045 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5046 * standalone procedure for queueing an asynchronous RENEW.
5047 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005048static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005049{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005050 struct nfs4_renewdata *data = calldata;
5051 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005052
Elena Reshetova212bf412017-10-20 12:53:38 +03005053 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005054 nfs4_schedule_state_renewal(clp);
5055 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005056 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005057}
5058
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005059static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005061 struct nfs4_renewdata *data = calldata;
5062 struct nfs_client *clp = data->client;
5063 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005064
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005065 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005066 switch (task->tk_status) {
5067 case 0:
5068 break;
5069 case -NFS4ERR_LEASE_MOVED:
5070 nfs4_schedule_lease_moved_recovery(clp);
5071 break;
5072 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005073 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005074 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5075 return;
5076 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005077 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005078 return;
5079 }
5080 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005081 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005082 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083}
5084
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005085static const struct rpc_call_ops nfs4_renew_ops = {
5086 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005087 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005088};
5089
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005090static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091{
5092 struct rpc_message msg = {
5093 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5094 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005095 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005097 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005098
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005099 if (renew_flags == 0)
5100 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005101 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005102 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005103 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005104 if (data == NULL) {
5105 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005106 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005107 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005108 data->client = clp;
5109 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005110 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005111 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112}
5113
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005114static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115{
5116 struct rpc_message msg = {
5117 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5118 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005119 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005120 };
5121 unsigned long now = jiffies;
5122 int status;
5123
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005124 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 if (status < 0)
5126 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005127 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 return 0;
5129}
5130
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005131static inline int nfs4_server_supports_acls(struct nfs_server *server)
5132{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005133 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005134}
5135
Trond Myklebust21f498c2012-08-24 10:59:25 -04005136/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5137 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005138 * the stack.
5139 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005140#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005141
Neil Hormane9e3d722011-03-04 19:26:03 -05005142static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005143 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005144{
5145 struct page *newpage, **spages;
5146 int rc = 0;
5147 size_t len;
5148 spages = pages;
5149
5150 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005151 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005152 newpage = alloc_page(GFP_KERNEL);
5153
5154 if (newpage == NULL)
5155 goto unwind;
5156 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005157 buf += len;
5158 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005159 *pages++ = newpage;
5160 rc++;
5161 } while (buflen != 0);
5162
5163 return rc;
5164
5165unwind:
5166 for(; rc > 0; rc--)
5167 __free_page(spages[rc-1]);
5168 return -ENOMEM;
5169}
5170
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005171struct nfs4_cached_acl {
5172 int cached;
5173 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005174 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005175};
5176
5177static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005178{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005179 struct nfs_inode *nfsi = NFS_I(inode);
5180
5181 spin_lock(&inode->i_lock);
5182 kfree(nfsi->nfs4_acl);
5183 nfsi->nfs4_acl = acl;
5184 spin_unlock(&inode->i_lock);
5185}
5186
5187static void nfs4_zap_acl_attr(struct inode *inode)
5188{
5189 nfs4_set_cached_acl(inode, NULL);
5190}
5191
5192static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5193{
5194 struct nfs_inode *nfsi = NFS_I(inode);
5195 struct nfs4_cached_acl *acl;
5196 int ret = -ENOENT;
5197
5198 spin_lock(&inode->i_lock);
5199 acl = nfsi->nfs4_acl;
5200 if (acl == NULL)
5201 goto out;
5202 if (buf == NULL) /* user is just asking for length */
5203 goto out_len;
5204 if (acl->cached == 0)
5205 goto out;
5206 ret = -ERANGE; /* see getxattr(2) man page */
5207 if (acl->len > buflen)
5208 goto out;
5209 memcpy(buf, acl->data, acl->len);
5210out_len:
5211 ret = acl->len;
5212out:
5213 spin_unlock(&inode->i_lock);
5214 return ret;
5215}
5216
Sachin Prabhu5794d212012-04-17 14:36:40 +01005217static 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 +00005218{
5219 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005220 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005221
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005222 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005223 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005224 if (acl == NULL)
5225 goto out;
5226 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005227 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005228 } else {
5229 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5230 if (acl == NULL)
5231 goto out;
5232 acl->cached = 0;
5233 }
5234 acl->len = acl_len;
5235out:
5236 nfs4_set_cached_acl(inode, acl);
5237}
5238
Andy Adamsonbf118a32011-12-07 11:55:27 -05005239/*
5240 * The getxattr API returns the required buffer length when called with a
5241 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5242 * the required buf. On a NULL buf, we send a page of data to the server
5243 * guessing that the ACL request can be serviced by a page. If so, we cache
5244 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5245 * the cache. If not so, we throw away the page, and cache the required
5246 * length. The next getxattr call will then produce another round trip to
5247 * the server, this time with the input buf of the required size.
5248 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005249static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005250{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005251 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005252 struct nfs_getaclargs args = {
5253 .fh = NFS_FH(inode),
5254 .acl_pages = pages,
5255 .acl_len = buflen,
5256 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005257 struct nfs_getaclres res = {
5258 .acl_len = buflen,
5259 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005260 struct rpc_message msg = {
5261 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5262 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005263 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005264 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005265 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005266 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005267
Trond Myklebust21f498c2012-08-24 10:59:25 -04005268 if (npages > ARRAY_SIZE(pages))
5269 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005270
Andy Adamsonbf118a32011-12-07 11:55:27 -05005271 for (i = 0; i < npages; i++) {
5272 pages[i] = alloc_page(GFP_KERNEL);
5273 if (!pages[i])
5274 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005275 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005276
5277 /* for decoding across pages */
5278 res.acl_scratch = alloc_page(GFP_KERNEL);
5279 if (!res.acl_scratch)
5280 goto out_free;
5281
Andy Adamsonbf118a32011-12-07 11:55:27 -05005282 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005283
Peng Taode040be2012-01-10 22:42:47 +08005284 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005285 __func__, buf, buflen, npages, args.acl_len);
5286 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5287 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005288 if (ret)
5289 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005290
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005291 /* Handle the case where the passed-in buffer is too short */
5292 if (res.acl_flags & NFS4_ACL_TRUNC) {
5293 /* Did the user only issue a request for the acl length? */
5294 if (buf == NULL)
5295 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005296 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005297 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005298 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005299 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005300 if (buf) {
5301 if (res.acl_len > buflen) {
5302 ret = -ERANGE;
5303 goto out_free;
5304 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005305 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005306 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005307out_ok:
5308 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005309out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005310 for (i = 0; i < npages; i++)
5311 if (pages[i])
5312 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005313 if (res.acl_scratch)
5314 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005315 return ret;
5316}
5317
Trond Myklebust16b42892006-08-24 12:27:15 -04005318static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5319{
5320 struct nfs4_exception exception = { };
5321 ssize_t ret;
5322 do {
5323 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005324 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005325 if (ret >= 0)
5326 break;
5327 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5328 } while (exception.retry);
5329 return ret;
5330}
5331
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005332static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5333{
5334 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005335 int ret;
5336
5337 if (!nfs4_server_supports_acls(server))
5338 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005339 ret = nfs_revalidate_inode(server, inode);
5340 if (ret < 0)
5341 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005342 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5343 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005344 ret = nfs4_read_cached_acl(inode, buf, buflen);
5345 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005346 /* -ENOENT is returned if there is no ACL or if there is an ACL
5347 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005348 return ret;
5349 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005350}
5351
Trond Myklebust16b42892006-08-24 12:27:15 -04005352static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005353{
5354 struct nfs_server *server = NFS_SERVER(inode);
5355 struct page *pages[NFS4ACL_MAXPAGES];
5356 struct nfs_setaclargs arg = {
5357 .fh = NFS_FH(inode),
5358 .acl_pages = pages,
5359 .acl_len = buflen,
5360 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005361 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005362 struct rpc_message msg = {
5363 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5364 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005365 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005366 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005367 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005368 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005369
5370 if (!nfs4_server_supports_acls(server))
5371 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005372 if (npages > ARRAY_SIZE(pages))
5373 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005374 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005375 if (i < 0)
5376 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005377 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005378 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005379
5380 /*
5381 * Free each page after tx, so the only ref left is
5382 * held by the network stack
5383 */
5384 for (; i > 0; i--)
5385 put_page(pages[i-1]);
5386
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005387 /*
5388 * Acl update can result in inode attribute update.
5389 * so mark the attribute cache invalid.
5390 */
5391 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005392 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5393 | NFS_INO_INVALID_CTIME;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005394 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005395 nfs_access_zap_cache(inode);
5396 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005397 return ret;
5398}
5399
Trond Myklebust16b42892006-08-24 12:27:15 -04005400static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5401{
5402 struct nfs4_exception exception = { };
5403 int err;
5404 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005405 err = __nfs4_proc_set_acl(inode, buf, buflen);
5406 trace_nfs4_set_acl(inode, err);
5407 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005408 &exception);
5409 } while (exception.retry);
5410 return err;
5411}
5412
David Quigleyaa9c2662013-05-22 12:50:44 -04005413#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5414static int _nfs4_get_security_label(struct inode *inode, void *buf,
5415 size_t buflen)
5416{
5417 struct nfs_server *server = NFS_SERVER(inode);
5418 struct nfs_fattr fattr;
5419 struct nfs4_label label = {0, 0, buflen, buf};
5420
5421 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005422 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005423 .fh = NFS_FH(inode),
5424 .bitmask = bitmask,
5425 };
5426 struct nfs4_getattr_res res = {
5427 .fattr = &fattr,
5428 .label = &label,
5429 .server = server,
5430 };
5431 struct rpc_message msg = {
5432 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005433 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005434 .rpc_resp = &res,
5435 };
5436 int ret;
5437
5438 nfs_fattr_init(&fattr);
5439
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005440 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005441 if (ret)
5442 return ret;
5443 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5444 return -ENOENT;
5445 if (buflen < label.len)
5446 return -ERANGE;
5447 return 0;
5448}
5449
5450static int nfs4_get_security_label(struct inode *inode, void *buf,
5451 size_t buflen)
5452{
5453 struct nfs4_exception exception = { };
5454 int err;
5455
5456 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5457 return -EOPNOTSUPP;
5458
5459 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005460 err = _nfs4_get_security_label(inode, buf, buflen);
5461 trace_nfs4_get_security_label(inode, err);
5462 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005463 &exception);
5464 } while (exception.retry);
5465 return err;
5466}
5467
5468static int _nfs4_do_set_security_label(struct inode *inode,
5469 struct nfs4_label *ilabel,
5470 struct nfs_fattr *fattr,
5471 struct nfs4_label *olabel)
5472{
5473
5474 struct iattr sattr = {0};
5475 struct nfs_server *server = NFS_SERVER(inode);
5476 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005477 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005478 .fh = NFS_FH(inode),
5479 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005480 .server = server,
5481 .bitmask = bitmask,
5482 .label = ilabel,
5483 };
5484 struct nfs_setattrres res = {
5485 .fattr = fattr,
5486 .label = olabel,
5487 .server = server,
5488 };
5489 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005490 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5491 .rpc_argp = &arg,
5492 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005493 };
5494 int status;
5495
Jeff Layton12207f62013-11-01 10:49:32 -04005496 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005497
Jeff Layton12207f62013-11-01 10:49:32 -04005498 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005499 if (status)
5500 dprintk("%s failed: %d\n", __func__, status);
5501
5502 return status;
5503}
5504
5505static int nfs4_do_set_security_label(struct inode *inode,
5506 struct nfs4_label *ilabel,
5507 struct nfs_fattr *fattr,
5508 struct nfs4_label *olabel)
5509{
5510 struct nfs4_exception exception = { };
5511 int err;
5512
5513 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005514 err = _nfs4_do_set_security_label(inode, ilabel,
5515 fattr, olabel);
5516 trace_nfs4_set_security_label(inode, err);
5517 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005518 &exception);
5519 } while (exception.retry);
5520 return err;
5521}
5522
5523static int
Al Viro59301222016-05-27 10:19:30 -04005524nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005525{
5526 struct nfs4_label ilabel, *olabel = NULL;
5527 struct nfs_fattr fattr;
5528 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005529 int status;
5530
5531 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5532 return -EOPNOTSUPP;
5533
5534 nfs_fattr_init(&fattr);
5535
5536 ilabel.pi = 0;
5537 ilabel.lfs = 0;
5538 ilabel.label = (char *)buf;
5539 ilabel.len = buflen;
5540
5541 cred = rpc_lookup_cred();
5542 if (IS_ERR(cred))
5543 return PTR_ERR(cred);
5544
5545 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5546 if (IS_ERR(olabel)) {
5547 status = -PTR_ERR(olabel);
5548 goto out;
5549 }
5550
5551 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5552 if (status == 0)
5553 nfs_setsecurity(inode, &fattr, olabel);
5554
5555 nfs4_label_free(olabel);
5556out:
5557 put_rpccred(cred);
5558 return status;
5559}
5560#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5561
5562
Chuck Leverf0920752012-05-21 22:45:41 -04005563static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5564 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005565{
5566 __be32 verf[2];
5567
Chuck Lever2c820d92012-05-21 22:45:33 -04005568 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5569 /* An impossible timestamp guarantees this value
5570 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005571 verf[0] = cpu_to_be32(U32_MAX);
5572 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005573 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005574 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005575 u64 ns = ktime_to_ns(nn->boot_time);
5576
5577 verf[0] = cpu_to_be32(ns >> 32);
5578 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005579 }
Chuck Levercd937102012-03-02 17:14:31 -05005580 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5581}
5582
Jeff Laytona3192682015-06-09 19:43:59 -04005583static int
5584nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005585{
Jeff Laytona3192682015-06-09 19:43:59 -04005586 size_t len;
5587 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005588
Trond Myklebustceb3a162015-01-03 15:16:04 -05005589 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005590 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005591
Jeff Laytona3192682015-06-09 19:43:59 -04005592 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005593 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005594 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5595 1 +
5596 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5597 1;
5598 rcu_read_unlock();
5599
5600 if (len > NFS4_OPAQUE_LIMIT + 1)
5601 return -EINVAL;
5602
5603 /*
5604 * Since this string is allocated at mount time, and held until the
5605 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5606 * about a memory-reclaim deadlock.
5607 */
5608 str = kmalloc(len, GFP_KERNEL);
5609 if (!str)
5610 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005611
Chuck Levere984a552012-09-14 17:24:21 -04005612 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005613 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005614 clp->cl_ipaddr,
5615 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5616 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005617 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005618
Jeff Laytona3192682015-06-09 19:43:59 -04005619 clp->cl_owner_id = str;
5620 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005621}
5622
Jeff Layton873e3852015-06-09 19:44:00 -04005623static int
5624nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005625{
Jeff Layton873e3852015-06-09 19:44:00 -04005626 size_t len;
5627 char *str;
5628
5629 len = 10 + 10 + 1 + 10 + 1 +
5630 strlen(nfs4_client_id_uniquifier) + 1 +
5631 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5632
5633 if (len > NFS4_OPAQUE_LIMIT + 1)
5634 return -EINVAL;
5635
5636 /*
5637 * Since this string is allocated at mount time, and held until the
5638 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5639 * about a memory-reclaim deadlock.
5640 */
5641 str = kmalloc(len, GFP_KERNEL);
5642 if (!str)
5643 return -ENOMEM;
5644
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005645 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005646 clp->rpc_ops->version, clp->cl_minorversion,
5647 nfs4_client_id_uniquifier,
5648 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005649 clp->cl_owner_id = str;
5650 return 0;
5651}
5652
5653static int
5654nfs4_init_uniform_client_string(struct nfs_client *clp)
5655{
Jeff Layton873e3852015-06-09 19:44:00 -04005656 size_t len;
5657 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005658
5659 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005660 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005661
5662 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005663 return nfs4_init_uniquifier_client_string(clp);
5664
5665 len = 10 + 10 + 1 + 10 + 1 +
5666 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5667
5668 if (len > NFS4_OPAQUE_LIMIT + 1)
5669 return -EINVAL;
5670
5671 /*
5672 * Since this string is allocated at mount time, and held until the
5673 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5674 * about a memory-reclaim deadlock.
5675 */
5676 str = kmalloc(len, GFP_KERNEL);
5677 if (!str)
5678 return -ENOMEM;
5679
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005680 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005681 clp->rpc_ops->version, clp->cl_minorversion,
5682 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005683 clp->cl_owner_id = str;
5684 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005685}
5686
Chuck Lever706cb8d2014-03-12 12:51:47 -04005687/*
5688 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5689 * services. Advertise one based on the address family of the
5690 * clientaddr.
5691 */
5692static unsigned int
5693nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5694{
5695 if (strchr(clp->cl_ipaddr, ':') != NULL)
5696 return scnprintf(buf, len, "tcp6");
5697 else
5698 return scnprintf(buf, len, "tcp");
5699}
5700
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005701static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5702{
5703 struct nfs4_setclientid *sc = calldata;
5704
5705 if (task->tk_status == 0)
5706 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5707}
5708
5709static const struct rpc_call_ops nfs4_setclientid_ops = {
5710 .rpc_call_done = nfs4_setclientid_done,
5711};
5712
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005713/**
5714 * nfs4_proc_setclientid - Negotiate client ID
5715 * @clp: state data structure
5716 * @program: RPC program for NFSv4 callback service
5717 * @port: IP port number for NFS4 callback service
5718 * @cred: RPC credential to use for this call
5719 * @res: where to place the result
5720 *
5721 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5722 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005723int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5724 unsigned short port, struct rpc_cred *cred,
5725 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005726{
5727 nfs4_verifier sc_verifier;
5728 struct nfs4_setclientid setclientid = {
5729 .sc_verifier = &sc_verifier,
5730 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005731 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005732 };
5733 struct rpc_message msg = {
5734 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5735 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005736 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005737 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005738 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005739 struct rpc_task *task;
5740 struct rpc_task_setup task_setup_data = {
5741 .rpc_client = clp->cl_rpcclient,
5742 .rpc_message = &msg,
5743 .callback_ops = &nfs4_setclientid_ops,
5744 .callback_data = &setclientid,
5745 .flags = RPC_TASK_TIMEOUT,
5746 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005747 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005748
Chuck Leverde734832012-07-11 16:30:50 -04005749 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005750 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005751
5752 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5753 status = nfs4_init_uniform_client_string(clp);
5754 else
Jeff Laytona3192682015-06-09 19:43:59 -04005755 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005756
5757 if (status)
5758 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005759
Chuck Leverde734832012-07-11 16:30:50 -04005760 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005761 setclientid.sc_netid_len =
5762 nfs4_init_callback_netid(clp,
5763 setclientid.sc_netid,
5764 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005765 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005766 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 clp->cl_ipaddr, port >> 8, port & 255);
5768
Jeff Layton3a6bb732015-06-09 19:43:57 -04005769 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005770 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005771 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005772 task = rpc_run_task(&task_setup_data);
5773 if (IS_ERR(task)) {
5774 status = PTR_ERR(task);
5775 goto out;
5776 }
5777 status = task->tk_status;
5778 if (setclientid.sc_cred) {
5779 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5780 put_rpccred(setclientid.sc_cred);
5781 }
5782 rpc_put_task(task);
5783out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005784 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005785 dprintk("NFS reply setclientid: %d\n", status);
5786 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005787}
5788
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005789/**
5790 * nfs4_proc_setclientid_confirm - Confirm client ID
5791 * @clp: state data structure
5792 * @res: result of a previous SETCLIENTID
5793 * @cred: RPC credential to use for this call
5794 *
5795 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5796 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005797int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005798 struct nfs4_setclientid_res *arg,
5799 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005800{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005801 struct rpc_message msg = {
5802 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005803 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005804 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005805 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005806 int status;
5807
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005808 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5809 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5810 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005811 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005812 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005813 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005814 return status;
5815}
5816
Trond Myklebustfe650402006-01-03 09:55:18 +01005817struct nfs4_delegreturndata {
5818 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005819 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005820 struct nfs_fh fh;
5821 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005822 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005823 struct {
5824 struct nfs4_layoutreturn_args arg;
5825 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005826 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005827 u32 roc_barrier;
5828 bool roc;
5829 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005830 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005831 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005832 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005833};
5834
Trond Myklebustfe650402006-01-03 09:55:18 +01005835static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5836{
5837 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005838 struct nfs4_exception exception = {
5839 .inode = data->inode,
5840 .stateid = &data->stateid,
5841 };
Andy Adamson938e1012009-04-01 09:22:28 -04005842
Trond Myklebust14516c32010-07-31 14:29:06 -04005843 if (!nfs4_sequence_done(task, &data->res.seq_res))
5844 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005845
Trond Myklebustca8acf82013-08-13 10:36:56 -04005846 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005847
5848 /* Handle Layoutreturn errors */
5849 if (data->args.lr_args && task->tk_status != 0) {
5850 switch(data->res.lr_ret) {
5851 default:
5852 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5853 break;
5854 case 0:
5855 data->args.lr_args = NULL;
5856 data->res.lr_res = NULL;
5857 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005858 case -NFS4ERR_OLD_STATEID:
5859 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005860 data->inode))
5861 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005862 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005863 case -NFS4ERR_ADMIN_REVOKED:
5864 case -NFS4ERR_DELEG_REVOKED:
5865 case -NFS4ERR_EXPIRED:
5866 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005867 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5868 case -NFS4ERR_WRONG_CRED:
5869 data->args.lr_args = NULL;
5870 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005871 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005872 }
5873 }
5874
Ricardo Labiaga79708862009-12-07 09:23:21 -05005875 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005876 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005877 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005878 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005879 case -NFS4ERR_ADMIN_REVOKED:
5880 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005881 case -NFS4ERR_EXPIRED:
5882 nfs4_free_revoked_stateid(data->res.server,
5883 data->args.stateid,
5884 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005885 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005886 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005887 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005888 task->tk_status = 0;
5889 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005890 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005891 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5892 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005893 task->tk_status = 0;
5894 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005895 case -NFS4ERR_ACCESS:
5896 if (data->args.bitmask) {
5897 data->args.bitmask = NULL;
5898 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005899 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005900 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005901 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005902 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005903 task->tk_status = nfs4_async_handle_exception(task,
5904 data->res.server, task->tk_status,
5905 &exception);
5906 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005907 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005908 }
5909 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005910 return;
5911lr_restart:
5912 data->res.lr_ret = 0;
5913out_restart:
5914 task->tk_status = 0;
5915 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005916}
5917
5918static void nfs4_delegreturn_release(void *calldata)
5919{
Peng Tao039b7562014-07-03 13:05:02 +08005920 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005921 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005922
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005923 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005924 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005925 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5926 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005927 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005928 nfs_iput_and_deactive(inode);
5929 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005930 kfree(calldata);
5931}
5932
Andy Adamson938e1012009-04-01 09:22:28 -04005933static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5934{
5935 struct nfs4_delegreturndata *d_data;
5936
5937 d_data = (struct nfs4_delegreturndata *)data;
5938
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005939 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005940 return;
5941
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005942 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005943 &d_data->args.seq_args,
5944 &d_data->res.seq_res,
5945 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005946}
Andy Adamson938e1012009-04-01 09:22:28 -04005947
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005948static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005949 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005950 .rpc_call_done = nfs4_delegreturn_done,
5951 .rpc_release = nfs4_delegreturn_release,
5952};
5953
Trond Myklebuste6f81072008-01-24 18:14:34 -05005954static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005955{
5956 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005957 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005958 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005959 struct rpc_message msg = {
5960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5961 .rpc_cred = cred,
5962 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005963 struct rpc_task_setup task_setup_data = {
5964 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005965 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005966 .callback_ops = &nfs4_delegreturn_ops,
5967 .flags = RPC_TASK_ASYNC,
5968 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005969 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005970
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005971 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005972 if (data == NULL)
5973 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04005974 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005975
5976 nfs4_state_protect(server->nfs_client,
5977 NFS_SP4_MACH_CRED_CLEANUP,
5978 &task_setup_data.rpc_client, &msg);
5979
Trond Myklebustfe650402006-01-03 09:55:18 +01005980 data->args.fhandle = &data->fh;
5981 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005982 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005983 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005984 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005985 data->res.fattr = &data->fattr;
5986 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005987 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005988 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005989 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005990 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005991 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005992 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005993 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005994 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005995 if (data->lr.roc) {
5996 data->args.lr_args = &data->lr.arg;
5997 data->res.lr_res = &data->lr.res;
5998 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005999 } else if (data->lr.roc) {
6000 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6001 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006002 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006003
Trond Myklebustc970aa82007-07-14 15:39:59 -04006004 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006005 msg.rpc_argp = &data->args;
6006 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006007 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006008 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006009 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006010 if (!issync)
6011 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006012 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006013 if (status != 0)
6014 goto out;
6015 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006016out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006017 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006018 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006019}
6020
Trond Myklebuste6f81072008-01-24 18:14:34 -05006021int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022{
6023 struct nfs_server *server = NFS_SERVER(inode);
6024 struct nfs4_exception exception = { };
6025 int err;
6026 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006027 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006028 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006029 switch (err) {
6030 case -NFS4ERR_STALE_STATEID:
6031 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 case 0:
6033 return 0;
6034 }
6035 err = nfs4_handle_exception(server, err, &exception);
6036 } while (exception.retry);
6037 return err;
6038}
6039
Linus Torvalds1da177e2005-04-16 15:20:36 -07006040static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6041{
6042 struct inode *inode = state->inode;
6043 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006044 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006045 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006047 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006048 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006049 struct nfs_lockt_res res = {
6050 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 };
6052 struct rpc_message msg = {
6053 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006054 .rpc_argp = &arg,
6055 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 .rpc_cred = state->owner->so_cred,
6057 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006058 struct nfs4_lock_state *lsp;
6059 int status;
6060
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006061 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006062 status = nfs4_set_lock_state(state, request);
6063 if (status != 0)
6064 goto out;
6065 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006066 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006067 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006068 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006069 switch (status) {
6070 case 0:
6071 request->fl_type = F_UNLCK;
6072 break;
6073 case -NFS4ERR_DENIED:
6074 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006075 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006076 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006077 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006078out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006079 return status;
6080}
6081
6082static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6083{
6084 struct nfs4_exception exception = { };
6085 int err;
6086
6087 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006088 err = _nfs4_proc_getlk(state, cmd, request);
6089 trace_nfs4_get_lock(request, state, cmd, err);
6090 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006091 &exception);
6092 } while (exception.retry);
6093 return err;
6094}
6095
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006096struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006097 struct nfs_locku_args arg;
6098 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006099 struct nfs4_lock_state *lsp;
6100 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006101 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006102 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006103 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006104 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006105};
6106
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006107static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6108 struct nfs_open_context *ctx,
6109 struct nfs4_lock_state *lsp,
6110 struct nfs_seqid *seqid)
6111{
6112 struct nfs4_unlockdata *p;
6113 struct inode *inode = lsp->ls_state->inode;
6114
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006115 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006116 if (p == NULL)
6117 return NULL;
6118 p->arg.fh = NFS_FH(inode);
6119 p->arg.fl = &p->fl;
6120 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006121 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006122 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006123 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006124 /* Ensure we don't close file until we're done freeing locks! */
6125 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006126 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006127 memcpy(&p->fl, fl, sizeof(p->fl));
6128 p->server = NFS_SERVER(inode);
6129 return p;
6130}
6131
Trond Myklebust06f814a2006-01-03 09:55:07 +01006132static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006133{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006134 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006135 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006136 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006137 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006138 put_nfs_open_context(calldata->ctx);
6139 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006140}
6141
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006142static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006143{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006144 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006145 struct nfs4_exception exception = {
6146 .inode = calldata->lsp->ls_state->inode,
6147 .stateid = &calldata->arg.stateid,
6148 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006149
Trond Myklebust14516c32010-07-31 14:29:06 -04006150 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6151 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006152 switch (task->tk_status) {
6153 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006154 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006155 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006156 if (nfs4_update_lock_stateid(calldata->lsp,
6157 &calldata->res.stateid))
6158 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006159 case -NFS4ERR_ADMIN_REVOKED:
6160 case -NFS4ERR_EXPIRED:
6161 nfs4_free_revoked_stateid(calldata->server,
6162 &calldata->arg.stateid,
6163 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006164 case -NFS4ERR_BAD_STATEID:
6165 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006166 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006167 if (!nfs4_stateid_match(&calldata->arg.stateid,
6168 &calldata->lsp->ls_stateid))
6169 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006170 break;
6171 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006172 task->tk_status = nfs4_async_handle_exception(task,
6173 calldata->server, task->tk_status,
6174 &exception);
6175 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006176 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006177 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006178 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006179}
6180
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006181static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006182{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006183 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006185 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6186 nfs_async_iocounter_wait(task, calldata->l_ctx))
6187 return;
6188
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006189 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006190 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006191 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006192 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006193 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006194 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006195 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006196 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006197 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006198 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006199 &calldata->res.seq_res,
6200 task) != 0)
6201 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006202 return;
6203out_no_action:
6204 task->tk_action = NULL;
6205out_wait:
6206 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006207}
6208
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006209static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006210 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006211 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006212 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006213};
6214
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006215static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6216 struct nfs_open_context *ctx,
6217 struct nfs4_lock_state *lsp,
6218 struct nfs_seqid *seqid)
6219{
6220 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006221 struct rpc_message msg = {
6222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6223 .rpc_cred = ctx->cred,
6224 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006225 struct rpc_task_setup task_setup_data = {
6226 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006227 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006228 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006229 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006230 .flags = RPC_TASK_ASYNC,
6231 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006232
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006233 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6234 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6235
Frank Filz137d6ac2007-07-09 15:32:29 -07006236 /* Ensure this is an unlock - when canceling a lock, the
6237 * canceled lock is passed in, and it won't be an unlock.
6238 */
6239 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006240 if (fl->fl_flags & FL_CLOSE)
6241 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006242
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006243 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6244 if (data == NULL) {
6245 nfs_free_seqid(seqid);
6246 return ERR_PTR(-ENOMEM);
6247 }
6248
Anna Schumakerfba83f32018-05-04 16:22:50 -04006249 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006250 msg.rpc_argp = &data->arg;
6251 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006252 task_setup_data.callback_data = data;
6253 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006254}
6255
Linus Torvalds1da177e2005-04-16 15:20:36 -07006256static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6257{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006258 struct inode *inode = state->inode;
6259 struct nfs4_state_owner *sp = state->owner;
6260 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006261 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006262 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006263 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006264 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006265 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006266 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006267
Trond Myklebust9b073572006-06-29 16:38:34 -04006268 status = nfs4_set_lock_state(state, request);
6269 /* Unlock _before_ we do the RPC call */
6270 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006271 /* Exclude nfs_delegation_claim_locks() */
6272 mutex_lock(&sp->so_delegreturn_mutex);
6273 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006274 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006275 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006276 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006277 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006278 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006279 }
6280 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006281 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006282 if (status != 0)
6283 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006284 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006285 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006286 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6287 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006288 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6289 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006290 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006291 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006292 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006293 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006294 status = PTR_ERR(task);
6295 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006296 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006297 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006298 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006299out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006300 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006301 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006302 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006303}
6304
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006305struct nfs4_lockdata {
6306 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006307 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006308 struct nfs4_lock_state *lsp;
6309 struct nfs_open_context *ctx;
6310 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006311 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006312 int rpc_status;
6313 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006314 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315};
6316
6317static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006318 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6319 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006320{
6321 struct nfs4_lockdata *p;
6322 struct inode *inode = lsp->ls_state->inode;
6323 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006324 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006325
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006326 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006327 if (p == NULL)
6328 return NULL;
6329
6330 p->arg.fh = NFS_FH(inode);
6331 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006332 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006333 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006334 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006335 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6336 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006337 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006338 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006339 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006340 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006341 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006342 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006343 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006344 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006345 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006346 p->ctx = get_nfs_open_context(ctx);
6347 memcpy(&p->fl, fl, sizeof(p->fl));
6348 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006349out_free_seqid:
6350 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006351out_free:
6352 kfree(p);
6353 return NULL;
6354}
6355
6356static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6357{
6358 struct nfs4_lockdata *data = calldata;
6359 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006360
Harvey Harrison3110ff82008-05-02 13:42:44 -07006361 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006362 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006363 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006364 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006365 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006366 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006367 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006368 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006369 nfs4_stateid_copy(&data->arg.open_stateid,
6370 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006371 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006372 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006373 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006374 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006375 nfs4_stateid_copy(&data->arg.lock_stateid,
6376 &data->lsp->ls_stateid);
6377 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006378 if (!nfs4_valid_open_stateid(state)) {
6379 data->rpc_status = -EBADF;
6380 task->tk_action = NULL;
6381 goto out_release_open_seqid;
6382 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006383 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006384 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006385 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006386 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006387 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006388 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006389out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006390 nfs_release_seqid(data->arg.open_seqid);
6391out_release_lock_seqid:
6392 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006393out_wait:
6394 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006395 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006396}
6397
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006398static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6399{
6400 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006401 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006402
Harvey Harrison3110ff82008-05-02 13:42:44 -07006403 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006404
Trond Myklebust14516c32010-07-31 14:29:06 -04006405 if (!nfs4_sequence_done(task, &data->res.seq_res))
6406 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006407
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006408 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006409 switch (task->tk_status) {
6410 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006411 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006412 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006413 if (data->arg.new_lock) {
6414 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006415 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006416 rpc_restart_call_prepare(task);
6417 break;
6418 }
6419 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006420 if (data->arg.new_lock_owner != 0) {
6421 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6422 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6423 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6424 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6425 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006426 break;
6427 case -NFS4ERR_BAD_STATEID:
6428 case -NFS4ERR_OLD_STATEID:
6429 case -NFS4ERR_STALE_STATEID:
6430 case -NFS4ERR_EXPIRED:
6431 if (data->arg.new_lock_owner != 0) {
6432 if (!nfs4_stateid_match(&data->arg.open_stateid,
6433 &lsp->ls_state->open_stateid))
6434 rpc_restart_call_prepare(task);
6435 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6436 &lsp->ls_stateid))
6437 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006438 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006439 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006440}
6441
6442static void nfs4_lock_release(void *calldata)
6443{
6444 struct nfs4_lockdata *data = calldata;
6445
Harvey Harrison3110ff82008-05-02 13:42:44 -07006446 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006447 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006448 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006449 struct rpc_task *task;
6450 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6451 data->arg.lock_seqid);
6452 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006453 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006454 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006455 } else
6456 nfs_free_seqid(data->arg.lock_seqid);
6457 nfs4_put_lock_state(data->lsp);
6458 put_nfs_open_context(data->ctx);
6459 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006460 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006461}
6462
6463static const struct rpc_call_ops nfs4_lock_ops = {
6464 .rpc_call_prepare = nfs4_lock_prepare,
6465 .rpc_call_done = nfs4_lock_done,
6466 .rpc_release = nfs4_lock_release,
6467};
6468
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006469static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6470{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006471 switch (error) {
6472 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006473 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006474 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006475 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006476 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006477 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006478 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006479 break;
6480 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006481 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006482 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006483 };
6484}
6485
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006486static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006487{
6488 struct nfs4_lockdata *data;
6489 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006490 struct rpc_message msg = {
6491 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6492 .rpc_cred = state->owner->so_cred,
6493 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006494 struct rpc_task_setup task_setup_data = {
6495 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006496 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006497 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006498 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006499 .flags = RPC_TASK_ASYNC,
6500 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006501 int ret;
6502
Harvey Harrison3110ff82008-05-02 13:42:44 -07006503 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006504 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006505 fl->fl_u.nfs4_fl.owner,
6506 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006507 if (data == NULL)
6508 return -ENOMEM;
6509 if (IS_SETLKW(cmd))
6510 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006511 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6512 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006513 msg.rpc_argp = &data->arg;
6514 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006515 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006516 if (recovery_type > NFS_LOCK_NEW) {
6517 if (recovery_type == NFS_LOCK_RECLAIM)
6518 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006519 } else
6520 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006521 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006522 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006523 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006524 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006525 if (ret == 0) {
6526 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006527 if (ret)
6528 nfs4_handle_setlk_error(data->server, data->lsp,
6529 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006530 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006531 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006532 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006533 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006534 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006535 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006536}
6537
6538static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6539{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006540 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006541 struct nfs4_exception exception = {
6542 .inode = state->inode,
6543 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006544 int err;
6545
6546 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006547 /* Cache the lock if possible... */
6548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6549 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006550 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006551 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006552 break;
6553 nfs4_handle_exception(server, err, &exception);
6554 } while (exception.retry);
6555 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006556}
6557
6558static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6559{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006560 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006561 struct nfs4_exception exception = {
6562 .inode = state->inode,
6563 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006564 int err;
6565
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006566 err = nfs4_set_lock_state(state, request);
6567 if (err != 0)
6568 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006569 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006570 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6571 return 0;
6572 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006573 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006574 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6575 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006576 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006577 switch (err) {
6578 default:
6579 goto out;
6580 case -NFS4ERR_GRACE:
6581 case -NFS4ERR_DELAY:
6582 nfs4_handle_exception(server, err, &exception);
6583 err = 0;
6584 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006585 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006586out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006587 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006588}
6589
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006590#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006591static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6592{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006593 struct nfs4_lock_state *lsp;
6594 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006595
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006596 status = nfs4_set_lock_state(state, request);
6597 if (status != 0)
6598 return status;
6599 lsp = request->fl_u.nfs4_fl.owner;
6600 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6601 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6602 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006603 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006604}
6605#endif
6606
Linus Torvalds1da177e2005-04-16 15:20:36 -07006607static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6608{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006609 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006610 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006611 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006612 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613
Trond Myklebust01c3b862006-06-29 16:38:39 -04006614 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006615 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006616 if (status < 0)
6617 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006618 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006619 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006620 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006621 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006622 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006623 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006624 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006625 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006626 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006627 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006628 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006629 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006630 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006631 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006632out:
6633 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006634 return status;
6635}
6636
6637static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6638{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006639 struct nfs4_exception exception = {
6640 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006641 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006642 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006643 int err;
6644
6645 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006646 err = _nfs4_proc_setlk(state, cmd, request);
6647 if (err == -NFS4ERR_DENIED)
6648 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006649 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006650 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 } while (exception.retry);
6652 return err;
6653}
6654
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006655#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6656#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6657
6658static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006659nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6660 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006661{
6662 int status = -ERESTARTSYS;
6663 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6664
6665 while(!signalled()) {
6666 status = nfs4_proc_setlk(state, cmd, request);
6667 if ((status != -EAGAIN) || IS_SETLK(cmd))
6668 break;
6669 freezable_schedule_timeout_interruptible(timeout);
6670 timeout *= 2;
6671 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6672 status = -ERESTARTSYS;
6673 }
6674 return status;
6675}
6676
Jeff Laytona1d617d82016-09-17 18:17:39 -04006677#ifdef CONFIG_NFS_V4_1
6678struct nfs4_lock_waiter {
6679 struct task_struct *task;
6680 struct inode *inode;
6681 struct nfs_lowner *owner;
6682 bool notified;
6683};
6684
6685static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006686nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006687{
6688 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006689 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006690
Jeff Layton57174592018-03-18 08:37:03 -04006691 /* NULL key means to wake up everyone */
6692 if (key) {
6693 struct cb_notify_lock_args *cbnl = key;
6694 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6695 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006696
Jeff Layton57174592018-03-18 08:37:03 -04006697 /* Only wake if the callback was for the same owner. */
6698 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6699 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006700
Jeff Layton57174592018-03-18 08:37:03 -04006701 /* Make sure it's for the right inode */
6702 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6703 return 0;
6704
6705 waiter->notified = true;
6706 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006707
6708 /* override "private" so we can use default_wake_function */
6709 wait->private = waiter->task;
6710 ret = autoremove_wake_function(wait, mode, flags, key);
6711 wait->private = waiter;
6712 return ret;
6713}
6714
6715static int
6716nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6717{
6718 int status = -ERESTARTSYS;
6719 unsigned long flags;
6720 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6721 struct nfs_server *server = NFS_SERVER(state->inode);
6722 struct nfs_client *clp = server->nfs_client;
6723 wait_queue_head_t *q = &clp->cl_lock_waitq;
6724 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6725 .id = lsp->ls_seqid.owner_id,
6726 .s_dev = server->s_dev };
6727 struct nfs4_lock_waiter waiter = { .task = current,
6728 .inode = state->inode,
6729 .owner = &owner,
6730 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006731 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006732
6733 /* Don't bother with waitqueue if we don't expect a callback */
6734 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6735 return nfs4_retry_setlk_simple(state, cmd, request);
6736
6737 init_wait(&wait);
6738 wait.private = &waiter;
6739 wait.func = nfs4_wake_lock_waiter;
6740 add_wait_queue(q, &wait);
6741
6742 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006743 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006744 status = nfs4_proc_setlk(state, cmd, request);
6745 if ((status != -EAGAIN) || IS_SETLK(cmd))
6746 break;
6747
6748 status = -ERESTARTSYS;
6749 spin_lock_irqsave(&q->lock, flags);
6750 if (waiter.notified) {
6751 spin_unlock_irqrestore(&q->lock, flags);
6752 continue;
6753 }
6754 set_current_state(TASK_INTERRUPTIBLE);
6755 spin_unlock_irqrestore(&q->lock, flags);
6756
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006757 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006758 }
6759
6760 finish_wait(q, &wait);
6761 return status;
6762}
6763#else /* !CONFIG_NFS_V4_1 */
6764static inline int
6765nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6766{
6767 return nfs4_retry_setlk_simple(state, cmd, request);
6768}
6769#endif
6770
Linus Torvalds1da177e2005-04-16 15:20:36 -07006771static int
6772nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6773{
6774 struct nfs_open_context *ctx;
6775 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006776 int status;
6777
6778 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006779 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006780 state = ctx->state;
6781
Trond Myklebustd9531262009-07-21 19:22:38 -04006782 if (IS_GETLK(cmd)) {
6783 if (state != NULL)
6784 return nfs4_proc_getlk(state, F_GETLK, request);
6785 return 0;
6786 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006787
6788 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6789 return -EINVAL;
6790
Trond Myklebustd9531262009-07-21 19:22:38 -04006791 if (request->fl_type == F_UNLCK) {
6792 if (state != NULL)
6793 return nfs4_proc_unlck(state, cmd, request);
6794 return 0;
6795 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006796
Trond Myklebustd9531262009-07-21 19:22:38 -04006797 if (state == NULL)
6798 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006799
6800 if ((request->fl_flags & FL_POSIX) &&
6801 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6802 return -ENOLCK;
6803
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006804 /*
6805 * Don't rely on the VFS having checked the file open mode,
6806 * since it won't do this for flock() locks.
6807 */
6808 switch (request->fl_type) {
6809 case F_RDLCK:
6810 if (!(filp->f_mode & FMODE_READ))
6811 return -EBADF;
6812 break;
6813 case F_WRLCK:
6814 if (!(filp->f_mode & FMODE_WRITE))
6815 return -EBADF;
6816 }
6817
Jeff Layton1ea67db2016-09-17 18:17:37 -04006818 status = nfs4_set_lock_state(state, request);
6819 if (status != 0)
6820 return status;
6821
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006822 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006823}
6824
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006825int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006826{
6827 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006828 int err;
6829
6830 err = nfs4_set_lock_state(state, fl);
6831 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006832 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006833 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006834 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006835}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006836
Trond Myklebustcf470c32012-03-07 13:49:12 -05006837struct nfs_release_lockowner_data {
6838 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006839 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006840 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006841 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006842 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006843};
6844
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006845static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6846{
6847 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006848 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006849 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6850 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006851 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006852 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006853}
6854
6855static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6856{
6857 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006858 struct nfs_server *server = data->server;
6859
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006860 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006861
6862 switch (task->tk_status) {
6863 case 0:
6864 renew_lease(server, data->timestamp);
6865 break;
6866 case -NFS4ERR_STALE_CLIENTID:
6867 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006868 nfs4_schedule_lease_recovery(server->nfs_client);
6869 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006870 case -NFS4ERR_LEASE_MOVED:
6871 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006872 if (nfs4_async_handle_error(task, server,
6873 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006874 rpc_restart_call_prepare(task);
6875 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006876}
6877
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006878static void nfs4_release_lockowner_release(void *calldata)
6879{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006880 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006881 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006882 kfree(calldata);
6883}
6884
Trond Myklebust17280172012-03-11 13:11:00 -04006885static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006886 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6887 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006888 .rpc_release = nfs4_release_lockowner_release,
6889};
6890
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006891static void
6892nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006893{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006894 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006895 struct rpc_message msg = {
6896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6897 };
6898
6899 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006900 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006901
Trond Myklebustcf470c32012-03-07 13:49:12 -05006902 data = kmalloc(sizeof(*data), GFP_NOFS);
6903 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006904 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006905 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006906 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006907 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6908 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6909 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006910
Trond Myklebustcf470c32012-03-07 13:49:12 -05006911 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006912 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006913 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006914 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006915}
6916
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006917#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6918
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006919static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006920 struct dentry *unused, struct inode *inode,
6921 const char *key, const void *buf,
6922 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006923{
Al Viro59301222016-05-27 10:19:30 -04006924 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006925}
6926
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006927static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006928 struct dentry *unused, struct inode *inode,
6929 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006930{
Al Virob2968212016-04-10 20:48:24 -04006931 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006932}
6933
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006934static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006935{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006936 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006937}
6938
David Quigleyc9bccef2013-05-22 12:50:45 -04006939#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006940
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006941static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006942 struct dentry *unused, struct inode *inode,
6943 const char *key, const void *buf,
6944 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006945{
6946 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006947 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006948
6949 return -EOPNOTSUPP;
6950}
6951
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006952static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006953 struct dentry *unused, struct inode *inode,
6954 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006955{
6956 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006957 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006958 return -EOPNOTSUPP;
6959}
6960
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006961static ssize_t
6962nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006963{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006964 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006965
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006966 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6967 len = security_inode_listsecurity(inode, list, list_len);
6968 if (list_len && len > list_len)
6969 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006970 }
6971 return len;
6972}
6973
6974static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6975 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006976 .get = nfs4_xattr_get_nfs4_label,
6977 .set = nfs4_xattr_set_nfs4_label,
6978};
David Quigleyc9bccef2013-05-22 12:50:45 -04006979
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006980#else
6981
6982static ssize_t
6983nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6984{
6985 return 0;
6986}
6987
6988#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006989
Andy Adamson533eb462011-06-13 18:25:56 -04006990/*
6991 * nfs_fhget will use either the mounted_on_fileid or the fileid
6992 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006993static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6994{
Andy Adamson533eb462011-06-13 18:25:56 -04006995 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6996 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6997 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006998 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006999 return;
7000
7001 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007002 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007003 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7004 fattr->nlink = 2;
7005}
7006
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007007static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7008 const struct qstr *name,
7009 struct nfs4_fs_locations *fs_locations,
7010 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007011{
7012 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007013 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007014 struct nfs4_fs_locations_arg args = {
7015 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007016 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007017 .page = page,
7018 .bitmask = bitmask,
7019 };
Benny Halevy22958462009-04-01 09:22:02 -04007020 struct nfs4_fs_locations_res res = {
7021 .fs_locations = fs_locations,
7022 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007023 struct rpc_message msg = {
7024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7025 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007026 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007027 };
7028 int status;
7029
Harvey Harrison3110ff82008-05-02 13:42:44 -07007030 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007031
Chuck Leverc05cefc2017-11-05 15:45:22 -05007032 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7033 bitmask[1] = nfs4_fattr_bitmap[1];
7034
Andy Adamson533eb462011-06-13 18:25:56 -04007035 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7036 * is not supported */
7037 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007038 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007039 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007040 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007041
Trond Myklebustc228fd32007-01-13 02:28:11 -05007042 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007043 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007044 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007045 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007046 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007047 return status;
7048}
7049
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007050int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7051 const struct qstr *name,
7052 struct nfs4_fs_locations *fs_locations,
7053 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007054{
7055 struct nfs4_exception exception = { };
7056 int err;
7057 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007058 err = _nfs4_proc_fs_locations(client, dir, name,
7059 fs_locations, page);
7060 trace_nfs4_get_fs_locations(dir, name, err);
7061 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007062 &exception);
7063 } while (exception.retry);
7064 return err;
7065}
7066
Chuck Leverb03d7352013-10-17 14:12:50 -04007067/*
7068 * This operation also signals the server that this client is
7069 * performing migration recovery. The server can stop returning
7070 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7071 * appended to this compound to identify the client ID which is
7072 * performing recovery.
7073 */
7074static int _nfs40_proc_get_locations(struct inode *inode,
7075 struct nfs4_fs_locations *locations,
7076 struct page *page, struct rpc_cred *cred)
7077{
7078 struct nfs_server *server = NFS_SERVER(inode);
7079 struct rpc_clnt *clnt = server->client;
7080 u32 bitmask[2] = {
7081 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7082 };
7083 struct nfs4_fs_locations_arg args = {
7084 .clientid = server->nfs_client->cl_clientid,
7085 .fh = NFS_FH(inode),
7086 .page = page,
7087 .bitmask = bitmask,
7088 .migration = 1, /* skip LOOKUP */
7089 .renew = 1, /* append RENEW */
7090 };
7091 struct nfs4_fs_locations_res res = {
7092 .fs_locations = locations,
7093 .migration = 1,
7094 .renew = 1,
7095 };
7096 struct rpc_message msg = {
7097 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7098 .rpc_argp = &args,
7099 .rpc_resp = &res,
7100 .rpc_cred = cred,
7101 };
7102 unsigned long now = jiffies;
7103 int status;
7104
7105 nfs_fattr_init(&locations->fattr);
7106 locations->server = server;
7107 locations->nlocations = 0;
7108
Anna Schumakerfba83f32018-05-04 16:22:50 -04007109 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007110 status = nfs4_call_sync_sequence(clnt, server, &msg,
7111 &args.seq_args, &res.seq_res);
7112 if (status)
7113 return status;
7114
7115 renew_lease(server, now);
7116 return 0;
7117}
7118
7119#ifdef CONFIG_NFS_V4_1
7120
7121/*
7122 * This operation also signals the server that this client is
7123 * performing migration recovery. The server can stop asserting
7124 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7125 * performing this operation is identified in the SEQUENCE
7126 * operation in this compound.
7127 *
7128 * When the client supports GETATTR(fs_locations_info), it can
7129 * be plumbed in here.
7130 */
7131static int _nfs41_proc_get_locations(struct inode *inode,
7132 struct nfs4_fs_locations *locations,
7133 struct page *page, struct rpc_cred *cred)
7134{
7135 struct nfs_server *server = NFS_SERVER(inode);
7136 struct rpc_clnt *clnt = server->client;
7137 u32 bitmask[2] = {
7138 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7139 };
7140 struct nfs4_fs_locations_arg args = {
7141 .fh = NFS_FH(inode),
7142 .page = page,
7143 .bitmask = bitmask,
7144 .migration = 1, /* skip LOOKUP */
7145 };
7146 struct nfs4_fs_locations_res res = {
7147 .fs_locations = locations,
7148 .migration = 1,
7149 };
7150 struct rpc_message msg = {
7151 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7152 .rpc_argp = &args,
7153 .rpc_resp = &res,
7154 .rpc_cred = cred,
7155 };
7156 int status;
7157
7158 nfs_fattr_init(&locations->fattr);
7159 locations->server = server;
7160 locations->nlocations = 0;
7161
Anna Schumakerfba83f32018-05-04 16:22:50 -04007162 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007163 status = nfs4_call_sync_sequence(clnt, server, &msg,
7164 &args.seq_args, &res.seq_res);
7165 if (status == NFS4_OK &&
7166 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7167 status = -NFS4ERR_LEASE_MOVED;
7168 return status;
7169}
7170
7171#endif /* CONFIG_NFS_V4_1 */
7172
7173/**
7174 * nfs4_proc_get_locations - discover locations for a migrated FSID
7175 * @inode: inode on FSID that is migrating
7176 * @locations: result of query
7177 * @page: buffer
7178 * @cred: credential to use for this operation
7179 *
7180 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7181 * operation failed, or a negative errno if a local error occurred.
7182 *
7183 * On success, "locations" is filled in, but if the server has
7184 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7185 * asserted.
7186 *
7187 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7188 * from this client that require migration recovery.
7189 */
7190int nfs4_proc_get_locations(struct inode *inode,
7191 struct nfs4_fs_locations *locations,
7192 struct page *page, struct rpc_cred *cred)
7193{
7194 struct nfs_server *server = NFS_SERVER(inode);
7195 struct nfs_client *clp = server->nfs_client;
7196 const struct nfs4_mig_recovery_ops *ops =
7197 clp->cl_mvops->mig_recovery_ops;
7198 struct nfs4_exception exception = { };
7199 int status;
7200
7201 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7202 (unsigned long long)server->fsid.major,
7203 (unsigned long long)server->fsid.minor,
7204 clp->cl_hostname);
7205 nfs_display_fhandle(NFS_FH(inode), __func__);
7206
7207 do {
7208 status = ops->get_locations(inode, locations, page, cred);
7209 if (status != -NFS4ERR_DELAY)
7210 break;
7211 nfs4_handle_exception(server, status, &exception);
7212 } while (exception.retry);
7213 return status;
7214}
7215
Chuck Lever44c99932013-10-17 14:13:30 -04007216/*
7217 * This operation also signals the server that this client is
7218 * performing "lease moved" recovery. The server can stop
7219 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7220 * is appended to this compound to identify the client ID which is
7221 * performing recovery.
7222 */
7223static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7224{
7225 struct nfs_server *server = NFS_SERVER(inode);
7226 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7227 struct rpc_clnt *clnt = server->client;
7228 struct nfs4_fsid_present_arg args = {
7229 .fh = NFS_FH(inode),
7230 .clientid = clp->cl_clientid,
7231 .renew = 1, /* append RENEW */
7232 };
7233 struct nfs4_fsid_present_res res = {
7234 .renew = 1,
7235 };
7236 struct rpc_message msg = {
7237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7238 .rpc_argp = &args,
7239 .rpc_resp = &res,
7240 .rpc_cred = cred,
7241 };
7242 unsigned long now = jiffies;
7243 int status;
7244
7245 res.fh = nfs_alloc_fhandle();
7246 if (res.fh == NULL)
7247 return -ENOMEM;
7248
Anna Schumakerfba83f32018-05-04 16:22:50 -04007249 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007250 status = nfs4_call_sync_sequence(clnt, server, &msg,
7251 &args.seq_args, &res.seq_res);
7252 nfs_free_fhandle(res.fh);
7253 if (status)
7254 return status;
7255
7256 do_renew_lease(clp, now);
7257 return 0;
7258}
7259
7260#ifdef CONFIG_NFS_V4_1
7261
7262/*
7263 * This operation also signals the server that this client is
7264 * performing "lease moved" recovery. The server can stop asserting
7265 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7266 * this operation is identified in the SEQUENCE operation in this
7267 * compound.
7268 */
7269static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7270{
7271 struct nfs_server *server = NFS_SERVER(inode);
7272 struct rpc_clnt *clnt = server->client;
7273 struct nfs4_fsid_present_arg args = {
7274 .fh = NFS_FH(inode),
7275 };
7276 struct nfs4_fsid_present_res res = {
7277 };
7278 struct rpc_message msg = {
7279 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7280 .rpc_argp = &args,
7281 .rpc_resp = &res,
7282 .rpc_cred = cred,
7283 };
7284 int status;
7285
7286 res.fh = nfs_alloc_fhandle();
7287 if (res.fh == NULL)
7288 return -ENOMEM;
7289
Anna Schumakerfba83f32018-05-04 16:22:50 -04007290 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007291 status = nfs4_call_sync_sequence(clnt, server, &msg,
7292 &args.seq_args, &res.seq_res);
7293 nfs_free_fhandle(res.fh);
7294 if (status == NFS4_OK &&
7295 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7296 status = -NFS4ERR_LEASE_MOVED;
7297 return status;
7298}
7299
7300#endif /* CONFIG_NFS_V4_1 */
7301
7302/**
7303 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7304 * @inode: inode on FSID to check
7305 * @cred: credential to use for this operation
7306 *
7307 * Server indicates whether the FSID is present, moved, or not
7308 * recognized. This operation is necessary to clear a LEASE_MOVED
7309 * condition for this client ID.
7310 *
7311 * Returns NFS4_OK if the FSID is present on this server,
7312 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7313 * NFS4ERR code if some error occurred on the server, or a
7314 * negative errno if a local failure occurred.
7315 */
7316int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7317{
7318 struct nfs_server *server = NFS_SERVER(inode);
7319 struct nfs_client *clp = server->nfs_client;
7320 const struct nfs4_mig_recovery_ops *ops =
7321 clp->cl_mvops->mig_recovery_ops;
7322 struct nfs4_exception exception = { };
7323 int status;
7324
7325 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7326 (unsigned long long)server->fsid.major,
7327 (unsigned long long)server->fsid.minor,
7328 clp->cl_hostname);
7329 nfs_display_fhandle(NFS_FH(inode), __func__);
7330
7331 do {
7332 status = ops->fsid_present(inode, cred);
7333 if (status != -NFS4ERR_DELAY)
7334 break;
7335 nfs4_handle_exception(server, status, &exception);
7336 } while (exception.retry);
7337 return status;
7338}
7339
Andy Adamson5ec16a82013-08-08 10:57:55 -04007340/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007341 * If 'use_integrity' is true and the state managment nfs_client
7342 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7343 * and the machine credential as per RFC3530bis and RFC5661 Security
7344 * Considerations sections. Otherwise, just use the user cred with the
7345 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007346 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007347static 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 +00007348{
7349 int status;
7350 struct nfs4_secinfo_arg args = {
7351 .dir_fh = NFS_FH(dir),
7352 .name = name,
7353 };
7354 struct nfs4_secinfo_res res = {
7355 .flavors = flavors,
7356 };
7357 struct rpc_message msg = {
7358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7359 .rpc_argp = &args,
7360 .rpc_resp = &res,
7361 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007362 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007363 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007364
7365 if (use_integrity) {
7366 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007367 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7368 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007369 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007370
7371 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007372
7373 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7374 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7375
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007376 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7377 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007378 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007379
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007380 if (cred)
7381 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007382
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007383 return status;
7384}
7385
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007386int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7387 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007388{
7389 struct nfs4_exception exception = { };
7390 int err;
7391 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007392 err = -NFS4ERR_WRONGSEC;
7393
7394 /* try to use integrity protection with machine cred */
7395 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7396 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7397
7398 /*
7399 * if unable to use integrity protection, or SECINFO with
7400 * integrity protection returns NFS4ERR_WRONGSEC (which is
7401 * disallowed by spec, but exists in deployed servers) use
7402 * the current filesystem's rpc_client and the user cred.
7403 */
7404 if (err == -NFS4ERR_WRONGSEC)
7405 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7406
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007407 trace_nfs4_secinfo(dir, name, err);
7408 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007409 &exception);
7410 } while (exception.retry);
7411 return err;
7412}
7413
Andy Adamson557134a2009-04-01 09:21:53 -04007414#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007415/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007416 * Check the exchange flags returned by the server for invalid flags, having
7417 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7418 * DS flags set.
7419 */
7420static int nfs4_check_cl_exchange_flags(u32 flags)
7421{
7422 if (flags & ~EXCHGID4_FLAG_MASK_R)
7423 goto out_inval;
7424 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7425 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7426 goto out_inval;
7427 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7428 goto out_inval;
7429 return NFS_OK;
7430out_inval:
7431 return -NFS4ERR_INVAL;
7432}
7433
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007434static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007435nfs41_same_server_scope(struct nfs41_server_scope *a,
7436 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007437{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007438 if (a->server_scope_sz != b->server_scope_sz)
7439 return false;
7440 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007441}
7442
Andy Adamson02a95de2016-02-05 16:08:37 -05007443static void
7444nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7445{
7446}
7447
7448static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7449 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7450};
7451
Andy Adamson357f54d2010-12-14 10:11:57 -05007452/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007453 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007454 *
7455 * The 4.1 client currently uses the same TCP connection for the
7456 * fore and backchannel.
7457 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007458static
7459int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7460 struct rpc_xprt *xprt,
7461 struct nfs_client *clp,
7462 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007463{
7464 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007465 struct nfs41_bind_conn_to_session_args args = {
7466 .client = clp,
7467 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7468 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007469 struct nfs41_bind_conn_to_session_res res;
7470 struct rpc_message msg = {
7471 .rpc_proc =
7472 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007473 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007474 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007475 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007476 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007477 struct rpc_task_setup task_setup_data = {
7478 .rpc_client = clnt,
7479 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007480 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007481 .rpc_message = &msg,
7482 .flags = RPC_TASK_TIMEOUT,
7483 };
7484 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007485
Trond Myklebust71a097c2015-02-18 09:27:18 -08007486 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7487 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7488 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007489
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007490 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7491 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7492 args.dir = NFS4_CDFC4_FORE;
7493
7494 task = rpc_run_task(&task_setup_data);
7495 if (!IS_ERR(task)) {
7496 status = task->tk_status;
7497 rpc_put_task(task);
7498 } else
7499 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007500 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007501 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007502 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007503 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7504 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007505 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007506 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007507 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007508 dprintk("NFS: %s: Unexpected direction from server\n",
7509 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007510 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007511 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007512 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007513 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7514 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007515 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007516 }
7517 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007518
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007519 return status;
7520}
7521
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007522struct rpc_bind_conn_calldata {
7523 struct nfs_client *clp;
7524 struct rpc_cred *cred;
7525};
7526
7527static int
7528nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7529 struct rpc_xprt *xprt,
7530 void *calldata)
7531{
7532 struct rpc_bind_conn_calldata *p = calldata;
7533
7534 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7535}
7536
7537int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7538{
7539 struct rpc_bind_conn_calldata data = {
7540 .clp = clp,
7541 .cred = cred,
7542 };
7543 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7544 nfs4_proc_bind_conn_to_session_callback, &data);
7545}
7546
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007547/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007548 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7549 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007550 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007551static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7552 .how = SP4_MACH_CRED,
7553 .enforce.u.words = {
7554 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7555 1 << (OP_EXCHANGE_ID - 32) |
7556 1 << (OP_CREATE_SESSION - 32) |
7557 1 << (OP_DESTROY_SESSION - 32) |
7558 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007559 },
7560 .allow.u.words = {
7561 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007562 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007563 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007564 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007565 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007566 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007567 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007568 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007569 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007570 1 << (OP_FREE_STATEID - 32) |
7571 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007572 }
7573};
7574
7575/*
7576 * Select the state protection mode for client `clp' given the server results
7577 * from exchange_id in `sp'.
7578 *
7579 * Returns 0 on success, negative errno otherwise.
7580 */
7581static int nfs4_sp4_select_mode(struct nfs_client *clp,
7582 struct nfs41_state_protection *sp)
7583{
7584 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7585 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7586 1 << (OP_EXCHANGE_ID - 32) |
7587 1 << (OP_CREATE_SESSION - 32) |
7588 1 << (OP_DESTROY_SESSION - 32) |
7589 1 << (OP_DESTROY_CLIENTID - 32)
7590 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007591 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007592 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007593 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007594
7595 if (sp->how == SP4_MACH_CRED) {
7596 /* Print state protect result */
7597 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7598 for (i = 0; i <= LAST_NFS4_OP; i++) {
7599 if (test_bit(i, sp->enforce.u.longs))
7600 dfprintk(MOUNT, " enforce op %d\n", i);
7601 if (test_bit(i, sp->allow.u.longs))
7602 dfprintk(MOUNT, " allow op %d\n", i);
7603 }
7604
7605 /* make sure nothing is on enforce list that isn't supported */
7606 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7607 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7608 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007609 ret = -EINVAL;
7610 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007611 }
7612 }
7613
7614 /*
7615 * Minimal mode - state operations are allowed to use machine
7616 * credential. Note this already happens by default, so the
7617 * client doesn't have to do anything more than the negotiation.
7618 *
7619 * NOTE: we don't care if EXCHANGE_ID is in the list -
7620 * we're already using the machine cred for exchange_id
7621 * and will never use a different cred.
7622 */
7623 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7624 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7625 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7626 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7627 dfprintk(MOUNT, "sp4_mach_cred:\n");
7628 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007629 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007630 } else {
7631 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007632 ret = -EINVAL;
7633 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007634 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007635
7636 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007637 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7638 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007639 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7640 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007641 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007642 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007643
Andrew Elble99ade3c2015-12-02 09:39:51 -05007644 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7645 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007646 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007647 }
7648
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007649 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7650 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7651 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007652 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007653 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007654
7655 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7656 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7657 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007658 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007659 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007660
7661 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7662 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007663 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007664 }
7665
7666 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7667 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007668 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007669 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007670 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007671out:
7672 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007673 return 0;
7674}
7675
Andy Adamson8d89bd72016-09-09 09:22:18 -04007676struct nfs41_exchange_id_data {
7677 struct nfs41_exchange_id_res res;
7678 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007679};
7680
Andy Adamson8d89bd72016-09-09 09:22:18 -04007681static void nfs4_exchange_id_release(void *data)
7682{
7683 struct nfs41_exchange_id_data *cdata =
7684 (struct nfs41_exchange_id_data *)data;
7685
Olga Kornievskaia63513232017-03-13 10:36:19 -04007686 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007687 kfree(cdata->res.impl_id);
7688 kfree(cdata->res.server_scope);
7689 kfree(cdata->res.server_owner);
7690 kfree(cdata);
7691}
7692
7693static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007694 .rpc_release = nfs4_exchange_id_release,
7695};
7696
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007697/*
7698 * _nfs4_proc_exchange_id()
7699 *
7700 * Wrapper for EXCHANGE_ID operation.
7701 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007702static struct rpc_task *
7703nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007704 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007705{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007706 struct rpc_message msg = {
7707 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007708 .rpc_cred = cred,
7709 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007710 struct rpc_task_setup task_setup_data = {
7711 .rpc_client = clp->cl_rpcclient,
7712 .callback_ops = &nfs4_exchange_id_call_ops,
7713 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007714 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007715 };
7716 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007717 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007718
Elena Reshetova212bf412017-10-20 12:53:38 +03007719 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007720 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007721
Trond Myklebust9c760d12017-07-31 18:38:50 -04007722 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007723 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007724 if (!calldata)
7725 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007726
Trond Myklebustfd405592017-08-01 16:02:47 -04007727 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007728
7729 status = nfs4_init_uniform_client_string(clp);
7730 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007731 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007732
Andy Adamson8d89bd72016-09-09 09:22:18 -04007733 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7734 GFP_NOFS);
7735 status = -ENOMEM;
7736 if (unlikely(calldata->res.server_owner == NULL))
7737 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007738
Andy Adamson8d89bd72016-09-09 09:22:18 -04007739 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007740 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007741 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007742 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007743
Andy Adamson8d89bd72016-09-09 09:22:18 -04007744 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7745 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007746 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007747
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007748 switch (sp4_how) {
7749 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007750 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007751 break;
7752
7753 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007754 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007755 break;
7756
7757 default:
7758 /* unsupported! */
7759 WARN_ON_ONCE(1);
7760 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007761 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007762 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007763 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007764 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007765 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007766 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7767 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007768 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007769 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007770 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7771 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007772#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007773 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007774#endif
7775 msg.rpc_argp = &calldata->args;
7776 msg.rpc_resp = &calldata->res;
7777 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007778
Trond Myklebust9c760d12017-07-31 18:38:50 -04007779 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007780
7781out_impl_id:
7782 kfree(calldata->res.impl_id);
7783out_server_scope:
7784 kfree(calldata->res.server_scope);
7785out_server_owner:
7786 kfree(calldata->res.server_owner);
7787out_calldata:
7788 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007789out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007790 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007791 return ERR_PTR(status);
7792}
7793
7794/*
7795 * _nfs4_proc_exchange_id()
7796 *
7797 * Wrapper for EXCHANGE_ID operation.
7798 */
7799static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7800 u32 sp4_how)
7801{
7802 struct rpc_task *task;
7803 struct nfs41_exchange_id_args *argp;
7804 struct nfs41_exchange_id_res *resp;
7805 int status;
7806
7807 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7808 if (IS_ERR(task))
7809 return PTR_ERR(task);
7810
7811 argp = task->tk_msg.rpc_argp;
7812 resp = task->tk_msg.rpc_resp;
7813 status = task->tk_status;
7814 if (status != 0)
7815 goto out;
7816
7817 status = nfs4_check_cl_exchange_flags(resp->flags);
7818 if (status != 0)
7819 goto out;
7820
7821 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7822 if (status != 0)
7823 goto out;
7824
7825 clp->cl_clientid = resp->clientid;
7826 clp->cl_exchange_flags = resp->flags;
7827 clp->cl_seqid = resp->seqid;
7828 /* Client ID is not confirmed */
7829 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7830 clear_bit(NFS4_SESSION_ESTABLISHED,
7831 &clp->cl_session->session_state);
7832
7833 if (clp->cl_serverscope != NULL &&
7834 !nfs41_same_server_scope(clp->cl_serverscope,
7835 resp->server_scope)) {
7836 dprintk("%s: server_scope mismatch detected\n",
7837 __func__);
7838 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7839 }
7840
7841 swap(clp->cl_serverowner, resp->server_owner);
7842 swap(clp->cl_serverscope, resp->server_scope);
7843 swap(clp->cl_implid, resp->impl_id);
7844
7845 /* Save the EXCHANGE_ID verifier session trunk tests */
7846 memcpy(clp->cl_confirm.data, argp->verifier.data,
7847 sizeof(clp->cl_confirm.data));
7848out:
7849 trace_nfs4_exchange_id(clp, status);
7850 rpc_put_task(task);
7851 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007852}
7853
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007854/*
7855 * nfs4_proc_exchange_id()
7856 *
7857 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7858 *
7859 * Since the clientid has expired, all compounds using sessions
7860 * associated with the stale clientid will be returning
7861 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7862 * be in some phase of session reset.
7863 *
7864 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7865 */
7866int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7867{
7868 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7869 int status;
7870
7871 /* try SP4_MACH_CRED if krb5i/p */
7872 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7873 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007874 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007875 if (!status)
7876 return 0;
7877 }
7878
7879 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007880 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007881}
7882
Andy Adamson04fa2c62016-09-09 09:22:29 -04007883/**
7884 * nfs4_test_session_trunk
7885 *
7886 * This is an add_xprt_test() test function called from
7887 * rpc_clnt_setup_test_and_add_xprt.
7888 *
7889 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7890 * and is dereferrenced in nfs4_exchange_id_release
7891 *
7892 * Upon success, add the new transport to the rpc_clnt
7893 *
7894 * @clnt: struct rpc_clnt to get new transport
7895 * @xprt: the rpc_xprt to test
7896 * @data: call data for _nfs4_proc_exchange_id.
7897 */
7898int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7899 void *data)
7900{
7901 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007902 struct rpc_task *task;
7903 int status;
7904
Andy Adamson04fa2c62016-09-09 09:22:29 -04007905 u32 sp4_how;
7906
7907 dprintk("--> %s try %s\n", __func__,
7908 xprt->address_strings[RPC_DISPLAY_ADDR]);
7909
7910 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7911
7912 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007913 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7914 if (IS_ERR(task))
7915 return PTR_ERR(task);
7916
7917 status = task->tk_status;
7918 if (status == 0)
7919 status = nfs4_detect_session_trunking(adata->clp,
7920 task->tk_msg.rpc_resp, xprt);
7921
7922 rpc_put_task(task);
7923 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007924}
7925EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7926
Trond Myklebust66245532012-05-25 17:18:09 -04007927static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7928 struct rpc_cred *cred)
7929{
7930 struct rpc_message msg = {
7931 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7932 .rpc_argp = clp,
7933 .rpc_cred = cred,
7934 };
7935 int status;
7936
7937 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007938 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007939 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007940 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007941 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7942 return status;
7943}
7944
7945static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7946 struct rpc_cred *cred)
7947{
7948 unsigned int loop;
7949 int ret;
7950
7951 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7952 ret = _nfs4_proc_destroy_clientid(clp, cred);
7953 switch (ret) {
7954 case -NFS4ERR_DELAY:
7955 case -NFS4ERR_CLIENTID_BUSY:
7956 ssleep(1);
7957 break;
7958 default:
7959 return ret;
7960 }
7961 }
7962 return 0;
7963}
7964
7965int nfs4_destroy_clientid(struct nfs_client *clp)
7966{
7967 struct rpc_cred *cred;
7968 int ret = 0;
7969
7970 if (clp->cl_mvops->minor_version < 1)
7971 goto out;
7972 if (clp->cl_exchange_flags == 0)
7973 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007974 if (clp->cl_preserve_clid)
7975 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007976 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007977 ret = nfs4_proc_destroy_clientid(clp, cred);
7978 if (cred)
7979 put_rpccred(cred);
7980 switch (ret) {
7981 case 0:
7982 case -NFS4ERR_STALE_CLIENTID:
7983 clp->cl_exchange_flags = 0;
7984 }
7985out:
7986 return ret;
7987}
7988
Andy Adamson2050f0c2009-04-01 09:22:30 -04007989struct nfs4_get_lease_time_data {
7990 struct nfs4_get_lease_time_args *args;
7991 struct nfs4_get_lease_time_res *res;
7992 struct nfs_client *clp;
7993};
7994
7995static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7996 void *calldata)
7997{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007998 struct nfs4_get_lease_time_data *data =
7999 (struct nfs4_get_lease_time_data *)calldata;
8000
8001 dprintk("--> %s\n", __func__);
8002 /* just setup sequence, do not trigger session recovery
8003 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008004 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008005 &data->args->la_seq_args,
8006 &data->res->lr_seq_res,
8007 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008008 dprintk("<-- %s\n", __func__);
8009}
8010
8011/*
8012 * Called from nfs4_state_manager thread for session setup, so don't recover
8013 * from sequence operation or clientid errors.
8014 */
8015static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8016{
8017 struct nfs4_get_lease_time_data *data =
8018 (struct nfs4_get_lease_time_data *)calldata;
8019
8020 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008021 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8022 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008023 switch (task->tk_status) {
8024 case -NFS4ERR_DELAY:
8025 case -NFS4ERR_GRACE:
8026 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8027 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8028 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008029 /* fall through */
8030 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008031 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008032 return;
8033 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008034 dprintk("<-- %s\n", __func__);
8035}
8036
Trond Myklebust17280172012-03-11 13:11:00 -04008037static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008038 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8039 .rpc_call_done = nfs4_get_lease_time_done,
8040};
8041
8042int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8043{
8044 struct rpc_task *task;
8045 struct nfs4_get_lease_time_args args;
8046 struct nfs4_get_lease_time_res res = {
8047 .lr_fsinfo = fsinfo,
8048 };
8049 struct nfs4_get_lease_time_data data = {
8050 .args = &args,
8051 .res = &res,
8052 .clp = clp,
8053 };
8054 struct rpc_message msg = {
8055 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8056 .rpc_argp = &args,
8057 .rpc_resp = &res,
8058 };
8059 struct rpc_task_setup task_setup = {
8060 .rpc_client = clp->cl_rpcclient,
8061 .rpc_message = &msg,
8062 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008063 .callback_data = &data,
8064 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008065 };
8066 int status;
8067
Anna Schumakerfba83f32018-05-04 16:22:50 -04008068 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008069 task = rpc_run_task(&task_setup);
8070
8071 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008072 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008073
Anna Schumakerf6148712017-04-07 14:15:23 -04008074 status = task->tk_status;
8075 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008076 return status;
8077}
8078
Andy Adamsonfc931582009-04-01 09:22:31 -04008079/*
8080 * Initialize the values to be used by the client in CREATE_SESSION
8081 * If nfs4_init_session set the fore channel request and response sizes,
8082 * use them.
8083 *
8084 * Set the back channel max_resp_sz_cached to zero to force the client to
8085 * always set csa_cachethis to FALSE because the current implementation
8086 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8087 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008088static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8089 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008090{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008091 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008092 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008093
Andy Adamson18aad3d2013-06-26 12:21:49 -04008094 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8095 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8096
Andy Adamsonfc931582009-04-01 09:22:31 -04008097 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008098 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8099 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008100 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008101 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008102
8103 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008104 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008105 __func__,
8106 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008107 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008108
8109 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008110 args->bc_attrs.max_rqst_sz = max_bc_payload;
8111 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008112 args->bc_attrs.max_resp_sz_cached = 0;
8113 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008114 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008115
8116 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8117 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8118 __func__,
8119 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8120 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8121 args->bc_attrs.max_reqs);
8122}
8123
Trond Myklebust79969dd12015-02-18 11:30:18 -08008124static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8125 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008126{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008127 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008128 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008129
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008130 if (rcvd->max_resp_sz > sent->max_resp_sz)
8131 return -EINVAL;
8132 /*
8133 * Our requested max_ops is the minimum we need; we're not
8134 * prepared to break up compounds into smaller pieces than that.
8135 * So, no point even trying to continue if the server won't
8136 * cooperate:
8137 */
8138 if (rcvd->max_ops < sent->max_ops)
8139 return -EINVAL;
8140 if (rcvd->max_reqs == 0)
8141 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008142 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8143 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008144 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008145}
8146
Trond Myklebust79969dd12015-02-18 11:30:18 -08008147static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8148 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008149{
8150 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008151 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008152
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008153 if (!(res->flags & SESSION4_BACK_CHAN))
8154 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008155 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8156 return -EINVAL;
8157 if (rcvd->max_resp_sz < sent->max_resp_sz)
8158 return -EINVAL;
8159 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8160 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008161 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008162 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008163 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008164 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008165out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008166 return 0;
8167}
Andy Adamson8d353012009-04-01 09:22:32 -04008168
Andy Adamson8d353012009-04-01 09:22:32 -04008169static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008170 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008171{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008172 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008173
Trond Myklebust79969dd12015-02-18 11:30:18 -08008174 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008175 if (ret)
8176 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008177 return nfs4_verify_back_channel_attrs(args, res);
8178}
8179
8180static void nfs4_update_session(struct nfs4_session *session,
8181 struct nfs41_create_session_res *res)
8182{
8183 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008184 /* Mark client id and session as being confirmed */
8185 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8186 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008187 session->flags = res->flags;
8188 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008189 if (res->flags & SESSION4_BACK_CHAN)
8190 memcpy(&session->bc_attrs, &res->bc_attrs,
8191 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008192}
8193
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008194static int _nfs4_proc_create_session(struct nfs_client *clp,
8195 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008196{
8197 struct nfs4_session *session = clp->cl_session;
8198 struct nfs41_create_session_args args = {
8199 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008200 .clientid = clp->cl_clientid,
8201 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008202 .cb_program = NFS4_CALLBACK,
8203 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008204 struct nfs41_create_session_res res;
8205
Andy Adamsonfc931582009-04-01 09:22:31 -04008206 struct rpc_message msg = {
8207 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8208 .rpc_argp = &args,
8209 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008210 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008211 };
8212 int status;
8213
Chuck Lever6b26cc82016-05-02 14:40:40 -04008214 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008215 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008216
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008217 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008218 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008219
Trond Myklebustb519d402016-09-11 14:50:01 -04008220 switch (status) {
8221 case -NFS4ERR_STALE_CLIENTID:
8222 case -NFS4ERR_DELAY:
8223 case -ETIMEDOUT:
8224 case -EACCES:
8225 case -EAGAIN:
8226 goto out;
8227 };
8228
8229 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008230 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008231 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008232 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008233 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008234 if (status)
8235 goto out;
8236 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008237 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008238out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008239 return status;
8240}
8241
8242/*
8243 * Issues a CREATE_SESSION operation to the server.
8244 * It is the responsibility of the caller to verify the session is
8245 * expired before calling this routine.
8246 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008247int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008248{
8249 int status;
8250 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008251 struct nfs4_session *session = clp->cl_session;
8252
8253 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8254
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008255 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008256 if (status)
8257 goto out;
8258
Andy Adamsonaacd5532011-11-09 13:58:21 -05008259 /* Init or reset the session slot tables */
8260 status = nfs4_setup_session_slot_tables(session);
8261 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008262 if (status)
8263 goto out;
8264
8265 ptr = (unsigned *)&session->sess_id.data[0];
8266 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8267 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008268out:
8269 dprintk("<-- %s\n", __func__);
8270 return status;
8271}
8272
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008273/*
8274 * Issue the over-the-wire RPC DESTROY_SESSION.
8275 * The caller must serialize access to this routine.
8276 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008277int nfs4_proc_destroy_session(struct nfs4_session *session,
8278 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008279{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008280 struct rpc_message msg = {
8281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8282 .rpc_argp = session,
8283 .rpc_cred = cred,
8284 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008285 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008286
8287 dprintk("--> nfs4_proc_destroy_session\n");
8288
8289 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008290 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8291 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008292
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008293 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008294 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008295
8296 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008297 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008298 "Session has been destroyed regardless...\n", status);
8299
8300 dprintk("<-- nfs4_proc_destroy_session\n");
8301 return status;
8302}
8303
Trond Myklebust7b38c362012-05-23 13:23:31 -04008304/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008305 * Renew the cl_session lease.
8306 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008307struct nfs4_sequence_data {
8308 struct nfs_client *clp;
8309 struct nfs4_sequence_args args;
8310 struct nfs4_sequence_res res;
8311};
8312
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008313static void nfs41_sequence_release(void *data)
8314{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008315 struct nfs4_sequence_data *calldata = data;
8316 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008317
Elena Reshetova212bf412017-10-20 12:53:38 +03008318 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008319 nfs4_schedule_state_renewal(clp);
8320 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008321 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008322}
8323
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008324static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8325{
8326 switch(task->tk_status) {
8327 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008328 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8329 return -EAGAIN;
8330 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008331 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008332 }
8333 return 0;
8334}
8335
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008336static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008337{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008338 struct nfs4_sequence_data *calldata = data;
8339 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008340
Trond Myklebust14516c32010-07-31 14:29:06 -04008341 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8342 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008343
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008344 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008345 if (task->tk_status < 0) {
8346 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008347 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008348 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008349
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008350 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8351 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008352 return;
8353 }
8354 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008355 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008356out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008357 dprintk("<-- %s\n", __func__);
8358}
8359
8360static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8361{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008362 struct nfs4_sequence_data *calldata = data;
8363 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008364 struct nfs4_sequence_args *args;
8365 struct nfs4_sequence_res *res;
8366
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008367 args = task->tk_msg.rpc_argp;
8368 res = task->tk_msg.rpc_resp;
8369
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008370 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008371}
8372
8373static const struct rpc_call_ops nfs41_sequence_ops = {
8374 .rpc_call_done = nfs41_sequence_call_done,
8375 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008376 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008377};
8378
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008379static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8380 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008381 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008382 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008383{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008384 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008385 struct rpc_message msg = {
8386 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8387 .rpc_cred = cred,
8388 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008389 struct rpc_task_setup task_setup_data = {
8390 .rpc_client = clp->cl_rpcclient,
8391 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008392 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008393 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008394 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008395 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008396
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008397 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008398 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008399 goto out_err;
8400
8401 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008402 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008403 if (calldata == NULL)
8404 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008405 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008406 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008407 msg.rpc_argp = &calldata->args;
8408 msg.rpc_resp = &calldata->res;
8409 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008410 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008411
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008412 ret = rpc_run_task(&task_setup_data);
8413 if (IS_ERR(ret))
8414 goto out_err;
8415 return ret;
8416out_put_clp:
8417 nfs_put_client(clp);
8418out_err:
8419 nfs41_release_slot(slot);
8420 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008421}
8422
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008423static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008424{
8425 struct rpc_task *task;
8426 int ret = 0;
8427
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008428 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008429 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008430 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008431 if (IS_ERR(task))
8432 ret = PTR_ERR(task);
8433 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008434 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008435 dprintk("<-- %s status=%d\n", __func__, ret);
8436 return ret;
8437}
8438
8439static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8440{
8441 struct rpc_task *task;
8442 int ret;
8443
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008444 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008445 if (IS_ERR(task)) {
8446 ret = PTR_ERR(task);
8447 goto out;
8448 }
8449 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008450 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008451 ret = task->tk_status;
8452 rpc_put_task(task);
8453out:
8454 dprintk("<-- %s status=%d\n", __func__, ret);
8455 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008456}
8457
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008458struct nfs4_reclaim_complete_data {
8459 struct nfs_client *clp;
8460 struct nfs41_reclaim_complete_args arg;
8461 struct nfs41_reclaim_complete_res res;
8462};
8463
8464static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8465{
8466 struct nfs4_reclaim_complete_data *calldata = data;
8467
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008468 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008469 &calldata->arg.seq_args,
8470 &calldata->res.seq_res,
8471 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008472}
8473
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008474static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8475{
8476 switch(task->tk_status) {
8477 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008478 wake_up_all(&clp->cl_lock_waitq);
8479 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008480 case -NFS4ERR_COMPLETE_ALREADY:
8481 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8482 break;
8483 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008484 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008485 /* fall through */
8486 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008487 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008488 case -NFS4ERR_BADSESSION:
8489 case -NFS4ERR_DEADSESSION:
8490 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8491 nfs4_schedule_session_recovery(clp->cl_session,
8492 task->tk_status);
8493 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008494 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008495 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008496 }
8497 return 0;
8498}
8499
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008500static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8501{
8502 struct nfs4_reclaim_complete_data *calldata = data;
8503 struct nfs_client *clp = calldata->clp;
8504 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8505
8506 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008507 if (!nfs41_sequence_done(task, res))
8508 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008509
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008510 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008511 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8512 rpc_restart_call_prepare(task);
8513 return;
8514 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008515 dprintk("<-- %s\n", __func__);
8516}
8517
8518static void nfs4_free_reclaim_complete_data(void *data)
8519{
8520 struct nfs4_reclaim_complete_data *calldata = data;
8521
8522 kfree(calldata);
8523}
8524
8525static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8526 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8527 .rpc_call_done = nfs4_reclaim_complete_done,
8528 .rpc_release = nfs4_free_reclaim_complete_data,
8529};
8530
8531/*
8532 * Issue a global reclaim complete.
8533 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008534static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8535 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008536{
8537 struct nfs4_reclaim_complete_data *calldata;
8538 struct rpc_task *task;
8539 struct rpc_message msg = {
8540 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008541 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008542 };
8543 struct rpc_task_setup task_setup_data = {
8544 .rpc_client = clp->cl_rpcclient,
8545 .rpc_message = &msg,
8546 .callback_ops = &nfs4_reclaim_complete_call_ops,
8547 .flags = RPC_TASK_ASYNC,
8548 };
8549 int status = -ENOMEM;
8550
8551 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008552 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008553 if (calldata == NULL)
8554 goto out;
8555 calldata->clp = clp;
8556 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008557
Anna Schumakerfba83f32018-05-04 16:22:50 -04008558 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008559 msg.rpc_argp = &calldata->arg;
8560 msg.rpc_resp = &calldata->res;
8561 task_setup_data.callback_data = calldata;
8562 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008563 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008564 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008565 goto out;
8566 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008567 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008568 if (status == 0)
8569 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008570 rpc_put_task(task);
8571out:
8572 dprintk("<-- %s status=%d\n", __func__, status);
8573 return status;
8574}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008575
8576static void
8577nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8578{
8579 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008580 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008581
8582 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008583 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008584 &lgp->res.seq_res, task);
8585 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008586}
8587
8588static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8589{
8590 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008591
8592 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008593 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008594 dprintk("<-- %s\n", __func__);
8595}
8596
8597static int
8598nfs4_layoutget_handle_exception(struct rpc_task *task,
8599 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8600{
Trond Myklebustee314c22012-10-01 17:25:48 -07008601 struct inode *inode = lgp->args.inode;
8602 struct nfs_server *server = NFS_SERVER(inode);
8603 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008604 int nfs4err = task->tk_status;
8605 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008606 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008607
Boaz Harroshed7e5422014-01-22 20:34:54 +02008608 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008609
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008610 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008611 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008612 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008613
8614 /*
8615 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8616 * on the file. set tk_status to -ENODATA to tell upper layer to
8617 * retry go inband.
8618 */
8619 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008620 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008621 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008622 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008623 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8624 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8625 */
8626 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008627 status = -EOVERFLOW;
8628 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008629 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008630 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008631 * (or clients) writing to the same RAID stripe except when
8632 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008633 *
8634 * Treat it like we would RECALLCONFLICT -- we retry for a little
8635 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008636 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008637 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008638 if (lgp->args.minlength == 0) {
8639 status = -EOVERFLOW;
8640 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008641 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008642 status = -EBUSY;
8643 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008644 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008645 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008646 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008647 case -NFS4ERR_DELEG_REVOKED:
8648 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008649 case -NFS4ERR_EXPIRED:
8650 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008651 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008652 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008653 lo = NFS_I(inode)->layout;
8654 /* If the open stateid was bad, then recover it. */
8655 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008656 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008657 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008658 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008659 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008660 break;
8661 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008662
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008663 /*
8664 * Mark the bad layout state as invalid, then retry
8665 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008666 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008667 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008668 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008669 pnfs_free_lseg_list(&head);
8670 status = -EAGAIN;
8671 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008672 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008673
Trond Myklebust8ac09252017-01-23 22:44:12 -05008674 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008675 err = nfs4_handle_exception(server, nfs4err, exception);
8676 if (!status) {
8677 if (exception->retry)
8678 status = -EAGAIN;
8679 else
8680 status = err;
8681 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008682out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008684 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008685}
8686
Idan Kedar85541162012-08-02 11:47:10 +03008687static size_t max_response_pages(struct nfs_server *server)
8688{
8689 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8690 return nfs_page_array_len(0, max_resp_sz);
8691}
8692
8693static void nfs4_free_pages(struct page **pages, size_t size)
8694{
8695 int i;
8696
8697 if (!pages)
8698 return;
8699
8700 for (i = 0; i < size; i++) {
8701 if (!pages[i])
8702 break;
8703 __free_page(pages[i]);
8704 }
8705 kfree(pages);
8706}
8707
8708static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8709{
8710 struct page **pages;
8711 int i;
8712
8713 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8714 if (!pages) {
8715 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8716 return NULL;
8717 }
8718
8719 for (i = 0; i < size; i++) {
8720 pages[i] = alloc_page(gfp_flags);
8721 if (!pages[i]) {
8722 dprintk("%s: failed to allocate page\n", __func__);
8723 nfs4_free_pages(pages, size);
8724 return NULL;
8725 }
8726 }
8727
8728 return pages;
8729}
8730
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008731static void nfs4_layoutget_release(void *calldata)
8732{
8733 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008734 struct inode *inode = lgp->args.inode;
8735 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008736 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008737
8738 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008739 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008740 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008741 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008742 put_nfs_open_context(lgp->args.ctx);
8743 kfree(calldata);
8744 dprintk("<-- %s\n", __func__);
8745}
8746
8747static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8748 .rpc_call_prepare = nfs4_layoutget_prepare,
8749 .rpc_call_done = nfs4_layoutget_done,
8750 .rpc_release = nfs4_layoutget_release,
8751};
8752
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008753struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008754nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008755{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008756 struct inode *inode = lgp->args.inode;
8757 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008758 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008759 struct rpc_task *task;
8760 struct rpc_message msg = {
8761 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8762 .rpc_argp = &lgp->args,
8763 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008764 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008765 };
8766 struct rpc_task_setup task_setup_data = {
8767 .rpc_client = server->client,
8768 .rpc_message = &msg,
8769 .callback_ops = &nfs4_layoutget_call_ops,
8770 .callback_data = lgp,
8771 .flags = RPC_TASK_ASYNC,
8772 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008773 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008774 struct nfs4_exception exception = {
8775 .inode = inode,
8776 .timeout = *timeout,
8777 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008778 int status = 0;
8779
8780 dprintk("--> %s\n", __func__);
8781
Peng Tao4bd5a982014-11-17 11:05:17 +08008782 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8783 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8784
Idan Kedar85541162012-08-02 11:47:10 +03008785 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8786 if (!lgp->args.layout.pages) {
8787 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008788 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008789 }
8790 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8791
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008792 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008793 lgp->res.seq_res.sr_slot = NULL;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008794 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008795
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008796 task = rpc_run_task(&task_setup_data);
8797 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008798 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008799 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008800 if (status == 0) {
8801 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8802 *timeout = exception.timeout;
8803 }
8804
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008805 trace_nfs4_layoutget(lgp->args.ctx,
8806 &lgp->args.range,
8807 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008808 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008809 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008810
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008811 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8812 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008813 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008814 rpc_put_task(task);
8815 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008816 if (status)
8817 return ERR_PTR(status);
8818 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008819}
8820
Benny Halevycbe82602011-05-22 19:52:37 +03008821static void
8822nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8823{
8824 struct nfs4_layoutreturn *lrp = calldata;
8825
8826 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008827 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008828 &lrp->args.seq_args,
8829 &lrp->res.seq_res,
8830 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008831}
8832
8833static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8834{
8835 struct nfs4_layoutreturn *lrp = calldata;
8836 struct nfs_server *server;
8837
8838 dprintk("--> %s\n", __func__);
8839
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008840 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008841 return;
8842
8843 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008844 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008845 case -NFS4ERR_OLD_STATEID:
8846 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8847 lrp->args.inode))
8848 goto out_restart;
8849 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008850 default:
8851 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008852 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008853 case 0:
8854 break;
8855 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008856 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008857 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008858 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008859 }
Benny Halevycbe82602011-05-22 19:52:37 +03008860 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008861 return;
8862out_restart:
8863 task->tk_status = 0;
8864 nfs4_sequence_free_slot(&lrp->res.seq_res);
8865 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008866}
8867
8868static void nfs4_layoutreturn_release(void *calldata)
8869{
8870 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008871 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008872
8873 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008874 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008875 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008876 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008877 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8878 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008879 pnfs_put_layout_hdr(lrp->args.layout);
8880 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008881 kfree(calldata);
8882 dprintk("<-- %s\n", __func__);
8883}
8884
8885static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8886 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8887 .rpc_call_done = nfs4_layoutreturn_done,
8888 .rpc_release = nfs4_layoutreturn_release,
8889};
8890
Peng Tao6c166052014-11-17 09:30:40 +08008891int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008892{
8893 struct rpc_task *task;
8894 struct rpc_message msg = {
8895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8896 .rpc_argp = &lrp->args,
8897 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008898 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008899 };
8900 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04008901 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008902 .rpc_message = &msg,
8903 .callback_ops = &nfs4_layoutreturn_call_ops,
8904 .callback_data = lrp,
8905 };
Peng Tao6c166052014-11-17 09:30:40 +08008906 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008907
Andrew Elble99ade3c2015-12-02 09:39:51 -05008908 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8909 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8910 &task_setup_data.rpc_client, &msg);
8911
Benny Halevycbe82602011-05-22 19:52:37 +03008912 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008913 if (!sync) {
8914 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8915 if (!lrp->inode) {
8916 nfs4_layoutreturn_release(lrp);
8917 return -EAGAIN;
8918 }
8919 task_setup_data.flags |= RPC_TASK_ASYNC;
8920 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008921 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008922 task = rpc_run_task(&task_setup_data);
8923 if (IS_ERR(task))
8924 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008925 if (sync)
8926 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008927 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008928 dprintk("<-- %s status=%d\n", __func__, status);
8929 rpc_put_task(task);
8930 return status;
8931}
8932
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008933static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008934_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8935 struct pnfs_device *pdev,
8936 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008937{
8938 struct nfs4_getdeviceinfo_args args = {
8939 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008940 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8941 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008942 };
8943 struct nfs4_getdeviceinfo_res res = {
8944 .pdev = pdev,
8945 };
8946 struct rpc_message msg = {
8947 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8948 .rpc_argp = &args,
8949 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008950 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008951 };
8952 int status;
8953
8954 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008955 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008956 if (res.notification & ~args.notify_types)
8957 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008958 if (res.notification != args.notify_types)
8959 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008960
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008961 dprintk("<-- %s status=%d\n", __func__, status);
8962
8963 return status;
8964}
8965
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008966int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8967 struct pnfs_device *pdev,
8968 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008969{
8970 struct nfs4_exception exception = { };
8971 int err;
8972
8973 do {
8974 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008975 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008976 &exception);
8977 } while (exception.retry);
8978 return err;
8979}
8980EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8981
Andy Adamson863a3c62011-03-23 13:27:54 +00008982static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8983{
8984 struct nfs4_layoutcommit_data *data = calldata;
8985 struct nfs_server *server = NFS_SERVER(data->args.inode);
8986
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008987 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008988 &data->args.seq_args,
8989 &data->res.seq_res,
8990 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008991}
8992
8993static void
8994nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8995{
8996 struct nfs4_layoutcommit_data *data = calldata;
8997 struct nfs_server *server = NFS_SERVER(data->args.inode);
8998
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008999 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009000 return;
9001
9002 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009003 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9004 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9005 case -NFS4ERR_BADLAYOUT: /* no layout */
9006 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009007 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009008 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009009 break;
9010 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009011 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009012 rpc_restart_call_prepare(task);
9013 return;
9014 }
9015 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009016}
9017
9018static void nfs4_layoutcommit_release(void *calldata)
9019{
9020 struct nfs4_layoutcommit_data *data = calldata;
9021
Andy Adamsondb29c082011-07-30 20:52:38 -04009022 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009023 nfs_post_op_update_inode_force_wcc(data->args.inode,
9024 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009025 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009026 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009027 kfree(data);
9028}
9029
9030static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9031 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9032 .rpc_call_done = nfs4_layoutcommit_done,
9033 .rpc_release = nfs4_layoutcommit_release,
9034};
9035
9036int
Andy Adamsonef311532011-03-12 02:58:10 -05009037nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009038{
9039 struct rpc_message msg = {
9040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9041 .rpc_argp = &data->args,
9042 .rpc_resp = &data->res,
9043 .rpc_cred = data->cred,
9044 };
9045 struct rpc_task_setup task_setup_data = {
9046 .task = &data->task,
9047 .rpc_client = NFS_CLIENT(data->args.inode),
9048 .rpc_message = &msg,
9049 .callback_ops = &nfs4_layoutcommit_ops,
9050 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009051 };
9052 struct rpc_task *task;
9053 int status = 0;
9054
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009055 dprintk("NFS: initiating layoutcommit call. sync %d "
9056 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009057 data->args.lastbytewritten,
9058 data->args.inode->i_ino);
9059
Trond Myklebust472e2592015-02-05 16:50:30 -05009060 if (!sync) {
9061 data->inode = nfs_igrab_and_active(data->args.inode);
9062 if (data->inode == NULL) {
9063 nfs4_layoutcommit_release(data);
9064 return -EAGAIN;
9065 }
9066 task_setup_data.flags = RPC_TASK_ASYNC;
9067 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009068 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009069 task = rpc_run_task(&task_setup_data);
9070 if (IS_ERR(task))
9071 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009072 if (sync)
9073 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009074 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009075 dprintk("%s: status %d\n", __func__, status);
9076 rpc_put_task(task);
9077 return status;
9078}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009079
Andy Adamson97431202013-08-08 10:57:56 -04009080/**
9081 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9082 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9083 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009084static int
9085_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009086 struct nfs_fsinfo *info,
9087 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009088{
9089 struct nfs41_secinfo_no_name_args args = {
9090 .style = SECINFO_STYLE_CURRENT_FH,
9091 };
9092 struct nfs4_secinfo_res res = {
9093 .flavors = flavors,
9094 };
9095 struct rpc_message msg = {
9096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9097 .rpc_argp = &args,
9098 .rpc_resp = &res,
9099 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009100 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009101 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009102 int status;
9103
9104 if (use_integrity) {
9105 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009106 cred = nfs4_get_clid_cred(server->nfs_client);
9107 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009108 }
9109
9110 dprintk("--> %s\n", __func__);
9111 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9112 &res.seq_res, 0);
9113 dprintk("<-- %s status=%d\n", __func__, status);
9114
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009115 if (cred)
9116 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009117
9118 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009119}
9120
9121static int
9122nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9123 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9124{
9125 struct nfs4_exception exception = { };
9126 int err;
9127 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009128 /* first try using integrity protection */
9129 err = -NFS4ERR_WRONGSEC;
9130
9131 /* try to use integrity protection with machine cred */
9132 if (_nfs4_is_integrity_protected(server->nfs_client))
9133 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9134 flavors, true);
9135
9136 /*
9137 * if unable to use integrity protection, or SECINFO with
9138 * integrity protection returns NFS4ERR_WRONGSEC (which is
9139 * disallowed by spec, but exists in deployed servers) use
9140 * the current filesystem's rpc_client and the user cred.
9141 */
9142 if (err == -NFS4ERR_WRONGSEC)
9143 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9144 flavors, false);
9145
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009146 switch (err) {
9147 case 0:
9148 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009149 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009150 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009151 default:
9152 err = nfs4_handle_exception(server, err, &exception);
9153 }
9154 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009155out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009156 return err;
9157}
9158
9159static int
9160nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9161 struct nfs_fsinfo *info)
9162{
9163 int err;
9164 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009165 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009166 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009167 struct nfs4_secinfo4 *secinfo;
9168 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009169
9170 page = alloc_page(GFP_KERNEL);
9171 if (!page) {
9172 err = -ENOMEM;
9173 goto out;
9174 }
9175
9176 flavors = page_address(page);
9177 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9178
9179 /*
9180 * Fall back on "guess and check" method if
9181 * the server doesn't support SECINFO_NO_NAME
9182 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009183 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009184 err = nfs4_find_root_sec(server, fhandle, info);
9185 goto out_freepage;
9186 }
9187 if (err)
9188 goto out_freepage;
9189
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009190 for (i = 0; i < flavors->num_flavors; i++) {
9191 secinfo = &flavors->flavors[i];
9192
9193 switch (secinfo->flavor) {
9194 case RPC_AUTH_NULL:
9195 case RPC_AUTH_UNIX:
9196 case RPC_AUTH_GSS:
9197 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9198 &secinfo->flavor_info);
9199 break;
9200 default:
9201 flavor = RPC_AUTH_MAXFLAVOR;
9202 break;
9203 }
9204
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009205 if (!nfs_auth_info_match(&server->auth_info, flavor))
9206 flavor = RPC_AUTH_MAXFLAVOR;
9207
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009208 if (flavor != RPC_AUTH_MAXFLAVOR) {
9209 err = nfs4_lookup_root_sec(server, fhandle,
9210 info, flavor);
9211 if (!err)
9212 break;
9213 }
9214 }
9215
9216 if (flavor == RPC_AUTH_MAXFLAVOR)
9217 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009218
9219out_freepage:
9220 put_page(page);
9221 if (err == -EACCES)
9222 return -EPERM;
9223out:
9224 return err;
9225}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009226
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009227static int _nfs41_test_stateid(struct nfs_server *server,
9228 nfs4_stateid *stateid,
9229 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009230{
9231 int status;
9232 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009233 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009234 };
9235 struct nfs41_test_stateid_res res;
9236 struct rpc_message msg = {
9237 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9238 .rpc_argp = &args,
9239 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009240 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009241 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009242 struct rpc_clnt *rpc_client = server->client;
9243
9244 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9245 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009246
Chuck Lever38527b12012-07-11 16:30:23 -04009247 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009248 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009249 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009250 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009251 if (status != NFS_OK) {
9252 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009253 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009254 }
9255 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009256 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009257}
9258
Trond Myklebust43912bb2016-09-22 13:38:56 -04009259static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9260 int err, struct nfs4_exception *exception)
9261{
9262 exception->retry = 0;
9263 switch(err) {
9264 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009265 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009266 nfs4_handle_exception(server, err, exception);
9267 break;
9268 case -NFS4ERR_BADSESSION:
9269 case -NFS4ERR_BADSLOT:
9270 case -NFS4ERR_BAD_HIGH_SLOT:
9271 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9272 case -NFS4ERR_DEADSESSION:
9273 nfs4_do_handle_exception(server, err, exception);
9274 }
9275}
9276
Chuck Lever38527b12012-07-11 16:30:23 -04009277/**
9278 * nfs41_test_stateid - perform a TEST_STATEID operation
9279 *
9280 * @server: server / transport on which to perform the operation
9281 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009282 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009283 *
9284 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9285 * Otherwise a negative NFS4ERR value is returned if the operation
9286 * failed or the state ID is not currently valid.
9287 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009288static int nfs41_test_stateid(struct nfs_server *server,
9289 nfs4_stateid *stateid,
9290 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009291{
9292 struct nfs4_exception exception = { };
9293 int err;
9294 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009295 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009296 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009297 } while (exception.retry);
9298 return err;
9299}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009300
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009301struct nfs_free_stateid_data {
9302 struct nfs_server *server;
9303 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009304 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009305};
9306
9307static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9308{
9309 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009310 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009311 &data->args.seq_args,
9312 &data->res.seq_res,
9313 task);
9314}
9315
9316static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9317{
9318 struct nfs_free_stateid_data *data = calldata;
9319
9320 nfs41_sequence_done(task, &data->res.seq_res);
9321
9322 switch (task->tk_status) {
9323 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009324 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009325 rpc_restart_call_prepare(task);
9326 }
9327}
9328
9329static void nfs41_free_stateid_release(void *calldata)
9330{
9331 kfree(calldata);
9332}
9333
Trond Myklebust17f26b12013-08-21 15:48:42 -04009334static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009335 .rpc_call_prepare = nfs41_free_stateid_prepare,
9336 .rpc_call_done = nfs41_free_stateid_done,
9337 .rpc_release = nfs41_free_stateid_release,
9338};
9339
Anna Schumaker2f261022018-05-15 13:03:39 -04009340/**
9341 * nfs41_free_stateid - perform a FREE_STATEID operation
9342 *
9343 * @server: server / transport on which to perform the operation
9344 * @stateid: state ID to release
9345 * @cred: credential
9346 * @is_recovery: set to true if this call needs to be privileged
9347 *
9348 * Note: this function is always asynchronous.
9349 */
9350static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009351 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009352 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009353 bool privileged)
9354{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009355 struct rpc_message msg = {
9356 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009357 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009358 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009359 struct rpc_task_setup task_setup = {
9360 .rpc_client = server->client,
9361 .rpc_message = &msg,
9362 .callback_ops = &nfs41_free_stateid_ops,
9363 .flags = RPC_TASK_ASYNC,
9364 };
9365 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009366 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009367
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009368 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9369 &task_setup.rpc_client, &msg);
9370
Chuck Lever38527b12012-07-11 16:30:23 -04009371 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009372 data = kmalloc(sizeof(*data), GFP_NOFS);
9373 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009374 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009375 data->server = server;
9376 nfs4_stateid_copy(&data->args.stateid, stateid);
9377
9378 task_setup.callback_data = data;
9379
9380 msg.rpc_argp = &data->args;
9381 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009382 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009383 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009384 if (IS_ERR(task))
9385 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009386 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009387 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009388}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009389
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009390static void
9391nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009392{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009393 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009394
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009395 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009396 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009397}
9398
Trond Myklebust36281ca2012-03-04 18:13:56 -05009399static bool nfs41_match_stateid(const nfs4_stateid *s1,
9400 const nfs4_stateid *s2)
9401{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009402 if (s1->type != s2->type)
9403 return false;
9404
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009405 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009406 return false;
9407
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009408 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009409 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009410
Anna Schumaker045c5512017-01-11 16:59:48 -05009411 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009412}
9413
Andy Adamson557134a2009-04-01 09:21:53 -04009414#endif /* CONFIG_NFS_V4_1 */
9415
Trond Myklebust36281ca2012-03-04 18:13:56 -05009416static bool nfs4_match_stateid(const nfs4_stateid *s1,
9417 const nfs4_stateid *s2)
9418{
Trond Myklebustf597c532012-03-04 18:13:56 -05009419 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009420}
9421
9422
Trond Myklebust17280172012-03-11 13:11:00 -04009423static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009424 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009425 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009426 .recover_open = nfs4_open_reclaim,
9427 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009428 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009429 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009430};
9431
Andy Adamson591d71c2009-04-01 09:22:47 -04009432#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009433static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009434 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9435 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9436 .recover_open = nfs4_open_reclaim,
9437 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009438 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009439 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009440 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009441};
9442#endif /* CONFIG_NFS_V4_1 */
9443
Trond Myklebust17280172012-03-11 13:11:00 -04009444static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009445 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009446 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009447 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009449 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450};
9451
Andy Adamson591d71c2009-04-01 09:22:47 -04009452#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009453static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009454 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9455 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009456 .recover_open = nfs41_open_expired,
9457 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009458 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009459};
9460#endif /* CONFIG_NFS_V4_1 */
9461
Trond Myklebust17280172012-03-11 13:11:00 -04009462static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009463 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009464 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009465 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009466};
9467
9468#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009469static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009470 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009471 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009472 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009473};
9474#endif
9475
Chuck Leverec011fe2013-10-17 14:12:39 -04009476static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009477 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009478 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009479};
9480
9481#if defined(CONFIG_NFS_V4_1)
9482static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009483 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009484 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009485};
9486#endif /* CONFIG_NFS_V4_1 */
9487
Trond Myklebust97dc1352010-06-16 09:52:26 -04009488static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9489 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009490 .init_caps = NFS_CAP_READDIRPLUS
9491 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009492 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009493 .init_client = nfs40_init_client,
9494 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009495 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009496 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009497 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009498 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009499 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009500 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009501 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9502 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9503 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009504 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009505};
9506
9507#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009508static struct nfs_seqid *
9509nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9510{
9511 return NULL;
9512}
9513
Trond Myklebust97dc1352010-06-16 09:52:26 -04009514static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9515 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009516 .init_caps = NFS_CAP_READDIRPLUS
9517 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009518 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009519 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009520 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009521 .init_client = nfs41_init_client,
9522 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009523 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009524 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009525 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009526 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009527 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009528 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009529 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009530 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9531 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9532 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009533 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009534};
9535#endif
9536
Steve Dickson42c2c422013-05-22 12:50:38 -04009537#if defined(CONFIG_NFS_V4_2)
9538static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9539 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009540 .init_caps = NFS_CAP_READDIRPLUS
9541 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009542 | NFS_CAP_POSIX_LOCK
9543 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009544 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009545 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009546 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009547 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009548 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009549 | NFS_CAP_LAYOUTSTATS
9550 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009551 .init_client = nfs41_init_client,
9552 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009553 .match_stateid = nfs41_match_stateid,
9554 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009555 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009556 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009557 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009558 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009559 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009560 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9561 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9562 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009563 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009564};
9565#endif
9566
Trond Myklebust97dc1352010-06-16 09:52:26 -04009567const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9568 [0] = &nfs_v4_0_minor_ops,
9569#if defined(CONFIG_NFS_V4_1)
9570 [1] = &nfs_v4_1_minor_ops,
9571#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009572#if defined(CONFIG_NFS_V4_2)
9573 [2] = &nfs_v4_2_minor_ops,
9574#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009575};
9576
Trond Myklebust13997822016-07-24 17:10:52 -04009577static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009578{
9579 ssize_t error, error2;
9580
9581 error = generic_listxattr(dentry, list, size);
9582 if (error < 0)
9583 return error;
9584 if (list) {
9585 list += error;
9586 size -= error;
9587 }
9588
9589 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9590 if (error2 < 0)
9591 return error2;
9592 return error + error2;
9593}
9594
Trond Myklebust17f26b12013-08-21 15:48:42 -04009595static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009596 .create = nfs_create,
9597 .lookup = nfs_lookup,
9598 .atomic_open = nfs_atomic_open,
9599 .link = nfs_link,
9600 .unlink = nfs_unlink,
9601 .symlink = nfs_symlink,
9602 .mkdir = nfs_mkdir,
9603 .rmdir = nfs_rmdir,
9604 .mknod = nfs_mknod,
9605 .rename = nfs_rename,
9606 .permission = nfs_permission,
9607 .getattr = nfs_getattr,
9608 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009609 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009610};
9611
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009612static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009613 .permission = nfs_permission,
9614 .getattr = nfs_getattr,
9615 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009616 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009617};
9618
David Howells509de812006-08-22 20:06:11 -04009619const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009620 .version = 4, /* protocol version */
9621 .dentry_ops = &nfs4_dentry_operations,
9622 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009623 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009624 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009625 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009626 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009627 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009628 .getattr = nfs4_proc_getattr,
9629 .setattr = nfs4_proc_setattr,
9630 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009631 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 .access = nfs4_proc_access,
9633 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009634 .create = nfs4_proc_create,
9635 .remove = nfs4_proc_remove,
9636 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009637 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009638 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009639 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009640 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009641 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009642 .link = nfs4_proc_link,
9643 .symlink = nfs4_proc_symlink,
9644 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009645 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646 .readdir = nfs4_proc_readdir,
9647 .mknod = nfs4_proc_mknod,
9648 .statfs = nfs4_proc_statfs,
9649 .fsinfo = nfs4_proc_fsinfo,
9650 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009651 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009653 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009655 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009657 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009659 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009660 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009661 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009662 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009663 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009664 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009665 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009666 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009667 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009668 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009669 .create_server = nfs4_create_server,
9670 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009671};
9672
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009673static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009674 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009675 .list = nfs4_xattr_list_nfs4_acl,
9676 .get = nfs4_xattr_get_nfs4_acl,
9677 .set = nfs4_xattr_set_nfs4_acl,
9678};
9679
9680const struct xattr_handler *nfs4_xattr_handlers[] = {
9681 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009682#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9683 &nfs4_xattr_nfs4_label_handler,
9684#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009685 NULL
9686};
9687
Linus Torvalds1da177e2005-04-16 15:20:36 -07009688/*
9689 * Local variables:
9690 * c-basic-offset: 8
9691 * End:
9692 */