blob: e5ad2c18692707fe1f079445076d343c835ed27e [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,
611 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400612{
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
616
617 res->sr_slot = NULL;
618}
619
620static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621{
622 args->sa_privileged = 1;
623}
624
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400625static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400626{
627 struct nfs4_slot *slot = res->sr_slot;
628 struct nfs4_slot_table *tbl;
629
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 tbl = slot->table;
631 spin_lock(&tbl->slot_tbl_lock);
632 if (!nfs41_wake_and_assign_slot(tbl, slot))
633 nfs4_free_slot(tbl, slot);
634 spin_unlock(&tbl->slot_tbl_lock);
635
636 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400637}
638
639static int nfs40_sequence_done(struct rpc_task *task,
640 struct nfs4_sequence_res *res)
641{
642 if (res->sr_slot != NULL)
643 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400644 return 1;
645}
646
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400647#if defined(CONFIG_NFS_V4_1)
648
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400649static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400650{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500651 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400652 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500653 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400654
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400655 if (!slot)
656 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500657 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500658 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500659
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400660 /* Bump the slot sequence number */
661 if (slot->seq_done)
662 slot->seq_nr++;
663 slot->seq_done = 0;
664
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500665 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500666 /* Be nice to the server: try to ensure that the last transmitted
667 * value for highest_user_slotid <= target_highest_slotid
668 */
669 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
670 send_new_highest_used_slotid = true;
671
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500672 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500673 send_new_highest_used_slotid = false;
674 goto out_unlock;
675 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500676 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677
678 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
679 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500680out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500681 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500682 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400683 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400684 if (waitqueue_active(&tbl->slot_waitq))
685 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400686}
687
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400688static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
689{
690 nfs41_release_slot(res->sr_slot);
691 res->sr_slot = NULL;
692}
693
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400694static int nfs41_sequence_process(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500698 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400699 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500700 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500701 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 if (slot == NULL)
704 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400705 /* don't increment the sequence number if the task wasn't sent */
706 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400707 goto out;
708
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500709 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500710
Trond Myklebustac20d162012-12-15 15:36:07 -0500711 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400712 if (res->sr_status != -NFS4ERR_DELAY)
713 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500714 interrupted = true;
715 }
716
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400717 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500718 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400719 switch (res->sr_status) {
720 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400721 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400722 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500724 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500725 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400726 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
727 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500728 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400729 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500730 case 1:
731 /*
732 * sr_status remains 1 if an RPC level error occurred.
733 * The server may or may not have processed the sequence
734 * operation..
735 * Mark the slot as having hosted an interrupted RPC call.
736 */
737 slot->interrupted = 1;
738 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400739 case -NFS4ERR_DELAY:
740 /* The server detected a resend of the RPC call and
741 * returned NFS4ERR_DELAY as per Section 2.10.6.2
742 * of RFC5661.
743 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500744 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400745 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500746 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500747 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400748 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500749 case -NFS4ERR_BADSLOT:
750 /*
751 * The slot id we used was probably retired. Try again
752 * using a different slot id.
753 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400754 if (slot->seq_nr < slot->table->target_highest_slotid)
755 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500756 goto retry_nowait;
757 case -NFS4ERR_SEQ_MISORDERED:
758 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500759 * Was the last operation on this sequence interrupted?
760 * If so, retry after bumping the sequence number.
761 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400762 if (interrupted)
763 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500764 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500765 * Could this slot have been previously retired?
766 * If so, then the server may be expecting seq_nr = 1!
767 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500768 if (slot->seq_nr != 1) {
769 slot->seq_nr = 1;
770 goto retry_nowait;
771 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400772 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500773 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400774 if (interrupted)
775 goto retry_new_seq;
776 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 default:
778 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400779 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400780 }
781out:
782 /* The session may be reset by one of the error handlers. */
783 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500784out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500785 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400786session_recover:
787 nfs4_schedule_session_recovery(session, res->sr_status);
788 goto retry_nowait;
789retry_new_seq:
790 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500791retry_nowait:
792 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 task->tk_status = 0;
795 ret = 0;
796 }
797 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400799 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 goto out;
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
802 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400804
805int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (!nfs41_sequence_process(task, res))
808 return 0;
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
811 return 1;
812
813}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500814EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400815
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400816static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
817{
818 if (res->sr_slot == NULL)
819 return 1;
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
823}
824
825static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
826{
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
830 else
831 nfs40_sequence_free_slot(res);
832 }
833}
834
Peng Tao2c4b1312014-06-11 05:24:16 +0800835int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400836{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500837 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842}
Peng Tao2c4b1312014-06-11 05:24:16 +0800843EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844
Andy Adamsonce5039c2009-04-01 09:22:13 -0400845static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
846{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400847 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400848
Trond Myklebust035168ab2010-06-16 09:52:26 -0400849 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
850
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500851 nfs4_setup_sequence(data->seq_server->nfs_client,
852 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400853}
854
Andy Adamson69ab40c2009-04-01 09:22:19 -0400855static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
856{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400857 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400858
Trond Myklebust14516c32010-07-31 14:29:06 -0400859 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400860}
861
Trond Myklebust17280172012-03-11 13:11:00 -0400862static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400863 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400864 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400865};
866
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400867static void
868nfs4_sequence_process_interrupted(struct nfs_client *client,
869 struct nfs4_slot *slot, struct rpc_cred *cred)
870{
871 struct rpc_task *task;
872
873 task = _nfs41_proc_sequence(client, cred, slot, true);
874 if (!IS_ERR(task))
875 rpc_put_task_async(task);
876}
877
Chuck Lever3bd23842013-08-09 12:49:19 -0400878#else /* !CONFIG_NFS_V4_1 */
879
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400880static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
881{
882 return nfs40_sequence_done(task, res);
883}
884
885static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
886{
887 if (res->sr_slot != NULL)
888 nfs40_sequence_free_slot(res);
889}
890
Peng Tao2c4b1312014-06-11 05:24:16 +0800891int nfs4_sequence_done(struct rpc_task *task,
892 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400893{
Chuck Lever3bd23842013-08-09 12:49:19 -0400894 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400895}
Peng Tao2c4b1312014-06-11 05:24:16 +0800896EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400897
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400898static void
899nfs4_sequence_process_interrupted(struct nfs_client *client,
900 struct nfs4_slot *slot, struct rpc_cred *cred)
901{
902 WARN_ON_ONCE(1);
903 slot->interrupted = 0;
904}
905
Chuck Lever3bd23842013-08-09 12:49:19 -0400906#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400907
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400908static
909void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
910 struct nfs4_sequence_res *res,
911 struct nfs4_slot *slot)
912{
913 if (!slot)
914 return;
915 slot->privileged = args->sa_privileged ? 1 : 0;
916 args->sa_slot = slot;
917
918 res->sr_slot = slot;
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
922
923}
924
925int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500926 struct nfs4_sequence_args *args,
927 struct nfs4_sequence_res *res,
928 struct rpc_task *task)
929{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500930 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500931 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500932 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500933
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500934 /* slot already allocated? */
935 if (res->sr_slot != NULL)
936 goto out_start;
937
Anna Schumaker76ee0352017-01-10 16:49:31 -0500938 if (session) {
939 tbl = &session->fc_slot_table;
940 task->tk_timeout = 0;
941 }
942
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400943 for (;;) {
944 spin_lock(&tbl->slot_tbl_lock);
945 /* The state manager will wait until the slot table is empty */
946 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
947 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500948
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400949 slot = nfs4_alloc_slot(tbl);
950 if (IS_ERR(slot)) {
951 /* Try again in 1/4 second */
952 if (slot == ERR_PTR(-ENOMEM))
953 task->tk_timeout = HZ >> 2;
954 goto out_sleep;
955 }
956 spin_unlock(&tbl->slot_tbl_lock);
957
958 if (likely(!slot->interrupted))
959 break;
960 nfs4_sequence_process_interrupted(client,
961 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500962 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500963
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400964 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500965
Anna Schumakerad05cc02017-01-11 13:37:06 -0500966 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500967out_start:
968 rpc_call_start(task);
969 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500970
971out_sleep:
972 if (args->sa_privileged)
973 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
974 NULL, RPC_PRIORITY_PRIVILEGED);
975 else
976 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
977 spin_unlock(&tbl->slot_tbl_lock);
978 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500979}
980EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
981
Chuck Lever9915ea72013-08-09 12:48:27 -0400982static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
983{
984 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500985 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400986 data->seq_args, data->seq_res, task);
987}
988
989static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
990{
991 struct nfs4_call_sync_data *data = calldata;
992 nfs4_sequence_done(task, data->seq_res);
993}
994
995static const struct rpc_call_ops nfs40_call_sync_ops = {
996 .rpc_call_prepare = nfs40_call_sync_prepare,
997 .rpc_call_done = nfs40_call_sync_done,
998};
999
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001000static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001001 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 struct nfs4_sequence_args *args,
1004 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001005{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 int ret;
1007 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001008 struct nfs_client *clp = server->nfs_client;
1009 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 .seq_server = server,
1011 .seq_args = args,
1012 .seq_res = res,
1013 };
1014 struct rpc_task_setup task_setup = {
1015 .rpc_client = clnt,
1016 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001017 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 .callback_data = &data
1019 };
1020
1021 task = rpc_run_task(&task_setup);
1022 if (IS_ERR(task))
1023 ret = PTR_ERR(task);
1024 else {
1025 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001026 rpc_put_task(task);
1027 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001028 return ret;
1029}
1030
Bryan Schumaker7c513052011-03-24 17:12:24 +00001031int nfs4_call_sync(struct rpc_clnt *clnt,
1032 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001033 struct rpc_message *msg,
1034 struct nfs4_sequence_args *args,
1035 struct nfs4_sequence_res *res,
1036 int cache_reply)
1037{
Chuck Levera9c92d62013-08-09 12:48:18 -04001038 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001039 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001042static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1043 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct nfs_inode *nfsi = NFS_I(dir);
1046
1047 spin_lock(&dir->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04001048 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1049 | NFS_INO_INVALID_MTIME
1050 | NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001051 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001052 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1053 nfsi->attrtimeo_timestamp = jiffies;
1054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001056 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001057 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1058 NFS_INO_INVALID_ACL;
1059 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001060 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001061 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001062 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001063 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock(&dir->i_lock);
1065}
1066
1067struct nfs4_opendata {
1068 struct kref kref;
1069 struct nfs_openargs o_arg;
1070 struct nfs_openres o_res;
1071 struct nfs_open_confirmargs c_arg;
1072 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001073 struct nfs4_string owner_name;
1074 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001075 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001077 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001079 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct nfs4_state_owner *owner;
1081 struct nfs4_state *state;
1082 struct iattr attrs;
1083 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001084 bool rpc_done;
1085 bool file_created;
1086 bool is_recover;
1087 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001088 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001090
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001091struct nfs4_open_createattrs {
1092 struct nfs4_label *label;
1093 struct iattr *sattr;
1094 const __u32 verf[2];
1095};
1096
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001097static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1098 int err, struct nfs4_exception *exception)
1099{
1100 if (err != -EINVAL)
1101 return false;
1102 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1103 return false;
1104 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1105 exception->retry = 1;
1106 return true;
1107}
1108
Trond Myklebust6ae37332015-01-30 14:21:14 -05001109static u32
1110nfs4_map_atomic_open_share(struct nfs_server *server,
1111 fmode_t fmode, int openflags)
1112{
1113 u32 res = 0;
1114
1115 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1116 case FMODE_READ:
1117 res = NFS4_SHARE_ACCESS_READ;
1118 break;
1119 case FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_WRITE;
1121 break;
1122 case FMODE_READ|FMODE_WRITE:
1123 res = NFS4_SHARE_ACCESS_BOTH;
1124 }
1125 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1126 goto out;
1127 /* Want no delegation if we're using O_DIRECT */
1128 if (openflags & O_DIRECT)
1129 res |= NFS4_SHARE_WANT_NO_DELEG;
1130out:
1131 return res;
1132}
1133
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001134static enum open_claim_type4
1135nfs4_map_atomic_open_claim(struct nfs_server *server,
1136 enum open_claim_type4 claim)
1137{
1138 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1139 return claim;
1140 switch (claim) {
1141 default:
1142 return claim;
1143 case NFS4_OPEN_CLAIM_FH:
1144 return NFS4_OPEN_CLAIM_NULL;
1145 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1146 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1147 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1148 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1149 }
1150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153{
1154 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001155 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 p->o_res.seqid = p->o_arg.seqid;
1157 p->c_res.seqid = p->c_arg.seqid;
1158 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001159 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001160 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001161 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162}
1163
Al Viro82a2c1b2011-06-22 18:30:55 -04001164static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001166 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001167 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001168 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169{
Al Viro82a2c1b2011-06-22 18:30:55 -04001170 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001171 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001173 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001174 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 struct nfs4_opendata *p;
1176
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001177 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 if (p == NULL)
1179 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001180
1181 p->f_label = nfs4_label_alloc(server, gfp_mask);
1182 if (IS_ERR(p->f_label))
1183 goto err_free_p;
1184
Kinglong Meea49c2692015-07-27 15:31:38 +08001185 p->a_label = nfs4_label_alloc(server, gfp_mask);
1186 if (IS_ERR(p->a_label))
1187 goto err_free_f;
1188
Trond Myklebust63f5f792015-01-23 19:19:25 -05001189 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1190 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001191 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001192 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001193 nfs_sb_active(dentry->d_sb);
1194 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 p->dir = parent;
1196 p->owner = sp;
1197 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001198 p->o_arg.open_flags = flags;
1199 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001200 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001201 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001202 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1203 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001204 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1205 * will return permission denied for all bits until close */
1206 if (!(flags & O_EXCL)) {
1207 /* ask server to check for all possible rights as results
1208 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001209 switch (p->o_arg.claim) {
1210 default:
1211 break;
1212 case NFS4_OPEN_CLAIM_NULL:
1213 case NFS4_OPEN_CLAIM_FH:
1214 p->o_arg.access = NFS4_ACCESS_READ |
1215 NFS4_ACCESS_MODIFY |
1216 NFS4_ACCESS_EXTEND |
1217 NFS4_ACCESS_EXECUTE;
1218 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 }
David Howells7539bba2006-08-22 20:06:09 -04001220 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001221 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1222 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001224 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001225 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001226 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001228 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001229 case NFS4_OPEN_CLAIM_NULL:
1230 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1231 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1232 p->o_arg.fh = NFS_FH(dir);
1233 break;
1234 case NFS4_OPEN_CLAIM_PREVIOUS:
1235 case NFS4_OPEN_CLAIM_FH:
1236 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1237 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001238 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001239 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001240 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001241 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001242 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001243
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001244 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001245 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247 p->c_arg.fh = &p->o_res.fh;
1248 p->c_arg.stateid = &p->o_res.stateid;
1249 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001252 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001253
1254err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001255 nfs4_label_free(p->a_label);
1256err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001257 nfs4_label_free(p->f_label);
1258err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 kfree(p);
1260err:
1261 dput(parent);
1262 return NULL;
1263}
1264
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001265static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267 struct nfs4_opendata *p = container_of(kref,
1268 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001269 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270
1271 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001272 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 if (p->state != NULL)
1274 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001276
Kinglong Meea49c2692015-07-27 15:31:38 +08001277 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001278 nfs4_label_free(p->f_label);
1279
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 dput(p->dentry);
1282 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001283 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001284 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001285 kfree(p);
1286}
1287
1288static void nfs4_opendata_put(struct nfs4_opendata *p)
1289{
1290 if (p != NULL)
1291 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292}
1293
Trond Myklebust24311f82015-09-20 10:50:17 -04001294static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1295 fmode_t fmode)
1296{
1297 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1298 case FMODE_READ|FMODE_WRITE:
1299 return state->n_rdwr != 0;
1300 case FMODE_WRITE:
1301 return state->n_wronly != 0;
1302 case FMODE_READ:
1303 return state->n_rdonly != 0;
1304 }
1305 WARN_ON_ONCE(1);
1306 return false;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310{
1311 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312
Trond Myklebust536e43d2012-01-17 22:04:26 -05001313 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 goto out;
1315 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001317 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1318 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001319 break;
1320 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001321 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1322 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001323 break;
1324 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001325 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1326 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001327 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001328out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001329 return ret;
1330}
1331
Trond Myklebust2a606182015-08-19 22:30:00 -05001332static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1333 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001334{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001335 if (delegation == NULL)
1336 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001339 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1340 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001341 switch (claim) {
1342 case NFS4_OPEN_CLAIM_NULL:
1343 case NFS4_OPEN_CLAIM_FH:
1344 break;
1345 case NFS4_OPEN_CLAIM_PREVIOUS:
1346 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1347 break;
1348 default:
1349 return 0;
1350 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001351 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001352 return 1;
1353}
1354
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001356{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001358 case FMODE_WRITE:
1359 state->n_wronly++;
1360 break;
1361 case FMODE_READ:
1362 state->n_rdonly++;
1363 break;
1364 case FMODE_READ|FMODE_WRITE:
1365 state->n_rdwr++;
1366 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001368}
1369
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001370#ifdef CONFIG_NFS_V4_1
1371static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1372{
1373 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1374 return true;
1375 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1376 return true;
1377 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1378 return true;
1379 return false;
1380}
1381#endif /* CONFIG_NFS_V4_1 */
1382
Trond Myklebustc9399f22017-11-06 15:28:01 -05001383static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1384{
1385 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1386 wake_up_all(&state->waitq);
1387}
1388
1389static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1390 const nfs4_stateid *stateid)
1391{
1392 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1393 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1394
1395 if (stateid_seqid == state_seqid + 1U ||
1396 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1397 nfs_state_log_update_open_stateid(state);
1398 else
1399 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1400}
1401
Trond Myklebust4f14c192014-02-12 19:15:06 -05001402static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001403{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001404 struct nfs_client *clp = state->owner->so_server->nfs_client;
1405 bool need_recover = false;
1406
1407 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1410 need_recover = true;
1411 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1412 need_recover = true;
1413 if (need_recover)
1414 nfs4_state_mark_reclaim_nograce(clp, state);
1415}
1416
Trond Myklebustc9399f22017-11-06 15:28:01 -05001417/*
1418 * Check for whether or not the caller may update the open stateid
1419 * to the value passed in by stateid.
1420 *
1421 * Note: This function relies heavily on the server implementing
1422 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1423 * correctly.
1424 * i.e. The stateid seqids have to be initialised to 1, and
1425 * are then incremented on every state transition.
1426 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001427static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001428 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001429{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001430 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1431 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1432 if (stateid->seqid == cpu_to_be32(1))
1433 nfs_state_log_update_open_stateid(state);
1434 else
1435 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001436 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001438
1439 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1440 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001441 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001442 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001443 return false;
1444}
1445
Trond Myklebustf95549c2015-01-23 18:06:09 -05001446static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1447{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001448 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1449 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001450 if (state->n_wronly)
1451 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1452 if (state->n_rdonly)
1453 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1454 if (state->n_rdwr)
1455 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001456 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001457}
1458
Trond Myklebust226056c2014-02-11 10:41:07 -05001459static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1460 nfs4_stateid *stateid, fmode_t fmode)
1461{
1462 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1463 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1464 case FMODE_WRITE:
1465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 break;
1467 case FMODE_READ:
1468 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1469 break;
1470 case 0:
1471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1472 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1473 clear_bit(NFS_OPEN_STATE, &state->flags);
1474 }
1475 if (stateid == NULL)
1476 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001477 /* Handle OPEN+OPEN_DOWNGRADE races */
1478 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1479 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001480 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001481 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001482 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001484 nfs4_stateid_copy(&state->stateid, stateid);
1485 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001486 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001487out:
1488 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001489}
1490
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001491static void nfs_clear_open_stateid(struct nfs4_state *state,
1492 nfs4_stateid *arg_stateid,
1493 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001494{
1495 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001496 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1497 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1498 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001500 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1501 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001502}
1503
Trond Myklebust1393d962016-09-22 13:39:13 -04001504static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001505 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001506{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001507 DEFINE_WAIT(wait);
1508 int status = 0;
1509 for (;;) {
1510
1511 if (!nfs_need_update_open_stateid(state, stateid))
1512 return;
1513 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001514 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001515 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001516 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001517 /* Rely on seqids for serialisation with NFSv4.0 */
1518 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1519 break;
1520
1521 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1522 /*
1523 * Ensure we process the state changes in the same order
1524 * in which the server processed them by delaying the
1525 * update of the stateid until we are in sequence.
1526 */
1527 write_sequnlock(&state->seqlock);
1528 spin_unlock(&state->owner->so_lock);
1529 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001530 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001531 if (!signal_pending(current)) {
1532 if (schedule_timeout(5*HZ) == 0)
1533 status = -EAGAIN;
1534 else
1535 status = 0;
1536 } else
1537 status = -EINTR;
1538 finish_wait(&state->waitq, &wait);
1539 rcu_read_lock();
1540 spin_lock(&state->owner->so_lock);
1541 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001542 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001544 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1545 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001546 nfs4_stateid_copy(freeme, &state->open_stateid);
1547 nfs_test_and_clear_all_open_stateid(state);
1548 }
1549
Trond Myklebuste999e802014-02-10 18:20:47 -05001550 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1551 nfs4_stateid_copy(&state->stateid, stateid);
1552 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001553 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001555}
1556
Trond Myklebustc9399f22017-11-06 15:28:01 -05001557static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001558 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 fmode_t fmode,
1560 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001562 /*
1563 * Protect the call to nfs4_state_set_mode_locked and
1564 * serialise the stateid update
1565 */
1566 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1568 switch (fmode) {
1569 case FMODE_READ:
1570 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_WRITE:
1573 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1574 break;
1575 case FMODE_READ|FMODE_WRITE:
1576 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001577 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001579 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001580}
1581
1582static void nfs_state_set_delegation(struct nfs4_state *state,
1583 const nfs4_stateid *deleg_stateid,
1584 fmode_t fmode)
1585{
1586 /*
1587 * Protect the call to nfs4_state_set_mode_locked and
1588 * serialise the stateid update
1589 */
1590 write_seqlock(&state->seqlock);
1591 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1592 set_bit(NFS_DELEGATED_STATE, &state->flags);
1593 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Trond Myklebust1393d962016-09-22 13:39:13 -04001596static int update_open_stateid(struct nfs4_state *state,
1597 const nfs4_stateid *open_stateid,
1598 const nfs4_stateid *delegation,
1599 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001600{
Trond Myklebust1393d962016-09-22 13:39:13 -04001601 struct nfs_server *server = NFS_SERVER(state->inode);
1602 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001603 struct nfs_inode *nfsi = NFS_I(state->inode);
1604 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001605 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001606 int ret = 0;
1607
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001609
1610 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001611 spin_lock(&state->owner->so_lock);
1612 if (open_stateid != NULL) {
1613 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1614 ret = 1;
1615 }
1616
Trond Myklebust34310432008-12-23 15:21:38 -05001617 deleg_cur = rcu_dereference(nfsi->delegation);
1618 if (deleg_cur == NULL)
1619 goto no_delegation;
1620
1621 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001622 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001623 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001624 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001625 goto no_delegation_unlock;
1626
1627 if (delegation == NULL)
1628 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001629 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001630 goto no_delegation_unlock;
1631
Trond Myklebustb7391f42008-12-23 15:21:52 -05001632 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001633 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001634 ret = 1;
1635no_delegation_unlock:
1636 spin_unlock(&deleg_cur->lock);
1637no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001638 if (ret)
1639 update_open_stateflags(state, fmode);
1640 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001641 rcu_read_unlock();
1642
Trond Myklebust4f14c192014-02-12 19:15:06 -05001643 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001644 nfs4_schedule_state_manager(clp);
1645 if (freeme.type != 0)
1646 nfs4_test_and_free_stateid(server, &freeme,
1647 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001648
1649 return ret;
1650}
1651
Trond Myklebust39071e62015-01-24 15:07:56 -05001652static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1653 const nfs4_stateid *stateid)
1654{
1655 struct nfs4_state *state = lsp->ls_state;
1656 bool ret = false;
1657
1658 spin_lock(&state->state_lock);
1659 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1660 goto out_noupdate;
1661 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1662 goto out_noupdate;
1663 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1664 ret = true;
1665out_noupdate:
1666 spin_unlock(&state->state_lock);
1667 return ret;
1668}
Trond Myklebust34310432008-12-23 15:21:38 -05001669
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001670static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671{
1672 struct nfs_delegation *delegation;
1673
Trond Myklebustf5086242018-03-20 16:43:13 -04001674 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001675 rcu_read_lock();
1676 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001677 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001678 rcu_read_unlock();
1679 return;
1680 }
1681 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001682 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001683}
1684
Trond Myklebust6ee41262007-07-08 14:11:36 -04001685static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001686{
1687 struct nfs4_state *state = opendata->state;
1688 struct nfs_inode *nfsi = NFS_I(state->inode);
1689 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001690 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001692 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001693 nfs4_stateid stateid;
1694 int ret = -EAGAIN;
1695
Trond Myklebustaac00a82007-07-05 19:02:21 -04001696 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001698 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001700 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001701 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001702 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001703 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001704 rcu_read_lock();
1705 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001706 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001707 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001708 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001709 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001711 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001712 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001713 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001714 if (!opendata->is_recover) {
1715 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1716 if (ret != 0)
1717 goto out;
1718 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001720
1721 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001722 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001723 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001724 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001725out:
1726 return ERR_PTR(ret);
1727out_return_state:
1728 atomic_inc(&state->count);
1729 return state;
1730}
1731
Andy Adamsone23008e2012-10-02 21:07:32 -04001732static void
1733nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1734{
1735 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1736 struct nfs_delegation *delegation;
1737 int delegation_flags = 0;
1738
1739 rcu_read_lock();
1740 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1741 if (delegation)
1742 delegation_flags = delegation->flags;
1743 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001744 switch (data->o_arg.claim) {
1745 default:
1746 break;
1747 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1748 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001749 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1750 "returning a delegation for "
1751 "OPEN(CLAIM_DELEGATE_CUR)\n",
1752 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001753 return;
1754 }
1755 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001756 nfs_inode_set_delegation(state->inode,
1757 data->owner->so_cred,
1758 &data->o_res);
1759 else
1760 nfs_inode_reclaim_delegation(state->inode,
1761 data->owner->so_cred,
1762 &data->o_res);
1763}
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
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002186 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002187 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002188 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002189 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002190 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002191 if (data->is_recover)
2192 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002193 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002194 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002195 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002196 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002197 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002198 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002199 smp_wmb();
2200 } else
2201 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002202 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 return status;
2204}
2205
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002206static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002208 struct nfs4_opendata *data = calldata;
2209 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002210 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002211 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002212
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002213 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002214 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002215 /*
2216 * Check if we still need to send an OPEN call, or if we can use
2217 * a delegation instead.
2218 */
2219 if (data->state != NULL) {
2220 struct nfs_delegation *delegation;
2221
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002222 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002223 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002224 rcu_read_lock();
2225 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002226 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002227 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002228 rcu_read_unlock();
2229 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002230 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002231 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002232 switch (claim) {
2233 default:
2234 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002235 case NFS4_OPEN_CLAIM_PREVIOUS:
2236 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2237 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002238 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002239 case NFS4_OPEN_CLAIM_FH:
2240 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002241 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002242 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002243 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002244 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002245 &data->o_res.seq_res,
2246 task) != 0)
2247 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002248
2249 /* Set the create mode (note dependency on the session type) */
2250 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2251 if (data->o_arg.open_flags & O_EXCL) {
2252 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2253 if (nfs4_has_persistent_session(clp))
2254 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2255 else if (clp->cl_mvops->minor_version > 0)
2256 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2257 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002258 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002259unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002260 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002261 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002262out_no_action:
2263 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002264out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002265 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002266}
2267
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002268static void nfs4_open_done(struct rpc_task *task, void *calldata)
2269{
2270 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002271
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002272 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002273
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002274 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002275 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002276
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002277 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002278 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2279 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002280 case S_IFREG:
2281 break;
2282 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002284 break;
2285 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002286 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002287 break;
2288 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002289 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002290 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002291 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002292 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002293 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2294 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002295 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002296 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002297}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002298
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002299static void nfs4_open_release(void *calldata)
2300{
2301 struct nfs4_opendata *data = calldata;
2302 struct nfs4_state *state = NULL;
2303
2304 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002305 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002306 goto out_free;
2307 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002308 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002309 goto out_free;
2310 /* In case we need an open_confirm, no cleanup! */
2311 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2312 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002313 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002314 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002315 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002316out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002317 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002318}
2319
2320static const struct rpc_call_ops nfs4_open_ops = {
2321 .rpc_call_prepare = nfs4_open_prepare,
2322 .rpc_call_done = nfs4_open_done,
2323 .rpc_release = nfs4_open_release,
2324};
2325
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002326static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002327{
David Howells2b0143b2015-03-17 22:25:59 +00002328 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002329 struct nfs_server *server = NFS_SERVER(dir);
2330 struct nfs_openargs *o_arg = &data->o_arg;
2331 struct nfs_openres *o_res = &data->o_res;
2332 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002333 struct rpc_message msg = {
2334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2335 .rpc_argp = o_arg,
2336 .rpc_resp = o_res,
2337 .rpc_cred = data->owner->so_cred,
2338 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002339 struct rpc_task_setup task_setup_data = {
2340 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002341 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002342 .callback_ops = &nfs4_open_ops,
2343 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002344 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002345 .flags = RPC_TASK_ASYNC,
2346 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002347 int status;
2348
Chuck Levera9c92d62013-08-09 12:48:18 -04002349 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002350 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002351 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002352 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002353 data->cancelled = false;
2354 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002355 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002356 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002357 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002358 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002359 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002360 if (IS_ERR(task))
2361 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002362 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002363 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002364 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002365 smp_wmb();
2366 } else
2367 status = data->rpc_status;
2368 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002369
2370 return status;
2371}
2372
2373static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2374{
David Howells2b0143b2015-03-17 22:25:59 +00002375 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002376 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002377 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002378
2379 status = nfs4_run_open_task(data, 1);
2380 if (status != 0 || !data->rpc_done)
2381 return status;
2382
Trond Myklebust6926afd12012-01-07 13:22:46 -05002383 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2384
Anna Schumakerd7e98252017-01-11 16:13:29 -05002385 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002386 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002387
2388 return status;
2389}
2390
Trond Myklebustf3792d62014-07-10 08:54:32 -04002391/*
2392 * Additional permission checks in order to distinguish between an
2393 * open for read, and an open for execute. This works around the
2394 * fact that NFSv4 OPEN treats read and execute permissions as being
2395 * the same.
2396 * Note that in the non-execute case, we want to turn off permission
2397 * checking if we just created a new file (POSIX open() semantics).
2398 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002399static int nfs4_opendata_access(struct rpc_cred *cred,
2400 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002401 struct nfs4_state *state, fmode_t fmode,
2402 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002403{
2404 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002405 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002406
2407 /* access call failed or for some reason the server doesn't
2408 * support any access modes -- defer access call until later */
2409 if (opendata->o_res.access_supported == 0)
2410 return 0;
2411
2412 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002413 /*
2414 * Use openflags to check for exec, because fmode won't
2415 * always have FMODE_EXEC set when file open for exec.
2416 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002417 if (openflags & __FMODE_EXEC) {
2418 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002419 if (S_ISDIR(state->inode->i_mode))
2420 mask = NFS4_ACCESS_LOOKUP;
2421 else
2422 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002423 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002424 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002425
2426 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002427 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2428 nfs_access_add_cache(state->inode, &cache);
2429
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002430 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2431 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002432 return 0;
2433
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002434 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002435}
2436
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002437/*
2438 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2439 */
2440static int _nfs4_proc_open(struct nfs4_opendata *data)
2441{
David Howells2b0143b2015-03-17 22:25:59 +00002442 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002443 struct nfs_server *server = NFS_SERVER(dir);
2444 struct nfs_openargs *o_arg = &data->o_arg;
2445 struct nfs_openres *o_res = &data->o_res;
2446 int status;
2447
2448 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002449 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002450 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002451 if (status != 0) {
2452 if (status == -NFS4ERR_BADNAME &&
2453 !(o_arg->open_flags & O_CREAT))
2454 return -ENOENT;
2455 return status;
2456 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002457
Trond Myklebust6926afd12012-01-07 13:22:46 -05002458 nfs_fattr_map_and_free_names(server, &data->f_attr);
2459
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002460 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002461 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002462 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002463 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002464 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002465 if (data->file_created ||
2466 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002467 update_changeattr(dir, &o_res->cinfo,
2468 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002469 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002470 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2471 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002473 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002475 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002477 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2478 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002479 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002480 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002481 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482}
2483
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484/*
2485 * OPEN_EXPIRED:
2486 * reclaim state on the server after a network partition.
2487 * Assumes caller holds the appropriate lock
2488 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002489static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002490{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002491 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002492 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002494 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002495 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002496 if (IS_ERR(opendata))
2497 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002498 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002499 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002500 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002501 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002502 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002503}
2504
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002505static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002506{
Trond Myklebust539cd032007-06-05 11:46:42 -04002507 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002508 struct nfs4_exception exception = { };
2509 int err;
2510
2511 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002512 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002513 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002514 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2515 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002516 switch (err) {
2517 default:
2518 goto out;
2519 case -NFS4ERR_GRACE:
2520 case -NFS4ERR_DELAY:
2521 nfs4_handle_exception(server, err, &exception);
2522 err = 0;
2523 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002524 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002525out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002526 return err;
2527}
2528
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2530{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002532 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533
Trond Myklebust864472e2006-01-03 09:55:15 +01002534 ctx = nfs4_state_find_open_context(state);
2535 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002536 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002537 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002538 put_nfs_open_context(ctx);
2539 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002540}
2541
Trond Myklebust41020b62016-09-22 13:38:58 -04002542static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2543 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002544{
Trond Myklebust41020b62016-09-22 13:38:58 -04002545 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002546 write_seqlock(&state->seqlock);
2547 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2548 write_sequnlock(&state->seqlock);
2549 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2550}
2551
2552static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2553{
2554 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002555 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002556}
2557
2558static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2559{
2560 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2561 nfs40_clear_delegation_stateid(state);
2562 return nfs4_open_expired(sp, state);
2563}
2564
Trond Myklebust45870d62016-09-22 13:38:59 -04002565static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2566 nfs4_stateid *stateid,
2567 struct rpc_cred *cred)
2568{
2569 return -NFS4ERR_BAD_STATEID;
2570}
2571
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002572#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002573static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2574 nfs4_stateid *stateid,
2575 struct rpc_cred *cred)
2576{
2577 int status;
2578
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002579 switch (stateid->type) {
2580 default:
2581 break;
2582 case NFS4_INVALID_STATEID_TYPE:
2583 case NFS4_SPECIAL_STATEID_TYPE:
2584 return -NFS4ERR_BAD_STATEID;
2585 case NFS4_REVOKED_STATEID_TYPE:
2586 goto out_free;
2587 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002588
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002589 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002590 switch (status) {
2591 case -NFS4ERR_EXPIRED:
2592 case -NFS4ERR_ADMIN_REVOKED:
2593 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002594 break;
2595 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002596 return status;
2597 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002598out_free:
2599 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002600 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002601 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002602}
2603
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002604static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002605{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002606 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002607 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002608 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002609 struct rpc_cred *cred;
2610 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002611
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002612 /* Get the delegation credential for use by test/free_stateid */
2613 rcu_read_lock();
2614 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002615 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002616 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002617 return;
2618 }
2619
2620 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002621 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2622 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2623 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002624 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002625 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002626 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002627 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002628
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002629 cred = get_rpccred(delegation->cred);
2630 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002631 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002632 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002633 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002634 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002635
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002636 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002637}
2638
2639/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002640 * nfs41_check_expired_locks - possibly free a lock stateid
2641 *
2642 * @state: NFSv4 state for an inode
2643 *
2644 * Returns NFS_OK if recovery for this stateid is now finished.
2645 * Otherwise a negative NFS4ERR value is returned.
2646 */
2647static int nfs41_check_expired_locks(struct nfs4_state *state)
2648{
2649 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002650 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002651 struct nfs_server *server = NFS_SERVER(state->inode);
2652
2653 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2654 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002655
2656 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002657 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2658 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2659 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2660
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002661 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002662 spin_unlock(&state->state_lock);
2663
2664 nfs4_put_lock_state(prev);
2665 prev = lsp;
2666
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002667 status = nfs41_test_and_free_expired_stateid(server,
2668 &lsp->ls_stateid,
2669 cred);
2670 trace_nfs4_test_lock_stateid(state, lsp, status);
2671 if (status == -NFS4ERR_EXPIRED ||
2672 status == -NFS4ERR_BAD_STATEID) {
2673 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002674 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002675 if (!recover_lost_locks)
2676 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2677 } else if (status != NFS_OK) {
2678 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002679 nfs4_put_lock_state(prev);
2680 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002681 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002682 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002683 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002684 }
2685 spin_unlock(&state->state_lock);
2686 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002687out:
2688 return ret;
2689}
2690
2691/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002692 * nfs41_check_open_stateid - possibly free an open stateid
2693 *
2694 * @state: NFSv4 state for an inode
2695 *
2696 * Returns NFS_OK if recovery for this stateid is now finished.
2697 * Otherwise a negative NFS4ERR value is returned.
2698 */
2699static int nfs41_check_open_stateid(struct nfs4_state *state)
2700{
2701 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002702 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002703 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002704 int status;
2705
Trond Myklebustb134fc42016-09-22 13:39:16 -04002706 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002707 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2708 if (nfs4_have_delegation(state->inode, state->state))
2709 return NFS_OK;
2710 return -NFS4ERR_OPENMODE;
2711 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002712 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002713 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002714 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002715 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002716 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002717 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2718 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2719 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002720 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002721 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002722 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002723 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002724 if (nfs_open_stateid_recover_openmode(state))
2725 return -NFS4ERR_OPENMODE;
2726 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002727}
2728
2729static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2730{
Chuck Levereb64cf92012-07-11 16:30:05 -04002731 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002732
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002733 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002734 status = nfs41_check_expired_locks(state);
2735 if (status != NFS_OK)
2736 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002737 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002738 if (status != NFS_OK)
2739 status = nfs4_open_expired(sp, state);
2740 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002741}
2742#endif
2743
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002745 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2746 * fields corresponding to attributes that were used to store the verifier.
2747 * Make sure we clobber those fields in the later setattr call
2748 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002749static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2750 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002751{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002752 const u32 *attrset = opendata->o_res.attrset;
2753
2754 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002755 !(sattr->ia_valid & ATTR_ATIME_SET))
2756 sattr->ia_valid |= ATTR_ATIME;
2757
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002758 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002759 !(sattr->ia_valid & ATTR_MTIME_SET))
2760 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002761
2762 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002763 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002764 (attrset[1] & FATTR4_WORD1_MODE ||
2765 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002766 sattr->ia_valid &= ~ATTR_MODE;
2767
2768 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2769 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002770}
2771
Trond Myklebustc21443c2013-02-07 14:26:21 -05002772static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2773 fmode_t fmode,
2774 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002775 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002776{
2777 struct nfs4_state_owner *sp = opendata->owner;
2778 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002779 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002780 struct nfs4_state *state;
2781 unsigned int seq;
2782 int ret;
2783
2784 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2785
2786 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002787 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002788 goto out;
2789
2790 state = nfs4_opendata_to_nfs4_state(opendata);
2791 ret = PTR_ERR(state);
2792 if (IS_ERR(state))
2793 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002794 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002795 if (server->caps & NFS_CAP_POSIX_LOCK)
2796 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002797 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2798 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002799
Trond Myklebust275bb302013-05-29 13:11:28 -04002800 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002801 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002802 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002803 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002804 alias = d_exact_alias(dentry, state->inode);
2805 if (!alias)
2806 alias = d_splice_alias(igrab(state->inode), dentry);
2807 /* d_splice_alias() can't fail here - it's a non-directory */
2808 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002809 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002810 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002811 }
2812 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002813 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002814 }
2815
Trond Myklebustc21443c2013-02-07 14:26:21 -05002816 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2817 if (ret != 0)
2818 goto out;
2819
David Howells2b0143b2015-03-17 22:25:59 +00002820 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002821 nfs_inode_attach_open_context(ctx);
2822 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2823 nfs4_schedule_stateid_recovery(server, state);
2824 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002825out:
2826 return ret;
2827}
2828
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002829/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002830 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002831 */
Andy Adamson82be4172012-05-23 05:02:35 -04002832static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002833 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002834 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002835 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002836 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837{
2838 struct nfs4_state_owner *sp;
2839 struct nfs4_state *state = NULL;
2840 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002841 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002842 struct dentry *dentry = ctx->dentry;
2843 struct rpc_cred *cred = ctx->cred;
2844 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2845 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002846 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002847 struct iattr *sattr = c->sattr;
2848 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002849 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002850 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851
2852 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002854 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2855 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002856 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2857 goto out_err;
2858 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002859 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002860 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002861 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002862 if (d_really_is_positive(dentry))
2863 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002864 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002865 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002866 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002867 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2868 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002869 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002870 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
David Quigley14c43f72013-05-22 12:50:43 -04002872 if (label) {
2873 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2874 if (IS_ERR(olabel)) {
2875 status = PTR_ERR(olabel);
2876 goto err_opendata_put;
2877 }
2878 }
2879
Trond Myklebuste911b812014-03-26 13:24:37 -07002880 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2881 if (!opendata->f_attr.mdsthreshold) {
2882 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2883 if (!opendata->f_attr.mdsthreshold)
2884 goto err_free_label;
2885 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002886 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002887 }
David Howells2b0143b2015-03-17 22:25:59 +00002888 if (d_really_is_positive(dentry))
2889 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002890
Trond Myklebust3efb9722013-05-29 13:17:04 -04002891 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002892 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002893 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002894 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002895
NeilBrownefcbc042015-07-30 13:00:56 +10002896 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002897 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002898 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002899 /*
2900 * send create attributes which was not set by open
2901 * with an extra setattr.
2902 */
2903 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2904 nfs_fattr_init(opendata->o_res.f_attr);
2905 status = nfs4_do_setattr(state->inode, cred,
2906 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002907 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002908 if (status == 0) {
2909 nfs_setattr_update_inode(state->inode, sattr,
2910 opendata->o_res.f_attr);
2911 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2912 }
David Quigley1775fd32013-05-22 12:50:42 -04002913 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002914 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002915 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002916 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002917
Trond Myklebuste911b812014-03-26 13:24:37 -07002918 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002919 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002920 opendata->f_attr.mdsthreshold = NULL;
2921 }
Andy Adamson82be4172012-05-23 05:02:35 -04002922
David Quigley14c43f72013-05-22 12:50:43 -04002923 nfs4_label_free(olabel);
2924
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002925 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002926 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002927 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002928err_free_label:
2929 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002930err_opendata_put:
2931 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002932err_put_state_owner:
2933 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002934out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002935 return status;
2936}
2937
2938
Andy Adamson82be4172012-05-23 05:02:35 -04002939static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002940 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002941 int flags,
2942 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002943 struct nfs4_label *label,
2944 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002946 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 struct nfs4_exception exception = { };
2948 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002949 struct nfs4_open_createattrs c = {
2950 .label = label,
2951 .sattr = sattr,
2952 .verf = {
2953 [0] = (__u32)jiffies,
2954 [1] = (__u32)current->pid,
2955 },
2956 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 int status;
2958
2959 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002960 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002961 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002962 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002963 if (status == 0)
2964 break;
2965 /* NOTE: BAD_SEQID means the server and client disagree about the
2966 * book-keeping w.r.t. state-changing operations
2967 * (OPEN/CLOSE/LOCK/LOCKU...)
2968 * It is actually a sign of a bug on the client or on the server.
2969 *
2970 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002971 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002972 * have unhashed the old state_owner for us, and that we can
2973 * therefore safely retry using a new one. We should still warn
2974 * the user though...
2975 */
2976 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002977 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002978 " returned a bad sequence-id error!\n",
2979 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002980 exception.retry = 1;
2981 continue;
2982 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002983 /*
2984 * BAD_STATEID on OPEN means that the server cancelled our
2985 * state before it received the OPEN_CONFIRM.
2986 * Recover by retrying the request as per the discussion
2987 * on Page 181 of RFC3530.
2988 */
2989 if (status == -NFS4ERR_BAD_STATEID) {
2990 exception.retry = 1;
2991 continue;
2992 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002993 if (status == -EAGAIN) {
2994 /* We must have found a delegation */
2995 exception.retry = 1;
2996 continue;
2997 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002998 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2999 continue;
3000 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 status, &exception));
3002 } while (exception.retry);
3003 return res;
3004}
3005
Trond Myklebust8487c472016-06-26 08:44:35 -04003006static int _nfs4_do_setattr(struct inode *inode,
3007 struct nfs_setattrargs *arg,
3008 struct nfs_setattrres *res,
3009 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003010 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003011{
3012 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003013 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3015 .rpc_argp = arg,
3016 .rpc_resp = res,
3017 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003018 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003019 struct rpc_cred *delegation_cred = NULL;
3020 unsigned long timestamp = jiffies;
3021 fmode_t fmode;
3022 bool truncate;
3023 int status;
3024
3025 nfs_fattr_init(res->fattr);
3026
3027 /* Servers should only apply open mode checks for file size changes */
3028 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3029 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3030
3031 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3032 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003033 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003034 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003035 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003036 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003037 l_ctx = nfs_get_lock_context(ctx);
3038 if (IS_ERR(l_ctx))
3039 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003040 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3041 &arg->stateid, &delegation_cred);
3042 nfs_put_lock_context(l_ctx);
3043 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003044 return -EBADF;
3045 } else
3046 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3047 if (delegation_cred)
3048 msg.rpc_cred = delegation_cred;
3049
3050 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3051
3052 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003053 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003054 renew_lease(server, timestamp);
3055 trace_nfs4_setattr(inode, &arg->stateid, status);
3056 return status;
3057}
3058
3059static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3060 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003061 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003062 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003063{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003064 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003065 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003066 struct nfs_setattrargs arg = {
3067 .fh = NFS_FH(inode),
3068 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003069 .server = server,
3070 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003071 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003072 };
3073 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003075 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003076 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003077 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003078 struct nfs4_exception exception = {
3079 .state = state,
3080 .inode = inode,
3081 .stateid = &arg.stateid,
3082 };
3083 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084
David Quigleyaa9c2662013-05-22 12:50:44 -04003085 arg.bitmask = nfs4_bitmask(server, ilabel);
3086 if (ilabel)
3087 arg.bitmask = nfs4_bitmask(server, olabel);
3088
Linus Torvalds1da177e2005-04-16 15:20:36 -07003089 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003090 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003091 switch (err) {
3092 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003093 if (!(sattr->ia_valid & ATTR_SIZE)) {
3094 pr_warn_once("NFSv4: server %s is incorrectly "
3095 "applying open mode checks to "
3096 "a SETATTR that is not "
3097 "changing file size.\n",
3098 server->nfs_client->cl_hostname);
3099 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003100 if (state && !(state->state & FMODE_WRITE)) {
3101 err = -EBADF;
3102 if (sattr->ia_valid & ATTR_OPEN)
3103 err = -EACCES;
3104 goto out;
3105 }
3106 }
3107 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003109out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 return err;
3111}
3112
Peng Tao500d7012015-09-22 11:35:22 +08003113static bool
3114nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3115{
3116 if (inode == NULL || !nfs_have_layout(inode))
3117 return false;
3118
3119 return pnfs_wait_on_layoutreturn(inode, task);
3120}
3121
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122struct nfs4_closedata {
3123 struct inode *inode;
3124 struct nfs4_state *state;
3125 struct nfs_closeargs arg;
3126 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003127 struct {
3128 struct nfs4_layoutreturn_args arg;
3129 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003130 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003131 u32 roc_barrier;
3132 bool roc;
3133 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003134 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003135 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003136};
3137
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003138static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003139{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003140 struct nfs4_closedata *calldata = data;
3141 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003142 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003143
Trond Myklebustcf805162016-11-15 14:56:07 -05003144 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003145 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3146 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003147 nfs4_put_open_state(calldata->state);
3148 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003149 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003150 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003151 kfree(calldata);
3152}
3153
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003154static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003156 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003158 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003159 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003160 struct nfs4_exception exception = {
3161 .state = state,
3162 .inode = calldata->inode,
3163 .stateid = &calldata->arg.stateid,
3164 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003165
Chuck Levera3ca5652012-03-01 17:00:40 -05003166 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003167 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3168 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003169 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003170
3171 /* Handle Layoutreturn errors */
3172 if (calldata->arg.lr_args && task->tk_status != 0) {
3173 switch (calldata->res.lr_ret) {
3174 default:
3175 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3176 break;
3177 case 0:
3178 calldata->arg.lr_args = NULL;
3179 calldata->res.lr_res = NULL;
3180 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003181 case -NFS4ERR_OLD_STATEID:
3182 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003183 calldata->inode))
3184 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003185 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003186 case -NFS4ERR_ADMIN_REVOKED:
3187 case -NFS4ERR_DELEG_REVOKED:
3188 case -NFS4ERR_EXPIRED:
3189 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003190 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3191 case -NFS4ERR_WRONG_CRED:
3192 calldata->arg.lr_args = NULL;
3193 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003194 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003195 }
3196 }
3197
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003198 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 * the state_owner. we keep this around to process errors
3200 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003201 switch (task->tk_status) {
3202 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003203 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003204 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003205 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003206 case -NFS4ERR_ACCESS:
3207 if (calldata->arg.bitmask != NULL) {
3208 calldata->arg.bitmask = NULL;
3209 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003210 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003211
3212 }
3213 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003214 case -NFS4ERR_OLD_STATEID:
3215 /* Did we race with OPEN? */
3216 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003217 state))
3218 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003219 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003220 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003221 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003222 case -NFS4ERR_EXPIRED:
3223 nfs4_free_revoked_stateid(server,
3224 &calldata->arg.stateid,
3225 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003226 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003227 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003228 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003229 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003230 task->tk_status = nfs4_async_handle_exception(task,
3231 server, task->tk_status, &exception);
3232 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003233 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003234 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003235 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3236 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003237out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003238 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003239 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003240 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003241 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003242 return;
3243lr_restart:
3244 calldata->res.lr_ret = 0;
3245out_restart:
3246 task->tk_status = 0;
3247 rpc_restart_call_prepare(task);
3248 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003249}
3250
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003251static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003252{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003253 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003254 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003255 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003256 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003257 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003258
Chuck Levera3ca5652012-03-01 17:00:40 -05003259 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003260 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003261 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003262
Trond Myklebust88069f72009-12-08 08:33:16 -05003263 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003264 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003265 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3266 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3267 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003268 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003269 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003270 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003271 if (state->n_rdonly == 0)
3272 call_close |= is_rdonly;
3273 else if (is_rdonly)
3274 calldata->arg.fmode |= FMODE_READ;
3275 if (state->n_wronly == 0)
3276 call_close |= is_wronly;
3277 else if (is_wronly)
3278 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003279 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3280 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003281 } else if (is_rdwr)
3282 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3283
Trond Myklebust5cc78612016-11-14 11:19:56 -05003284 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003285 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003286 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003287 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003288
3289 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003290 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003291 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003292 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003293
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003294 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003295 nfs_release_seqid(calldata->arg.seqid);
3296 goto out_wait;
3297 }
3298
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003299 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003300 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003301
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003302 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003303 /* Close-to-open cache consistency revalidation */
3304 if (!nfs4_have_delegation(inode, FMODE_READ))
3305 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3306 else
3307 calldata->arg.bitmask = NULL;
3308 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003309
Trond Myklebust6ae37332015-01-30 14:21:14 -05003310 calldata->arg.share_access =
3311 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3312 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003313
Trond Myklebustd8d84982016-12-19 12:14:44 -05003314 if (calldata->res.fattr == NULL)
3315 calldata->arg.bitmask = NULL;
3316 else if (calldata->arg.bitmask == NULL)
3317 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003318 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003319 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003320 &calldata->arg.seq_args,
3321 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003322 task) != 0)
3323 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003324 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003325 return;
3326out_no_action:
3327 task->tk_action = NULL;
3328out_wait:
3329 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003330}
3331
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003332static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003333 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003334 .rpc_call_done = nfs4_close_done,
3335 .rpc_release = nfs4_free_closedata,
3336};
3337
Linus Torvalds1da177e2005-04-16 15:20:36 -07003338/*
3339 * It is possible for data to be read/written from a mem-mapped file
3340 * after the sys_close call (which hits the vfs layer as a flush).
3341 * This means that we can't safely call nfsv4 close on a file until
3342 * the inode is cleared. This in turn means that we are not good
3343 * NFSv4 citizens - we do not indicate to the server to update the file's
3344 * share state even when we are done with one of the three share
3345 * stateid's in the inode.
3346 *
3347 * NOTE: Caller must be holding the sp->so_owner semaphore!
3348 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003349int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003351 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003352 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003353 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003354 struct nfs4_state_owner *sp = state->owner;
3355 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003356 struct rpc_message msg = {
3357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3358 .rpc_cred = state->owner->so_cred,
3359 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003360 struct rpc_task_setup task_setup_data = {
3361 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003362 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003363 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003364 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003365 .flags = RPC_TASK_ASYNC,
3366 };
Trond Myklebust95121352005-10-18 14:20:12 -07003367 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003368
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003369 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3370 &task_setup_data.rpc_client, &msg);
3371
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003372 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003373 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003374 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003375 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003376 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003377 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003378 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003379 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3380 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003381 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003382 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3383 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003384 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003385 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003386 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003387 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003388 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003389 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003390 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003391 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003392 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003393 calldata->lr.roc = pnfs_roc(state->inode,
3394 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3395 if (calldata->lr.roc) {
3396 calldata->arg.lr_args = &calldata->lr.arg;
3397 calldata->res.lr_res = &calldata->lr.res;
3398 }
Al Viro643168c2011-06-22 18:20:23 -04003399 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003400
Trond Myklebust1174dd12010-12-21 10:52:24 -05003401 msg.rpc_argp = &calldata->arg;
3402 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003403 task_setup_data.callback_data = calldata;
3404 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003405 if (IS_ERR(task))
3406 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003407 status = 0;
3408 if (wait)
3409 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003410 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003411 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003412out_free_calldata:
3413 kfree(calldata);
3414out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003415 nfs4_put_open_state(state);
3416 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003417 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003418}
3419
Trond Myklebust2b484292010-09-17 10:56:51 -04003420static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003421nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3422 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003423{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003424 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003425 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3426
3427 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003428
Trond Myklebust565277f2007-10-15 18:17:53 -04003429 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003430 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003431
3432 nfs4_label_release_security(label);
3433
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003434 if (IS_ERR(state))
3435 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003436 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003437}
3438
Trond Myklebust1185a552009-12-03 15:54:02 -05003439static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003440{
3441 if (ctx->state == NULL)
3442 return;
3443 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003444 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003445 else
Al Viro643168c2011-06-22 18:20:23 -04003446 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003447}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003448
Trond Myklebustb944dba2013-11-04 15:20:20 -05003449#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3450#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003451#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003452
Linus Torvalds1da177e2005-04-16 15:20:36 -07003453static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3454{
Kinglong Mee8c612822015-08-26 21:12:58 +08003455 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003456 struct nfs4_server_caps_arg args = {
3457 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003458 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003459 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460 struct nfs4_server_caps_res res = {};
3461 struct rpc_message msg = {
3462 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003463 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003464 .rpc_resp = &res,
3465 };
3466 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003467 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003468
Kinglong Mee8c612822015-08-26 21:12:58 +08003469 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3470 FATTR4_WORD0_FH_EXPIRE_TYPE |
3471 FATTR4_WORD0_LINK_SUPPORT |
3472 FATTR4_WORD0_SYMLINK_SUPPORT |
3473 FATTR4_WORD0_ACLSUPPORT;
3474 if (minorversion)
3475 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3476
Bryan Schumaker7c513052011-03-24 17:12:24 +00003477 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003478 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003479 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003480 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003481 case 0:
3482 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3483 res.attr_bitmask[2] = 0;
3484 break;
3485 case 1:
3486 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3487 break;
3488 case 2:
3489 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3490 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003491 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003492 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3493 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3494 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3495 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003496 NFS_CAP_CTIME|NFS_CAP_MTIME|
3497 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003498 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3499 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003500 server->caps |= NFS_CAP_ACLS;
3501 if (res.has_links != 0)
3502 server->caps |= NFS_CAP_HARDLINKS;
3503 if (res.has_symlinks != 0)
3504 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003505 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3506 server->caps |= NFS_CAP_FILEID;
3507 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3508 server->caps |= NFS_CAP_MODE;
3509 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3510 server->caps |= NFS_CAP_NLINK;
3511 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3512 server->caps |= NFS_CAP_OWNER;
3513 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3514 server->caps |= NFS_CAP_OWNER_GROUP;
3515 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3516 server->caps |= NFS_CAP_ATIME;
3517 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3518 server->caps |= NFS_CAP_CTIME;
3519 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3520 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003521#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3522 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3523 server->caps |= NFS_CAP_SECURITY_LABEL;
3524#endif
3525 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3526 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003527 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003528
Trond Myklebusta65318b2009-03-11 14:10:28 -04003529 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3530 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3531 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003532 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003533
3534 /* Avoid a regression due to buggy server */
3535 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3536 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003537 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3538 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003539
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003541 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003543
Linus Torvalds1da177e2005-04-16 15:20:36 -07003544 return status;
3545}
3546
Trond Myklebust55a97592006-06-09 09:34:19 -04003547int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003548{
3549 struct nfs4_exception exception = { };
3550 int err;
3551 do {
3552 err = nfs4_handle_exception(server,
3553 _nfs4_server_capabilities(server, fhandle),
3554 &exception);
3555 } while (exception.retry);
3556 return err;
3557}
3558
3559static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3560 struct nfs_fsinfo *info)
3561{
David Quigleyaa9c2662013-05-22 12:50:44 -04003562 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003564 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 };
3566 struct nfs4_lookup_res res = {
3567 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003568 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569 .fh = fhandle,
3570 };
3571 struct rpc_message msg = {
3572 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3573 .rpc_argp = &args,
3574 .rpc_resp = &res,
3575 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003576
David Quigleyaa9c2662013-05-22 12:50:44 -04003577 bitmask[0] = nfs4_fattr_bitmap[0];
3578 bitmask[1] = nfs4_fattr_bitmap[1];
3579 /*
3580 * Process the label in the upcoming getfattr
3581 */
3582 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3583
Trond Myklebust0e574af2005-10-27 22:12:38 -04003584 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003585 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586}
3587
3588static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3589 struct nfs_fsinfo *info)
3590{
3591 struct nfs4_exception exception = { };
3592 int err;
3593 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003594 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003595 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003596 switch (err) {
3597 case 0:
3598 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003599 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003600 default:
3601 err = nfs4_handle_exception(server, err, &exception);
3602 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003604out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003605 return err;
3606}
3607
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003608static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3609 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3610{
Trond Myklebustc2190662013-08-26 19:23:04 -04003611 struct rpc_auth_create_args auth_args = {
3612 .pseudoflavor = flavor,
3613 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003614 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003615
Trond Myklebustc2190662013-08-26 19:23:04 -04003616 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003617 if (IS_ERR(auth))
3618 return -EACCES;
3619 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003620}
3621
Chuck Lever9a744ba2013-03-16 15:56:02 -04003622/*
3623 * Retry pseudoroot lookup with various security flavors. We do this when:
3624 *
3625 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3626 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3627 *
3628 * Returns zero on success, or a negative NFS4ERR value, or a
3629 * negative errno value.
3630 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003631static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003632 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003633{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003634 /* Per 3530bis 15.33.5 */
3635 static const rpc_authflavor_t flav_array[] = {
3636 RPC_AUTH_GSS_KRB5P,
3637 RPC_AUTH_GSS_KRB5I,
3638 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003639 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003640 RPC_AUTH_NULL,
3641 };
3642 int status = -EPERM;
3643 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003644
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003645 if (server->auth_info.flavor_len > 0) {
3646 /* try each flavor specified by user */
3647 for (i = 0; i < server->auth_info.flavor_len; i++) {
3648 status = nfs4_lookup_root_sec(server, fhandle, info,
3649 server->auth_info.flavors[i]);
3650 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3651 continue;
3652 break;
3653 }
3654 } else {
3655 /* no flavors specified by user, try default list */
3656 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3657 status = nfs4_lookup_root_sec(server, fhandle, info,
3658 flav_array[i]);
3659 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3660 continue;
3661 break;
3662 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003663 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003664
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003665 /*
3666 * -EACCESS could mean that the user doesn't have correct permissions
3667 * to access the mount. It could also mean that we tried to mount
3668 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3669 * existing mount programs don't handle -EACCES very well so it should
3670 * be mapped to -EPERM instead.
3671 */
3672 if (status == -EACCES)
3673 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003674 return status;
3675}
3676
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003677/**
3678 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3679 * @server: initialized nfs_server handle
3680 * @fhandle: we fill in the pseudo-fs root file handle
3681 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003682 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003683 *
3684 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003685 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003686int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003687 struct nfs_fsinfo *info,
3688 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003689{
Andre Przywarac7757072015-04-23 17:17:40 +01003690 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003691
Andre Przywarac7757072015-04-23 17:17:40 +01003692 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003693 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003694
3695 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003696 status = server->nfs_client->cl_mvops->find_root_sec(server,
3697 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003698
Linus Torvalds1da177e2005-04-16 15:20:36 -07003699 if (status == 0)
3700 status = nfs4_server_capabilities(server, fhandle);
3701 if (status == 0)
3702 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003703
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003704 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003705}
3706
Bryan Schumakerbae36242012-05-10 15:07:31 -04003707static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3708 struct nfs_fsinfo *info)
3709{
3710 int error;
3711 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003712 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003713
3714 error = nfs4_server_capabilities(server, mntfh);
3715 if (error < 0) {
3716 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3717 return error;
3718 }
3719
David Quigley14c43f72013-05-22 12:50:43 -04003720 label = nfs4_label_alloc(server, GFP_KERNEL);
3721 if (IS_ERR(label))
3722 return PTR_ERR(label);
3723
David Quigley1775fd32013-05-22 12:50:42 -04003724 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003725 if (error < 0) {
3726 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003727 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003728 }
3729
3730 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3731 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3732 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3733
David Quigley14c43f72013-05-22 12:50:43 -04003734err_free_label:
3735 nfs4_label_free(label);
3736
Bryan Schumakerbae36242012-05-10 15:07:31 -04003737 return error;
3738}
3739
Manoj Naik6b97fd32006-06-09 09:34:29 -04003740/*
3741 * Get locations and (maybe) other attributes of a referral.
3742 * Note that we'll actually follow the referral later when
3743 * we detect fsid mismatch in inode revalidation
3744 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003745static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3746 const struct qstr *name, struct nfs_fattr *fattr,
3747 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003748{
3749 int status = -ENOMEM;
3750 struct page *page = NULL;
3751 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003752
3753 page = alloc_page(GFP_KERNEL);
3754 if (page == NULL)
3755 goto out;
3756 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3757 if (locations == NULL)
3758 goto out;
3759
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003760 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003761 if (status != 0)
3762 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003763
3764 /*
3765 * If the fsid didn't change, this is a migration event, not a
3766 * referral. Cause us to drop into the exception handler, which
3767 * will kick off migration recovery.
3768 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003769 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003770 dprintk("%s: server did not return a different fsid for"
3771 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003772 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003773 goto out;
3774 }
Andy Adamson533eb462011-06-13 18:25:56 -04003775 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3776 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003777
Andy Adamson533eb462011-06-13 18:25:56 -04003778 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003779 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003780 memset(fhandle, 0, sizeof(struct nfs_fh));
3781out:
3782 if (page)
3783 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003784 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003785 return status;
3786}
3787
David Quigley1775fd32013-05-22 12:50:42 -04003788static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3789 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003790{
3791 struct nfs4_getattr_arg args = {
3792 .fh = fhandle,
3793 .bitmask = server->attr_bitmask,
3794 };
3795 struct nfs4_getattr_res res = {
3796 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003797 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003798 .server = server,
3799 };
3800 struct rpc_message msg = {
3801 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3802 .rpc_argp = &args,
3803 .rpc_resp = &res,
3804 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003805
3806 args.bitmask = nfs4_bitmask(server, label);
3807
Trond Myklebust0e574af2005-10-27 22:12:38 -04003808 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003809 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810}
3811
David Quigley1775fd32013-05-22 12:50:42 -04003812static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3813 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003814{
3815 struct nfs4_exception exception = { };
3816 int err;
3817 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003818 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3819 trace_nfs4_getattr(server, fhandle, fattr, err);
3820 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003821 &exception);
3822 } while (exception.retry);
3823 return err;
3824}
3825
3826/*
3827 * The file is not closed if it is opened due to the a request to change
3828 * the size of the file. The open call will not be needed once the
3829 * VFS layer lookup-intents are implemented.
3830 *
3831 * Close is called when the inode is destroyed.
3832 * If we haven't opened the file for O_WRONLY, we
3833 * need to in the size_change case to obtain a stateid.
3834 *
3835 * Got race?
3836 * Because OPEN is always done by name in nfsv4, it is
3837 * possible that we opened a different file by the same
3838 * name. We can recognize this race condition, but we
3839 * can't do anything about it besides returning an error.
3840 *
3841 * This will be fixed with VFS changes (lookup-intent).
3842 */
3843static int
3844nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3845 struct iattr *sattr)
3846{
David Howells2b0143b2015-03-17 22:25:59 +00003847 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003848 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003849 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003850 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003851 int status;
3852
Peng Tao88ac8152014-09-12 11:04:10 +08003853 if (pnfs_ld_layoutret_on_setattr(inode) &&
3854 sattr->ia_valid & ATTR_SIZE &&
3855 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003856 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003857
Trond Myklebust0e574af2005-10-27 22:12:38 -04003858 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003859
Andy Adamson26699402012-05-30 16:12:24 -04003860 /* Deal with open(O_TRUNC) */
3861 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003862 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003863
3864 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003865 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003866 return 0;
3867
Trond Myklebustd5308382005-11-04 15:33:38 -05003868 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003869 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003870
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003871 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003872 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003873 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003874 }
3875
David Quigley14c43f72013-05-22 12:50:43 -04003876 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3877 if (IS_ERR(label))
3878 return PTR_ERR(label);
3879
Trond Myklebust199366f2018-03-20 16:43:18 -04003880 /* Return any delegations if we're going to change ACLs */
3881 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003882 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003883
NeilBrown29b59f92016-10-13 15:26:47 +11003884 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003885 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003886 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003887 nfs_setsecurity(inode, fattr, label);
3888 }
David Quigley14c43f72013-05-22 12:50:43 -04003889 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003890 return status;
3891}
3892
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003893static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3894 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003895 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003896{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003897 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003898 int status;
3899 struct nfs4_lookup_arg args = {
3900 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003901 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003902 .name = name,
3903 };
3904 struct nfs4_lookup_res res = {
3905 .server = server,
3906 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003907 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003908 .fh = fhandle,
3909 };
3910 struct rpc_message msg = {
3911 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3912 .rpc_argp = &args,
3913 .rpc_resp = &res,
3914 };
3915
David Quigleyaa9c2662013-05-22 12:50:44 -04003916 args.bitmask = nfs4_bitmask(server, label);
3917
David Howells2b3de442006-08-22 20:06:09 -04003918 nfs_fattr_init(fattr);
3919
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003921 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003922 dprintk("NFS reply lookup: %d\n", status);
3923 return status;
3924}
3925
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003926static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003927{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003928 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003929 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003930 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3931 fattr->nlink = 2;
3932}
3933
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003934static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003935 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003936 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003937{
3938 struct nfs4_exception exception = { };
3939 struct rpc_clnt *client = *clnt;
3940 int err;
3941 do {
David Quigley1775fd32013-05-22 12:50:42 -04003942 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003943 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003944 switch (err) {
3945 case -NFS4ERR_BADNAME:
3946 err = -ENOENT;
3947 goto out;
3948 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003949 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003950 if (err == -NFS4ERR_MOVED)
3951 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003952 goto out;
3953 case -NFS4ERR_WRONGSEC:
3954 err = -EPERM;
3955 if (client != *clnt)
3956 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003957 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003958 if (IS_ERR(client))
3959 return PTR_ERR(client);
3960
3961 exception.retry = 1;
3962 break;
3963 default:
3964 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3965 }
3966 } while (exception.retry);
3967
3968out:
3969 if (err == 0)
3970 *clnt = client;
3971 else if (client != *clnt)
3972 rpc_shutdown_client(client);
3973
3974 return err;
3975}
3976
Al Virobeffb8f2016-07-20 16:34:42 -04003977static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003978 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3979 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003980{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003981 int status;
3982 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003983
David Quigley1775fd32013-05-22 12:50:42 -04003984 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003985 if (client != NFS_CLIENT(dir)) {
3986 rpc_shutdown_client(client);
3987 nfs_fixup_secinfo_attributes(fattr);
3988 }
3989 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003990}
3991
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003992struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003993nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003994 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3995{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003996 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003997 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003998
David Quigley1775fd32013-05-22 12:50:42 -04003999 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004000 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004001 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004002 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004003}
4004
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004005static int _nfs4_proc_lookupp(struct inode *inode,
4006 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4007 struct nfs4_label *label)
4008{
4009 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4010 struct nfs_server *server = NFS_SERVER(inode);
4011 int status;
4012 struct nfs4_lookupp_arg args = {
4013 .bitmask = server->attr_bitmask,
4014 .fh = NFS_FH(inode),
4015 };
4016 struct nfs4_lookupp_res res = {
4017 .server = server,
4018 .fattr = fattr,
4019 .label = label,
4020 .fh = fhandle,
4021 };
4022 struct rpc_message msg = {
4023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4024 .rpc_argp = &args,
4025 .rpc_resp = &res,
4026 };
4027
4028 args.bitmask = nfs4_bitmask(server, label);
4029
4030 nfs_fattr_init(fattr);
4031
4032 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4033 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4034 &res.seq_res, 0);
4035 dprintk("NFS reply lookupp: %d\n", status);
4036 return status;
4037}
4038
4039static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4040 struct nfs_fattr *fattr, struct nfs4_label *label)
4041{
4042 struct nfs4_exception exception = { };
4043 int err;
4044 do {
4045 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4046 trace_nfs4_lookupp(inode, err);
4047 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4048 &exception);
4049 } while (exception.retry);
4050 return err;
4051}
4052
Linus Torvalds1da177e2005-04-16 15:20:36 -07004053static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4054{
Trond Myklebust76b32992007-08-10 17:45:11 -04004055 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004056 struct nfs4_accessargs args = {
4057 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004058 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004059 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004060 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004061 struct nfs4_accessres res = {
4062 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004063 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004064 struct rpc_message msg = {
4065 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4066 .rpc_argp = &args,
4067 .rpc_resp = &res,
4068 .rpc_cred = entry->cred,
4069 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004070 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071
Trond Myklebustc407d412010-04-16 16:22:48 -04004072 res.fattr = nfs_alloc_fattr();
4073 if (res.fattr == NULL)
4074 return -ENOMEM;
4075
Bryan Schumaker7c513052011-03-24 17:12:24 +00004076 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004078 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004079 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004081 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082 return status;
4083}
4084
4085static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4086{
4087 struct nfs4_exception exception = { };
4088 int err;
4089 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004090 err = _nfs4_proc_access(inode, entry);
4091 trace_nfs4_access(inode, err);
4092 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004093 &exception);
4094 } while (exception.retry);
4095 return err;
4096}
4097
4098/*
4099 * TODO: For the time being, we don't try to get any attributes
4100 * along with any of the zero-copy operations READ, READDIR,
4101 * READLINK, WRITE.
4102 *
4103 * In the case of the first three, we want to put the GETATTR
4104 * after the read-type operation -- this is because it is hard
4105 * to predict the length of a GETATTR response in v4, and thus
4106 * align the READ data correctly. This means that the GETATTR
4107 * may end up partially falling into the page cache, and we should
4108 * shift it into the 'tail' of the xdr_buf before processing.
4109 * To do this efficiently, we need to know the total length
4110 * of data received, which doesn't seem to be available outside
4111 * of the RPC layer.
4112 *
4113 * In the case of WRITE, we also want to put the GETATTR after
4114 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004115 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004116 *
4117 * Both of these changes to the XDR layer would in fact be quite
4118 * minor, but I decided to leave them for a subsequent patch.
4119 */
4120static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4121 unsigned int pgbase, unsigned int pglen)
4122{
4123 struct nfs4_readlink args = {
4124 .fh = NFS_FH(inode),
4125 .pgbase = pgbase,
4126 .pglen = pglen,
4127 .pages = &page,
4128 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004129 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004130 struct rpc_message msg = {
4131 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4132 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004133 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004134 };
4135
Bryan Schumaker7c513052011-03-24 17:12:24 +00004136 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 -07004137}
4138
4139static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4140 unsigned int pgbase, unsigned int pglen)
4141{
4142 struct nfs4_exception exception = { };
4143 int err;
4144 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004145 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4146 trace_nfs4_readlink(inode, err);
4147 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004148 &exception);
4149 } while (exception.retry);
4150 return err;
4151}
4152
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004154 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004155 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004156static int
4157nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004158 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004159{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004160 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004161 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004162 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004164 int status = 0;
4165
NeilBrown532d4de2016-10-13 15:26:47 +11004166 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004167 if (IS_ERR(ctx))
4168 return PTR_ERR(ctx);
4169
David Quigleyaa9c2662013-05-22 12:50:44 -04004170 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4171
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004172 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4173 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004174 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004175 if (IS_ERR(state)) {
4176 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004177 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004180 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004181 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004182 return status;
4183}
4184
Al Virobeffb8f2016-07-20 16:34:42 -04004185static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004186{
Trond Myklebust16e42952005-10-27 22:12:44 -04004187 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004188 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004190 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004191 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004192 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004193 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004194 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004195 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004196 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4197 .rpc_argp = &args,
4198 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004200 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004201 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004202
Bryan Schumaker7c513052011-03-24 17:12:24 +00004203 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004204 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004205 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return status;
4207}
4208
Trond Myklebust912678d2018-03-20 16:43:15 -04004209static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4210{
4211 struct nfs4_exception exception = { };
4212 struct inode *inode = d_inode(dentry);
4213 int err;
4214
Trond Myklebustc01d3642018-03-20 16:43:20 -04004215 if (inode) {
4216 if (inode->i_nlink == 1)
4217 nfs4_inode_return_delegation(inode);
4218 else
4219 nfs4_inode_make_writeable(inode);
4220 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004221 do {
4222 err = _nfs4_proc_remove(dir, &dentry->d_name);
4223 trace_nfs4_remove(dir, &dentry->d_name, err);
4224 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4225 &exception);
4226 } while (exception.retry);
4227 return err;
4228}
4229
4230static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004231{
4232 struct nfs4_exception exception = { };
4233 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004234
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004236 err = _nfs4_proc_remove(dir, name);
4237 trace_nfs4_remove(dir, name, err);
4238 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239 &exception);
4240 } while (exception.retry);
4241 return err;
4242}
4243
Trond Myklebust977fcc22018-03-20 16:43:17 -04004244static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004246 struct nfs_removeargs *args = msg->rpc_argp;
4247 struct nfs_removeres *res = msg->rpc_resp;
Trond Myklebust977fcc22018-03-20 16:43:17 -04004248 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004249
Trond Myklebust977fcc22018-03-20 16:43:17 -04004250 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004251 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004252 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004253
4254 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004255
4256 if (inode)
4257 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004258}
4259
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004260static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4261{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004262 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004263 &data->args.seq_args,
4264 &data->res.seq_res,
4265 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004266}
4267
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004268static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004270 struct nfs_unlinkdata *data = task->tk_calldata;
4271 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004272
Trond Myklebust14516c32010-07-31 14:29:06 -04004273 if (!nfs4_sequence_done(task, &res->seq_res))
4274 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004275 if (nfs4_async_handle_error(task, res->server, NULL,
4276 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004277 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004278 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004279 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004280 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004281}
4282
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004283static void nfs4_proc_rename_setup(struct rpc_message *msg,
4284 struct dentry *old_dentry,
4285 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004286{
Jeff Laytond3d41522010-09-17 17:31:57 -04004287 struct nfs_renameargs *arg = msg->rpc_argp;
4288 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004289 struct inode *old_inode = d_inode(old_dentry);
4290 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004291
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004292 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004293 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004294 if (new_inode)
4295 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004296 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004297 res->server = NFS_SB(old_dentry->d_sb);
Chuck Levera9c92d62013-08-09 12:48:18 -04004298 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004299}
4300
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004301static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4302{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004303 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004304 &data->args.seq_args,
4305 &data->res.seq_res,
4306 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004307}
4308
4309static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4310 struct inode *new_dir)
4311{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004312 struct nfs_renamedata *data = task->tk_calldata;
4313 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004314
4315 if (!nfs4_sequence_done(task, &res->seq_res))
4316 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004317 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004318 return 0;
4319
Trond Myklebustc733c492017-01-11 12:32:26 -05004320 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004321 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004322 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004323 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004324 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004325 return 1;
4326}
4327
Al Virobeffb8f2016-07-20 16:34:42 -04004328static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004329{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004330 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 struct nfs4_link_arg arg = {
4332 .fh = NFS_FH(inode),
4333 .dir_fh = NFS_FH(dir),
4334 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004335 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004336 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004337 struct nfs4_link_res res = {
4338 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004339 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004340 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341 struct rpc_message msg = {
4342 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4343 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004344 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004346 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347
Trond Myklebust136f2622010-04-16 16:22:49 -04004348 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004349 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004350 goto out;
4351
David Quigley14c43f72013-05-22 12:50:43 -04004352 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4353 if (IS_ERR(res.label)) {
4354 status = PTR_ERR(res.label);
4355 goto out;
4356 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004357 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004358
Trond Myklebustc01d3642018-03-20 16:43:20 -04004359 nfs4_inode_make_writeable(inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004360
Bryan Schumaker7c513052011-03-24 17:12:24 +00004361 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004362 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004363 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004364 status = nfs_post_op_update_inode(inode, res.fattr);
4365 if (!status)
4366 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004367 }
David Quigley14c43f72013-05-22 12:50:43 -04004368
4369
4370 nfs4_label_free(res.label);
4371
Trond Myklebust136f2622010-04-16 16:22:49 -04004372out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004373 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004374 return status;
4375}
4376
Al Virobeffb8f2016-07-20 16:34:42 -04004377static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004378{
4379 struct nfs4_exception exception = { };
4380 int err;
4381 do {
4382 err = nfs4_handle_exception(NFS_SERVER(inode),
4383 _nfs4_proc_link(inode, dir, name),
4384 &exception);
4385 } while (exception.retry);
4386 return err;
4387}
4388
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004389struct nfs4_createdata {
4390 struct rpc_message msg;
4391 struct nfs4_create_arg arg;
4392 struct nfs4_create_res res;
4393 struct nfs_fh fh;
4394 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004395 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004396};
4397
4398static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004399 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004400{
4401 struct nfs4_createdata *data;
4402
4403 data = kzalloc(sizeof(*data), GFP_KERNEL);
4404 if (data != NULL) {
4405 struct nfs_server *server = NFS_SERVER(dir);
4406
David Quigley14c43f72013-05-22 12:50:43 -04004407 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4408 if (IS_ERR(data->label))
4409 goto out_free;
4410
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004411 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4412 data->msg.rpc_argp = &data->arg;
4413 data->msg.rpc_resp = &data->res;
4414 data->arg.dir_fh = NFS_FH(dir);
4415 data->arg.server = server;
4416 data->arg.name = name;
4417 data->arg.attrs = sattr;
4418 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004419 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004420 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004421 data->res.server = server;
4422 data->res.fh = &data->fh;
4423 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004424 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004425 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004426 }
4427 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004428out_free:
4429 kfree(data);
4430 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004431}
4432
4433static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4434{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004435 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004436 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004437 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004438 update_changeattr(dir, &data->res.dir_cinfo,
4439 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004440 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004441 }
4442 return status;
4443}
4444
4445static void nfs4_free_createdata(struct nfs4_createdata *data)
4446{
David Quigley14c43f72013-05-22 12:50:43 -04004447 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004448 kfree(data);
4449}
4450
Chuck Lever4f390c12006-08-22 20:06:22 -04004451static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004452 struct page *page, unsigned int len, struct iattr *sattr,
4453 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004454{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004455 struct nfs4_createdata *data;
4456 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004457
Chuck Lever94a6d752006-08-22 20:06:23 -04004458 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004459 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004460
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004461 status = -ENOMEM;
4462 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4463 if (data == NULL)
4464 goto out;
4465
4466 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4467 data->arg.u.symlink.pages = &page;
4468 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004469 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004471 status = nfs4_do_create(dir, dentry, data);
4472
4473 nfs4_free_createdata(data);
4474out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004475 return status;
4476}
4477
Chuck Lever4f390c12006-08-22 20:06:22 -04004478static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004479 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480{
4481 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004482 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004483 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004484
4485 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4486
Linus Torvalds1da177e2005-04-16 15:20:36 -07004487 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004488 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4489 trace_nfs4_symlink(dir, &dentry->d_name, err);
4490 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 &exception);
4492 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004493
4494 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004495 return err;
4496}
4497
4498static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004499 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004500{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004501 struct nfs4_createdata *data;
4502 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004504 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4505 if (data == NULL)
4506 goto out;
4507
David Quigley1775fd32013-05-22 12:50:42 -04004508 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004509 status = nfs4_do_create(dir, dentry, data);
4510
4511 nfs4_free_createdata(data);
4512out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004513 return status;
4514}
4515
4516static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4517 struct iattr *sattr)
4518{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004519 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004520 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004521 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004523
David Quigleyaa9c2662013-05-22 12:50:44 -04004524 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4525
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004526 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4527 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004528 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004529 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4530 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4531 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004532 &exception);
4533 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004534 nfs4_label_release_security(label);
4535
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 return err;
4537}
4538
4539static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004540 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004541{
David Howells2b0143b2015-03-17 22:25:59 +00004542 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004543 struct nfs4_readdir_arg args = {
4544 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004545 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 .pgbase = 0,
4547 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004548 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004549 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004550 };
4551 struct nfs4_readdir_res res;
4552 struct rpc_message msg = {
4553 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4554 .rpc_argp = &args,
4555 .rpc_resp = &res,
4556 .rpc_cred = cred,
4557 };
4558 int status;
4559
Al Viro6de14722013-09-16 10:53:17 -04004560 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4561 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004562 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004563 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004565 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 -05004566 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004567 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004568 status += args.pgbase;
4569 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004570
4571 nfs_invalidate_atime(dir);
4572
Harvey Harrison3110ff82008-05-02 13:42:44 -07004573 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 return status;
4575}
4576
4577static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004578 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004579{
4580 struct nfs4_exception exception = { };
4581 int err;
4582 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004583 err = _nfs4_proc_readdir(dentry, cred, cookie,
4584 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004585 trace_nfs4_readdir(d_inode(dentry), err);
4586 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004587 &exception);
4588 } while (exception.retry);
4589 return err;
4590}
4591
4592static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004593 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004594{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004595 struct nfs4_createdata *data;
4596 int mode = sattr->ia_mode;
4597 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004599 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4600 if (data == NULL)
4601 goto out;
4602
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004604 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004605 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004606 data->arg.ftype = NF4BLK;
4607 data->arg.u.device.specdata1 = MAJOR(rdev);
4608 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004609 }
4610 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004611 data->arg.ftype = NF4CHR;
4612 data->arg.u.device.specdata1 = MAJOR(rdev);
4613 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004614 } else if (!S_ISSOCK(mode)) {
4615 status = -EINVAL;
4616 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004617 }
David Quigley1775fd32013-05-22 12:50:42 -04004618
David Quigleyaa9c2662013-05-22 12:50:44 -04004619 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004620 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004621out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004622 nfs4_free_createdata(data);
4623out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004624 return status;
4625}
4626
4627static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4628 struct iattr *sattr, dev_t rdev)
4629{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004630 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004631 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004632 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004634
David Quigleyaa9c2662013-05-22 12:50:44 -04004635 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4636
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004637 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4638 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004639 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004640 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4641 trace_nfs4_mknod(dir, &dentry->d_name, err);
4642 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004643 &exception);
4644 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004645
4646 nfs4_label_release_security(label);
4647
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return err;
4649}
4650
4651static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4652 struct nfs_fsstat *fsstat)
4653{
4654 struct nfs4_statfs_arg args = {
4655 .fh = fhandle,
4656 .bitmask = server->attr_bitmask,
4657 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004658 struct nfs4_statfs_res res = {
4659 .fsstat = fsstat,
4660 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004661 struct rpc_message msg = {
4662 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4663 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004664 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004665 };
4666
Trond Myklebust0e574af2005-10-27 22:12:38 -04004667 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004668 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004669}
4670
4671static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4672{
4673 struct nfs4_exception exception = { };
4674 int err;
4675 do {
4676 err = nfs4_handle_exception(server,
4677 _nfs4_proc_statfs(server, fhandle, fsstat),
4678 &exception);
4679 } while (exception.retry);
4680 return err;
4681}
4682
4683static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4684 struct nfs_fsinfo *fsinfo)
4685{
4686 struct nfs4_fsinfo_arg args = {
4687 .fh = fhandle,
4688 .bitmask = server->attr_bitmask,
4689 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004690 struct nfs4_fsinfo_res res = {
4691 .fsinfo = fsinfo,
4692 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693 struct rpc_message msg = {
4694 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4695 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004696 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004697 };
4698
Bryan Schumaker7c513052011-03-24 17:12:24 +00004699 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700}
4701
4702static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4703{
4704 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004705 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004706 int err;
4707
4708 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004709 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004710 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004711 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004712 nfs4_set_lease_period(server->nfs_client,
4713 fsinfo->lease_time * HZ,
4714 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004715 break;
4716 }
4717 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004718 } while (exception.retry);
4719 return err;
4720}
4721
4722static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4723{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004724 int error;
4725
Trond Myklebust0e574af2005-10-27 22:12:38 -04004726 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004727 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004728 if (error == 0) {
4729 /* block layout checks this! */
4730 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004731 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004732 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004733
4734 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004735}
4736
4737static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4738 struct nfs_pathconf *pathconf)
4739{
4740 struct nfs4_pathconf_arg args = {
4741 .fh = fhandle,
4742 .bitmask = server->attr_bitmask,
4743 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004744 struct nfs4_pathconf_res res = {
4745 .pathconf = pathconf,
4746 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004747 struct rpc_message msg = {
4748 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4749 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004750 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751 };
4752
4753 /* None of the pathconf attributes are mandatory to implement */
4754 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4755 memset(pathconf, 0, sizeof(*pathconf));
4756 return 0;
4757 }
4758
Trond Myklebust0e574af2005-10-27 22:12:38 -04004759 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004760 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004761}
4762
4763static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4764 struct nfs_pathconf *pathconf)
4765{
4766 struct nfs4_exception exception = { };
4767 int err;
4768
4769 do {
4770 err = nfs4_handle_exception(server,
4771 _nfs4_proc_pathconf(server, fhandle, pathconf),
4772 &exception);
4773 } while (exception.retry);
4774 return err;
4775}
4776
Trond Myklebust5521abf2013-03-16 20:54:34 -04004777int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004778 const struct nfs_open_context *ctx,
4779 const struct nfs_lock_context *l_ctx,
4780 fmode_t fmode)
4781{
NeilBrown17393472016-10-13 15:26:47 +11004782 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004783}
4784EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4785
Trond Myklebust5521abf2013-03-16 20:54:34 -04004786static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4787 const struct nfs_open_context *ctx,
4788 const struct nfs_lock_context *l_ctx,
4789 fmode_t fmode)
4790{
4791 nfs4_stateid current_stateid;
4792
Trond Myklebuste1253be2014-03-05 08:44:23 -05004793 /* If the current stateid represents a lost lock, then exit */
4794 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4795 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004796 return nfs4_stateid_match(stateid, &current_stateid);
4797}
4798
4799static bool nfs4_error_stateid_expired(int err)
4800{
4801 switch (err) {
4802 case -NFS4ERR_DELEG_REVOKED:
4803 case -NFS4ERR_ADMIN_REVOKED:
4804 case -NFS4ERR_BAD_STATEID:
4805 case -NFS4ERR_STALE_STATEID:
4806 case -NFS4ERR_OLD_STATEID:
4807 case -NFS4ERR_OPENMODE:
4808 case -NFS4ERR_EXPIRED:
4809 return true;
4810 }
4811 return false;
4812}
4813
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004814static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004815{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004816 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004817
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004818 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004819 if (task->tk_status < 0) {
4820 struct nfs4_exception exception = {
4821 .inode = hdr->inode,
4822 .state = hdr->args.context->state,
4823 .stateid = &hdr->args.stateid,
4824 };
4825 task->tk_status = nfs4_async_handle_exception(task,
4826 server, task->tk_status, &exception);
4827 if (exception.retry) {
4828 rpc_restart_call_prepare(task);
4829 return -EAGAIN;
4830 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004832
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004834 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004835 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004836}
4837
Trond Myklebust5521abf2013-03-16 20:54:34 -04004838static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004839 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004840{
4841
4842 if (!nfs4_error_stateid_expired(task->tk_status) ||
4843 nfs4_stateid_is_current(&args->stateid,
4844 args->context,
4845 args->lock_context,
4846 FMODE_READ))
4847 return false;
4848 rpc_restart_call_prepare(task);
4849 return true;
4850}
4851
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004852static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004853{
4854
4855 dprintk("--> %s\n", __func__);
4856
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004857 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004858 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004859 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004860 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004861 if (task->tk_status > 0)
4862 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004863 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4864 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004865}
4866
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004867static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4868 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004870 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004871 if (!hdr->pgio_done_cb)
4872 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004873 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004874 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004875}
4876
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004877static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4878 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004879{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004880 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004881 &hdr->args.seq_args,
4882 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004883 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004884 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004885 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4886 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004887 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004888 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004889 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004890 return -EIO;
4891 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004892}
4893
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004894static int nfs4_write_done_cb(struct rpc_task *task,
4895 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004896{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004897 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004898
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004899 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004900 if (task->tk_status < 0) {
4901 struct nfs4_exception exception = {
4902 .inode = hdr->inode,
4903 .state = hdr->args.context->state,
4904 .stateid = &hdr->args.stateid,
4905 };
4906 task->tk_status = nfs4_async_handle_exception(task,
4907 NFS_SERVER(inode), task->tk_status,
4908 &exception);
4909 if (exception.retry) {
4910 rpc_restart_call_prepare(task);
4911 return -EAGAIN;
4912 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004914 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004915 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004916 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004917 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004918 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919}
4920
Trond Myklebust5521abf2013-03-16 20:54:34 -04004921static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004922 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004923{
4924
4925 if (!nfs4_error_stateid_expired(task->tk_status) ||
4926 nfs4_stateid_is_current(&args->stateid,
4927 args->context,
4928 args->lock_context,
4929 FMODE_WRITE))
4930 return false;
4931 rpc_restart_call_prepare(task);
4932 return true;
4933}
4934
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004935static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004936{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004937 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004938 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004939 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004940 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004941 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4942 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004943}
4944
Trond Myklebust5a37f852012-04-28 14:55:16 -04004945static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004946bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004947{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004948 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004949 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004950 return false;
4951 /* Otherwise, request attributes if and only if we don't hold
4952 * a delegation
4953 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004954 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004955}
Fred Isamana69aef12011-03-03 15:13:47 +00004956
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004957static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4958 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004959{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004960 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004961
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004962 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4963 hdr->args.bitmask = NULL;
4964 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004965 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004966 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004967
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004968 if (!hdr->pgio_done_cb)
4969 hdr->pgio_done_cb = nfs4_write_done_cb;
4970 hdr->res.server = server;
4971 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004972
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004973 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004974 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004975}
4976
Fred Isaman0b7c0152012-04-20 14:47:39 -04004977static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4978{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004979 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004980 &data->args.seq_args,
4981 &data->res.seq_res,
4982 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004983}
4984
Fred Isaman0b7c0152012-04-20 14:47:39 -04004985static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004987 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004988
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004989 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004990 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4991 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004992 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004993 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004994 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004995 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996}
4997
Fred Isaman0b7c0152012-04-20 14:47:39 -04004998static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004999{
5000 if (!nfs4_sequence_done(task, &data->res.seq_res))
5001 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005002 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005003}
5004
Fred Isaman0b7c0152012-04-20 14:47:39 -04005005static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005006{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005007 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005008
Fred Isaman0b7c0152012-04-20 14:47:39 -04005009 if (data->commit_done_cb == NULL)
5010 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005011 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005012 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04005013 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014}
5015
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005016struct nfs4_renewdata {
5017 struct nfs_client *client;
5018 unsigned long timestamp;
5019};
5020
Linus Torvalds1da177e2005-04-16 15:20:36 -07005021/*
5022 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5023 * standalone procedure for queueing an asynchronous RENEW.
5024 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005025static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005026{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005027 struct nfs4_renewdata *data = calldata;
5028 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005029
Elena Reshetova212bf412017-10-20 12:53:38 +03005030 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005031 nfs4_schedule_state_renewal(clp);
5032 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005033 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005034}
5035
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005036static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005037{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005038 struct nfs4_renewdata *data = calldata;
5039 struct nfs_client *clp = data->client;
5040 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005042 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005043 switch (task->tk_status) {
5044 case 0:
5045 break;
5046 case -NFS4ERR_LEASE_MOVED:
5047 nfs4_schedule_lease_moved_recovery(clp);
5048 break;
5049 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005050 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005051 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5052 return;
5053 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005054 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005055 return;
5056 }
5057 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005059 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005060}
5061
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005062static const struct rpc_call_ops nfs4_renew_ops = {
5063 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005064 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005065};
5066
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005067static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005068{
5069 struct rpc_message msg = {
5070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5071 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005072 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005073 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005074 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005075
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005076 if (renew_flags == 0)
5077 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005078 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005079 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005080 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005081 if (data == NULL) {
5082 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005083 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005084 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005085 data->client = clp;
5086 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005087 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005088 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005089}
5090
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005091static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092{
5093 struct rpc_message msg = {
5094 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5095 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005096 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005097 };
5098 unsigned long now = jiffies;
5099 int status;
5100
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005101 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005102 if (status < 0)
5103 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005104 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005105 return 0;
5106}
5107
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005108static inline int nfs4_server_supports_acls(struct nfs_server *server)
5109{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005110 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005111}
5112
Trond Myklebust21f498c2012-08-24 10:59:25 -04005113/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5114 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005115 * the stack.
5116 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005117#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005118
Neil Hormane9e3d722011-03-04 19:26:03 -05005119static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005120 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005121{
5122 struct page *newpage, **spages;
5123 int rc = 0;
5124 size_t len;
5125 spages = pages;
5126
5127 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005128 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005129 newpage = alloc_page(GFP_KERNEL);
5130
5131 if (newpage == NULL)
5132 goto unwind;
5133 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005134 buf += len;
5135 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005136 *pages++ = newpage;
5137 rc++;
5138 } while (buflen != 0);
5139
5140 return rc;
5141
5142unwind:
5143 for(; rc > 0; rc--)
5144 __free_page(spages[rc-1]);
5145 return -ENOMEM;
5146}
5147
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005148struct nfs4_cached_acl {
5149 int cached;
5150 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005151 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005152};
5153
5154static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005155{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005156 struct nfs_inode *nfsi = NFS_I(inode);
5157
5158 spin_lock(&inode->i_lock);
5159 kfree(nfsi->nfs4_acl);
5160 nfsi->nfs4_acl = acl;
5161 spin_unlock(&inode->i_lock);
5162}
5163
5164static void nfs4_zap_acl_attr(struct inode *inode)
5165{
5166 nfs4_set_cached_acl(inode, NULL);
5167}
5168
5169static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5170{
5171 struct nfs_inode *nfsi = NFS_I(inode);
5172 struct nfs4_cached_acl *acl;
5173 int ret = -ENOENT;
5174
5175 spin_lock(&inode->i_lock);
5176 acl = nfsi->nfs4_acl;
5177 if (acl == NULL)
5178 goto out;
5179 if (buf == NULL) /* user is just asking for length */
5180 goto out_len;
5181 if (acl->cached == 0)
5182 goto out;
5183 ret = -ERANGE; /* see getxattr(2) man page */
5184 if (acl->len > buflen)
5185 goto out;
5186 memcpy(buf, acl->data, acl->len);
5187out_len:
5188 ret = acl->len;
5189out:
5190 spin_unlock(&inode->i_lock);
5191 return ret;
5192}
5193
Sachin Prabhu5794d212012-04-17 14:36:40 +01005194static 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 +00005195{
5196 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005197 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005198
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005199 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005200 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005201 if (acl == NULL)
5202 goto out;
5203 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005204 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005205 } else {
5206 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5207 if (acl == NULL)
5208 goto out;
5209 acl->cached = 0;
5210 }
5211 acl->len = acl_len;
5212out:
5213 nfs4_set_cached_acl(inode, acl);
5214}
5215
Andy Adamsonbf118a32011-12-07 11:55:27 -05005216/*
5217 * The getxattr API returns the required buffer length when called with a
5218 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5219 * the required buf. On a NULL buf, we send a page of data to the server
5220 * guessing that the ACL request can be serviced by a page. If so, we cache
5221 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5222 * the cache. If not so, we throw away the page, and cache the required
5223 * length. The next getxattr call will then produce another round trip to
5224 * the server, this time with the input buf of the required size.
5225 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005226static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005227{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005228 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005229 struct nfs_getaclargs args = {
5230 .fh = NFS_FH(inode),
5231 .acl_pages = pages,
5232 .acl_len = buflen,
5233 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005234 struct nfs_getaclres res = {
5235 .acl_len = buflen,
5236 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005237 struct rpc_message msg = {
5238 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5239 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005240 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005241 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005242 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005243 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005244
Trond Myklebust21f498c2012-08-24 10:59:25 -04005245 if (npages > ARRAY_SIZE(pages))
5246 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005247
Andy Adamsonbf118a32011-12-07 11:55:27 -05005248 for (i = 0; i < npages; i++) {
5249 pages[i] = alloc_page(GFP_KERNEL);
5250 if (!pages[i])
5251 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005252 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005253
5254 /* for decoding across pages */
5255 res.acl_scratch = alloc_page(GFP_KERNEL);
5256 if (!res.acl_scratch)
5257 goto out_free;
5258
Andy Adamsonbf118a32011-12-07 11:55:27 -05005259 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005260
Peng Taode040be2012-01-10 22:42:47 +08005261 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005262 __func__, buf, buflen, npages, args.acl_len);
5263 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5264 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005265 if (ret)
5266 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005267
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005268 /* Handle the case where the passed-in buffer is too short */
5269 if (res.acl_flags & NFS4_ACL_TRUNC) {
5270 /* Did the user only issue a request for the acl length? */
5271 if (buf == NULL)
5272 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005273 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005274 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005275 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005276 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005277 if (buf) {
5278 if (res.acl_len > buflen) {
5279 ret = -ERANGE;
5280 goto out_free;
5281 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005282 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005283 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005284out_ok:
5285 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005286out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005287 for (i = 0; i < npages; i++)
5288 if (pages[i])
5289 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005290 if (res.acl_scratch)
5291 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005292 return ret;
5293}
5294
Trond Myklebust16b42892006-08-24 12:27:15 -04005295static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5296{
5297 struct nfs4_exception exception = { };
5298 ssize_t ret;
5299 do {
5300 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005301 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005302 if (ret >= 0)
5303 break;
5304 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5305 } while (exception.retry);
5306 return ret;
5307}
5308
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005309static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5310{
5311 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005312 int ret;
5313
5314 if (!nfs4_server_supports_acls(server))
5315 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005316 ret = nfs_revalidate_inode(server, inode);
5317 if (ret < 0)
5318 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005319 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5320 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005321 ret = nfs4_read_cached_acl(inode, buf, buflen);
5322 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005323 /* -ENOENT is returned if there is no ACL or if there is an ACL
5324 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005325 return ret;
5326 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005327}
5328
Trond Myklebust16b42892006-08-24 12:27:15 -04005329static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005330{
5331 struct nfs_server *server = NFS_SERVER(inode);
5332 struct page *pages[NFS4ACL_MAXPAGES];
5333 struct nfs_setaclargs arg = {
5334 .fh = NFS_FH(inode),
5335 .acl_pages = pages,
5336 .acl_len = buflen,
5337 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005338 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005339 struct rpc_message msg = {
5340 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5341 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005342 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005343 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005344 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005345 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005346
5347 if (!nfs4_server_supports_acls(server))
5348 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005349 if (npages > ARRAY_SIZE(pages))
5350 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005351 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005352 if (i < 0)
5353 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005354 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005355 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005356
5357 /*
5358 * Free each page after tx, so the only ref left is
5359 * held by the network stack
5360 */
5361 for (; i > 0; i--)
5362 put_page(pages[i-1]);
5363
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005364 /*
5365 * Acl update can result in inode attribute update.
5366 * so mark the attribute cache invalid.
5367 */
5368 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005369 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5370 | NFS_INO_INVALID_CTIME;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005371 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005372 nfs_access_zap_cache(inode);
5373 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005374 return ret;
5375}
5376
Trond Myklebust16b42892006-08-24 12:27:15 -04005377static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5378{
5379 struct nfs4_exception exception = { };
5380 int err;
5381 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005382 err = __nfs4_proc_set_acl(inode, buf, buflen);
5383 trace_nfs4_set_acl(inode, err);
5384 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005385 &exception);
5386 } while (exception.retry);
5387 return err;
5388}
5389
David Quigleyaa9c2662013-05-22 12:50:44 -04005390#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5391static int _nfs4_get_security_label(struct inode *inode, void *buf,
5392 size_t buflen)
5393{
5394 struct nfs_server *server = NFS_SERVER(inode);
5395 struct nfs_fattr fattr;
5396 struct nfs4_label label = {0, 0, buflen, buf};
5397
5398 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005399 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005400 .fh = NFS_FH(inode),
5401 .bitmask = bitmask,
5402 };
5403 struct nfs4_getattr_res res = {
5404 .fattr = &fattr,
5405 .label = &label,
5406 .server = server,
5407 };
5408 struct rpc_message msg = {
5409 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005410 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005411 .rpc_resp = &res,
5412 };
5413 int ret;
5414
5415 nfs_fattr_init(&fattr);
5416
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005417 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005418 if (ret)
5419 return ret;
5420 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5421 return -ENOENT;
5422 if (buflen < label.len)
5423 return -ERANGE;
5424 return 0;
5425}
5426
5427static int nfs4_get_security_label(struct inode *inode, void *buf,
5428 size_t buflen)
5429{
5430 struct nfs4_exception exception = { };
5431 int err;
5432
5433 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5434 return -EOPNOTSUPP;
5435
5436 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005437 err = _nfs4_get_security_label(inode, buf, buflen);
5438 trace_nfs4_get_security_label(inode, err);
5439 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005440 &exception);
5441 } while (exception.retry);
5442 return err;
5443}
5444
5445static int _nfs4_do_set_security_label(struct inode *inode,
5446 struct nfs4_label *ilabel,
5447 struct nfs_fattr *fattr,
5448 struct nfs4_label *olabel)
5449{
5450
5451 struct iattr sattr = {0};
5452 struct nfs_server *server = NFS_SERVER(inode);
5453 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005454 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005455 .fh = NFS_FH(inode),
5456 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005457 .server = server,
5458 .bitmask = bitmask,
5459 .label = ilabel,
5460 };
5461 struct nfs_setattrres res = {
5462 .fattr = fattr,
5463 .label = olabel,
5464 .server = server,
5465 };
5466 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005467 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5468 .rpc_argp = &arg,
5469 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005470 };
5471 int status;
5472
Jeff Layton12207f62013-11-01 10:49:32 -04005473 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005474
Jeff Layton12207f62013-11-01 10:49:32 -04005475 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005476 if (status)
5477 dprintk("%s failed: %d\n", __func__, status);
5478
5479 return status;
5480}
5481
5482static int nfs4_do_set_security_label(struct inode *inode,
5483 struct nfs4_label *ilabel,
5484 struct nfs_fattr *fattr,
5485 struct nfs4_label *olabel)
5486{
5487 struct nfs4_exception exception = { };
5488 int err;
5489
5490 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005491 err = _nfs4_do_set_security_label(inode, ilabel,
5492 fattr, olabel);
5493 trace_nfs4_set_security_label(inode, err);
5494 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005495 &exception);
5496 } while (exception.retry);
5497 return err;
5498}
5499
5500static int
Al Viro59301222016-05-27 10:19:30 -04005501nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005502{
5503 struct nfs4_label ilabel, *olabel = NULL;
5504 struct nfs_fattr fattr;
5505 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005506 int status;
5507
5508 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5509 return -EOPNOTSUPP;
5510
5511 nfs_fattr_init(&fattr);
5512
5513 ilabel.pi = 0;
5514 ilabel.lfs = 0;
5515 ilabel.label = (char *)buf;
5516 ilabel.len = buflen;
5517
5518 cred = rpc_lookup_cred();
5519 if (IS_ERR(cred))
5520 return PTR_ERR(cred);
5521
5522 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5523 if (IS_ERR(olabel)) {
5524 status = -PTR_ERR(olabel);
5525 goto out;
5526 }
5527
5528 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5529 if (status == 0)
5530 nfs_setsecurity(inode, &fattr, olabel);
5531
5532 nfs4_label_free(olabel);
5533out:
5534 put_rpccred(cred);
5535 return status;
5536}
5537#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5538
5539
Chuck Leverf0920752012-05-21 22:45:41 -04005540static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5541 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005542{
5543 __be32 verf[2];
5544
Chuck Lever2c820d92012-05-21 22:45:33 -04005545 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5546 /* An impossible timestamp guarantees this value
5547 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005548 verf[0] = cpu_to_be32(U32_MAX);
5549 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005550 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005551 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005552 u64 ns = ktime_to_ns(nn->boot_time);
5553
5554 verf[0] = cpu_to_be32(ns >> 32);
5555 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005556 }
Chuck Levercd937102012-03-02 17:14:31 -05005557 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5558}
5559
Jeff Laytona3192682015-06-09 19:43:59 -04005560static int
5561nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005562{
Jeff Laytona3192682015-06-09 19:43:59 -04005563 size_t len;
5564 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005565
Trond Myklebustceb3a162015-01-03 15:16:04 -05005566 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005567 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005568
Jeff Laytona3192682015-06-09 19:43:59 -04005569 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005570 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005571 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5572 1 +
5573 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5574 1;
5575 rcu_read_unlock();
5576
5577 if (len > NFS4_OPAQUE_LIMIT + 1)
5578 return -EINVAL;
5579
5580 /*
5581 * Since this string is allocated at mount time, and held until the
5582 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5583 * about a memory-reclaim deadlock.
5584 */
5585 str = kmalloc(len, GFP_KERNEL);
5586 if (!str)
5587 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005588
Chuck Levere984a552012-09-14 17:24:21 -04005589 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005590 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005591 clp->cl_ipaddr,
5592 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5593 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005594 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005595
Jeff Laytona3192682015-06-09 19:43:59 -04005596 clp->cl_owner_id = str;
5597 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005598}
5599
Jeff Layton873e3852015-06-09 19:44:00 -04005600static int
5601nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005602{
Jeff Layton873e3852015-06-09 19:44:00 -04005603 size_t len;
5604 char *str;
5605
5606 len = 10 + 10 + 1 + 10 + 1 +
5607 strlen(nfs4_client_id_uniquifier) + 1 +
5608 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5609
5610 if (len > NFS4_OPAQUE_LIMIT + 1)
5611 return -EINVAL;
5612
5613 /*
5614 * Since this string is allocated at mount time, and held until the
5615 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5616 * about a memory-reclaim deadlock.
5617 */
5618 str = kmalloc(len, GFP_KERNEL);
5619 if (!str)
5620 return -ENOMEM;
5621
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005622 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005623 clp->rpc_ops->version, clp->cl_minorversion,
5624 nfs4_client_id_uniquifier,
5625 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005626 clp->cl_owner_id = str;
5627 return 0;
5628}
5629
5630static int
5631nfs4_init_uniform_client_string(struct nfs_client *clp)
5632{
Jeff Layton873e3852015-06-09 19:44:00 -04005633 size_t len;
5634 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005635
5636 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005637 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005638
5639 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005640 return nfs4_init_uniquifier_client_string(clp);
5641
5642 len = 10 + 10 + 1 + 10 + 1 +
5643 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5644
5645 if (len > NFS4_OPAQUE_LIMIT + 1)
5646 return -EINVAL;
5647
5648 /*
5649 * Since this string is allocated at mount time, and held until the
5650 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5651 * about a memory-reclaim deadlock.
5652 */
5653 str = kmalloc(len, GFP_KERNEL);
5654 if (!str)
5655 return -ENOMEM;
5656
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005657 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005658 clp->rpc_ops->version, clp->cl_minorversion,
5659 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005660 clp->cl_owner_id = str;
5661 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005662}
5663
Chuck Lever706cb8d2014-03-12 12:51:47 -04005664/*
5665 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5666 * services. Advertise one based on the address family of the
5667 * clientaddr.
5668 */
5669static unsigned int
5670nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5671{
5672 if (strchr(clp->cl_ipaddr, ':') != NULL)
5673 return scnprintf(buf, len, "tcp6");
5674 else
5675 return scnprintf(buf, len, "tcp");
5676}
5677
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005678static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5679{
5680 struct nfs4_setclientid *sc = calldata;
5681
5682 if (task->tk_status == 0)
5683 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5684}
5685
5686static const struct rpc_call_ops nfs4_setclientid_ops = {
5687 .rpc_call_done = nfs4_setclientid_done,
5688};
5689
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005690/**
5691 * nfs4_proc_setclientid - Negotiate client ID
5692 * @clp: state data structure
5693 * @program: RPC program for NFSv4 callback service
5694 * @port: IP port number for NFS4 callback service
5695 * @cred: RPC credential to use for this call
5696 * @res: where to place the result
5697 *
5698 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5699 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005700int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5701 unsigned short port, struct rpc_cred *cred,
5702 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703{
5704 nfs4_verifier sc_verifier;
5705 struct nfs4_setclientid setclientid = {
5706 .sc_verifier = &sc_verifier,
5707 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005708 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005709 };
5710 struct rpc_message msg = {
5711 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5712 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005713 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005714 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005715 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005716 struct rpc_task *task;
5717 struct rpc_task_setup task_setup_data = {
5718 .rpc_client = clp->cl_rpcclient,
5719 .rpc_message = &msg,
5720 .callback_ops = &nfs4_setclientid_ops,
5721 .callback_data = &setclientid,
5722 .flags = RPC_TASK_TIMEOUT,
5723 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005724 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005725
Chuck Leverde734832012-07-11 16:30:50 -04005726 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005727 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005728
5729 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5730 status = nfs4_init_uniform_client_string(clp);
5731 else
Jeff Laytona3192682015-06-09 19:43:59 -04005732 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005733
5734 if (status)
5735 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005736
Chuck Leverde734832012-07-11 16:30:50 -04005737 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005738 setclientid.sc_netid_len =
5739 nfs4_init_callback_netid(clp,
5740 setclientid.sc_netid,
5741 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005742 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005743 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005744 clp->cl_ipaddr, port >> 8, port & 255);
5745
Jeff Layton3a6bb732015-06-09 19:43:57 -04005746 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005747 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005748 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005749 task = rpc_run_task(&task_setup_data);
5750 if (IS_ERR(task)) {
5751 status = PTR_ERR(task);
5752 goto out;
5753 }
5754 status = task->tk_status;
5755 if (setclientid.sc_cred) {
5756 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5757 put_rpccred(setclientid.sc_cred);
5758 }
5759 rpc_put_task(task);
5760out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005761 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005762 dprintk("NFS reply setclientid: %d\n", status);
5763 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005764}
5765
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005766/**
5767 * nfs4_proc_setclientid_confirm - Confirm client ID
5768 * @clp: state data structure
5769 * @res: result of a previous SETCLIENTID
5770 * @cred: RPC credential to use for this call
5771 *
5772 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5773 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005774int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005775 struct nfs4_setclientid_res *arg,
5776 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005777{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005778 struct rpc_message msg = {
5779 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005780 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005781 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005782 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005783 int status;
5784
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005785 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5786 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5787 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005788 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005789 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005790 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005791 return status;
5792}
5793
Trond Myklebustfe650402006-01-03 09:55:18 +01005794struct nfs4_delegreturndata {
5795 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005796 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005797 struct nfs_fh fh;
5798 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005799 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005800 struct {
5801 struct nfs4_layoutreturn_args arg;
5802 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005803 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005804 u32 roc_barrier;
5805 bool roc;
5806 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005807 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005808 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005809 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005810};
5811
Trond Myklebustfe650402006-01-03 09:55:18 +01005812static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5813{
5814 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005815 struct nfs4_exception exception = {
5816 .inode = data->inode,
5817 .stateid = &data->stateid,
5818 };
Andy Adamson938e1012009-04-01 09:22:28 -04005819
Trond Myklebust14516c32010-07-31 14:29:06 -04005820 if (!nfs4_sequence_done(task, &data->res.seq_res))
5821 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005822
Trond Myklebustca8acf82013-08-13 10:36:56 -04005823 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005824
5825 /* Handle Layoutreturn errors */
5826 if (data->args.lr_args && task->tk_status != 0) {
5827 switch(data->res.lr_ret) {
5828 default:
5829 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5830 break;
5831 case 0:
5832 data->args.lr_args = NULL;
5833 data->res.lr_res = NULL;
5834 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005835 case -NFS4ERR_OLD_STATEID:
5836 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005837 data->inode))
5838 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005839 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005840 case -NFS4ERR_ADMIN_REVOKED:
5841 case -NFS4ERR_DELEG_REVOKED:
5842 case -NFS4ERR_EXPIRED:
5843 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005844 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5845 case -NFS4ERR_WRONG_CRED:
5846 data->args.lr_args = NULL;
5847 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005848 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005849 }
5850 }
5851
Ricardo Labiaga79708862009-12-07 09:23:21 -05005852 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005853 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005854 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005855 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005856 case -NFS4ERR_ADMIN_REVOKED:
5857 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005858 case -NFS4ERR_EXPIRED:
5859 nfs4_free_revoked_stateid(data->res.server,
5860 data->args.stateid,
5861 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005862 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005863 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005864 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005865 task->tk_status = 0;
5866 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005867 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005868 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5869 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005870 task->tk_status = 0;
5871 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005872 case -NFS4ERR_ACCESS:
5873 if (data->args.bitmask) {
5874 data->args.bitmask = NULL;
5875 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005876 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005877 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005878 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005879 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005880 task->tk_status = nfs4_async_handle_exception(task,
5881 data->res.server, task->tk_status,
5882 &exception);
5883 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005884 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005885 }
5886 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005887 return;
5888lr_restart:
5889 data->res.lr_ret = 0;
5890out_restart:
5891 task->tk_status = 0;
5892 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005893}
5894
5895static void nfs4_delegreturn_release(void *calldata)
5896{
Peng Tao039b7562014-07-03 13:05:02 +08005897 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005898 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005899
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005900 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005901 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005902 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5903 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005904 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005905 nfs_iput_and_deactive(inode);
5906 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005907 kfree(calldata);
5908}
5909
Andy Adamson938e1012009-04-01 09:22:28 -04005910static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5911{
5912 struct nfs4_delegreturndata *d_data;
5913
5914 d_data = (struct nfs4_delegreturndata *)data;
5915
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005916 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005917 return;
5918
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005919 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005920 &d_data->args.seq_args,
5921 &d_data->res.seq_res,
5922 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005923}
Andy Adamson938e1012009-04-01 09:22:28 -04005924
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005925static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005926 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005927 .rpc_call_done = nfs4_delegreturn_done,
5928 .rpc_release = nfs4_delegreturn_release,
5929};
5930
Trond Myklebuste6f81072008-01-24 18:14:34 -05005931static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005932{
5933 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005934 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005935 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005936 struct rpc_message msg = {
5937 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5938 .rpc_cred = cred,
5939 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005940 struct rpc_task_setup task_setup_data = {
5941 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005942 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005943 .callback_ops = &nfs4_delegreturn_ops,
5944 .flags = RPC_TASK_ASYNC,
5945 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005946 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005947
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005948 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005949 if (data == NULL)
5950 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005951 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005952
5953 nfs4_state_protect(server->nfs_client,
5954 NFS_SP4_MACH_CRED_CLEANUP,
5955 &task_setup_data.rpc_client, &msg);
5956
Trond Myklebustfe650402006-01-03 09:55:18 +01005957 data->args.fhandle = &data->fh;
5958 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005959 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005960 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005961 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005962 data->res.fattr = &data->fattr;
5963 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005964 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005965 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005966 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005967 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005968 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005969 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005970 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005971 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005972 if (data->lr.roc) {
5973 data->args.lr_args = &data->lr.arg;
5974 data->res.lr_res = &data->lr.res;
5975 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005976 } else if (data->lr.roc) {
5977 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5978 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005979 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005980
Trond Myklebustc970aa82007-07-14 15:39:59 -04005981 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005982 msg.rpc_argp = &data->args;
5983 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005984 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005985 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005986 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005987 if (!issync)
5988 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005989 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005990 if (status != 0)
5991 goto out;
5992 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005993out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005994 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005995 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005996}
5997
Trond Myklebuste6f81072008-01-24 18:14:34 -05005998int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999{
6000 struct nfs_server *server = NFS_SERVER(inode);
6001 struct nfs4_exception exception = { };
6002 int err;
6003 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006004 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006005 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006 switch (err) {
6007 case -NFS4ERR_STALE_STATEID:
6008 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006009 case 0:
6010 return 0;
6011 }
6012 err = nfs4_handle_exception(server, err, &exception);
6013 } while (exception.retry);
6014 return err;
6015}
6016
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6018{
6019 struct inode *inode = state->inode;
6020 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006021 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006022 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006023 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006024 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006026 struct nfs_lockt_res res = {
6027 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 };
6029 struct rpc_message msg = {
6030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006031 .rpc_argp = &arg,
6032 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006033 .rpc_cred = state->owner->so_cred,
6034 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 struct nfs4_lock_state *lsp;
6036 int status;
6037
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006038 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006039 status = nfs4_set_lock_state(state, request);
6040 if (status != 0)
6041 goto out;
6042 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006043 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006044 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006045 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006046 switch (status) {
6047 case 0:
6048 request->fl_type = F_UNLCK;
6049 break;
6050 case -NFS4ERR_DENIED:
6051 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006052 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006053 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006054 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006055out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 return status;
6057}
6058
6059static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6060{
6061 struct nfs4_exception exception = { };
6062 int err;
6063
6064 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006065 err = _nfs4_proc_getlk(state, cmd, request);
6066 trace_nfs4_get_lock(request, state, cmd, err);
6067 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006068 &exception);
6069 } while (exception.retry);
6070 return err;
6071}
6072
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006073struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006074 struct nfs_locku_args arg;
6075 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006076 struct nfs4_lock_state *lsp;
6077 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006078 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006079 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006080 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006081 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006082};
6083
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006084static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6085 struct nfs_open_context *ctx,
6086 struct nfs4_lock_state *lsp,
6087 struct nfs_seqid *seqid)
6088{
6089 struct nfs4_unlockdata *p;
6090 struct inode *inode = lsp->ls_state->inode;
6091
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006092 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006093 if (p == NULL)
6094 return NULL;
6095 p->arg.fh = NFS_FH(inode);
6096 p->arg.fl = &p->fl;
6097 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006098 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006099 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006100 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006101 /* Ensure we don't close file until we're done freeing locks! */
6102 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006103 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006104 memcpy(&p->fl, fl, sizeof(p->fl));
6105 p->server = NFS_SERVER(inode);
6106 return p;
6107}
6108
Trond Myklebust06f814a2006-01-03 09:55:07 +01006109static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006110{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006111 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006112 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006113 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006114 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006115 put_nfs_open_context(calldata->ctx);
6116 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006117}
6118
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006119static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006120{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006121 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006122 struct nfs4_exception exception = {
6123 .inode = calldata->lsp->ls_state->inode,
6124 .stateid = &calldata->arg.stateid,
6125 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006126
Trond Myklebust14516c32010-07-31 14:29:06 -04006127 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6128 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006129 switch (task->tk_status) {
6130 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006131 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006132 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006133 if (nfs4_update_lock_stateid(calldata->lsp,
6134 &calldata->res.stateid))
6135 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006136 case -NFS4ERR_ADMIN_REVOKED:
6137 case -NFS4ERR_EXPIRED:
6138 nfs4_free_revoked_stateid(calldata->server,
6139 &calldata->arg.stateid,
6140 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006141 case -NFS4ERR_BAD_STATEID:
6142 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006143 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006144 if (!nfs4_stateid_match(&calldata->arg.stateid,
6145 &calldata->lsp->ls_stateid))
6146 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006147 break;
6148 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006149 task->tk_status = nfs4_async_handle_exception(task,
6150 calldata->server, task->tk_status,
6151 &exception);
6152 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006153 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006154 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006155 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006156}
6157
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006158static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006159{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006160 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006161
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006162 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6163 nfs_async_iocounter_wait(task, calldata->l_ctx))
6164 return;
6165
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006166 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006167 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006168 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006169 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006170 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006171 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006172 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006173 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006174 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006175 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006176 &calldata->res.seq_res,
6177 task) != 0)
6178 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006179 return;
6180out_no_action:
6181 task->tk_action = NULL;
6182out_wait:
6183 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006184}
6185
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006186static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006187 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006188 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006189 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006190};
6191
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006192static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6193 struct nfs_open_context *ctx,
6194 struct nfs4_lock_state *lsp,
6195 struct nfs_seqid *seqid)
6196{
6197 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006198 struct rpc_message msg = {
6199 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6200 .rpc_cred = ctx->cred,
6201 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006202 struct rpc_task_setup task_setup_data = {
6203 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006204 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006205 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006206 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006207 .flags = RPC_TASK_ASYNC,
6208 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006209
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006210 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6211 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6212
Frank Filz137d6ac2007-07-09 15:32:29 -07006213 /* Ensure this is an unlock - when canceling a lock, the
6214 * canceled lock is passed in, and it won't be an unlock.
6215 */
6216 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006217 if (fl->fl_flags & FL_CLOSE)
6218 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006219
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006220 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6221 if (data == NULL) {
6222 nfs_free_seqid(seqid);
6223 return ERR_PTR(-ENOMEM);
6224 }
6225
Chuck Levera9c92d62013-08-09 12:48:18 -04006226 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006227 msg.rpc_argp = &data->arg;
6228 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006229 task_setup_data.callback_data = data;
6230 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006231}
6232
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6234{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006235 struct inode *inode = state->inode;
6236 struct nfs4_state_owner *sp = state->owner;
6237 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006238 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006239 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006240 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006241 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006242 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006243 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006244
Trond Myklebust9b073572006-06-29 16:38:34 -04006245 status = nfs4_set_lock_state(state, request);
6246 /* Unlock _before_ we do the RPC call */
6247 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006248 /* Exclude nfs_delegation_claim_locks() */
6249 mutex_lock(&sp->so_delegreturn_mutex);
6250 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006251 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006252 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006253 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006254 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006255 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006256 }
6257 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006258 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006259 if (status != 0)
6260 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006261 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006262 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006263 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6264 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006265 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6266 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006267 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006268 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006269 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006270 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006271 status = PTR_ERR(task);
6272 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006273 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006274 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006275 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006276out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006277 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006278 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006279 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006280}
6281
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006282struct nfs4_lockdata {
6283 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006284 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006285 struct nfs4_lock_state *lsp;
6286 struct nfs_open_context *ctx;
6287 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006288 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006289 int rpc_status;
6290 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006291 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006292};
6293
6294static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006295 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6296 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006297{
6298 struct nfs4_lockdata *p;
6299 struct inode *inode = lsp->ls_state->inode;
6300 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006301 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006302
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006303 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006304 if (p == NULL)
6305 return NULL;
6306
6307 p->arg.fh = NFS_FH(inode);
6308 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006309 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006310 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006311 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006312 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6313 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006314 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006315 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006316 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006317 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006318 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006319 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006320 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006321 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006322 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006323 p->ctx = get_nfs_open_context(ctx);
6324 memcpy(&p->fl, fl, sizeof(p->fl));
6325 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006326out_free_seqid:
6327 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006328out_free:
6329 kfree(p);
6330 return NULL;
6331}
6332
6333static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6334{
6335 struct nfs4_lockdata *data = calldata;
6336 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006337
Harvey Harrison3110ff82008-05-02 13:42:44 -07006338 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006339 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006340 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006341 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006342 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006343 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006344 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006345 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006346 nfs4_stateid_copy(&data->arg.open_stateid,
6347 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006348 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006349 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006350 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006351 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006352 nfs4_stateid_copy(&data->arg.lock_stateid,
6353 &data->lsp->ls_stateid);
6354 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006355 if (!nfs4_valid_open_stateid(state)) {
6356 data->rpc_status = -EBADF;
6357 task->tk_action = NULL;
6358 goto out_release_open_seqid;
6359 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006360 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006361 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006362 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006363 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006364 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006365 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006366out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006367 nfs_release_seqid(data->arg.open_seqid);
6368out_release_lock_seqid:
6369 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006370out_wait:
6371 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006372 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006373}
6374
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006375static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6376{
6377 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006378 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006379
Harvey Harrison3110ff82008-05-02 13:42:44 -07006380 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006381
Trond Myklebust14516c32010-07-31 14:29:06 -04006382 if (!nfs4_sequence_done(task, &data->res.seq_res))
6383 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006384
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006385 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006386 switch (task->tk_status) {
6387 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006388 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006389 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006390 if (data->arg.new_lock) {
6391 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006392 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006393 rpc_restart_call_prepare(task);
6394 break;
6395 }
6396 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006397 if (data->arg.new_lock_owner != 0) {
6398 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6399 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6400 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6401 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6402 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006403 break;
6404 case -NFS4ERR_BAD_STATEID:
6405 case -NFS4ERR_OLD_STATEID:
6406 case -NFS4ERR_STALE_STATEID:
6407 case -NFS4ERR_EXPIRED:
6408 if (data->arg.new_lock_owner != 0) {
6409 if (!nfs4_stateid_match(&data->arg.open_stateid,
6410 &lsp->ls_state->open_stateid))
6411 rpc_restart_call_prepare(task);
6412 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6413 &lsp->ls_stateid))
6414 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006415 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006416 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006417}
6418
6419static void nfs4_lock_release(void *calldata)
6420{
6421 struct nfs4_lockdata *data = calldata;
6422
Harvey Harrison3110ff82008-05-02 13:42:44 -07006423 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006424 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006425 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006426 struct rpc_task *task;
6427 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6428 data->arg.lock_seqid);
6429 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006430 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006431 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006432 } else
6433 nfs_free_seqid(data->arg.lock_seqid);
6434 nfs4_put_lock_state(data->lsp);
6435 put_nfs_open_context(data->ctx);
6436 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006437 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006438}
6439
6440static const struct rpc_call_ops nfs4_lock_ops = {
6441 .rpc_call_prepare = nfs4_lock_prepare,
6442 .rpc_call_done = nfs4_lock_done,
6443 .rpc_release = nfs4_lock_release,
6444};
6445
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006446static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6447{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006448 switch (error) {
6449 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006450 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006451 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006452 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006453 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006454 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006455 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006456 break;
6457 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006458 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006459 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006460 };
6461}
6462
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006463static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006464{
6465 struct nfs4_lockdata *data;
6466 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006467 struct rpc_message msg = {
6468 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6469 .rpc_cred = state->owner->so_cred,
6470 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006471 struct rpc_task_setup task_setup_data = {
6472 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006473 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006474 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006475 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006476 .flags = RPC_TASK_ASYNC,
6477 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006478 int ret;
6479
Harvey Harrison3110ff82008-05-02 13:42:44 -07006480 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006481 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006482 fl->fl_u.nfs4_fl.owner,
6483 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006484 if (data == NULL)
6485 return -ENOMEM;
6486 if (IS_SETLKW(cmd))
6487 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006488 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006489 msg.rpc_argp = &data->arg;
6490 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006491 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006492 if (recovery_type > NFS_LOCK_NEW) {
6493 if (recovery_type == NFS_LOCK_RECLAIM)
6494 data->arg.reclaim = NFS_LOCK_RECLAIM;
6495 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006496 } else
6497 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006498 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006499 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006500 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006501 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006502 if (ret == 0) {
6503 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006504 if (ret)
6505 nfs4_handle_setlk_error(data->server, data->lsp,
6506 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006507 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006508 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006509 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006510 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006511 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006512 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006513}
6514
6515static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6516{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006517 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006518 struct nfs4_exception exception = {
6519 .inode = state->inode,
6520 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006521 int err;
6522
6523 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006524 /* Cache the lock if possible... */
6525 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6526 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006527 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006528 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006529 break;
6530 nfs4_handle_exception(server, err, &exception);
6531 } while (exception.retry);
6532 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006533}
6534
6535static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6536{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006537 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006538 struct nfs4_exception exception = {
6539 .inode = state->inode,
6540 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006541 int err;
6542
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006543 err = nfs4_set_lock_state(state, request);
6544 if (err != 0)
6545 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006546 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006547 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6548 return 0;
6549 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006550 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006551 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6552 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006553 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006554 switch (err) {
6555 default:
6556 goto out;
6557 case -NFS4ERR_GRACE:
6558 case -NFS4ERR_DELAY:
6559 nfs4_handle_exception(server, err, &exception);
6560 err = 0;
6561 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006562 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006563out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006564 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006565}
6566
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006567#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006568static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6569{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006570 struct nfs4_lock_state *lsp;
6571 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006572
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006573 status = nfs4_set_lock_state(state, request);
6574 if (status != 0)
6575 return status;
6576 lsp = request->fl_u.nfs4_fl.owner;
6577 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6578 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6579 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006580 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006581}
6582#endif
6583
Linus Torvalds1da177e2005-04-16 15:20:36 -07006584static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6585{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006586 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006587 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006588 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006589 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590
Trond Myklebust01c3b862006-06-29 16:38:39 -04006591 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006592 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006593 if (status < 0)
6594 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006595 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006596 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006597 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006598 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006599 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006600 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006601 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006602 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006603 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006604 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006605 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006606 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006607 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006608 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006609out:
6610 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006611 return status;
6612}
6613
6614static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6615{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006616 struct nfs4_exception exception = {
6617 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006618 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006619 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006620 int err;
6621
6622 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006623 err = _nfs4_proc_setlk(state, cmd, request);
6624 if (err == -NFS4ERR_DENIED)
6625 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006626 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006627 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006628 } while (exception.retry);
6629 return err;
6630}
6631
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006632#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6633#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6634
6635static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006636nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6637 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006638{
6639 int status = -ERESTARTSYS;
6640 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6641
6642 while(!signalled()) {
6643 status = nfs4_proc_setlk(state, cmd, request);
6644 if ((status != -EAGAIN) || IS_SETLK(cmd))
6645 break;
6646 freezable_schedule_timeout_interruptible(timeout);
6647 timeout *= 2;
6648 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6649 status = -ERESTARTSYS;
6650 }
6651 return status;
6652}
6653
Jeff Laytona1d617d82016-09-17 18:17:39 -04006654#ifdef CONFIG_NFS_V4_1
6655struct nfs4_lock_waiter {
6656 struct task_struct *task;
6657 struct inode *inode;
6658 struct nfs_lowner *owner;
6659 bool notified;
6660};
6661
6662static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006663nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006664{
6665 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006666 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006667
Jeff Layton57174592018-03-18 08:37:03 -04006668 /* NULL key means to wake up everyone */
6669 if (key) {
6670 struct cb_notify_lock_args *cbnl = key;
6671 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6672 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006673
Jeff Layton57174592018-03-18 08:37:03 -04006674 /* Only wake if the callback was for the same owner. */
6675 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6676 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006677
Jeff Layton57174592018-03-18 08:37:03 -04006678 /* Make sure it's for the right inode */
6679 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6680 return 0;
6681
6682 waiter->notified = true;
6683 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006684
6685 /* override "private" so we can use default_wake_function */
6686 wait->private = waiter->task;
6687 ret = autoremove_wake_function(wait, mode, flags, key);
6688 wait->private = waiter;
6689 return ret;
6690}
6691
6692static int
6693nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6694{
6695 int status = -ERESTARTSYS;
6696 unsigned long flags;
6697 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6698 struct nfs_server *server = NFS_SERVER(state->inode);
6699 struct nfs_client *clp = server->nfs_client;
6700 wait_queue_head_t *q = &clp->cl_lock_waitq;
6701 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6702 .id = lsp->ls_seqid.owner_id,
6703 .s_dev = server->s_dev };
6704 struct nfs4_lock_waiter waiter = { .task = current,
6705 .inode = state->inode,
6706 .owner = &owner,
6707 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006708 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006709
6710 /* Don't bother with waitqueue if we don't expect a callback */
6711 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6712 return nfs4_retry_setlk_simple(state, cmd, request);
6713
6714 init_wait(&wait);
6715 wait.private = &waiter;
6716 wait.func = nfs4_wake_lock_waiter;
6717 add_wait_queue(q, &wait);
6718
6719 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006720 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006721 status = nfs4_proc_setlk(state, cmd, request);
6722 if ((status != -EAGAIN) || IS_SETLK(cmd))
6723 break;
6724
6725 status = -ERESTARTSYS;
6726 spin_lock_irqsave(&q->lock, flags);
6727 if (waiter.notified) {
6728 spin_unlock_irqrestore(&q->lock, flags);
6729 continue;
6730 }
6731 set_current_state(TASK_INTERRUPTIBLE);
6732 spin_unlock_irqrestore(&q->lock, flags);
6733
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006734 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006735 }
6736
6737 finish_wait(q, &wait);
6738 return status;
6739}
6740#else /* !CONFIG_NFS_V4_1 */
6741static inline int
6742nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6743{
6744 return nfs4_retry_setlk_simple(state, cmd, request);
6745}
6746#endif
6747
Linus Torvalds1da177e2005-04-16 15:20:36 -07006748static int
6749nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6750{
6751 struct nfs_open_context *ctx;
6752 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753 int status;
6754
6755 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006756 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757 state = ctx->state;
6758
Trond Myklebustd9531262009-07-21 19:22:38 -04006759 if (IS_GETLK(cmd)) {
6760 if (state != NULL)
6761 return nfs4_proc_getlk(state, F_GETLK, request);
6762 return 0;
6763 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006764
6765 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6766 return -EINVAL;
6767
Trond Myklebustd9531262009-07-21 19:22:38 -04006768 if (request->fl_type == F_UNLCK) {
6769 if (state != NULL)
6770 return nfs4_proc_unlck(state, cmd, request);
6771 return 0;
6772 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773
Trond Myklebustd9531262009-07-21 19:22:38 -04006774 if (state == NULL)
6775 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006776
6777 if ((request->fl_flags & FL_POSIX) &&
6778 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6779 return -ENOLCK;
6780
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006781 /*
6782 * Don't rely on the VFS having checked the file open mode,
6783 * since it won't do this for flock() locks.
6784 */
6785 switch (request->fl_type) {
6786 case F_RDLCK:
6787 if (!(filp->f_mode & FMODE_READ))
6788 return -EBADF;
6789 break;
6790 case F_WRLCK:
6791 if (!(filp->f_mode & FMODE_WRITE))
6792 return -EBADF;
6793 }
6794
Jeff Layton1ea67db2016-09-17 18:17:37 -04006795 status = nfs4_set_lock_state(state, request);
6796 if (status != 0)
6797 return status;
6798
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006799 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006800}
6801
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006802int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006803{
6804 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006805 int err;
6806
6807 err = nfs4_set_lock_state(state, fl);
6808 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006809 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006810 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006811 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006812}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006813
Trond Myklebustcf470c32012-03-07 13:49:12 -05006814struct nfs_release_lockowner_data {
6815 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006816 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006817 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006818 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006819 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006820};
6821
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006822static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6823{
6824 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006825 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006826 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6827 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006828 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006829 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006830}
6831
6832static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6833{
6834 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006835 struct nfs_server *server = data->server;
6836
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006837 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006838
6839 switch (task->tk_status) {
6840 case 0:
6841 renew_lease(server, data->timestamp);
6842 break;
6843 case -NFS4ERR_STALE_CLIENTID:
6844 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006845 nfs4_schedule_lease_recovery(server->nfs_client);
6846 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006847 case -NFS4ERR_LEASE_MOVED:
6848 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006849 if (nfs4_async_handle_error(task, server,
6850 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006851 rpc_restart_call_prepare(task);
6852 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006853}
6854
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006855static void nfs4_release_lockowner_release(void *calldata)
6856{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006857 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006858 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006859 kfree(calldata);
6860}
6861
Trond Myklebust17280172012-03-11 13:11:00 -04006862static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006863 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6864 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006865 .rpc_release = nfs4_release_lockowner_release,
6866};
6867
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006868static void
6869nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006870{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006871 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006872 struct rpc_message msg = {
6873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6874 };
6875
6876 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006877 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006878
Trond Myklebustcf470c32012-03-07 13:49:12 -05006879 data = kmalloc(sizeof(*data), GFP_NOFS);
6880 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006881 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006882 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006883 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006884 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6885 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6886 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006887
Trond Myklebustcf470c32012-03-07 13:49:12 -05006888 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006889 msg.rpc_resp = &data->res;
6890 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006891 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006892}
6893
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006894#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6895
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006896static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006897 struct dentry *unused, struct inode *inode,
6898 const char *key, const void *buf,
6899 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006900{
Al Viro59301222016-05-27 10:19:30 -04006901 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006902}
6903
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006904static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006905 struct dentry *unused, struct inode *inode,
6906 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006907{
Al Virob2968212016-04-10 20:48:24 -04006908 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006909}
6910
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006911static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006912{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006913 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006914}
6915
David Quigleyc9bccef2013-05-22 12:50:45 -04006916#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006917
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006918static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006919 struct dentry *unused, struct inode *inode,
6920 const char *key, const void *buf,
6921 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006922{
6923 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006924 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006925
6926 return -EOPNOTSUPP;
6927}
6928
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006929static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006930 struct dentry *unused, struct inode *inode,
6931 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006932{
6933 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006934 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006935 return -EOPNOTSUPP;
6936}
6937
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006938static ssize_t
6939nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006940{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006941 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006942
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006943 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6944 len = security_inode_listsecurity(inode, list, list_len);
6945 if (list_len && len > list_len)
6946 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006947 }
6948 return len;
6949}
6950
6951static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6952 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006953 .get = nfs4_xattr_get_nfs4_label,
6954 .set = nfs4_xattr_set_nfs4_label,
6955};
David Quigleyc9bccef2013-05-22 12:50:45 -04006956
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006957#else
6958
6959static ssize_t
6960nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6961{
6962 return 0;
6963}
6964
6965#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006966
Andy Adamson533eb462011-06-13 18:25:56 -04006967/*
6968 * nfs_fhget will use either the mounted_on_fileid or the fileid
6969 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006970static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6971{
Andy Adamson533eb462011-06-13 18:25:56 -04006972 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6973 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6974 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006975 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006976 return;
6977
6978 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006979 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006980 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6981 fattr->nlink = 2;
6982}
6983
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006984static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6985 const struct qstr *name,
6986 struct nfs4_fs_locations *fs_locations,
6987 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006988{
6989 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006990 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006991 struct nfs4_fs_locations_arg args = {
6992 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006993 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006994 .page = page,
6995 .bitmask = bitmask,
6996 };
Benny Halevy22958462009-04-01 09:22:02 -04006997 struct nfs4_fs_locations_res res = {
6998 .fs_locations = fs_locations,
6999 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007000 struct rpc_message msg = {
7001 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7002 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007003 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007004 };
7005 int status;
7006
Harvey Harrison3110ff82008-05-02 13:42:44 -07007007 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007008
Chuck Leverc05cefc2017-11-05 15:45:22 -05007009 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7010 bitmask[1] = nfs4_fattr_bitmap[1];
7011
Andy Adamson533eb462011-06-13 18:25:56 -04007012 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7013 * is not supported */
7014 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007015 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007016 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007017 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007018
Trond Myklebustc228fd32007-01-13 02:28:11 -05007019 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007020 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007021 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007022 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007023 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007024 return status;
7025}
7026
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007027int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7028 const struct qstr *name,
7029 struct nfs4_fs_locations *fs_locations,
7030 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007031{
7032 struct nfs4_exception exception = { };
7033 int err;
7034 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007035 err = _nfs4_proc_fs_locations(client, dir, name,
7036 fs_locations, page);
7037 trace_nfs4_get_fs_locations(dir, name, err);
7038 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007039 &exception);
7040 } while (exception.retry);
7041 return err;
7042}
7043
Chuck Leverb03d7352013-10-17 14:12:50 -04007044/*
7045 * This operation also signals the server that this client is
7046 * performing migration recovery. The server can stop returning
7047 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7048 * appended to this compound to identify the client ID which is
7049 * performing recovery.
7050 */
7051static int _nfs40_proc_get_locations(struct inode *inode,
7052 struct nfs4_fs_locations *locations,
7053 struct page *page, struct rpc_cred *cred)
7054{
7055 struct nfs_server *server = NFS_SERVER(inode);
7056 struct rpc_clnt *clnt = server->client;
7057 u32 bitmask[2] = {
7058 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7059 };
7060 struct nfs4_fs_locations_arg args = {
7061 .clientid = server->nfs_client->cl_clientid,
7062 .fh = NFS_FH(inode),
7063 .page = page,
7064 .bitmask = bitmask,
7065 .migration = 1, /* skip LOOKUP */
7066 .renew = 1, /* append RENEW */
7067 };
7068 struct nfs4_fs_locations_res res = {
7069 .fs_locations = locations,
7070 .migration = 1,
7071 .renew = 1,
7072 };
7073 struct rpc_message msg = {
7074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7075 .rpc_argp = &args,
7076 .rpc_resp = &res,
7077 .rpc_cred = cred,
7078 };
7079 unsigned long now = jiffies;
7080 int status;
7081
7082 nfs_fattr_init(&locations->fattr);
7083 locations->server = server;
7084 locations->nlocations = 0;
7085
7086 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7087 nfs4_set_sequence_privileged(&args.seq_args);
7088 status = nfs4_call_sync_sequence(clnt, server, &msg,
7089 &args.seq_args, &res.seq_res);
7090 if (status)
7091 return status;
7092
7093 renew_lease(server, now);
7094 return 0;
7095}
7096
7097#ifdef CONFIG_NFS_V4_1
7098
7099/*
7100 * This operation also signals the server that this client is
7101 * performing migration recovery. The server can stop asserting
7102 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7103 * performing this operation is identified in the SEQUENCE
7104 * operation in this compound.
7105 *
7106 * When the client supports GETATTR(fs_locations_info), it can
7107 * be plumbed in here.
7108 */
7109static int _nfs41_proc_get_locations(struct inode *inode,
7110 struct nfs4_fs_locations *locations,
7111 struct page *page, struct rpc_cred *cred)
7112{
7113 struct nfs_server *server = NFS_SERVER(inode);
7114 struct rpc_clnt *clnt = server->client;
7115 u32 bitmask[2] = {
7116 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7117 };
7118 struct nfs4_fs_locations_arg args = {
7119 .fh = NFS_FH(inode),
7120 .page = page,
7121 .bitmask = bitmask,
7122 .migration = 1, /* skip LOOKUP */
7123 };
7124 struct nfs4_fs_locations_res res = {
7125 .fs_locations = locations,
7126 .migration = 1,
7127 };
7128 struct rpc_message msg = {
7129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7130 .rpc_argp = &args,
7131 .rpc_resp = &res,
7132 .rpc_cred = cred,
7133 };
7134 int status;
7135
7136 nfs_fattr_init(&locations->fattr);
7137 locations->server = server;
7138 locations->nlocations = 0;
7139
7140 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7141 nfs4_set_sequence_privileged(&args.seq_args);
7142 status = nfs4_call_sync_sequence(clnt, server, &msg,
7143 &args.seq_args, &res.seq_res);
7144 if (status == NFS4_OK &&
7145 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7146 status = -NFS4ERR_LEASE_MOVED;
7147 return status;
7148}
7149
7150#endif /* CONFIG_NFS_V4_1 */
7151
7152/**
7153 * nfs4_proc_get_locations - discover locations for a migrated FSID
7154 * @inode: inode on FSID that is migrating
7155 * @locations: result of query
7156 * @page: buffer
7157 * @cred: credential to use for this operation
7158 *
7159 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7160 * operation failed, or a negative errno if a local error occurred.
7161 *
7162 * On success, "locations" is filled in, but if the server has
7163 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7164 * asserted.
7165 *
7166 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7167 * from this client that require migration recovery.
7168 */
7169int nfs4_proc_get_locations(struct inode *inode,
7170 struct nfs4_fs_locations *locations,
7171 struct page *page, struct rpc_cred *cred)
7172{
7173 struct nfs_server *server = NFS_SERVER(inode);
7174 struct nfs_client *clp = server->nfs_client;
7175 const struct nfs4_mig_recovery_ops *ops =
7176 clp->cl_mvops->mig_recovery_ops;
7177 struct nfs4_exception exception = { };
7178 int status;
7179
7180 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7181 (unsigned long long)server->fsid.major,
7182 (unsigned long long)server->fsid.minor,
7183 clp->cl_hostname);
7184 nfs_display_fhandle(NFS_FH(inode), __func__);
7185
7186 do {
7187 status = ops->get_locations(inode, locations, page, cred);
7188 if (status != -NFS4ERR_DELAY)
7189 break;
7190 nfs4_handle_exception(server, status, &exception);
7191 } while (exception.retry);
7192 return status;
7193}
7194
Chuck Lever44c99932013-10-17 14:13:30 -04007195/*
7196 * This operation also signals the server that this client is
7197 * performing "lease moved" recovery. The server can stop
7198 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7199 * is appended to this compound to identify the client ID which is
7200 * performing recovery.
7201 */
7202static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7203{
7204 struct nfs_server *server = NFS_SERVER(inode);
7205 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7206 struct rpc_clnt *clnt = server->client;
7207 struct nfs4_fsid_present_arg args = {
7208 .fh = NFS_FH(inode),
7209 .clientid = clp->cl_clientid,
7210 .renew = 1, /* append RENEW */
7211 };
7212 struct nfs4_fsid_present_res res = {
7213 .renew = 1,
7214 };
7215 struct rpc_message msg = {
7216 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7217 .rpc_argp = &args,
7218 .rpc_resp = &res,
7219 .rpc_cred = cred,
7220 };
7221 unsigned long now = jiffies;
7222 int status;
7223
7224 res.fh = nfs_alloc_fhandle();
7225 if (res.fh == NULL)
7226 return -ENOMEM;
7227
7228 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7229 nfs4_set_sequence_privileged(&args.seq_args);
7230 status = nfs4_call_sync_sequence(clnt, server, &msg,
7231 &args.seq_args, &res.seq_res);
7232 nfs_free_fhandle(res.fh);
7233 if (status)
7234 return status;
7235
7236 do_renew_lease(clp, now);
7237 return 0;
7238}
7239
7240#ifdef CONFIG_NFS_V4_1
7241
7242/*
7243 * This operation also signals the server that this client is
7244 * performing "lease moved" recovery. The server can stop asserting
7245 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7246 * this operation is identified in the SEQUENCE operation in this
7247 * compound.
7248 */
7249static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7250{
7251 struct nfs_server *server = NFS_SERVER(inode);
7252 struct rpc_clnt *clnt = server->client;
7253 struct nfs4_fsid_present_arg args = {
7254 .fh = NFS_FH(inode),
7255 };
7256 struct nfs4_fsid_present_res res = {
7257 };
7258 struct rpc_message msg = {
7259 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7260 .rpc_argp = &args,
7261 .rpc_resp = &res,
7262 .rpc_cred = cred,
7263 };
7264 int status;
7265
7266 res.fh = nfs_alloc_fhandle();
7267 if (res.fh == NULL)
7268 return -ENOMEM;
7269
7270 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7271 nfs4_set_sequence_privileged(&args.seq_args);
7272 status = nfs4_call_sync_sequence(clnt, server, &msg,
7273 &args.seq_args, &res.seq_res);
7274 nfs_free_fhandle(res.fh);
7275 if (status == NFS4_OK &&
7276 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7277 status = -NFS4ERR_LEASE_MOVED;
7278 return status;
7279}
7280
7281#endif /* CONFIG_NFS_V4_1 */
7282
7283/**
7284 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7285 * @inode: inode on FSID to check
7286 * @cred: credential to use for this operation
7287 *
7288 * Server indicates whether the FSID is present, moved, or not
7289 * recognized. This operation is necessary to clear a LEASE_MOVED
7290 * condition for this client ID.
7291 *
7292 * Returns NFS4_OK if the FSID is present on this server,
7293 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7294 * NFS4ERR code if some error occurred on the server, or a
7295 * negative errno if a local failure occurred.
7296 */
7297int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7298{
7299 struct nfs_server *server = NFS_SERVER(inode);
7300 struct nfs_client *clp = server->nfs_client;
7301 const struct nfs4_mig_recovery_ops *ops =
7302 clp->cl_mvops->mig_recovery_ops;
7303 struct nfs4_exception exception = { };
7304 int status;
7305
7306 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7307 (unsigned long long)server->fsid.major,
7308 (unsigned long long)server->fsid.minor,
7309 clp->cl_hostname);
7310 nfs_display_fhandle(NFS_FH(inode), __func__);
7311
7312 do {
7313 status = ops->fsid_present(inode, cred);
7314 if (status != -NFS4ERR_DELAY)
7315 break;
7316 nfs4_handle_exception(server, status, &exception);
7317 } while (exception.retry);
7318 return status;
7319}
7320
Andy Adamson5ec16a82013-08-08 10:57:55 -04007321/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007322 * If 'use_integrity' is true and the state managment nfs_client
7323 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7324 * and the machine credential as per RFC3530bis and RFC5661 Security
7325 * Considerations sections. Otherwise, just use the user cred with the
7326 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007327 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007328static 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 +00007329{
7330 int status;
7331 struct nfs4_secinfo_arg args = {
7332 .dir_fh = NFS_FH(dir),
7333 .name = name,
7334 };
7335 struct nfs4_secinfo_res res = {
7336 .flavors = flavors,
7337 };
7338 struct rpc_message msg = {
7339 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7340 .rpc_argp = &args,
7341 .rpc_resp = &res,
7342 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007343 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007344 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007345
7346 if (use_integrity) {
7347 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007348 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7349 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007350 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007351
7352 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007353
7354 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7355 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7356
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007357 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7358 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007359 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007360
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007361 if (cred)
7362 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007363
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007364 return status;
7365}
7366
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007367int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7368 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007369{
7370 struct nfs4_exception exception = { };
7371 int err;
7372 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007373 err = -NFS4ERR_WRONGSEC;
7374
7375 /* try to use integrity protection with machine cred */
7376 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7377 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7378
7379 /*
7380 * if unable to use integrity protection, or SECINFO with
7381 * integrity protection returns NFS4ERR_WRONGSEC (which is
7382 * disallowed by spec, but exists in deployed servers) use
7383 * the current filesystem's rpc_client and the user cred.
7384 */
7385 if (err == -NFS4ERR_WRONGSEC)
7386 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7387
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007388 trace_nfs4_secinfo(dir, name, err);
7389 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007390 &exception);
7391 } while (exception.retry);
7392 return err;
7393}
7394
Andy Adamson557134a2009-04-01 09:21:53 -04007395#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007396/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007397 * Check the exchange flags returned by the server for invalid flags, having
7398 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7399 * DS flags set.
7400 */
7401static int nfs4_check_cl_exchange_flags(u32 flags)
7402{
7403 if (flags & ~EXCHGID4_FLAG_MASK_R)
7404 goto out_inval;
7405 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7406 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7407 goto out_inval;
7408 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7409 goto out_inval;
7410 return NFS_OK;
7411out_inval:
7412 return -NFS4ERR_INVAL;
7413}
7414
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007415static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007416nfs41_same_server_scope(struct nfs41_server_scope *a,
7417 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007418{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007419 if (a->server_scope_sz != b->server_scope_sz)
7420 return false;
7421 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007422}
7423
Andy Adamson02a95de2016-02-05 16:08:37 -05007424static void
7425nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7426{
7427}
7428
7429static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7430 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7431};
7432
Andy Adamson357f54d2010-12-14 10:11:57 -05007433/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007434 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007435 *
7436 * The 4.1 client currently uses the same TCP connection for the
7437 * fore and backchannel.
7438 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007439static
7440int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7441 struct rpc_xprt *xprt,
7442 struct nfs_client *clp,
7443 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007444{
7445 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007446 struct nfs41_bind_conn_to_session_args args = {
7447 .client = clp,
7448 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7449 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007450 struct nfs41_bind_conn_to_session_res res;
7451 struct rpc_message msg = {
7452 .rpc_proc =
7453 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007454 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007455 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007456 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007457 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007458 struct rpc_task_setup task_setup_data = {
7459 .rpc_client = clnt,
7460 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007461 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007462 .rpc_message = &msg,
7463 .flags = RPC_TASK_TIMEOUT,
7464 };
7465 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007466
Trond Myklebust71a097c2015-02-18 09:27:18 -08007467 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7468 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7469 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007470
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007471 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7472 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7473 args.dir = NFS4_CDFC4_FORE;
7474
7475 task = rpc_run_task(&task_setup_data);
7476 if (!IS_ERR(task)) {
7477 status = task->tk_status;
7478 rpc_put_task(task);
7479 } else
7480 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007481 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007482 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007483 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007484 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7485 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007486 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007487 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007488 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007489 dprintk("NFS: %s: Unexpected direction from server\n",
7490 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007491 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007492 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007493 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007494 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7495 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007496 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007497 }
7498 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007499
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007500 return status;
7501}
7502
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007503struct rpc_bind_conn_calldata {
7504 struct nfs_client *clp;
7505 struct rpc_cred *cred;
7506};
7507
7508static int
7509nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7510 struct rpc_xprt *xprt,
7511 void *calldata)
7512{
7513 struct rpc_bind_conn_calldata *p = calldata;
7514
7515 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7516}
7517
7518int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7519{
7520 struct rpc_bind_conn_calldata data = {
7521 .clp = clp,
7522 .cred = cred,
7523 };
7524 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7525 nfs4_proc_bind_conn_to_session_callback, &data);
7526}
7527
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007528/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007529 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7530 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007531 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007532static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7533 .how = SP4_MACH_CRED,
7534 .enforce.u.words = {
7535 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7536 1 << (OP_EXCHANGE_ID - 32) |
7537 1 << (OP_CREATE_SESSION - 32) |
7538 1 << (OP_DESTROY_SESSION - 32) |
7539 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007540 },
7541 .allow.u.words = {
7542 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007543 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007544 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007545 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007546 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007547 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007548 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007549 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007550 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007551 1 << (OP_FREE_STATEID - 32) |
7552 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007553 }
7554};
7555
7556/*
7557 * Select the state protection mode for client `clp' given the server results
7558 * from exchange_id in `sp'.
7559 *
7560 * Returns 0 on success, negative errno otherwise.
7561 */
7562static int nfs4_sp4_select_mode(struct nfs_client *clp,
7563 struct nfs41_state_protection *sp)
7564{
7565 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7566 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7567 1 << (OP_EXCHANGE_ID - 32) |
7568 1 << (OP_CREATE_SESSION - 32) |
7569 1 << (OP_DESTROY_SESSION - 32) |
7570 1 << (OP_DESTROY_CLIENTID - 32)
7571 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007572 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007573 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007574 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007575
7576 if (sp->how == SP4_MACH_CRED) {
7577 /* Print state protect result */
7578 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7579 for (i = 0; i <= LAST_NFS4_OP; i++) {
7580 if (test_bit(i, sp->enforce.u.longs))
7581 dfprintk(MOUNT, " enforce op %d\n", i);
7582 if (test_bit(i, sp->allow.u.longs))
7583 dfprintk(MOUNT, " allow op %d\n", i);
7584 }
7585
7586 /* make sure nothing is on enforce list that isn't supported */
7587 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7588 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7589 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007590 ret = -EINVAL;
7591 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007592 }
7593 }
7594
7595 /*
7596 * Minimal mode - state operations are allowed to use machine
7597 * credential. Note this already happens by default, so the
7598 * client doesn't have to do anything more than the negotiation.
7599 *
7600 * NOTE: we don't care if EXCHANGE_ID is in the list -
7601 * we're already using the machine cred for exchange_id
7602 * and will never use a different cred.
7603 */
7604 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7605 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7606 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7607 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7608 dfprintk(MOUNT, "sp4_mach_cred:\n");
7609 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007610 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007611 } else {
7612 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007613 ret = -EINVAL;
7614 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007615 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007616
7617 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007618 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7619 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007620 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7621 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007622 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007623 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007624
Andrew Elble99ade3c2015-12-02 09:39:51 -05007625 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7626 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007627 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007628 }
7629
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007630 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7631 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7632 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007633 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007634 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007635
7636 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7637 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7638 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007639 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007640 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007641
7642 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7643 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007644 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007645 }
7646
7647 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7648 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007649 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007650 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007651 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007652out:
7653 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007654 return 0;
7655}
7656
Andy Adamson8d89bd72016-09-09 09:22:18 -04007657struct nfs41_exchange_id_data {
7658 struct nfs41_exchange_id_res res;
7659 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007660};
7661
Andy Adamson8d89bd72016-09-09 09:22:18 -04007662static void nfs4_exchange_id_release(void *data)
7663{
7664 struct nfs41_exchange_id_data *cdata =
7665 (struct nfs41_exchange_id_data *)data;
7666
Olga Kornievskaia63513232017-03-13 10:36:19 -04007667 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007668 kfree(cdata->res.impl_id);
7669 kfree(cdata->res.server_scope);
7670 kfree(cdata->res.server_owner);
7671 kfree(cdata);
7672}
7673
7674static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007675 .rpc_release = nfs4_exchange_id_release,
7676};
7677
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007678/*
7679 * _nfs4_proc_exchange_id()
7680 *
7681 * Wrapper for EXCHANGE_ID operation.
7682 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007683static struct rpc_task *
7684nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007685 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007686{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007687 struct rpc_message msg = {
7688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007689 .rpc_cred = cred,
7690 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007691 struct rpc_task_setup task_setup_data = {
7692 .rpc_client = clp->cl_rpcclient,
7693 .callback_ops = &nfs4_exchange_id_call_ops,
7694 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007695 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007696 };
7697 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007698 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007699
Elena Reshetova212bf412017-10-20 12:53:38 +03007700 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007701 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007702
Trond Myklebust9c760d12017-07-31 18:38:50 -04007703 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007704 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007705 if (!calldata)
7706 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007707
Trond Myklebustfd405592017-08-01 16:02:47 -04007708 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007709
7710 status = nfs4_init_uniform_client_string(clp);
7711 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007712 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007713
Andy Adamson8d89bd72016-09-09 09:22:18 -04007714 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7715 GFP_NOFS);
7716 status = -ENOMEM;
7717 if (unlikely(calldata->res.server_owner == NULL))
7718 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007719
Andy Adamson8d89bd72016-09-09 09:22:18 -04007720 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007721 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007722 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007723 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007724
Andy Adamson8d89bd72016-09-09 09:22:18 -04007725 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7726 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007727 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007728
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007729 switch (sp4_how) {
7730 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007731 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007732 break;
7733
7734 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007735 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007736 break;
7737
7738 default:
7739 /* unsupported! */
7740 WARN_ON_ONCE(1);
7741 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007742 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007743 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007744 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007745 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007746 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007747 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7748 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007749 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007750 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007751 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7752 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007753#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007754 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007755#endif
7756 msg.rpc_argp = &calldata->args;
7757 msg.rpc_resp = &calldata->res;
7758 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007759
Trond Myklebust9c760d12017-07-31 18:38:50 -04007760 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007761
7762out_impl_id:
7763 kfree(calldata->res.impl_id);
7764out_server_scope:
7765 kfree(calldata->res.server_scope);
7766out_server_owner:
7767 kfree(calldata->res.server_owner);
7768out_calldata:
7769 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007770out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007771 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007772 return ERR_PTR(status);
7773}
7774
7775/*
7776 * _nfs4_proc_exchange_id()
7777 *
7778 * Wrapper for EXCHANGE_ID operation.
7779 */
7780static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7781 u32 sp4_how)
7782{
7783 struct rpc_task *task;
7784 struct nfs41_exchange_id_args *argp;
7785 struct nfs41_exchange_id_res *resp;
7786 int status;
7787
7788 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7789 if (IS_ERR(task))
7790 return PTR_ERR(task);
7791
7792 argp = task->tk_msg.rpc_argp;
7793 resp = task->tk_msg.rpc_resp;
7794 status = task->tk_status;
7795 if (status != 0)
7796 goto out;
7797
7798 status = nfs4_check_cl_exchange_flags(resp->flags);
7799 if (status != 0)
7800 goto out;
7801
7802 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7803 if (status != 0)
7804 goto out;
7805
7806 clp->cl_clientid = resp->clientid;
7807 clp->cl_exchange_flags = resp->flags;
7808 clp->cl_seqid = resp->seqid;
7809 /* Client ID is not confirmed */
7810 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7811 clear_bit(NFS4_SESSION_ESTABLISHED,
7812 &clp->cl_session->session_state);
7813
7814 if (clp->cl_serverscope != NULL &&
7815 !nfs41_same_server_scope(clp->cl_serverscope,
7816 resp->server_scope)) {
7817 dprintk("%s: server_scope mismatch detected\n",
7818 __func__);
7819 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7820 }
7821
7822 swap(clp->cl_serverowner, resp->server_owner);
7823 swap(clp->cl_serverscope, resp->server_scope);
7824 swap(clp->cl_implid, resp->impl_id);
7825
7826 /* Save the EXCHANGE_ID verifier session trunk tests */
7827 memcpy(clp->cl_confirm.data, argp->verifier.data,
7828 sizeof(clp->cl_confirm.data));
7829out:
7830 trace_nfs4_exchange_id(clp, status);
7831 rpc_put_task(task);
7832 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007833}
7834
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007835/*
7836 * nfs4_proc_exchange_id()
7837 *
7838 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7839 *
7840 * Since the clientid has expired, all compounds using sessions
7841 * associated with the stale clientid will be returning
7842 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7843 * be in some phase of session reset.
7844 *
7845 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7846 */
7847int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7848{
7849 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7850 int status;
7851
7852 /* try SP4_MACH_CRED if krb5i/p */
7853 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7854 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007855 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007856 if (!status)
7857 return 0;
7858 }
7859
7860 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007861 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007862}
7863
Andy Adamson04fa2c62016-09-09 09:22:29 -04007864/**
7865 * nfs4_test_session_trunk
7866 *
7867 * This is an add_xprt_test() test function called from
7868 * rpc_clnt_setup_test_and_add_xprt.
7869 *
7870 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7871 * and is dereferrenced in nfs4_exchange_id_release
7872 *
7873 * Upon success, add the new transport to the rpc_clnt
7874 *
7875 * @clnt: struct rpc_clnt to get new transport
7876 * @xprt: the rpc_xprt to test
7877 * @data: call data for _nfs4_proc_exchange_id.
7878 */
7879int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7880 void *data)
7881{
7882 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007883 struct rpc_task *task;
7884 int status;
7885
Andy Adamson04fa2c62016-09-09 09:22:29 -04007886 u32 sp4_how;
7887
7888 dprintk("--> %s try %s\n", __func__,
7889 xprt->address_strings[RPC_DISPLAY_ADDR]);
7890
7891 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7892
7893 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007894 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7895 if (IS_ERR(task))
7896 return PTR_ERR(task);
7897
7898 status = task->tk_status;
7899 if (status == 0)
7900 status = nfs4_detect_session_trunking(adata->clp,
7901 task->tk_msg.rpc_resp, xprt);
7902
7903 rpc_put_task(task);
7904 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007905}
7906EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7907
Trond Myklebust66245532012-05-25 17:18:09 -04007908static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7909 struct rpc_cred *cred)
7910{
7911 struct rpc_message msg = {
7912 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7913 .rpc_argp = clp,
7914 .rpc_cred = cred,
7915 };
7916 int status;
7917
7918 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007919 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007920 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007921 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007922 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7923 return status;
7924}
7925
7926static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7927 struct rpc_cred *cred)
7928{
7929 unsigned int loop;
7930 int ret;
7931
7932 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7933 ret = _nfs4_proc_destroy_clientid(clp, cred);
7934 switch (ret) {
7935 case -NFS4ERR_DELAY:
7936 case -NFS4ERR_CLIENTID_BUSY:
7937 ssleep(1);
7938 break;
7939 default:
7940 return ret;
7941 }
7942 }
7943 return 0;
7944}
7945
7946int nfs4_destroy_clientid(struct nfs_client *clp)
7947{
7948 struct rpc_cred *cred;
7949 int ret = 0;
7950
7951 if (clp->cl_mvops->minor_version < 1)
7952 goto out;
7953 if (clp->cl_exchange_flags == 0)
7954 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007955 if (clp->cl_preserve_clid)
7956 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007957 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007958 ret = nfs4_proc_destroy_clientid(clp, cred);
7959 if (cred)
7960 put_rpccred(cred);
7961 switch (ret) {
7962 case 0:
7963 case -NFS4ERR_STALE_CLIENTID:
7964 clp->cl_exchange_flags = 0;
7965 }
7966out:
7967 return ret;
7968}
7969
Andy Adamson2050f0c2009-04-01 09:22:30 -04007970struct nfs4_get_lease_time_data {
7971 struct nfs4_get_lease_time_args *args;
7972 struct nfs4_get_lease_time_res *res;
7973 struct nfs_client *clp;
7974};
7975
7976static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7977 void *calldata)
7978{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007979 struct nfs4_get_lease_time_data *data =
7980 (struct nfs4_get_lease_time_data *)calldata;
7981
7982 dprintk("--> %s\n", __func__);
7983 /* just setup sequence, do not trigger session recovery
7984 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007985 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007986 &data->args->la_seq_args,
7987 &data->res->lr_seq_res,
7988 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007989 dprintk("<-- %s\n", __func__);
7990}
7991
7992/*
7993 * Called from nfs4_state_manager thread for session setup, so don't recover
7994 * from sequence operation or clientid errors.
7995 */
7996static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7997{
7998 struct nfs4_get_lease_time_data *data =
7999 (struct nfs4_get_lease_time_data *)calldata;
8000
8001 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008002 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8003 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008004 switch (task->tk_status) {
8005 case -NFS4ERR_DELAY:
8006 case -NFS4ERR_GRACE:
8007 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8008 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8009 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008010 /* fall through */
8011 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008012 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008013 return;
8014 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008015 dprintk("<-- %s\n", __func__);
8016}
8017
Trond Myklebust17280172012-03-11 13:11:00 -04008018static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008019 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8020 .rpc_call_done = nfs4_get_lease_time_done,
8021};
8022
8023int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8024{
8025 struct rpc_task *task;
8026 struct nfs4_get_lease_time_args args;
8027 struct nfs4_get_lease_time_res res = {
8028 .lr_fsinfo = fsinfo,
8029 };
8030 struct nfs4_get_lease_time_data data = {
8031 .args = &args,
8032 .res = &res,
8033 .clp = clp,
8034 };
8035 struct rpc_message msg = {
8036 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8037 .rpc_argp = &args,
8038 .rpc_resp = &res,
8039 };
8040 struct rpc_task_setup task_setup = {
8041 .rpc_client = clp->cl_rpcclient,
8042 .rpc_message = &msg,
8043 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008044 .callback_data = &data,
8045 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008046 };
8047 int status;
8048
Chuck Levera9c92d62013-08-09 12:48:18 -04008049 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008050 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008051 task = rpc_run_task(&task_setup);
8052
8053 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008054 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008055
Anna Schumakerf6148712017-04-07 14:15:23 -04008056 status = task->tk_status;
8057 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008058 return status;
8059}
8060
Andy Adamsonfc931582009-04-01 09:22:31 -04008061/*
8062 * Initialize the values to be used by the client in CREATE_SESSION
8063 * If nfs4_init_session set the fore channel request and response sizes,
8064 * use them.
8065 *
8066 * Set the back channel max_resp_sz_cached to zero to force the client to
8067 * always set csa_cachethis to FALSE because the current implementation
8068 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8069 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008070static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8071 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008072{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008073 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008074 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008075
Andy Adamson18aad3d2013-06-26 12:21:49 -04008076 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8077 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8078
Andy Adamsonfc931582009-04-01 09:22:31 -04008079 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008080 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8081 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008082 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008083 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008084
8085 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008086 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008087 __func__,
8088 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008089 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008090
8091 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008092 args->bc_attrs.max_rqst_sz = max_bc_payload;
8093 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008094 args->bc_attrs.max_resp_sz_cached = 0;
8095 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008096 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008097
8098 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8099 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8100 __func__,
8101 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8102 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8103 args->bc_attrs.max_reqs);
8104}
8105
Trond Myklebust79969dd2015-02-18 11:30:18 -08008106static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8107 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008108{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008109 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008110 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008111
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008112 if (rcvd->max_resp_sz > sent->max_resp_sz)
8113 return -EINVAL;
8114 /*
8115 * Our requested max_ops is the minimum we need; we're not
8116 * prepared to break up compounds into smaller pieces than that.
8117 * So, no point even trying to continue if the server won't
8118 * cooperate:
8119 */
8120 if (rcvd->max_ops < sent->max_ops)
8121 return -EINVAL;
8122 if (rcvd->max_reqs == 0)
8123 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008124 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8125 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008126 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008127}
8128
Trond Myklebust79969dd2015-02-18 11:30:18 -08008129static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8130 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008131{
8132 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008133 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008134
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008135 if (!(res->flags & SESSION4_BACK_CHAN))
8136 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008137 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8138 return -EINVAL;
8139 if (rcvd->max_resp_sz < sent->max_resp_sz)
8140 return -EINVAL;
8141 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8142 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008143 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008144 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008145 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008146 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008147out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008148 return 0;
8149}
Andy Adamson8d353012009-04-01 09:22:32 -04008150
Andy Adamson8d353012009-04-01 09:22:32 -04008151static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008152 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008153{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008154 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008155
Trond Myklebust79969dd2015-02-18 11:30:18 -08008156 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008157 if (ret)
8158 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008159 return nfs4_verify_back_channel_attrs(args, res);
8160}
8161
8162static void nfs4_update_session(struct nfs4_session *session,
8163 struct nfs41_create_session_res *res)
8164{
8165 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008166 /* Mark client id and session as being confirmed */
8167 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8168 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008169 session->flags = res->flags;
8170 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008171 if (res->flags & SESSION4_BACK_CHAN)
8172 memcpy(&session->bc_attrs, &res->bc_attrs,
8173 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008174}
8175
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008176static int _nfs4_proc_create_session(struct nfs_client *clp,
8177 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008178{
8179 struct nfs4_session *session = clp->cl_session;
8180 struct nfs41_create_session_args args = {
8181 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008182 .clientid = clp->cl_clientid,
8183 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008184 .cb_program = NFS4_CALLBACK,
8185 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008186 struct nfs41_create_session_res res;
8187
Andy Adamsonfc931582009-04-01 09:22:31 -04008188 struct rpc_message msg = {
8189 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8190 .rpc_argp = &args,
8191 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008192 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008193 };
8194 int status;
8195
Chuck Lever6b26cc82016-05-02 14:40:40 -04008196 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008197 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008198
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008199 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008200 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008201
Trond Myklebustb519d402016-09-11 14:50:01 -04008202 switch (status) {
8203 case -NFS4ERR_STALE_CLIENTID:
8204 case -NFS4ERR_DELAY:
8205 case -ETIMEDOUT:
8206 case -EACCES:
8207 case -EAGAIN:
8208 goto out;
8209 };
8210
8211 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008212 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008213 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008214 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008215 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008216 if (status)
8217 goto out;
8218 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008219 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008220out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008221 return status;
8222}
8223
8224/*
8225 * Issues a CREATE_SESSION operation to the server.
8226 * It is the responsibility of the caller to verify the session is
8227 * expired before calling this routine.
8228 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008229int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008230{
8231 int status;
8232 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008233 struct nfs4_session *session = clp->cl_session;
8234
8235 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8236
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008237 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008238 if (status)
8239 goto out;
8240
Andy Adamsonaacd5532011-11-09 13:58:21 -05008241 /* Init or reset the session slot tables */
8242 status = nfs4_setup_session_slot_tables(session);
8243 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008244 if (status)
8245 goto out;
8246
8247 ptr = (unsigned *)&session->sess_id.data[0];
8248 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8249 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008250out:
8251 dprintk("<-- %s\n", __func__);
8252 return status;
8253}
8254
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008255/*
8256 * Issue the over-the-wire RPC DESTROY_SESSION.
8257 * The caller must serialize access to this routine.
8258 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008259int nfs4_proc_destroy_session(struct nfs4_session *session,
8260 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008261{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008262 struct rpc_message msg = {
8263 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8264 .rpc_argp = session,
8265 .rpc_cred = cred,
8266 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008267 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008268
8269 dprintk("--> nfs4_proc_destroy_session\n");
8270
8271 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008272 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8273 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008274
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008275 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008276 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008277
8278 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008279 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008280 "Session has been destroyed regardless...\n", status);
8281
8282 dprintk("<-- nfs4_proc_destroy_session\n");
8283 return status;
8284}
8285
Trond Myklebust7b38c362012-05-23 13:23:31 -04008286/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008287 * Renew the cl_session lease.
8288 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008289struct nfs4_sequence_data {
8290 struct nfs_client *clp;
8291 struct nfs4_sequence_args args;
8292 struct nfs4_sequence_res res;
8293};
8294
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008295static void nfs41_sequence_release(void *data)
8296{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008297 struct nfs4_sequence_data *calldata = data;
8298 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008299
Elena Reshetova212bf412017-10-20 12:53:38 +03008300 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008301 nfs4_schedule_state_renewal(clp);
8302 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008303 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008304}
8305
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008306static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8307{
8308 switch(task->tk_status) {
8309 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008310 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8311 return -EAGAIN;
8312 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008313 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008314 }
8315 return 0;
8316}
8317
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008318static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008319{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008320 struct nfs4_sequence_data *calldata = data;
8321 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008322
Trond Myklebust14516c32010-07-31 14:29:06 -04008323 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8324 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008325
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008326 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008327 if (task->tk_status < 0) {
8328 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008329 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008330 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008331
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008332 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8333 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008334 return;
8335 }
8336 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008337 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008338out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008339 dprintk("<-- %s\n", __func__);
8340}
8341
8342static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8343{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008344 struct nfs4_sequence_data *calldata = data;
8345 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008346 struct nfs4_sequence_args *args;
8347 struct nfs4_sequence_res *res;
8348
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008349 args = task->tk_msg.rpc_argp;
8350 res = task->tk_msg.rpc_resp;
8351
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008352 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008353}
8354
8355static const struct rpc_call_ops nfs41_sequence_ops = {
8356 .rpc_call_done = nfs41_sequence_call_done,
8357 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008358 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008359};
8360
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008361static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8362 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008363 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008364 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008365{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008366 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008367 struct rpc_message msg = {
8368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8369 .rpc_cred = cred,
8370 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008371 struct rpc_task_setup task_setup_data = {
8372 .rpc_client = clp->cl_rpcclient,
8373 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008374 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008375 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008376 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008377 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008378
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008379 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008380 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008381 goto out_err;
8382
8383 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008384 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008385 if (calldata == NULL)
8386 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008387 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008388 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008389 if (is_privileged)
8390 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008391 msg.rpc_argp = &calldata->args;
8392 msg.rpc_resp = &calldata->res;
8393 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008394 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008395
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008396 ret = rpc_run_task(&task_setup_data);
8397 if (IS_ERR(ret))
8398 goto out_err;
8399 return ret;
8400out_put_clp:
8401 nfs_put_client(clp);
8402out_err:
8403 nfs41_release_slot(slot);
8404 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008405}
8406
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008407static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008408{
8409 struct rpc_task *task;
8410 int ret = 0;
8411
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008412 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008413 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008414 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008415 if (IS_ERR(task))
8416 ret = PTR_ERR(task);
8417 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008418 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008419 dprintk("<-- %s status=%d\n", __func__, ret);
8420 return ret;
8421}
8422
8423static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8424{
8425 struct rpc_task *task;
8426 int ret;
8427
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008428 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008429 if (IS_ERR(task)) {
8430 ret = PTR_ERR(task);
8431 goto out;
8432 }
8433 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008434 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008435 ret = task->tk_status;
8436 rpc_put_task(task);
8437out:
8438 dprintk("<-- %s status=%d\n", __func__, ret);
8439 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008440}
8441
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008442struct nfs4_reclaim_complete_data {
8443 struct nfs_client *clp;
8444 struct nfs41_reclaim_complete_args arg;
8445 struct nfs41_reclaim_complete_res res;
8446};
8447
8448static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8449{
8450 struct nfs4_reclaim_complete_data *calldata = data;
8451
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008452 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008453 &calldata->arg.seq_args,
8454 &calldata->res.seq_res,
8455 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008456}
8457
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008458static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8459{
8460 switch(task->tk_status) {
8461 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008462 wake_up_all(&clp->cl_lock_waitq);
8463 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008464 case -NFS4ERR_COMPLETE_ALREADY:
8465 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8466 break;
8467 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008468 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008469 /* fall through */
8470 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008471 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008472 case -NFS4ERR_BADSESSION:
8473 case -NFS4ERR_DEADSESSION:
8474 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8475 nfs4_schedule_session_recovery(clp->cl_session,
8476 task->tk_status);
8477 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008478 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008479 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008480 }
8481 return 0;
8482}
8483
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008484static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8485{
8486 struct nfs4_reclaim_complete_data *calldata = data;
8487 struct nfs_client *clp = calldata->clp;
8488 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8489
8490 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008491 if (!nfs41_sequence_done(task, res))
8492 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008493
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008494 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008495 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8496 rpc_restart_call_prepare(task);
8497 return;
8498 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008499 dprintk("<-- %s\n", __func__);
8500}
8501
8502static void nfs4_free_reclaim_complete_data(void *data)
8503{
8504 struct nfs4_reclaim_complete_data *calldata = data;
8505
8506 kfree(calldata);
8507}
8508
8509static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8510 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8511 .rpc_call_done = nfs4_reclaim_complete_done,
8512 .rpc_release = nfs4_free_reclaim_complete_data,
8513};
8514
8515/*
8516 * Issue a global reclaim complete.
8517 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008518static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8519 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008520{
8521 struct nfs4_reclaim_complete_data *calldata;
8522 struct rpc_task *task;
8523 struct rpc_message msg = {
8524 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008525 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008526 };
8527 struct rpc_task_setup task_setup_data = {
8528 .rpc_client = clp->cl_rpcclient,
8529 .rpc_message = &msg,
8530 .callback_ops = &nfs4_reclaim_complete_call_ops,
8531 .flags = RPC_TASK_ASYNC,
8532 };
8533 int status = -ENOMEM;
8534
8535 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008536 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008537 if (calldata == NULL)
8538 goto out;
8539 calldata->clp = clp;
8540 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008541
Chuck Levera9c92d62013-08-09 12:48:18 -04008542 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008543 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008544 msg.rpc_argp = &calldata->arg;
8545 msg.rpc_resp = &calldata->res;
8546 task_setup_data.callback_data = calldata;
8547 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008548 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008549 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008550 goto out;
8551 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008552 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008553 if (status == 0)
8554 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008555 rpc_put_task(task);
8556out:
8557 dprintk("<-- %s status=%d\n", __func__, status);
8558 return status;
8559}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008560
8561static void
8562nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8563{
8564 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008565 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008566
8567 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008568 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008569 &lgp->res.seq_res, task);
8570 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008571}
8572
8573static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8574{
8575 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008576
8577 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008578 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008579 dprintk("<-- %s\n", __func__);
8580}
8581
8582static int
8583nfs4_layoutget_handle_exception(struct rpc_task *task,
8584 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8585{
Trond Myklebustee314c22012-10-01 17:25:48 -07008586 struct inode *inode = lgp->args.inode;
8587 struct nfs_server *server = NFS_SERVER(inode);
8588 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008589 int nfs4err = task->tk_status;
8590 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008591 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008592
Boaz Harroshed7e5422014-01-22 20:34:54 +02008593 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008594
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008595 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008596 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008597 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008598
8599 /*
8600 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8601 * on the file. set tk_status to -ENODATA to tell upper layer to
8602 * retry go inband.
8603 */
8604 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008605 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008606 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008607 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008608 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8609 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8610 */
8611 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008612 status = -EOVERFLOW;
8613 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008614 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008615 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008616 * (or clients) writing to the same RAID stripe except when
8617 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008618 *
8619 * Treat it like we would RECALLCONFLICT -- we retry for a little
8620 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008621 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008622 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008623 if (lgp->args.minlength == 0) {
8624 status = -EOVERFLOW;
8625 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008626 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008627 status = -EBUSY;
8628 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008629 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008630 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008631 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008632 case -NFS4ERR_DELEG_REVOKED:
8633 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008634 case -NFS4ERR_EXPIRED:
8635 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008636 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008637 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008638 lo = NFS_I(inode)->layout;
8639 /* If the open stateid was bad, then recover it. */
8640 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008641 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008642 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008643 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008644 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008645 break;
8646 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008647
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008648 /*
8649 * Mark the bad layout state as invalid, then retry
8650 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008651 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008652 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008653 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008654 pnfs_free_lseg_list(&head);
8655 status = -EAGAIN;
8656 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008657 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008658
Trond Myklebust8ac09252017-01-23 22:44:12 -05008659 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008660 err = nfs4_handle_exception(server, nfs4err, exception);
8661 if (!status) {
8662 if (exception->retry)
8663 status = -EAGAIN;
8664 else
8665 status = err;
8666 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008667out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008668 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008669 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008670}
8671
Idan Kedar85541162012-08-02 11:47:10 +03008672static size_t max_response_pages(struct nfs_server *server)
8673{
8674 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8675 return nfs_page_array_len(0, max_resp_sz);
8676}
8677
8678static void nfs4_free_pages(struct page **pages, size_t size)
8679{
8680 int i;
8681
8682 if (!pages)
8683 return;
8684
8685 for (i = 0; i < size; i++) {
8686 if (!pages[i])
8687 break;
8688 __free_page(pages[i]);
8689 }
8690 kfree(pages);
8691}
8692
8693static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8694{
8695 struct page **pages;
8696 int i;
8697
8698 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8699 if (!pages) {
8700 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8701 return NULL;
8702 }
8703
8704 for (i = 0; i < size; i++) {
8705 pages[i] = alloc_page(gfp_flags);
8706 if (!pages[i]) {
8707 dprintk("%s: failed to allocate page\n", __func__);
8708 nfs4_free_pages(pages, size);
8709 return NULL;
8710 }
8711 }
8712
8713 return pages;
8714}
8715
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008716static void nfs4_layoutget_release(void *calldata)
8717{
8718 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008719 struct inode *inode = lgp->args.inode;
8720 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008721 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008722
8723 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008724 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008725 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008726 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008727 put_nfs_open_context(lgp->args.ctx);
8728 kfree(calldata);
8729 dprintk("<-- %s\n", __func__);
8730}
8731
8732static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8733 .rpc_call_prepare = nfs4_layoutget_prepare,
8734 .rpc_call_done = nfs4_layoutget_done,
8735 .rpc_release = nfs4_layoutget_release,
8736};
8737
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008738struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008739nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008740{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008741 struct inode *inode = lgp->args.inode;
8742 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008743 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008744 struct rpc_task *task;
8745 struct rpc_message msg = {
8746 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8747 .rpc_argp = &lgp->args,
8748 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008749 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008750 };
8751 struct rpc_task_setup task_setup_data = {
8752 .rpc_client = server->client,
8753 .rpc_message = &msg,
8754 .callback_ops = &nfs4_layoutget_call_ops,
8755 .callback_data = lgp,
8756 .flags = RPC_TASK_ASYNC,
8757 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008758 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008759 struct nfs4_exception exception = {
8760 .inode = inode,
8761 .timeout = *timeout,
8762 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008763 int status = 0;
8764
8765 dprintk("--> %s\n", __func__);
8766
Peng Tao4bd5a982014-11-17 11:05:17 +08008767 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8768 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8769
Idan Kedar85541162012-08-02 11:47:10 +03008770 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8771 if (!lgp->args.layout.pages) {
8772 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008773 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008774 }
8775 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8776
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008777 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008778 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008779 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008780
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008781 task = rpc_run_task(&task_setup_data);
8782 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008783 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008784 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008785 if (status == 0) {
8786 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8787 *timeout = exception.timeout;
8788 }
8789
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008790 trace_nfs4_layoutget(lgp->args.ctx,
8791 &lgp->args.range,
8792 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008793 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008794 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008795
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008796 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8797 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008798 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008799 rpc_put_task(task);
8800 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008801 if (status)
8802 return ERR_PTR(status);
8803 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008804}
8805
Benny Halevycbe82602011-05-22 19:52:37 +03008806static void
8807nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8808{
8809 struct nfs4_layoutreturn *lrp = calldata;
8810
8811 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008812 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008813 &lrp->args.seq_args,
8814 &lrp->res.seq_res,
8815 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008816}
8817
8818static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8819{
8820 struct nfs4_layoutreturn *lrp = calldata;
8821 struct nfs_server *server;
8822
8823 dprintk("--> %s\n", __func__);
8824
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008825 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008826 return;
8827
8828 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008829 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008830 case -NFS4ERR_OLD_STATEID:
8831 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8832 lrp->args.inode))
8833 goto out_restart;
8834 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008835 default:
8836 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008837 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008838 case 0:
8839 break;
8840 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008841 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008842 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008843 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008844 }
Benny Halevycbe82602011-05-22 19:52:37 +03008845 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008846 return;
8847out_restart:
8848 task->tk_status = 0;
8849 nfs4_sequence_free_slot(&lrp->res.seq_res);
8850 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008851}
8852
8853static void nfs4_layoutreturn_release(void *calldata)
8854{
8855 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008856 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008857
8858 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008859 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008860 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008861 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008862 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8863 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008864 pnfs_put_layout_hdr(lrp->args.layout);
8865 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008866 kfree(calldata);
8867 dprintk("<-- %s\n", __func__);
8868}
8869
8870static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8871 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8872 .rpc_call_done = nfs4_layoutreturn_done,
8873 .rpc_release = nfs4_layoutreturn_release,
8874};
8875
Peng Tao6c166052014-11-17 09:30:40 +08008876int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008877{
8878 struct rpc_task *task;
8879 struct rpc_message msg = {
8880 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8881 .rpc_argp = &lrp->args,
8882 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008883 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008884 };
8885 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008886 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008887 .rpc_message = &msg,
8888 .callback_ops = &nfs4_layoutreturn_call_ops,
8889 .callback_data = lrp,
8890 };
Peng Tao6c166052014-11-17 09:30:40 +08008891 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008892
Andrew Elble99ade3c2015-12-02 09:39:51 -05008893 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8894 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8895 &task_setup_data.rpc_client, &msg);
8896
Benny Halevycbe82602011-05-22 19:52:37 +03008897 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008898 if (!sync) {
8899 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8900 if (!lrp->inode) {
8901 nfs4_layoutreturn_release(lrp);
8902 return -EAGAIN;
8903 }
8904 task_setup_data.flags |= RPC_TASK_ASYNC;
8905 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008906 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008907 task = rpc_run_task(&task_setup_data);
8908 if (IS_ERR(task))
8909 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008910 if (sync)
8911 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008912 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008913 dprintk("<-- %s status=%d\n", __func__, status);
8914 rpc_put_task(task);
8915 return status;
8916}
8917
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008918static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008919_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8920 struct pnfs_device *pdev,
8921 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008922{
8923 struct nfs4_getdeviceinfo_args args = {
8924 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008925 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8926 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008927 };
8928 struct nfs4_getdeviceinfo_res res = {
8929 .pdev = pdev,
8930 };
8931 struct rpc_message msg = {
8932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8933 .rpc_argp = &args,
8934 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008935 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008936 };
8937 int status;
8938
8939 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008940 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008941 if (res.notification & ~args.notify_types)
8942 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008943 if (res.notification != args.notify_types)
8944 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008945
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008946 dprintk("<-- %s status=%d\n", __func__, status);
8947
8948 return status;
8949}
8950
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008951int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8952 struct pnfs_device *pdev,
8953 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008954{
8955 struct nfs4_exception exception = { };
8956 int err;
8957
8958 do {
8959 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008960 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008961 &exception);
8962 } while (exception.retry);
8963 return err;
8964}
8965EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8966
Andy Adamson863a3c62011-03-23 13:27:54 +00008967static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8968{
8969 struct nfs4_layoutcommit_data *data = calldata;
8970 struct nfs_server *server = NFS_SERVER(data->args.inode);
8971
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008972 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008973 &data->args.seq_args,
8974 &data->res.seq_res,
8975 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008976}
8977
8978static void
8979nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8980{
8981 struct nfs4_layoutcommit_data *data = calldata;
8982 struct nfs_server *server = NFS_SERVER(data->args.inode);
8983
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008984 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008985 return;
8986
8987 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008988 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8989 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8990 case -NFS4ERR_BADLAYOUT: /* no layout */
8991 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008992 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008993 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008994 break;
8995 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008996 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008997 rpc_restart_call_prepare(task);
8998 return;
8999 }
9000 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009001}
9002
9003static void nfs4_layoutcommit_release(void *calldata)
9004{
9005 struct nfs4_layoutcommit_data *data = calldata;
9006
Andy Adamsondb29c082011-07-30 20:52:38 -04009007 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009008 nfs_post_op_update_inode_force_wcc(data->args.inode,
9009 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009010 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009011 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009012 kfree(data);
9013}
9014
9015static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9016 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9017 .rpc_call_done = nfs4_layoutcommit_done,
9018 .rpc_release = nfs4_layoutcommit_release,
9019};
9020
9021int
Andy Adamsonef311532011-03-12 02:58:10 -05009022nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009023{
9024 struct rpc_message msg = {
9025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9026 .rpc_argp = &data->args,
9027 .rpc_resp = &data->res,
9028 .rpc_cred = data->cred,
9029 };
9030 struct rpc_task_setup task_setup_data = {
9031 .task = &data->task,
9032 .rpc_client = NFS_CLIENT(data->args.inode),
9033 .rpc_message = &msg,
9034 .callback_ops = &nfs4_layoutcommit_ops,
9035 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009036 };
9037 struct rpc_task *task;
9038 int status = 0;
9039
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009040 dprintk("NFS: initiating layoutcommit call. sync %d "
9041 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009042 data->args.lastbytewritten,
9043 data->args.inode->i_ino);
9044
Trond Myklebust472e2592015-02-05 16:50:30 -05009045 if (!sync) {
9046 data->inode = nfs_igrab_and_active(data->args.inode);
9047 if (data->inode == NULL) {
9048 nfs4_layoutcommit_release(data);
9049 return -EAGAIN;
9050 }
9051 task_setup_data.flags = RPC_TASK_ASYNC;
9052 }
Chuck Levera9c92d62013-08-09 12:48:18 -04009053 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00009054 task = rpc_run_task(&task_setup_data);
9055 if (IS_ERR(task))
9056 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009057 if (sync)
9058 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009059 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009060 dprintk("%s: status %d\n", __func__, status);
9061 rpc_put_task(task);
9062 return status;
9063}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009064
Andy Adamson97431202013-08-08 10:57:56 -04009065/**
9066 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9067 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9068 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009069static int
9070_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009071 struct nfs_fsinfo *info,
9072 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009073{
9074 struct nfs41_secinfo_no_name_args args = {
9075 .style = SECINFO_STYLE_CURRENT_FH,
9076 };
9077 struct nfs4_secinfo_res res = {
9078 .flavors = flavors,
9079 };
9080 struct rpc_message msg = {
9081 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9082 .rpc_argp = &args,
9083 .rpc_resp = &res,
9084 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009085 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009086 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009087 int status;
9088
9089 if (use_integrity) {
9090 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009091 cred = nfs4_get_clid_cred(server->nfs_client);
9092 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009093 }
9094
9095 dprintk("--> %s\n", __func__);
9096 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9097 &res.seq_res, 0);
9098 dprintk("<-- %s status=%d\n", __func__, status);
9099
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009100 if (cred)
9101 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009102
9103 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009104}
9105
9106static int
9107nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9108 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9109{
9110 struct nfs4_exception exception = { };
9111 int err;
9112 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009113 /* first try using integrity protection */
9114 err = -NFS4ERR_WRONGSEC;
9115
9116 /* try to use integrity protection with machine cred */
9117 if (_nfs4_is_integrity_protected(server->nfs_client))
9118 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9119 flavors, true);
9120
9121 /*
9122 * if unable to use integrity protection, or SECINFO with
9123 * integrity protection returns NFS4ERR_WRONGSEC (which is
9124 * disallowed by spec, but exists in deployed servers) use
9125 * the current filesystem's rpc_client and the user cred.
9126 */
9127 if (err == -NFS4ERR_WRONGSEC)
9128 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9129 flavors, false);
9130
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009131 switch (err) {
9132 case 0:
9133 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009134 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009135 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009136 default:
9137 err = nfs4_handle_exception(server, err, &exception);
9138 }
9139 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009140out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009141 return err;
9142}
9143
9144static int
9145nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9146 struct nfs_fsinfo *info)
9147{
9148 int err;
9149 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009150 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009151 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009152 struct nfs4_secinfo4 *secinfo;
9153 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009154
9155 page = alloc_page(GFP_KERNEL);
9156 if (!page) {
9157 err = -ENOMEM;
9158 goto out;
9159 }
9160
9161 flavors = page_address(page);
9162 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9163
9164 /*
9165 * Fall back on "guess and check" method if
9166 * the server doesn't support SECINFO_NO_NAME
9167 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009168 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009169 err = nfs4_find_root_sec(server, fhandle, info);
9170 goto out_freepage;
9171 }
9172 if (err)
9173 goto out_freepage;
9174
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009175 for (i = 0; i < flavors->num_flavors; i++) {
9176 secinfo = &flavors->flavors[i];
9177
9178 switch (secinfo->flavor) {
9179 case RPC_AUTH_NULL:
9180 case RPC_AUTH_UNIX:
9181 case RPC_AUTH_GSS:
9182 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9183 &secinfo->flavor_info);
9184 break;
9185 default:
9186 flavor = RPC_AUTH_MAXFLAVOR;
9187 break;
9188 }
9189
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009190 if (!nfs_auth_info_match(&server->auth_info, flavor))
9191 flavor = RPC_AUTH_MAXFLAVOR;
9192
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009193 if (flavor != RPC_AUTH_MAXFLAVOR) {
9194 err = nfs4_lookup_root_sec(server, fhandle,
9195 info, flavor);
9196 if (!err)
9197 break;
9198 }
9199 }
9200
9201 if (flavor == RPC_AUTH_MAXFLAVOR)
9202 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009203
9204out_freepage:
9205 put_page(page);
9206 if (err == -EACCES)
9207 return -EPERM;
9208out:
9209 return err;
9210}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009211
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009212static int _nfs41_test_stateid(struct nfs_server *server,
9213 nfs4_stateid *stateid,
9214 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009215{
9216 int status;
9217 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009218 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009219 };
9220 struct nfs41_test_stateid_res res;
9221 struct rpc_message msg = {
9222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9223 .rpc_argp = &args,
9224 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009225 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009226 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009227 struct rpc_clnt *rpc_client = server->client;
9228
9229 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9230 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009231
Chuck Lever38527b12012-07-11 16:30:23 -04009232 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009233 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009234 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009235 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009236 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009237 if (status != NFS_OK) {
9238 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009239 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009240 }
9241 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009242 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009243}
9244
Trond Myklebust43912bb2016-09-22 13:38:56 -04009245static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9246 int err, struct nfs4_exception *exception)
9247{
9248 exception->retry = 0;
9249 switch(err) {
9250 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009251 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009252 nfs4_handle_exception(server, err, exception);
9253 break;
9254 case -NFS4ERR_BADSESSION:
9255 case -NFS4ERR_BADSLOT:
9256 case -NFS4ERR_BAD_HIGH_SLOT:
9257 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9258 case -NFS4ERR_DEADSESSION:
9259 nfs4_do_handle_exception(server, err, exception);
9260 }
9261}
9262
Chuck Lever38527b12012-07-11 16:30:23 -04009263/**
9264 * nfs41_test_stateid - perform a TEST_STATEID operation
9265 *
9266 * @server: server / transport on which to perform the operation
9267 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009268 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009269 *
9270 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9271 * Otherwise a negative NFS4ERR value is returned if the operation
9272 * failed or the state ID is not currently valid.
9273 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009274static int nfs41_test_stateid(struct nfs_server *server,
9275 nfs4_stateid *stateid,
9276 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009277{
9278 struct nfs4_exception exception = { };
9279 int err;
9280 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009281 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009282 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009283 } while (exception.retry);
9284 return err;
9285}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009286
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009287struct nfs_free_stateid_data {
9288 struct nfs_server *server;
9289 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009290 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009291};
9292
9293static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9294{
9295 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009296 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009297 &data->args.seq_args,
9298 &data->res.seq_res,
9299 task);
9300}
9301
9302static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9303{
9304 struct nfs_free_stateid_data *data = calldata;
9305
9306 nfs41_sequence_done(task, &data->res.seq_res);
9307
9308 switch (task->tk_status) {
9309 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009310 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009311 rpc_restart_call_prepare(task);
9312 }
9313}
9314
9315static void nfs41_free_stateid_release(void *calldata)
9316{
9317 kfree(calldata);
9318}
9319
Trond Myklebust17f26b12013-08-21 15:48:42 -04009320static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009321 .rpc_call_prepare = nfs41_free_stateid_prepare,
9322 .rpc_call_done = nfs41_free_stateid_done,
9323 .rpc_release = nfs41_free_stateid_release,
9324};
9325
9326static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009327 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009328 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009329 bool privileged)
9330{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009331 struct rpc_message msg = {
9332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009333 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009334 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009335 struct rpc_task_setup task_setup = {
9336 .rpc_client = server->client,
9337 .rpc_message = &msg,
9338 .callback_ops = &nfs41_free_stateid_ops,
9339 .flags = RPC_TASK_ASYNC,
9340 };
9341 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009342
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009343 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9344 &task_setup.rpc_client, &msg);
9345
Chuck Lever38527b12012-07-11 16:30:23 -04009346 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009347 data = kmalloc(sizeof(*data), GFP_NOFS);
9348 if (!data)
9349 return ERR_PTR(-ENOMEM);
9350 data->server = server;
9351 nfs4_stateid_copy(&data->args.stateid, stateid);
9352
9353 task_setup.callback_data = data;
9354
9355 msg.rpc_argp = &data->args;
9356 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009357 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009358 if (privileged)
9359 nfs4_set_sequence_privileged(&data->args.seq_args);
9360
9361 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009362}
9363
Chuck Lever38527b12012-07-11 16:30:23 -04009364/**
9365 * nfs41_free_stateid - perform a FREE_STATEID operation
9366 *
9367 * @server: server / transport on which to perform the operation
9368 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009369 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009370 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009371 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009372 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009373 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009374static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009375 const nfs4_stateid *stateid,
9376 struct rpc_cred *cred,
9377 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009378{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009379 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009380
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009381 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009382 if (IS_ERR(task))
9383 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009384 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009385 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009386}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009387
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009388static void
9389nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009390{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009391 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009392
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009393 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009394 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009395}
9396
Trond Myklebust36281ca2012-03-04 18:13:56 -05009397static bool nfs41_match_stateid(const nfs4_stateid *s1,
9398 const nfs4_stateid *s2)
9399{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009400 if (s1->type != s2->type)
9401 return false;
9402
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009403 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009404 return false;
9405
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009406 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009407 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009408
Anna Schumaker045c5512017-01-11 16:59:48 -05009409 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009410}
9411
Andy Adamson557134a2009-04-01 09:21:53 -04009412#endif /* CONFIG_NFS_V4_1 */
9413
Trond Myklebust36281ca2012-03-04 18:13:56 -05009414static bool nfs4_match_stateid(const nfs4_stateid *s1,
9415 const nfs4_stateid *s2)
9416{
Trond Myklebustf597c532012-03-04 18:13:56 -05009417 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009418}
9419
9420
Trond Myklebust17280172012-03-11 13:11:00 -04009421static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009422 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009423 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009424 .recover_open = nfs4_open_reclaim,
9425 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009426 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009427 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009428};
9429
Andy Adamson591d71c2009-04-01 09:22:47 -04009430#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009431static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009432 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9433 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9434 .recover_open = nfs4_open_reclaim,
9435 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009436 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009437 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009438 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009439};
9440#endif /* CONFIG_NFS_V4_1 */
9441
Trond Myklebust17280172012-03-11 13:11:00 -04009442static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009443 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009444 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009445 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009446 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009447 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009448};
9449
Andy Adamson591d71c2009-04-01 09:22:47 -04009450#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009451static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009452 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9453 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009454 .recover_open = nfs41_open_expired,
9455 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009456 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009457};
9458#endif /* CONFIG_NFS_V4_1 */
9459
Trond Myklebust17280172012-03-11 13:11:00 -04009460static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009461 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009462 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009463 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009464};
9465
9466#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009467static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009468 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009469 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009470 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009471};
9472#endif
9473
Chuck Leverec011fe2013-10-17 14:12:39 -04009474static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009475 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009476 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009477};
9478
9479#if defined(CONFIG_NFS_V4_1)
9480static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009481 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009482 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009483};
9484#endif /* CONFIG_NFS_V4_1 */
9485
Trond Myklebust97dc1352010-06-16 09:52:26 -04009486static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9487 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009488 .init_caps = NFS_CAP_READDIRPLUS
9489 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009490 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009491 .init_client = nfs40_init_client,
9492 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009493 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009494 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009495 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009496 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009497 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009498 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009499 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9500 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9501 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009502 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009503};
9504
9505#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009506static struct nfs_seqid *
9507nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9508{
9509 return NULL;
9510}
9511
Trond Myklebust97dc1352010-06-16 09:52:26 -04009512static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9513 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009514 .init_caps = NFS_CAP_READDIRPLUS
9515 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009516 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009517 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009518 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009519 .init_client = nfs41_init_client,
9520 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009521 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009522 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009523 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009524 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009525 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009526 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009527 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009528 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9529 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9530 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009531 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009532};
9533#endif
9534
Steve Dickson42c2c422013-05-22 12:50:38 -04009535#if defined(CONFIG_NFS_V4_2)
9536static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9537 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009538 .init_caps = NFS_CAP_READDIRPLUS
9539 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009540 | NFS_CAP_POSIX_LOCK
9541 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009542 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009543 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009544 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009545 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009546 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009547 | NFS_CAP_LAYOUTSTATS
9548 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009549 .init_client = nfs41_init_client,
9550 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009551 .match_stateid = nfs41_match_stateid,
9552 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009553 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009554 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009555 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009556 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009557 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009558 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9559 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9560 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009561 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009562};
9563#endif
9564
Trond Myklebust97dc1352010-06-16 09:52:26 -04009565const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9566 [0] = &nfs_v4_0_minor_ops,
9567#if defined(CONFIG_NFS_V4_1)
9568 [1] = &nfs_v4_1_minor_ops,
9569#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009570#if defined(CONFIG_NFS_V4_2)
9571 [2] = &nfs_v4_2_minor_ops,
9572#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009573};
9574
Trond Myklebust13997822016-07-24 17:10:52 -04009575static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009576{
9577 ssize_t error, error2;
9578
9579 error = generic_listxattr(dentry, list, size);
9580 if (error < 0)
9581 return error;
9582 if (list) {
9583 list += error;
9584 size -= error;
9585 }
9586
9587 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9588 if (error2 < 0)
9589 return error2;
9590 return error + error2;
9591}
9592
Trond Myklebust17f26b12013-08-21 15:48:42 -04009593static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009594 .create = nfs_create,
9595 .lookup = nfs_lookup,
9596 .atomic_open = nfs_atomic_open,
9597 .link = nfs_link,
9598 .unlink = nfs_unlink,
9599 .symlink = nfs_symlink,
9600 .mkdir = nfs_mkdir,
9601 .rmdir = nfs_rmdir,
9602 .mknod = nfs_mknod,
9603 .rename = nfs_rename,
9604 .permission = nfs_permission,
9605 .getattr = nfs_getattr,
9606 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009607 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009608};
9609
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009610static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009611 .permission = nfs_permission,
9612 .getattr = nfs_getattr,
9613 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009614 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009615};
9616
David Howells509de812006-08-22 20:06:11 -04009617const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618 .version = 4, /* protocol version */
9619 .dentry_ops = &nfs4_dentry_operations,
9620 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009621 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009622 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009624 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009625 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009626 .getattr = nfs4_proc_getattr,
9627 .setattr = nfs4_proc_setattr,
9628 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009629 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009630 .access = nfs4_proc_access,
9631 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009632 .create = nfs4_proc_create,
9633 .remove = nfs4_proc_remove,
9634 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009635 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009636 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009637 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009638 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009639 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009640 .link = nfs4_proc_link,
9641 .symlink = nfs4_proc_symlink,
9642 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009643 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644 .readdir = nfs4_proc_readdir,
9645 .mknod = nfs4_proc_mknod,
9646 .statfs = nfs4_proc_statfs,
9647 .fsinfo = nfs4_proc_fsinfo,
9648 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009649 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009650 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009651 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009653 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009654 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009655 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009657 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009658 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009660 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009661 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009662 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009663 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009664 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009665 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009666 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009667 .create_server = nfs4_create_server,
9668 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009669};
9670
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009671static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009672 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009673 .list = nfs4_xattr_list_nfs4_acl,
9674 .get = nfs4_xattr_get_nfs4_acl,
9675 .set = nfs4_xattr_set_nfs4_acl,
9676};
9677
9678const struct xattr_handler *nfs4_xattr_handlers[] = {
9679 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009680#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9681 &nfs4_xattr_nfs4_label_handler,
9682#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009683 NULL
9684};
9685
Linus Torvalds1da177e2005-04-16 15:20:36 -07009686/*
9687 * Local variables:
9688 * c-basic-offset: 8
9689 * End:
9690 */