blob: 21daeac114febd62a8b1bc9ce6fa392fe5f05369 [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 Myklebust359d7d12012-05-28 10:01:34 -04001048 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001049 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001050 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1051 nfsi->attrtimeo_timestamp = jiffies;
1052 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001054 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001055 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1056 NFS_INO_INVALID_ACL;
1057 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001058 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001059 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001060 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001061 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 spin_unlock(&dir->i_lock);
1063}
1064
1065struct nfs4_opendata {
1066 struct kref kref;
1067 struct nfs_openargs o_arg;
1068 struct nfs_openres o_res;
1069 struct nfs_open_confirmargs c_arg;
1070 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd2012-01-07 13:22:46 -05001071 struct nfs4_string owner_name;
1072 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001073 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001075 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001077 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct nfs4_state_owner *owner;
1079 struct nfs4_state *state;
1080 struct iattr attrs;
1081 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001082 bool rpc_done;
1083 bool file_created;
1084 bool is_recover;
1085 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001086 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001088
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001089struct nfs4_open_createattrs {
1090 struct nfs4_label *label;
1091 struct iattr *sattr;
1092 const __u32 verf[2];
1093};
1094
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001095static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1096 int err, struct nfs4_exception *exception)
1097{
1098 if (err != -EINVAL)
1099 return false;
1100 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1101 return false;
1102 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1103 exception->retry = 1;
1104 return true;
1105}
1106
Trond Myklebust6ae37332015-01-30 14:21:14 -05001107static u32
1108nfs4_map_atomic_open_share(struct nfs_server *server,
1109 fmode_t fmode, int openflags)
1110{
1111 u32 res = 0;
1112
1113 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1114 case FMODE_READ:
1115 res = NFS4_SHARE_ACCESS_READ;
1116 break;
1117 case FMODE_WRITE:
1118 res = NFS4_SHARE_ACCESS_WRITE;
1119 break;
1120 case FMODE_READ|FMODE_WRITE:
1121 res = NFS4_SHARE_ACCESS_BOTH;
1122 }
1123 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1124 goto out;
1125 /* Want no delegation if we're using O_DIRECT */
1126 if (openflags & O_DIRECT)
1127 res |= NFS4_SHARE_WANT_NO_DELEG;
1128out:
1129 return res;
1130}
1131
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001132static enum open_claim_type4
1133nfs4_map_atomic_open_claim(struct nfs_server *server,
1134 enum open_claim_type4 claim)
1135{
1136 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1137 return claim;
1138 switch (claim) {
1139 default:
1140 return claim;
1141 case NFS4_OPEN_CLAIM_FH:
1142 return NFS4_OPEN_CLAIM_NULL;
1143 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1144 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1145 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1146 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1147 }
1148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001151{
1152 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001153 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001154 p->o_res.seqid = p->o_arg.seqid;
1155 p->c_res.seqid = p->c_arg.seqid;
1156 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001157 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001158 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001159 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001160}
1161
Al Viro82a2c1b2011-06-22 18:30:55 -04001162static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001163 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001164 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001165 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001166 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001167{
Al Viro82a2c1b2011-06-22 18:30:55 -04001168 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001169 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001171 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001172 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 struct nfs4_opendata *p;
1174
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001175 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 if (p == NULL)
1177 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001178
1179 p->f_label = nfs4_label_alloc(server, gfp_mask);
1180 if (IS_ERR(p->f_label))
1181 goto err_free_p;
1182
Kinglong Meea49c2692015-07-27 15:31:38 +08001183 p->a_label = nfs4_label_alloc(server, gfp_mask);
1184 if (IS_ERR(p->a_label))
1185 goto err_free_f;
1186
Trond Myklebust63f5f792015-01-23 19:19:25 -05001187 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1188 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001189 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001190 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001191 nfs_sb_active(dentry->d_sb);
1192 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->dir = parent;
1194 p->owner = sp;
1195 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001196 p->o_arg.open_flags = flags;
1197 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001198 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001199 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001200 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1201 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001202 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1203 * will return permission denied for all bits until close */
1204 if (!(flags & O_EXCL)) {
1205 /* ask server to check for all possible rights as results
1206 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001207 switch (p->o_arg.claim) {
1208 default:
1209 break;
1210 case NFS4_OPEN_CLAIM_NULL:
1211 case NFS4_OPEN_CLAIM_FH:
1212 p->o_arg.access = NFS4_ACCESS_READ |
1213 NFS4_ACCESS_MODIFY |
1214 NFS4_ACCESS_EXTEND |
1215 NFS4_ACCESS_EXECUTE;
1216 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001217 }
David Howells7539bba2006-08-22 20:06:09 -04001218 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001219 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1220 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001221 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001223 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001224 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001225 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001226 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001227 case NFS4_OPEN_CLAIM_NULL:
1228 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1229 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1230 p->o_arg.fh = NFS_FH(dir);
1231 break;
1232 case NFS4_OPEN_CLAIM_PREVIOUS:
1233 case NFS4_OPEN_CLAIM_FH:
1234 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1235 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001236 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001237 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001238 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001239 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001240 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001241
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001242 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001243 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001244 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001245 p->c_arg.fh = &p->o_res.fh;
1246 p->c_arg.stateid = &p->o_res.stateid;
1247 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001248 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001249 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001250 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001251
1252err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001253 nfs4_label_free(p->a_label);
1254err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001255 nfs4_label_free(p->f_label);
1256err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 kfree(p);
1258err:
1259 dput(parent);
1260 return NULL;
1261}
1262
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001265 struct nfs4_opendata *p = container_of(kref,
1266 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001267 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001268
1269 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001270 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001271 if (p->state != NULL)
1272 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001274
Kinglong Meea49c2692015-07-27 15:31:38 +08001275 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001276 nfs4_label_free(p->f_label);
1277
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001279 dput(p->dentry);
1280 nfs_sb_deactive(sb);
Trond Myklebust6926afd2012-01-07 13:22:46 -05001281 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001282 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001283 kfree(p);
1284}
1285
1286static void nfs4_opendata_put(struct nfs4_opendata *p)
1287{
1288 if (p != NULL)
1289 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290}
1291
Trond Myklebust24311f82015-09-20 10:50:17 -04001292static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1293 fmode_t fmode)
1294{
1295 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1296 case FMODE_READ|FMODE_WRITE:
1297 return state->n_rdwr != 0;
1298 case FMODE_WRITE:
1299 return state->n_wronly != 0;
1300 case FMODE_READ:
1301 return state->n_rdonly != 0;
1302 }
1303 WARN_ON_ONCE(1);
1304 return false;
1305}
1306
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001307static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001308{
1309 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310
Trond Myklebust536e43d2012-01-17 22:04:26 -05001311 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312 goto out;
1313 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001314 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001315 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1316 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001317 break;
1318 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001319 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1320 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001321 break;
1322 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001323 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1324 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001327 return ret;
1328}
1329
Trond Myklebust2a606182015-08-19 22:30:00 -05001330static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1331 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001333 if (delegation == NULL)
1334 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001336 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001337 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1338 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001339 switch (claim) {
1340 case NFS4_OPEN_CLAIM_NULL:
1341 case NFS4_OPEN_CLAIM_FH:
1342 break;
1343 case NFS4_OPEN_CLAIM_PREVIOUS:
1344 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1345 break;
1346 default:
1347 return 0;
1348 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001349 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350 return 1;
1351}
1352
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001354{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001356 case FMODE_WRITE:
1357 state->n_wronly++;
1358 break;
1359 case FMODE_READ:
1360 state->n_rdonly++;
1361 break;
1362 case FMODE_READ|FMODE_WRITE:
1363 state->n_rdwr++;
1364 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001366}
1367
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001368#ifdef CONFIG_NFS_V4_1
1369static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1370{
1371 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1372 return true;
1373 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1374 return true;
1375 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1376 return true;
1377 return false;
1378}
1379#endif /* CONFIG_NFS_V4_1 */
1380
Trond Myklebustc9399f22017-11-06 15:28:01 -05001381static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1382{
1383 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1384 wake_up_all(&state->waitq);
1385}
1386
1387static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1388 const nfs4_stateid *stateid)
1389{
1390 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1391 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1392
1393 if (stateid_seqid == state_seqid + 1U ||
1394 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1395 nfs_state_log_update_open_stateid(state);
1396 else
1397 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1398}
1399
Trond Myklebust4f14c192014-02-12 19:15:06 -05001400static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001401{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001402 struct nfs_client *clp = state->owner->so_server->nfs_client;
1403 bool need_recover = false;
1404
1405 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1406 need_recover = true;
1407 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1410 need_recover = true;
1411 if (need_recover)
1412 nfs4_state_mark_reclaim_nograce(clp, state);
1413}
1414
Trond Myklebustc9399f22017-11-06 15:28:01 -05001415/*
1416 * Check for whether or not the caller may update the open stateid
1417 * to the value passed in by stateid.
1418 *
1419 * Note: This function relies heavily on the server implementing
1420 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1421 * correctly.
1422 * i.e. The stateid seqids have to be initialised to 1, and
1423 * are then incremented on every state transition.
1424 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001425static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001426 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001427{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001428 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1429 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1430 if (stateid->seqid == cpu_to_be32(1))
1431 nfs_state_log_update_open_stateid(state);
1432 else
1433 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001434 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001436
1437 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1438 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001439 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001440 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001441 return false;
1442}
1443
Trond Myklebustf95549c2015-01-23 18:06:09 -05001444static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1445{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001446 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1447 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001448 if (state->n_wronly)
1449 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1450 if (state->n_rdonly)
1451 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1452 if (state->n_rdwr)
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001454 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001455}
1456
Trond Myklebust226056c2014-02-11 10:41:07 -05001457static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1458 nfs4_stateid *stateid, fmode_t fmode)
1459{
1460 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1461 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1462 case FMODE_WRITE:
1463 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1464 break;
1465 case FMODE_READ:
1466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1467 break;
1468 case 0:
1469 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1470 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1471 clear_bit(NFS_OPEN_STATE, &state->flags);
1472 }
1473 if (stateid == NULL)
1474 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001475 /* Handle OPEN+OPEN_DOWNGRADE races */
1476 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1477 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001478 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001479 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001480 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001481 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001482 nfs4_stateid_copy(&state->stateid, stateid);
1483 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001484 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001485out:
1486 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001487}
1488
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001489static void nfs_clear_open_stateid(struct nfs4_state *state,
1490 nfs4_stateid *arg_stateid,
1491 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001492{
1493 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001494 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1495 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1496 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001497 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001498 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1499 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001500}
1501
Trond Myklebust1393d962016-09-22 13:39:13 -04001502static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001503 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001504{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001505 DEFINE_WAIT(wait);
1506 int status = 0;
1507 for (;;) {
1508
1509 if (!nfs_need_update_open_stateid(state, stateid))
1510 return;
1511 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001512 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001513 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001514 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001515 /* Rely on seqids for serialisation with NFSv4.0 */
1516 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1517 break;
1518
1519 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1520 /*
1521 * Ensure we process the state changes in the same order
1522 * in which the server processed them by delaying the
1523 * update of the stateid until we are in sequence.
1524 */
1525 write_sequnlock(&state->seqlock);
1526 spin_unlock(&state->owner->so_lock);
1527 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001528 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001529 if (!signal_pending(current)) {
1530 if (schedule_timeout(5*HZ) == 0)
1531 status = -EAGAIN;
1532 else
1533 status = 0;
1534 } else
1535 status = -EINTR;
1536 finish_wait(&state->waitq, &wait);
1537 rcu_read_lock();
1538 spin_lock(&state->owner->so_lock);
1539 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001540 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001541
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001542 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1543 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001544 nfs4_stateid_copy(freeme, &state->open_stateid);
1545 nfs_test_and_clear_all_open_stateid(state);
1546 }
1547
Trond Myklebuste999e802014-02-10 18:20:47 -05001548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1549 nfs4_stateid_copy(&state->stateid, stateid);
1550 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001551 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001552 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001553}
1554
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001556 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001557 fmode_t fmode,
1558 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001560 /*
1561 * Protect the call to nfs4_state_set_mode_locked and
1562 * serialise the stateid update
1563 */
1564 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001565 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1566 switch (fmode) {
1567 case FMODE_READ:
1568 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1569 break;
1570 case FMODE_WRITE:
1571 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1572 break;
1573 case FMODE_READ|FMODE_WRITE:
1574 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001575 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001576 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001577 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578}
1579
1580static void nfs_state_set_delegation(struct nfs4_state *state,
1581 const nfs4_stateid *deleg_stateid,
1582 fmode_t fmode)
1583{
1584 /*
1585 * Protect the call to nfs4_state_set_mode_locked and
1586 * serialise the stateid update
1587 */
1588 write_seqlock(&state->seqlock);
1589 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1590 set_bit(NFS_DELEGATED_STATE, &state->flags);
1591 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
Trond Myklebust1393d962016-09-22 13:39:13 -04001594static int update_open_stateid(struct nfs4_state *state,
1595 const nfs4_stateid *open_stateid,
1596 const nfs4_stateid *delegation,
1597 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001598{
Trond Myklebust1393d962016-09-22 13:39:13 -04001599 struct nfs_server *server = NFS_SERVER(state->inode);
1600 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001601 struct nfs_inode *nfsi = NFS_I(state->inode);
1602 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001603 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001604 int ret = 0;
1605
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001606 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001607
1608 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001609 spin_lock(&state->owner->so_lock);
1610 if (open_stateid != NULL) {
1611 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1612 ret = 1;
1613 }
1614
Trond Myklebust34310432008-12-23 15:21:38 -05001615 deleg_cur = rcu_dereference(nfsi->delegation);
1616 if (deleg_cur == NULL)
1617 goto no_delegation;
1618
1619 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001620 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001621 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001622 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001623 goto no_delegation_unlock;
1624
1625 if (delegation == NULL)
1626 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001627 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001628 goto no_delegation_unlock;
1629
Trond Myklebustb7391f42008-12-23 15:21:52 -05001630 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001631 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001632 ret = 1;
1633no_delegation_unlock:
1634 spin_unlock(&deleg_cur->lock);
1635no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636 if (ret)
1637 update_open_stateflags(state, fmode);
1638 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001639 rcu_read_unlock();
1640
Trond Myklebust4f14c192014-02-12 19:15:06 -05001641 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001642 nfs4_schedule_state_manager(clp);
1643 if (freeme.type != 0)
1644 nfs4_test_and_free_stateid(server, &freeme,
1645 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001646
1647 return ret;
1648}
1649
Trond Myklebust39071e62015-01-24 15:07:56 -05001650static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1651 const nfs4_stateid *stateid)
1652{
1653 struct nfs4_state *state = lsp->ls_state;
1654 bool ret = false;
1655
1656 spin_lock(&state->state_lock);
1657 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1658 goto out_noupdate;
1659 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1660 goto out_noupdate;
1661 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1662 ret = true;
1663out_noupdate:
1664 spin_unlock(&state->state_lock);
1665 return ret;
1666}
Trond Myklebust34310432008-12-23 15:21:38 -05001667
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001668static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669{
1670 struct nfs_delegation *delegation;
1671
Trond Myklebustf5086242018-03-20 16:43:13 -04001672 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001673 rcu_read_lock();
1674 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001675 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 rcu_read_unlock();
1677 return;
1678 }
1679 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001680 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681}
1682
Trond Myklebust6ee41262007-07-08 14:11:36 -04001683static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001684{
1685 struct nfs4_state *state = opendata->state;
1686 struct nfs_inode *nfsi = NFS_I(state->inode);
1687 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001688 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001690 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691 nfs4_stateid stateid;
1692 int ret = -EAGAIN;
1693
Trond Myklebustaac00a82007-07-05 19:02:21 -04001694 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001696 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001700 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001701 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001702 rcu_read_lock();
1703 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001704 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001705 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001706 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001707 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001709 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001711 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001712 if (!opendata->is_recover) {
1713 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1714 if (ret != 0)
1715 goto out;
1716 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001717 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001718
1719 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001720 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001721 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001722 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723out:
1724 return ERR_PTR(ret);
1725out_return_state:
1726 atomic_inc(&state->count);
1727 return state;
1728}
1729
Andy Adamsone23008e2012-10-02 21:07:32 -04001730static void
1731nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1732{
1733 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1734 struct nfs_delegation *delegation;
1735 int delegation_flags = 0;
1736
1737 rcu_read_lock();
1738 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1739 if (delegation)
1740 delegation_flags = delegation->flags;
1741 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001742 switch (data->o_arg.claim) {
1743 default:
1744 break;
1745 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1746 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001747 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1748 "returning a delegation for "
1749 "OPEN(CLAIM_DELEGATE_CUR)\n",
1750 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001751 return;
1752 }
1753 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001754 nfs_inode_set_delegation(state->inode,
1755 data->owner->so_cred,
1756 &data->o_res);
1757 else
1758 nfs_inode_reclaim_delegation(state->inode,
1759 data->owner->so_cred,
1760 &data->o_res);
1761}
1762
1763/*
1764 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1765 * and update the nfs4_state.
1766 */
1767static struct nfs4_state *
1768_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1769{
1770 struct inode *inode = data->state->inode;
1771 struct nfs4_state *state = data->state;
1772 int ret;
1773
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001774 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001775 if (data->rpc_status)
1776 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001777 /* cached opens have already been processed */
1778 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 }
1780
Andy Adamsone23008e2012-10-02 21:07:32 -04001781 ret = nfs_refresh_inode(inode, &data->f_attr);
1782 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001783 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001784
1785 if (data->o_res.delegation_type != 0)
1786 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001787update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001788 update_open_stateid(state, &data->o_res.stateid, NULL,
1789 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001790 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001791
1792 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001793}
1794
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001795static struct inode *
1796nfs4_opendata_get_inode(struct nfs4_opendata *data)
1797{
1798 struct inode *inode;
1799
1800 switch (data->o_arg.claim) {
1801 case NFS4_OPEN_CLAIM_NULL:
1802 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1803 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1804 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1805 return ERR_PTR(-EAGAIN);
1806 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1807 &data->f_attr, data->f_label);
1808 break;
1809 default:
1810 inode = d_inode(data->dentry);
1811 ihold(inode);
1812 nfs_refresh_inode(inode, &data->f_attr);
1813 }
1814 return inode;
1815}
1816
Andy Adamsone23008e2012-10-02 21:07:32 -04001817static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001818nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1819{
1820 struct nfs4_state *state;
1821 struct inode *inode;
1822
1823 inode = nfs4_opendata_get_inode(data);
1824 if (IS_ERR(inode))
1825 return ERR_CAST(inode);
1826 if (data->state != NULL && data->state->inode == inode) {
1827 state = data->state;
1828 atomic_inc(&state->count);
1829 } else
1830 state = nfs4_get_open_state(inode, data->owner);
1831 iput(inode);
1832 if (state == NULL)
1833 state = ERR_PTR(-ENOMEM);
1834 return state;
1835}
1836
Andy Adamsone23008e2012-10-02 21:07:32 -04001837static struct nfs4_state *
1838_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001840 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001843 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001844 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 goto out;
1846 }
1847
Trond Myklebust75e8c482017-08-08 10:38:07 -04001848 state = nfs4_opendata_find_nfs4_state(data);
1849 if (IS_ERR(state))
1850 goto out;
1851
Andy Adamsone23008e2012-10-02 21:07:32 -04001852 if (data->o_res.delegation_type != 0)
1853 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001854 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001855 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001857 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001858 return state;
1859}
1860
Andy Adamsone23008e2012-10-02 21:07:32 -04001861static struct nfs4_state *
1862nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1863{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001864 struct nfs4_state *ret;
1865
Andy Adamsone23008e2012-10-02 21:07:32 -04001866 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001867 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1868 else
1869 ret = _nfs4_opendata_to_nfs4_state(data);
1870 nfs4_sequence_free_slot(&data->o_res.seq_res);
1871 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001872}
1873
Trond Myklebust864472e2006-01-03 09:55:15 +01001874static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1875{
1876 struct nfs_inode *nfsi = NFS_I(state->inode);
1877 struct nfs_open_context *ctx;
1878
1879 spin_lock(&state->inode->i_lock);
1880 list_for_each_entry(ctx, &nfsi->open_files, list) {
1881 if (ctx->state != state)
1882 continue;
1883 get_nfs_open_context(ctx);
1884 spin_unlock(&state->inode->i_lock);
1885 return ctx;
1886 }
1887 spin_unlock(&state->inode->i_lock);
1888 return ERR_PTR(-ENOENT);
1889}
1890
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001891static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1892 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001893{
1894 struct nfs4_opendata *opendata;
1895
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001896 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001897 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001898 if (opendata == NULL)
1899 return ERR_PTR(-ENOMEM);
1900 opendata->state = state;
1901 atomic_inc(&state->count);
1902 return opendata;
1903}
1904
Trond Myklebust24311f82015-09-20 10:50:17 -04001905static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1906 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001907{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001908 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001909 int ret;
1910
Trond Myklebust24311f82015-09-20 10:50:17 -04001911 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001912 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001913 opendata->o_arg.open_flags = 0;
1914 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001915 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1916 NFS_SB(opendata->dentry->d_sb),
1917 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001918 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1919 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1920 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001921 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 if (ret != 0)
1923 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001924 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001925 if (IS_ERR(newstate))
1926 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001927 if (newstate != opendata->state)
1928 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001929 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001930 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001931}
1932
1933static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1934{
Trond Myklebust864472e2006-01-03 09:55:15 +01001935 int ret;
1936
Trond Myklebust4f14c192014-02-12 19:15:06 -05001937 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1938 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1939 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1940 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001941 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001942 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001943 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001944 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001945 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1946 if (ret != 0)
1947 return ret;
1948 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1949 if (ret != 0)
1950 return ret;
1951 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1952 if (ret != 0)
1953 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001954 /*
1955 * We may have performed cached opens for all three recoveries.
1956 * Check if we need to update the current stateid.
1957 */
1958 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001959 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001960 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001961 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001962 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001963 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001964 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001965 return 0;
1966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * OPEN_RECLAIM:
1970 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001972static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001974 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001975 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001976 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int status;
1978
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001979 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1980 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001981 if (IS_ERR(opendata))
1982 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001983 rcu_read_lock();
1984 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001985 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001986 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001987 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001988 opendata->o_arg.u.delegation_type = delegation_type;
1989 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001990 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return status;
1992}
1993
Trond Myklebust539cd032007-06-05 11:46:42 -04001994static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 struct nfs_server *server = NFS_SERVER(state->inode);
1997 struct nfs4_exception exception = { };
1998 int err;
1999 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002000 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002001 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002002 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2003 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002004 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002005 break;
2006 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 } while (exception.retry);
2008 return err;
2009}
2010
Trond Myklebust864472e2006-01-03 09:55:15 +01002011static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2012{
2013 struct nfs_open_context *ctx;
2014 int ret;
2015
2016 ctx = nfs4_state_find_open_context(state);
2017 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002018 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002019 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002020 put_nfs_open_context(ctx);
2021 return ret;
2022}
2023
NeilBrowndce26302017-12-13 09:57:09 +11002024static 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 -07002025{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002026 switch (err) {
2027 default:
2028 printk(KERN_ERR "NFS: %s: unhandled error "
2029 "%d.\n", __func__, err);
2030 case 0:
2031 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002032 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002033 case -ESTALE:
2034 break;
2035 case -NFS4ERR_BADSESSION:
2036 case -NFS4ERR_BADSLOT:
2037 case -NFS4ERR_BAD_HIGH_SLOT:
2038 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2039 case -NFS4ERR_DEADSESSION:
2040 set_bit(NFS_DELEGATED_STATE, &state->flags);
2041 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2042 return -EAGAIN;
2043 case -NFS4ERR_STALE_CLIENTID:
2044 case -NFS4ERR_STALE_STATEID:
2045 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002046 /* Don't recall a delegation if it was lost */
2047 nfs4_schedule_lease_recovery(server->nfs_client);
2048 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002049 case -NFS4ERR_MOVED:
2050 nfs4_schedule_migration_recovery(server);
2051 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002052 case -NFS4ERR_LEASE_MOVED:
2053 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2054 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002055 case -NFS4ERR_DELEG_REVOKED:
2056 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002057 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002059 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002060 nfs_inode_find_state_and_recover(state->inode,
2061 stateid);
2062 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002063 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002064 case -NFS4ERR_DELAY:
2065 case -NFS4ERR_GRACE:
2066 set_bit(NFS_DELEGATED_STATE, &state->flags);
2067 ssleep(1);
2068 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002069 case -ENOMEM:
2070 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002071 if (fl) {
2072 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2073 if (lsp)
2074 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2075 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002076 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return err;
2079}
2080
Trond Myklebust24311f82015-09-20 10:50:17 -04002081int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2082 struct nfs4_state *state, const nfs4_stateid *stateid,
2083 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002084{
2085 struct nfs_server *server = NFS_SERVER(state->inode);
2086 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002087 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002088
2089 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2090 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2091 if (IS_ERR(opendata))
2092 return PTR_ERR(opendata);
2093 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002094 write_seqlock(&state->seqlock);
2095 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2096 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002097 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2098 switch (type & (FMODE_READ|FMODE_WRITE)) {
2099 case FMODE_READ|FMODE_WRITE:
2100 case FMODE_WRITE:
2101 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2102 if (err)
2103 break;
2104 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2105 if (err)
2106 break;
2107 case FMODE_READ:
2108 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2109 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002110 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002111 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002112}
2113
Chuck Leverbe05c862013-08-09 12:49:47 -04002114static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2115{
2116 struct nfs4_opendata *data = calldata;
2117
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002118 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2119 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002120}
2121
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002122static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2123{
2124 struct nfs4_opendata *data = calldata;
2125
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002126 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002127
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002128 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002129 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002130 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002131 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002132 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002133 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002134 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002135}
2136
2137static void nfs4_open_confirm_release(void *calldata)
2138{
2139 struct nfs4_opendata *data = calldata;
2140 struct nfs4_state *state = NULL;
2141
2142 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002143 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002144 goto out_free;
2145 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002146 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002148 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002149 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002150 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002151out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002152 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002153}
2154
2155static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002156 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002157 .rpc_call_done = nfs4_open_confirm_done,
2158 .rpc_release = nfs4_open_confirm_release,
2159};
2160
2161/*
2162 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2163 */
2164static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2165{
David Howells2b0143b2015-03-17 22:25:59 +00002166 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002167 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002168 struct rpc_message msg = {
2169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2170 .rpc_argp = &data->c_arg,
2171 .rpc_resp = &data->c_res,
2172 .rpc_cred = data->owner->so_cred,
2173 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002174 struct rpc_task_setup task_setup_data = {
2175 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002176 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002177 .callback_ops = &nfs4_open_confirm_ops,
2178 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002179 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002180 .flags = RPC_TASK_ASYNC,
2181 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int status;
2183
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002184 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002185 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002186 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002187 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002188 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002189 if (data->is_recover)
2190 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002191 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002192 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002193 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002194 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002195 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002196 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002197 smp_wmb();
2198 } else
2199 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002200 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return status;
2202}
2203
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002206 struct nfs4_opendata *data = calldata;
2207 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002208 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002209 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002210
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002211 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002212 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002213 /*
2214 * Check if we still need to send an OPEN call, or if we can use
2215 * a delegation instead.
2216 */
2217 if (data->state != NULL) {
2218 struct nfs_delegation *delegation;
2219
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002220 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002221 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002222 rcu_read_lock();
2223 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002224 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002225 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002226 rcu_read_unlock();
2227 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002228 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002229 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002230 switch (claim) {
2231 default:
2232 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002233 case NFS4_OPEN_CLAIM_PREVIOUS:
2234 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2235 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002236 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002237 case NFS4_OPEN_CLAIM_FH:
2238 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002239 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002240 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002241 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002242 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002243 &data->o_res.seq_res,
2244 task) != 0)
2245 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002246
2247 /* Set the create mode (note dependency on the session type) */
2248 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2249 if (data->o_arg.open_flags & O_EXCL) {
2250 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2251 if (nfs4_has_persistent_session(clp))
2252 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2253 else if (clp->cl_mvops->minor_version > 0)
2254 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2255 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002256 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002257unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002258 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002259 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002260out_no_action:
2261 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002262out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002263 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002264}
2265
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266static void nfs4_open_done(struct rpc_task *task, void *calldata)
2267{
2268 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002270 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002271
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002272 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002273 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002274
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002275 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002276 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2277 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002278 case S_IFREG:
2279 break;
2280 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002282 break;
2283 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002284 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002285 break;
2286 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002287 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002288 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002289 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002290 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002291 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2292 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002293 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002294 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002296
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002297static void nfs4_open_release(void *calldata)
2298{
2299 struct nfs4_opendata *data = calldata;
2300 struct nfs4_state *state = NULL;
2301
2302 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002303 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002304 goto out_free;
2305 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002306 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 goto out_free;
2308 /* In case we need an open_confirm, no cleanup! */
2309 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2310 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002312 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002313 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002314out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002315 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002316}
2317
2318static const struct rpc_call_ops nfs4_open_ops = {
2319 .rpc_call_prepare = nfs4_open_prepare,
2320 .rpc_call_done = nfs4_open_done,
2321 .rpc_release = nfs4_open_release,
2322};
2323
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002324static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002325{
David Howells2b0143b2015-03-17 22:25:59 +00002326 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002327 struct nfs_server *server = NFS_SERVER(dir);
2328 struct nfs_openargs *o_arg = &data->o_arg;
2329 struct nfs_openres *o_res = &data->o_res;
2330 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002331 struct rpc_message msg = {
2332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2333 .rpc_argp = o_arg,
2334 .rpc_resp = o_res,
2335 .rpc_cred = data->owner->so_cred,
2336 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 struct rpc_task_setup task_setup_data = {
2338 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002339 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002340 .callback_ops = &nfs4_open_ops,
2341 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002342 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 .flags = RPC_TASK_ASYNC,
2344 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002345 int status;
2346
Chuck Levera9c92d62013-08-09 12:48:18 -04002347 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002348 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002349 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002350 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002351 data->cancelled = false;
2352 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002353 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002354 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002355 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002356 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002357 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002358 if (IS_ERR(task))
2359 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002360 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002361 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002362 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002363 smp_wmb();
2364 } else
2365 status = data->rpc_status;
2366 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002367
2368 return status;
2369}
2370
2371static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2372{
David Howells2b0143b2015-03-17 22:25:59 +00002373 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002374 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002375 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002376
2377 status = nfs4_run_open_task(data, 1);
2378 if (status != 0 || !data->rpc_done)
2379 return status;
2380
Trond Myklebust6926afd2012-01-07 13:22:46 -05002381 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2382
Anna Schumakerd7e98252017-01-11 16:13:29 -05002383 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002384 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002385
2386 return status;
2387}
2388
Trond Myklebustf3792d62014-07-10 08:54:32 -04002389/*
2390 * Additional permission checks in order to distinguish between an
2391 * open for read, and an open for execute. This works around the
2392 * fact that NFSv4 OPEN treats read and execute permissions as being
2393 * the same.
2394 * Note that in the non-execute case, we want to turn off permission
2395 * checking if we just created a new file (POSIX open() semantics).
2396 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002397static int nfs4_opendata_access(struct rpc_cred *cred,
2398 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002399 struct nfs4_state *state, fmode_t fmode,
2400 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002401{
2402 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002403 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002404
2405 /* access call failed or for some reason the server doesn't
2406 * support any access modes -- defer access call until later */
2407 if (opendata->o_res.access_supported == 0)
2408 return 0;
2409
2410 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002411 /*
2412 * Use openflags to check for exec, because fmode won't
2413 * always have FMODE_EXEC set when file open for exec.
2414 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002415 if (openflags & __FMODE_EXEC) {
2416 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002417 if (S_ISDIR(state->inode->i_mode))
2418 mask = NFS4_ACCESS_LOOKUP;
2419 else
2420 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002421 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002422 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002423
2424 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002425 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2426 nfs_access_add_cache(state->inode, &cache);
2427
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002428 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2429 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002430 return 0;
2431
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002432 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002433}
2434
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002435/*
2436 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2437 */
2438static int _nfs4_proc_open(struct nfs4_opendata *data)
2439{
David Howells2b0143b2015-03-17 22:25:59 +00002440 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002441 struct nfs_server *server = NFS_SERVER(dir);
2442 struct nfs_openargs *o_arg = &data->o_arg;
2443 struct nfs_openres *o_res = &data->o_res;
2444 int status;
2445
2446 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002447 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002448 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002449 if (status != 0) {
2450 if (status == -NFS4ERR_BADNAME &&
2451 !(o_arg->open_flags & O_CREAT))
2452 return -ENOENT;
2453 return status;
2454 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002455
Trond Myklebust6926afd2012-01-07 13:22:46 -05002456 nfs_fattr_map_and_free_names(server, &data->f_attr);
2457
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002458 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002459 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002460 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002461 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002462 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002463 if (data->file_created ||
2464 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002465 update_changeattr(dir, &o_res->cinfo,
2466 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002467 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002468 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2469 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002471 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002473 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002475 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2476 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002477 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002478 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482/*
2483 * OPEN_EXPIRED:
2484 * reclaim state on the server after a network partition.
2485 * Assumes caller holds the appropriate lock
2486 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002487static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002489 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002490 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002492 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002493 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002494 if (IS_ERR(opendata))
2495 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002496 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002497 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002498 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002499 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002503static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002504{
Trond Myklebust539cd032007-06-05 11:46:42 -04002505 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002506 struct nfs4_exception exception = { };
2507 int err;
2508
2509 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002510 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002511 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002512 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2513 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002514 switch (err) {
2515 default:
2516 goto out;
2517 case -NFS4ERR_GRACE:
2518 case -NFS4ERR_DELAY:
2519 nfs4_handle_exception(server, err, &exception);
2520 err = 0;
2521 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002522 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002523out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002524 return err;
2525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002530 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Trond Myklebust864472e2006-01-03 09:55:15 +01002532 ctx = nfs4_state_find_open_context(state);
2533 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002534 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002535 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002536 put_nfs_open_context(ctx);
2537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Trond Myklebust41020b62016-09-22 13:38:58 -04002540static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2541 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002542{
Trond Myklebust41020b62016-09-22 13:38:58 -04002543 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002544 write_seqlock(&state->seqlock);
2545 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2546 write_sequnlock(&state->seqlock);
2547 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2548}
2549
2550static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2551{
2552 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002553 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002554}
2555
2556static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2557{
2558 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2559 nfs40_clear_delegation_stateid(state);
2560 return nfs4_open_expired(sp, state);
2561}
2562
Trond Myklebust45870d62016-09-22 13:38:59 -04002563static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2564 nfs4_stateid *stateid,
2565 struct rpc_cred *cred)
2566{
2567 return -NFS4ERR_BAD_STATEID;
2568}
2569
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002570#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002571static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2572 nfs4_stateid *stateid,
2573 struct rpc_cred *cred)
2574{
2575 int status;
2576
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002577 switch (stateid->type) {
2578 default:
2579 break;
2580 case NFS4_INVALID_STATEID_TYPE:
2581 case NFS4_SPECIAL_STATEID_TYPE:
2582 return -NFS4ERR_BAD_STATEID;
2583 case NFS4_REVOKED_STATEID_TYPE:
2584 goto out_free;
2585 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002586
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002587 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002588 switch (status) {
2589 case -NFS4ERR_EXPIRED:
2590 case -NFS4ERR_ADMIN_REVOKED:
2591 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002592 break;
2593 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002594 return status;
2595 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002596out_free:
2597 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002598 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002599 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002600}
2601
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002602static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002603{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002604 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002605 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002606 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002607 struct rpc_cred *cred;
2608 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002609
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002610 /* Get the delegation credential for use by test/free_stateid */
2611 rcu_read_lock();
2612 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002613 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002614 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002615 return;
2616 }
2617
2618 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002619 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2620 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2621 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002622 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002623 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002624 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002626
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002627 cred = get_rpccred(delegation->cred);
2628 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002629 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002630 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002631 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002632 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002633
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002634 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002635}
2636
2637/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002638 * nfs41_check_expired_locks - possibly free a lock stateid
2639 *
2640 * @state: NFSv4 state for an inode
2641 *
2642 * Returns NFS_OK if recovery for this stateid is now finished.
2643 * Otherwise a negative NFS4ERR value is returned.
2644 */
2645static int nfs41_check_expired_locks(struct nfs4_state *state)
2646{
2647 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002648 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002649 struct nfs_server *server = NFS_SERVER(state->inode);
2650
2651 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2652 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002653
2654 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002655 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2656 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2657 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2658
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002659 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002660 spin_unlock(&state->state_lock);
2661
2662 nfs4_put_lock_state(prev);
2663 prev = lsp;
2664
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002665 status = nfs41_test_and_free_expired_stateid(server,
2666 &lsp->ls_stateid,
2667 cred);
2668 trace_nfs4_test_lock_stateid(state, lsp, status);
2669 if (status == -NFS4ERR_EXPIRED ||
2670 status == -NFS4ERR_BAD_STATEID) {
2671 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002672 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002673 if (!recover_lost_locks)
2674 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2675 } else if (status != NFS_OK) {
2676 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002677 nfs4_put_lock_state(prev);
2678 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002679 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002680 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002681 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002682 }
2683 spin_unlock(&state->state_lock);
2684 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002685out:
2686 return ret;
2687}
2688
2689/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002690 * nfs41_check_open_stateid - possibly free an open stateid
2691 *
2692 * @state: NFSv4 state for an inode
2693 *
2694 * Returns NFS_OK if recovery for this stateid is now finished.
2695 * Otherwise a negative NFS4ERR value is returned.
2696 */
2697static int nfs41_check_open_stateid(struct nfs4_state *state)
2698{
2699 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002700 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002701 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002702 int status;
2703
Trond Myklebustb134fc42016-09-22 13:39:16 -04002704 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002705 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2706 if (nfs4_have_delegation(state->inode, state->state))
2707 return NFS_OK;
2708 return -NFS4ERR_OPENMODE;
2709 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002710 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002711 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002712 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002713 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002714 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002715 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2716 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2717 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002718 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002719 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002720 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002721 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002722 if (nfs_open_stateid_recover_openmode(state))
2723 return -NFS4ERR_OPENMODE;
2724 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002725}
2726
2727static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2728{
Chuck Levereb64cf92012-07-11 16:30:05 -04002729 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002730
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002731 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002732 status = nfs41_check_expired_locks(state);
2733 if (status != NFS_OK)
2734 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002735 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002736 if (status != NFS_OK)
2737 status = nfs4_open_expired(sp, state);
2738 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002739}
2740#endif
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002743 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2744 * fields corresponding to attributes that were used to store the verifier.
2745 * Make sure we clobber those fields in the later setattr call
2746 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002747static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2748 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002749{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002750 const u32 *attrset = opendata->o_res.attrset;
2751
2752 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002753 !(sattr->ia_valid & ATTR_ATIME_SET))
2754 sattr->ia_valid |= ATTR_ATIME;
2755
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002756 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002757 !(sattr->ia_valid & ATTR_MTIME_SET))
2758 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002759
2760 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002761 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002762 (attrset[1] & FATTR4_WORD1_MODE ||
2763 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002764 sattr->ia_valid &= ~ATTR_MODE;
2765
2766 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2767 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002768}
2769
Trond Myklebustc21443c2013-02-07 14:26:21 -05002770static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2771 fmode_t fmode,
2772 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002773 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002774{
2775 struct nfs4_state_owner *sp = opendata->owner;
2776 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002777 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002778 struct nfs4_state *state;
2779 unsigned int seq;
2780 int ret;
2781
2782 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2783
2784 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002785 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002786 goto out;
2787
2788 state = nfs4_opendata_to_nfs4_state(opendata);
2789 ret = PTR_ERR(state);
2790 if (IS_ERR(state))
2791 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002792 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002793 if (server->caps & NFS_CAP_POSIX_LOCK)
2794 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002795 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2796 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002797
Trond Myklebust275bb302013-05-29 13:11:28 -04002798 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002799 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002800 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002801 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002802 alias = d_exact_alias(dentry, state->inode);
2803 if (!alias)
2804 alias = d_splice_alias(igrab(state->inode), dentry);
2805 /* d_splice_alias() can't fail here - it's a non-directory */
2806 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002807 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002808 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002809 }
2810 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002811 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002812 }
2813
Trond Myklebustc21443c2013-02-07 14:26:21 -05002814 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2815 if (ret != 0)
2816 goto out;
2817
David Howells2b0143b2015-03-17 22:25:59 +00002818 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002819 nfs_inode_attach_open_context(ctx);
2820 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2821 nfs4_schedule_stateid_recovery(server, state);
2822 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002823out:
2824 return ret;
2825}
2826
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002827/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002828 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 */
Andy Adamson82be4172012-05-23 05:02:35 -04002830static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002831 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002832 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002833 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002834 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 struct nfs4_state_owner *sp;
2837 struct nfs4_state *state = NULL;
2838 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002839 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002840 struct dentry *dentry = ctx->dentry;
2841 struct rpc_cred *cred = ctx->cred;
2842 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2843 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002844 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002845 struct iattr *sattr = c->sattr;
2846 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002847 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002848 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002852 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2853 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2855 goto out_err;
2856 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002857 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002858 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002859 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002860 if (d_really_is_positive(dentry))
2861 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002862 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002863 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002864 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002865 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2866 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002867 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002868 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
David Quigley14c43f72013-05-22 12:50:43 -04002870 if (label) {
2871 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2872 if (IS_ERR(olabel)) {
2873 status = PTR_ERR(olabel);
2874 goto err_opendata_put;
2875 }
2876 }
2877
Trond Myklebuste911b812014-03-26 13:24:37 -07002878 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2879 if (!opendata->f_attr.mdsthreshold) {
2880 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2881 if (!opendata->f_attr.mdsthreshold)
2882 goto err_free_label;
2883 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002884 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002885 }
David Howells2b0143b2015-03-17 22:25:59 +00002886 if (d_really_is_positive(dentry))
2887 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002888
Trond Myklebust3efb9722013-05-29 13:17:04 -04002889 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002890 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002891 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002892 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002893
NeilBrownefcbc042015-07-30 13:00:56 +10002894 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002895 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002896 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002897 /*
2898 * send create attributes which was not set by open
2899 * with an extra setattr.
2900 */
2901 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2902 nfs_fattr_init(opendata->o_res.f_attr);
2903 status = nfs4_do_setattr(state->inode, cred,
2904 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002905 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002906 if (status == 0) {
2907 nfs_setattr_update_inode(state->inode, sattr,
2908 opendata->o_res.f_attr);
2909 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2910 }
David Quigley1775fd32013-05-22 12:50:42 -04002911 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002912 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002913 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002914 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002915
Trond Myklebuste911b812014-03-26 13:24:37 -07002916 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002917 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002918 opendata->f_attr.mdsthreshold = NULL;
2919 }
Andy Adamson82be4172012-05-23 05:02:35 -04002920
David Quigley14c43f72013-05-22 12:50:43 -04002921 nfs4_label_free(olabel);
2922
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002923 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002926err_free_label:
2927 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002928err_opendata_put:
2929 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002930err_put_state_owner:
2931 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return status;
2934}
2935
2936
Andy Adamson82be4172012-05-23 05:02:35 -04002937static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002938 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002939 int flags,
2940 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002941 struct nfs4_label *label,
2942 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002944 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 struct nfs4_exception exception = { };
2946 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002947 struct nfs4_open_createattrs c = {
2948 .label = label,
2949 .sattr = sattr,
2950 .verf = {
2951 [0] = (__u32)jiffies,
2952 [1] = (__u32)current->pid,
2953 },
2954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 int status;
2956
2957 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002958 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002959 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002960 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (status == 0)
2962 break;
2963 /* NOTE: BAD_SEQID means the server and client disagree about the
2964 * book-keeping w.r.t. state-changing operations
2965 * (OPEN/CLOSE/LOCK/LOCKU...)
2966 * It is actually a sign of a bug on the client or on the server.
2967 *
2968 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002969 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 * have unhashed the old state_owner for us, and that we can
2971 * therefore safely retry using a new one. We should still warn
2972 * the user though...
2973 */
2974 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002975 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002976 " returned a bad sequence-id error!\n",
2977 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 exception.retry = 1;
2979 continue;
2980 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002981 /*
2982 * BAD_STATEID on OPEN means that the server cancelled our
2983 * state before it received the OPEN_CONFIRM.
2984 * Recover by retrying the request as per the discussion
2985 * on Page 181 of RFC3530.
2986 */
2987 if (status == -NFS4ERR_BAD_STATEID) {
2988 exception.retry = 1;
2989 continue;
2990 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002991 if (status == -EAGAIN) {
2992 /* We must have found a delegation */
2993 exception.retry = 1;
2994 continue;
2995 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002996 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2997 continue;
2998 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 status, &exception));
3000 } while (exception.retry);
3001 return res;
3002}
3003
Trond Myklebust8487c472016-06-26 08:44:35 -04003004static int _nfs4_do_setattr(struct inode *inode,
3005 struct nfs_setattrargs *arg,
3006 struct nfs_setattrres *res,
3007 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003008 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003009{
3010 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003011 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3013 .rpc_argp = arg,
3014 .rpc_resp = res,
3015 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003016 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003017 struct rpc_cred *delegation_cred = NULL;
3018 unsigned long timestamp = jiffies;
3019 fmode_t fmode;
3020 bool truncate;
3021 int status;
3022
3023 nfs_fattr_init(res->fattr);
3024
3025 /* Servers should only apply open mode checks for file size changes */
3026 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3027 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3028
3029 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3030 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003031 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003032 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003033 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003034 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003035 l_ctx = nfs_get_lock_context(ctx);
3036 if (IS_ERR(l_ctx))
3037 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003038 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3039 &arg->stateid, &delegation_cred);
3040 nfs_put_lock_context(l_ctx);
3041 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003042 return -EBADF;
3043 } else
3044 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3045 if (delegation_cred)
3046 msg.rpc_cred = delegation_cred;
3047
3048 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3049
3050 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003051 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003052 renew_lease(server, timestamp);
3053 trace_nfs4_setattr(inode, &arg->stateid, status);
3054 return status;
3055}
3056
3057static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3058 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003059 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003060 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003062 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003063 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003064 struct nfs_setattrargs arg = {
3065 .fh = NFS_FH(inode),
3066 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .server = server,
3068 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003069 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003070 };
3071 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003073 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003075 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003076 struct nfs4_exception exception = {
3077 .state = state,
3078 .inode = inode,
3079 .stateid = &arg.stateid,
3080 };
3081 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
David Quigleyaa9c2662013-05-22 12:50:44 -04003083 arg.bitmask = nfs4_bitmask(server, ilabel);
3084 if (ilabel)
3085 arg.bitmask = nfs4_bitmask(server, olabel);
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003088 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003089 switch (err) {
3090 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003091 if (!(sattr->ia_valid & ATTR_SIZE)) {
3092 pr_warn_once("NFSv4: server %s is incorrectly "
3093 "applying open mode checks to "
3094 "a SETATTR that is not "
3095 "changing file size.\n",
3096 server->nfs_client->cl_hostname);
3097 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003098 if (state && !(state->state & FMODE_WRITE)) {
3099 err = -EBADF;
3100 if (sattr->ia_valid & ATTR_OPEN)
3101 err = -EACCES;
3102 goto out;
3103 }
3104 }
3105 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003107out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return err;
3109}
3110
Peng Tao500d7012015-09-22 11:35:22 +08003111static bool
3112nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3113{
3114 if (inode == NULL || !nfs_have_layout(inode))
3115 return false;
3116
3117 return pnfs_wait_on_layoutreturn(inode, task);
3118}
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120struct nfs4_closedata {
3121 struct inode *inode;
3122 struct nfs4_state *state;
3123 struct nfs_closeargs arg;
3124 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003125 struct {
3126 struct nfs4_layoutreturn_args arg;
3127 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003128 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003129 u32 roc_barrier;
3130 bool roc;
3131 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003132 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003133 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134};
3135
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003136static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003137{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003138 struct nfs4_closedata *calldata = data;
3139 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003140 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003141
Trond Myklebustcf805162016-11-15 14:56:07 -05003142 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003143 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3144 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003145 nfs4_put_open_state(calldata->state);
3146 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003147 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003148 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003149 kfree(calldata);
3150}
3151
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003152static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003154 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003157 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003158 struct nfs4_exception exception = {
3159 .state = state,
3160 .inode = calldata->inode,
3161 .stateid = &calldata->arg.stateid,
3162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Chuck Levera3ca5652012-03-01 17:00:40 -05003164 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003165 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3166 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003167 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003168
3169 /* Handle Layoutreturn errors */
3170 if (calldata->arg.lr_args && task->tk_status != 0) {
3171 switch (calldata->res.lr_ret) {
3172 default:
3173 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3174 break;
3175 case 0:
3176 calldata->arg.lr_args = NULL;
3177 calldata->res.lr_res = NULL;
3178 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003179 case -NFS4ERR_OLD_STATEID:
3180 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003181 calldata->inode))
3182 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003183 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003184 case -NFS4ERR_ADMIN_REVOKED:
3185 case -NFS4ERR_DELEG_REVOKED:
3186 case -NFS4ERR_EXPIRED:
3187 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003188 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3189 case -NFS4ERR_WRONG_CRED:
3190 calldata->arg.lr_args = NULL;
3191 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003192 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003193 }
3194 }
3195
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003196 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 * the state_owner. we keep this around to process errors
3198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 switch (task->tk_status) {
3200 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003201 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003202 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003203 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003204 case -NFS4ERR_ACCESS:
3205 if (calldata->arg.bitmask != NULL) {
3206 calldata->arg.bitmask = NULL;
3207 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003208 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003209
3210 }
3211 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003212 case -NFS4ERR_OLD_STATEID:
3213 /* Did we race with OPEN? */
3214 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003215 state))
3216 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003217 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003218 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003220 case -NFS4ERR_EXPIRED:
3221 nfs4_free_revoked_stateid(server,
3222 &calldata->arg.stateid,
3223 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003224 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003225 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003228 task->tk_status = nfs4_async_handle_exception(task,
3229 server, task->tk_status, &exception);
3230 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003231 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003233 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3234 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003235out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003236 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003237 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003238 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003239 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003240 return;
3241lr_restart:
3242 calldata->res.lr_ret = 0;
3243out_restart:
3244 task->tk_status = 0;
3245 rpc_restart_call_prepare(task);
3246 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003249static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003251 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003252 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003253 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003254 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003255 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003256
Chuck Levera3ca5652012-03-01 17:00:40 -05003257 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003258 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003259 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003260
Trond Myklebust88069f72009-12-08 08:33:16 -05003261 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003262 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003263 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3264 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3265 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003266 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003267 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003268 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003269 if (state->n_rdonly == 0)
3270 call_close |= is_rdonly;
3271 else if (is_rdonly)
3272 calldata->arg.fmode |= FMODE_READ;
3273 if (state->n_wronly == 0)
3274 call_close |= is_wronly;
3275 else if (is_wronly)
3276 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003277 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3278 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003279 } else if (is_rdwr)
3280 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3281
Trond Myklebust5cc78612016-11-14 11:19:56 -05003282 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003283 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003284 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003285 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003286
3287 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003288 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003289 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003290 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003291
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003292 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003293 nfs_release_seqid(calldata->arg.seqid);
3294 goto out_wait;
3295 }
3296
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003297 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003298 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003299
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003300 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003301 /* Close-to-open cache consistency revalidation */
3302 if (!nfs4_have_delegation(inode, FMODE_READ))
3303 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3304 else
3305 calldata->arg.bitmask = NULL;
3306 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003307
Trond Myklebust6ae37332015-01-30 14:21:14 -05003308 calldata->arg.share_access =
3309 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3310 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003311
Trond Myklebustd8d84982016-12-19 12:14:44 -05003312 if (calldata->res.fattr == NULL)
3313 calldata->arg.bitmask = NULL;
3314 else if (calldata->arg.bitmask == NULL)
3315 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003316 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003317 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003318 &calldata->arg.seq_args,
3319 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003320 task) != 0)
3321 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003322 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003323 return;
3324out_no_action:
3325 task->tk_action = NULL;
3326out_wait:
3327 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003330static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003331 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003332 .rpc_call_done = nfs4_close_done,
3333 .rpc_release = nfs4_free_closedata,
3334};
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336/*
3337 * It is possible for data to be read/written from a mem-mapped file
3338 * after the sys_close call (which hits the vfs layer as a flush).
3339 * This means that we can't safely call nfsv4 close on a file until
3340 * the inode is cleared. This in turn means that we are not good
3341 * NFSv4 citizens - we do not indicate to the server to update the file's
3342 * share state even when we are done with one of the three share
3343 * stateid's in the inode.
3344 *
3345 * NOTE: Caller must be holding the sp->so_owner semaphore!
3346 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003347int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003349 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003350 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003352 struct nfs4_state_owner *sp = state->owner;
3353 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003354 struct rpc_message msg = {
3355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3356 .rpc_cred = state->owner->so_cred,
3357 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003358 struct rpc_task_setup task_setup_data = {
3359 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003360 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003361 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003362 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003363 .flags = RPC_TASK_ASYNC,
3364 };
Trond Myklebust95121352005-10-18 14:20:12 -07003365 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003367 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3368 &task_setup_data.rpc_client, &msg);
3369
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003370 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003372 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003373 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003374 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003376 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003377 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3378 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003380 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3381 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003382 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003383 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003384 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003385 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003386 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003387 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003388 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003389 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003390 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003391 calldata->lr.roc = pnfs_roc(state->inode,
3392 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3393 if (calldata->lr.roc) {
3394 calldata->arg.lr_args = &calldata->lr.arg;
3395 calldata->res.lr_res = &calldata->lr.res;
3396 }
Al Viro643168c2011-06-22 18:20:23 -04003397 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003398
Trond Myklebust1174dd12010-12-21 10:52:24 -05003399 msg.rpc_argp = &calldata->arg;
3400 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003401 task_setup_data.callback_data = calldata;
3402 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003403 if (IS_ERR(task))
3404 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003405 status = 0;
3406 if (wait)
3407 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003408 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003409 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003410out_free_calldata:
3411 kfree(calldata);
3412out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003413 nfs4_put_open_state(state);
3414 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003415 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Trond Myklebust2b484292010-09-17 10:56:51 -04003418static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003419nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3420 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003423 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3424
3425 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Trond Myklebust565277f2007-10-15 18:17:53 -04003427 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003428 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003429
3430 nfs4_label_release_security(label);
3431
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003432 if (IS_ERR(state))
3433 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003434 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Trond Myklebust1185a552009-12-03 15:54:02 -05003437static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003438{
3439 if (ctx->state == NULL)
3440 return;
3441 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003442 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003443 else
Al Viro643168c2011-06-22 18:20:23 -04003444 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Trond Myklebustb944dba2013-11-04 15:20:20 -05003447#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3448#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003449#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3452{
Kinglong Mee8c612822015-08-26 21:12:58 +08003453 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003454 struct nfs4_server_caps_arg args = {
3455 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003456 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003457 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 struct nfs4_server_caps_res res = {};
3459 struct rpc_message msg = {
3460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003461 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 .rpc_resp = &res,
3463 };
3464 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003465 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Kinglong Mee8c612822015-08-26 21:12:58 +08003467 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3468 FATTR4_WORD0_FH_EXPIRE_TYPE |
3469 FATTR4_WORD0_LINK_SUPPORT |
3470 FATTR4_WORD0_SYMLINK_SUPPORT |
3471 FATTR4_WORD0_ACLSUPPORT;
3472 if (minorversion)
3473 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3474
Bryan Schumaker7c513052011-03-24 17:12:24 +00003475 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003477 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003478 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003479 case 0:
3480 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3481 res.attr_bitmask[2] = 0;
3482 break;
3483 case 1:
3484 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3485 break;
3486 case 2:
3487 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003490 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3491 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3492 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3493 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003494 NFS_CAP_CTIME|NFS_CAP_MTIME|
3495 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003496 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3497 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 server->caps |= NFS_CAP_ACLS;
3499 if (res.has_links != 0)
3500 server->caps |= NFS_CAP_HARDLINKS;
3501 if (res.has_symlinks != 0)
3502 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003503 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3504 server->caps |= NFS_CAP_FILEID;
3505 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3506 server->caps |= NFS_CAP_MODE;
3507 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3508 server->caps |= NFS_CAP_NLINK;
3509 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3510 server->caps |= NFS_CAP_OWNER;
3511 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3512 server->caps |= NFS_CAP_OWNER_GROUP;
3513 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3514 server->caps |= NFS_CAP_ATIME;
3515 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3516 server->caps |= NFS_CAP_CTIME;
3517 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3518 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003519#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3520 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3521 server->caps |= NFS_CAP_SECURITY_LABEL;
3522#endif
3523 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3524 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003525 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003526
Trond Myklebusta65318b2009-03-11 14:10:28 -04003527 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3528 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3529 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003530 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003531
3532 /* Avoid a regression due to buggy server */
3533 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3534 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003535 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3536 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003539 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return status;
3543}
3544
Trond Myklebust55a97592006-06-09 09:34:19 -04003545int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 struct nfs4_exception exception = { };
3548 int err;
3549 do {
3550 err = nfs4_handle_exception(server,
3551 _nfs4_server_capabilities(server, fhandle),
3552 &exception);
3553 } while (exception.retry);
3554 return err;
3555}
3556
3557static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3558 struct nfs_fsinfo *info)
3559{
David Quigleyaa9c2662013-05-22 12:50:44 -04003560 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003562 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 };
3564 struct nfs4_lookup_res res = {
3565 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003566 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 .fh = fhandle,
3568 };
3569 struct rpc_message msg = {
3570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3571 .rpc_argp = &args,
3572 .rpc_resp = &res,
3573 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003574
David Quigleyaa9c2662013-05-22 12:50:44 -04003575 bitmask[0] = nfs4_fattr_bitmap[0];
3576 bitmask[1] = nfs4_fattr_bitmap[1];
3577 /*
3578 * Process the label in the upcoming getfattr
3579 */
3580 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3581
Trond Myklebust0e574af2005-10-27 22:12:38 -04003582 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003583 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
3585
3586static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3587 struct nfs_fsinfo *info)
3588{
3589 struct nfs4_exception exception = { };
3590 int err;
3591 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003592 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003593 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003594 switch (err) {
3595 case 0:
3596 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003597 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003598 default:
3599 err = nfs4_handle_exception(server, err, &exception);
3600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003602out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 return err;
3604}
3605
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003606static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3607 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3608{
Trond Myklebustc2190662013-08-26 19:23:04 -04003609 struct rpc_auth_create_args auth_args = {
3610 .pseudoflavor = flavor,
3611 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003612 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003613
Trond Myklebustc2190662013-08-26 19:23:04 -04003614 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003615 if (IS_ERR(auth))
3616 return -EACCES;
3617 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003618}
3619
Chuck Lever9a744ba2013-03-16 15:56:02 -04003620/*
3621 * Retry pseudoroot lookup with various security flavors. We do this when:
3622 *
3623 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3624 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3625 *
3626 * Returns zero on success, or a negative NFS4ERR value, or a
3627 * negative errno value.
3628 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003629static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003630 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003632 /* Per 3530bis 15.33.5 */
3633 static const rpc_authflavor_t flav_array[] = {
3634 RPC_AUTH_GSS_KRB5P,
3635 RPC_AUTH_GSS_KRB5I,
3636 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003637 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003638 RPC_AUTH_NULL,
3639 };
3640 int status = -EPERM;
3641 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003643 if (server->auth_info.flavor_len > 0) {
3644 /* try each flavor specified by user */
3645 for (i = 0; i < server->auth_info.flavor_len; i++) {
3646 status = nfs4_lookup_root_sec(server, fhandle, info,
3647 server->auth_info.flavors[i]);
3648 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3649 continue;
3650 break;
3651 }
3652 } else {
3653 /* no flavors specified by user, try default list */
3654 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3655 status = nfs4_lookup_root_sec(server, fhandle, info,
3656 flav_array[i]);
3657 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3658 continue;
3659 break;
3660 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003661 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003662
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003663 /*
3664 * -EACCESS could mean that the user doesn't have correct permissions
3665 * to access the mount. It could also mean that we tried to mount
3666 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3667 * existing mount programs don't handle -EACCES very well so it should
3668 * be mapped to -EPERM instead.
3669 */
3670 if (status == -EACCES)
3671 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003672 return status;
3673}
3674
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003675/**
3676 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3677 * @server: initialized nfs_server handle
3678 * @fhandle: we fill in the pseudo-fs root file handle
3679 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003680 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003681 *
3682 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003683 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003684int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003685 struct nfs_fsinfo *info,
3686 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003687{
Andre Przywarac7757072015-04-23 17:17:40 +01003688 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003689
Andre Przywarac7757072015-04-23 17:17:40 +01003690 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003691 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003692
3693 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003694 status = server->nfs_client->cl_mvops->find_root_sec(server,
3695 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 if (status == 0)
3698 status = nfs4_server_capabilities(server, fhandle);
3699 if (status == 0)
3700 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003701
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003702 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}
3704
Bryan Schumakerbae36242012-05-10 15:07:31 -04003705static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3706 struct nfs_fsinfo *info)
3707{
3708 int error;
3709 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003710 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003711
3712 error = nfs4_server_capabilities(server, mntfh);
3713 if (error < 0) {
3714 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3715 return error;
3716 }
3717
David Quigley14c43f72013-05-22 12:50:43 -04003718 label = nfs4_label_alloc(server, GFP_KERNEL);
3719 if (IS_ERR(label))
3720 return PTR_ERR(label);
3721
David Quigley1775fd32013-05-22 12:50:42 -04003722 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003723 if (error < 0) {
3724 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003725 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003726 }
3727
3728 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3729 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3730 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3731
David Quigley14c43f72013-05-22 12:50:43 -04003732err_free_label:
3733 nfs4_label_free(label);
3734
Bryan Schumakerbae36242012-05-10 15:07:31 -04003735 return error;
3736}
3737
Manoj Naik6b97fd32006-06-09 09:34:29 -04003738/*
3739 * Get locations and (maybe) other attributes of a referral.
3740 * Note that we'll actually follow the referral later when
3741 * we detect fsid mismatch in inode revalidation
3742 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003743static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3744 const struct qstr *name, struct nfs_fattr *fattr,
3745 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003746{
3747 int status = -ENOMEM;
3748 struct page *page = NULL;
3749 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003750
3751 page = alloc_page(GFP_KERNEL);
3752 if (page == NULL)
3753 goto out;
3754 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3755 if (locations == NULL)
3756 goto out;
3757
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003758 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003759 if (status != 0)
3760 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003761
3762 /*
3763 * If the fsid didn't change, this is a migration event, not a
3764 * referral. Cause us to drop into the exception handler, which
3765 * will kick off migration recovery.
3766 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003767 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003768 dprintk("%s: server did not return a different fsid for"
3769 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003770 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003771 goto out;
3772 }
Andy Adamson533eb462011-06-13 18:25:56 -04003773 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3774 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003775
Andy Adamson533eb462011-06-13 18:25:56 -04003776 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003777 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003778 memset(fhandle, 0, sizeof(struct nfs_fh));
3779out:
3780 if (page)
3781 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003782 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003783 return status;
3784}
3785
David Quigley1775fd32013-05-22 12:50:42 -04003786static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3787 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
3789 struct nfs4_getattr_arg args = {
3790 .fh = fhandle,
3791 .bitmask = server->attr_bitmask,
3792 };
3793 struct nfs4_getattr_res res = {
3794 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003795 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 .server = server,
3797 };
3798 struct rpc_message msg = {
3799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3800 .rpc_argp = &args,
3801 .rpc_resp = &res,
3802 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003803
3804 args.bitmask = nfs4_bitmask(server, label);
3805
Trond Myklebust0e574af2005-10-27 22:12:38 -04003806 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003807 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
David Quigley1775fd32013-05-22 12:50:42 -04003810static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3811 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 struct nfs4_exception exception = { };
3814 int err;
3815 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003816 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3817 trace_nfs4_getattr(server, fhandle, fattr, err);
3818 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 &exception);
3820 } while (exception.retry);
3821 return err;
3822}
3823
3824/*
3825 * The file is not closed if it is opened due to the a request to change
3826 * the size of the file. The open call will not be needed once the
3827 * VFS layer lookup-intents are implemented.
3828 *
3829 * Close is called when the inode is destroyed.
3830 * If we haven't opened the file for O_WRONLY, we
3831 * need to in the size_change case to obtain a stateid.
3832 *
3833 * Got race?
3834 * Because OPEN is always done by name in nfsv4, it is
3835 * possible that we opened a different file by the same
3836 * name. We can recognize this race condition, but we
3837 * can't do anything about it besides returning an error.
3838 *
3839 * This will be fixed with VFS changes (lookup-intent).
3840 */
3841static int
3842nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3843 struct iattr *sattr)
3844{
David Howells2b0143b2015-03-17 22:25:59 +00003845 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003846 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003847 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003848 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 int status;
3850
Peng Tao88ac8152014-09-12 11:04:10 +08003851 if (pnfs_ld_layoutret_on_setattr(inode) &&
3852 sattr->ia_valid & ATTR_SIZE &&
3853 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003854 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003855
Trond Myklebust0e574af2005-10-27 22:12:38 -04003856 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Andy Adamson26699402012-05-30 16:12:24 -04003858 /* Deal with open(O_TRUNC) */
3859 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003860 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003861
3862 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003863 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003864 return 0;
3865
Trond Myklebustd5308382005-11-04 15:33:38 -05003866 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003867 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003868
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003869 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003870 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003871 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003872 }
3873
David Quigley14c43f72013-05-22 12:50:43 -04003874 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3875 if (IS_ERR(label))
3876 return PTR_ERR(label);
3877
NeilBrown29b59f92016-10-13 15:26:47 +11003878 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003879 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003880 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003881 nfs_setsecurity(inode, fattr, label);
3882 }
David Quigley14c43f72013-05-22 12:50:43 -04003883 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003884 return status;
3885}
3886
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003887static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3888 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003889 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003890{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003891 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003892 int status;
3893 struct nfs4_lookup_arg args = {
3894 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003895 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003896 .name = name,
3897 };
3898 struct nfs4_lookup_res res = {
3899 .server = server,
3900 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003901 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003902 .fh = fhandle,
3903 };
3904 struct rpc_message msg = {
3905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3906 .rpc_argp = &args,
3907 .rpc_resp = &res,
3908 };
3909
David Quigleyaa9c2662013-05-22 12:50:44 -04003910 args.bitmask = nfs4_bitmask(server, label);
3911
David Howells2b3de442006-08-22 20:06:09 -04003912 nfs_fattr_init(fattr);
3913
Linus Torvalds1da177e2005-04-16 15:20:36 -07003914 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003915 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003916 dprintk("NFS reply lookup: %d\n", status);
3917 return status;
3918}
3919
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003920static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003921{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003922 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003923 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003924 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3925 fattr->nlink = 2;
3926}
3927
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003928static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003929 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003930 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003931{
3932 struct nfs4_exception exception = { };
3933 struct rpc_clnt *client = *clnt;
3934 int err;
3935 do {
David Quigley1775fd32013-05-22 12:50:42 -04003936 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003937 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003938 switch (err) {
3939 case -NFS4ERR_BADNAME:
3940 err = -ENOENT;
3941 goto out;
3942 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003943 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003944 if (err == -NFS4ERR_MOVED)
3945 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003946 goto out;
3947 case -NFS4ERR_WRONGSEC:
3948 err = -EPERM;
3949 if (client != *clnt)
3950 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003951 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003952 if (IS_ERR(client))
3953 return PTR_ERR(client);
3954
3955 exception.retry = 1;
3956 break;
3957 default:
3958 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3959 }
3960 } while (exception.retry);
3961
3962out:
3963 if (err == 0)
3964 *clnt = client;
3965 else if (client != *clnt)
3966 rpc_shutdown_client(client);
3967
3968 return err;
3969}
3970
Al Virobeffb8f2016-07-20 16:34:42 -04003971static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003972 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3973 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003974{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003975 int status;
3976 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003977
David Quigley1775fd32013-05-22 12:50:42 -04003978 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003979 if (client != NFS_CLIENT(dir)) {
3980 rpc_shutdown_client(client);
3981 nfs_fixup_secinfo_attributes(fattr);
3982 }
3983 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003984}
3985
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003986struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003987nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003988 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3989{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003990 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003991 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003992
David Quigley1775fd32013-05-22 12:50:42 -04003993 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003994 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003995 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003996 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003997}
3998
Jeff Layton5b5faaf2017-06-29 06:34:52 -07003999static int _nfs4_proc_lookupp(struct inode *inode,
4000 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4001 struct nfs4_label *label)
4002{
4003 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4004 struct nfs_server *server = NFS_SERVER(inode);
4005 int status;
4006 struct nfs4_lookupp_arg args = {
4007 .bitmask = server->attr_bitmask,
4008 .fh = NFS_FH(inode),
4009 };
4010 struct nfs4_lookupp_res res = {
4011 .server = server,
4012 .fattr = fattr,
4013 .label = label,
4014 .fh = fhandle,
4015 };
4016 struct rpc_message msg = {
4017 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4018 .rpc_argp = &args,
4019 .rpc_resp = &res,
4020 };
4021
4022 args.bitmask = nfs4_bitmask(server, label);
4023
4024 nfs_fattr_init(fattr);
4025
4026 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4027 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4028 &res.seq_res, 0);
4029 dprintk("NFS reply lookupp: %d\n", status);
4030 return status;
4031}
4032
4033static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4034 struct nfs_fattr *fattr, struct nfs4_label *label)
4035{
4036 struct nfs4_exception exception = { };
4037 int err;
4038 do {
4039 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4040 trace_nfs4_lookupp(inode, err);
4041 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4042 &exception);
4043 } while (exception.retry);
4044 return err;
4045}
4046
Linus Torvalds1da177e2005-04-16 15:20:36 -07004047static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4048{
Trond Myklebust76b32992007-08-10 17:45:11 -04004049 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004050 struct nfs4_accessargs args = {
4051 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004052 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004053 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004055 struct nfs4_accessres res = {
4056 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004057 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 struct rpc_message msg = {
4059 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4060 .rpc_argp = &args,
4061 .rpc_resp = &res,
4062 .rpc_cred = entry->cred,
4063 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004064 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065
Trond Myklebustc407d412010-04-16 16:22:48 -04004066 res.fattr = nfs_alloc_fattr();
4067 if (res.fattr == NULL)
4068 return -ENOMEM;
4069
Bryan Schumaker7c513052011-03-24 17:12:24 +00004070 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004072 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004073 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004075 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004076 return status;
4077}
4078
4079static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4080{
4081 struct nfs4_exception exception = { };
4082 int err;
4083 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004084 err = _nfs4_proc_access(inode, entry);
4085 trace_nfs4_access(inode, err);
4086 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004087 &exception);
4088 } while (exception.retry);
4089 return err;
4090}
4091
4092/*
4093 * TODO: For the time being, we don't try to get any attributes
4094 * along with any of the zero-copy operations READ, READDIR,
4095 * READLINK, WRITE.
4096 *
4097 * In the case of the first three, we want to put the GETATTR
4098 * after the read-type operation -- this is because it is hard
4099 * to predict the length of a GETATTR response in v4, and thus
4100 * align the READ data correctly. This means that the GETATTR
4101 * may end up partially falling into the page cache, and we should
4102 * shift it into the 'tail' of the xdr_buf before processing.
4103 * To do this efficiently, we need to know the total length
4104 * of data received, which doesn't seem to be available outside
4105 * of the RPC layer.
4106 *
4107 * In the case of WRITE, we also want to put the GETATTR after
4108 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004109 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004110 *
4111 * Both of these changes to the XDR layer would in fact be quite
4112 * minor, but I decided to leave them for a subsequent patch.
4113 */
4114static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4115 unsigned int pgbase, unsigned int pglen)
4116{
4117 struct nfs4_readlink args = {
4118 .fh = NFS_FH(inode),
4119 .pgbase = pgbase,
4120 .pglen = pglen,
4121 .pages = &page,
4122 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004123 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004124 struct rpc_message msg = {
4125 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4126 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004127 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 };
4129
Bryan Schumaker7c513052011-03-24 17:12:24 +00004130 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 -07004131}
4132
4133static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4134 unsigned int pgbase, unsigned int pglen)
4135{
4136 struct nfs4_exception exception = { };
4137 int err;
4138 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004139 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4140 trace_nfs4_readlink(inode, err);
4141 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004142 &exception);
4143 } while (exception.retry);
4144 return err;
4145}
4146
Linus Torvalds1da177e2005-04-16 15:20:36 -07004147/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004148 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004150static int
4151nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004152 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004154 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004155 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004156 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 int status = 0;
4159
NeilBrown532d4de2016-10-13 15:26:47 +11004160 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004161 if (IS_ERR(ctx))
4162 return PTR_ERR(ctx);
4163
David Quigleyaa9c2662013-05-22 12:50:44 -04004164 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4165
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004166 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4167 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004168 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004169 if (IS_ERR(state)) {
4170 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004171 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004174 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004175 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 return status;
4177}
4178
Al Virobeffb8f2016-07-20 16:34:42 -04004179static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180{
Trond Myklebust16e42952005-10-27 22:12:44 -04004181 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004182 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004184 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004185 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004186 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004187 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004188 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004190 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4191 .rpc_argp = &args,
4192 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004194 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004195 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004196
Bryan Schumaker7c513052011-03-24 17:12:24 +00004197 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004198 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004199 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004200 return status;
4201}
4202
Trond Myklebust912678d2018-03-20 16:43:15 -04004203static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4204{
4205 struct nfs4_exception exception = { };
4206 struct inode *inode = d_inode(dentry);
4207 int err;
4208
4209 if (inode)
4210 nfs4_inode_return_delegation(inode);
4211 do {
4212 err = _nfs4_proc_remove(dir, &dentry->d_name);
4213 trace_nfs4_remove(dir, &dentry->d_name, err);
4214 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4215 &exception);
4216 } while (exception.retry);
4217 return err;
4218}
4219
4220static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221{
4222 struct nfs4_exception exception = { };
4223 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004224
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004226 err = _nfs4_proc_remove(dir, name);
4227 trace_nfs4_remove(dir, name, err);
4228 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 &exception);
4230 } while (exception.retry);
4231 return err;
4232}
4233
Trond Myklebust977fcc22018-03-20 16:43:17 -04004234static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004236 struct nfs_removeargs *args = msg->rpc_argp;
4237 struct nfs_removeres *res = msg->rpc_resp;
Trond Myklebust977fcc22018-03-20 16:43:17 -04004238 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239
Trond Myklebust977fcc22018-03-20 16:43:17 -04004240 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004241 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004242 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004243
4244 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004245
4246 if (inode)
4247 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004248}
4249
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004250static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4251{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004252 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004253 &data->args.seq_args,
4254 &data->res.seq_res,
4255 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004256}
4257
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004258static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004260 struct nfs_unlinkdata *data = task->tk_calldata;
4261 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004262
Trond Myklebust14516c32010-07-31 14:29:06 -04004263 if (!nfs4_sequence_done(task, &res->seq_res))
4264 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004265 if (nfs4_async_handle_error(task, res->server, NULL,
4266 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004267 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004268 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004269 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004270 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004271}
4272
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004273static void nfs4_proc_rename_setup(struct rpc_message *msg,
4274 struct dentry *old_dentry,
4275 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004276{
Jeff Laytond3d41522010-09-17 17:31:57 -04004277 struct nfs_renameargs *arg = msg->rpc_argp;
4278 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004279 struct inode *old_inode = d_inode(old_dentry);
4280 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004281
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004282 if (old_inode)
4283 nfs4_inode_return_delegation(old_inode);
4284 if (new_inode)
4285 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004286 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004287 res->server = NFS_SB(old_dentry->d_sb);
Chuck Levera9c92d62013-08-09 12:48:18 -04004288 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004289}
4290
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004291static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4292{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004293 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004294 &data->args.seq_args,
4295 &data->res.seq_res,
4296 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004297}
4298
4299static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4300 struct inode *new_dir)
4301{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004302 struct nfs_renamedata *data = task->tk_calldata;
4303 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004304
4305 if (!nfs4_sequence_done(task, &res->seq_res))
4306 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004307 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004308 return 0;
4309
Trond Myklebustc733c492017-01-11 12:32:26 -05004310 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004311 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004312 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004313 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004314 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004315 return 1;
4316}
4317
Al Virobeffb8f2016-07-20 16:34:42 -04004318static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004319{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004320 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004321 struct nfs4_link_arg arg = {
4322 .fh = NFS_FH(inode),
4323 .dir_fh = NFS_FH(dir),
4324 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004325 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004326 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004327 struct nfs4_link_res res = {
4328 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004329 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004330 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004331 struct rpc_message msg = {
4332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4333 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004334 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004336 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004337
Trond Myklebust136f2622010-04-16 16:22:49 -04004338 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004339 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004340 goto out;
4341
David Quigley14c43f72013-05-22 12:50:43 -04004342 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4343 if (IS_ERR(res.label)) {
4344 status = PTR_ERR(res.label);
4345 goto out;
4346 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004347 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004348
Trond Myklebust9f768272018-03-20 16:43:14 -04004349 nfs4_inode_return_delegation(inode);
4350
Bryan Schumaker7c513052011-03-24 17:12:24 +00004351 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004352 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004353 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004354 status = nfs_post_op_update_inode(inode, res.fattr);
4355 if (!status)
4356 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004357 }
David Quigley14c43f72013-05-22 12:50:43 -04004358
4359
4360 nfs4_label_free(res.label);
4361
Trond Myklebust136f2622010-04-16 16:22:49 -04004362out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004363 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004364 return status;
4365}
4366
Al Virobeffb8f2016-07-20 16:34:42 -04004367static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368{
4369 struct nfs4_exception exception = { };
4370 int err;
4371 do {
4372 err = nfs4_handle_exception(NFS_SERVER(inode),
4373 _nfs4_proc_link(inode, dir, name),
4374 &exception);
4375 } while (exception.retry);
4376 return err;
4377}
4378
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004379struct nfs4_createdata {
4380 struct rpc_message msg;
4381 struct nfs4_create_arg arg;
4382 struct nfs4_create_res res;
4383 struct nfs_fh fh;
4384 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004385 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386};
4387
4388static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004389 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004390{
4391 struct nfs4_createdata *data;
4392
4393 data = kzalloc(sizeof(*data), GFP_KERNEL);
4394 if (data != NULL) {
4395 struct nfs_server *server = NFS_SERVER(dir);
4396
David Quigley14c43f72013-05-22 12:50:43 -04004397 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4398 if (IS_ERR(data->label))
4399 goto out_free;
4400
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4402 data->msg.rpc_argp = &data->arg;
4403 data->msg.rpc_resp = &data->res;
4404 data->arg.dir_fh = NFS_FH(dir);
4405 data->arg.server = server;
4406 data->arg.name = name;
4407 data->arg.attrs = sattr;
4408 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004409 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004410 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004411 data->res.server = server;
4412 data->res.fh = &data->fh;
4413 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004414 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004415 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004416 }
4417 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004418out_free:
4419 kfree(data);
4420 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004421}
4422
4423static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4424{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004425 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004426 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004427 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004428 update_changeattr(dir, &data->res.dir_cinfo,
4429 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004430 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004431 }
4432 return status;
4433}
4434
4435static void nfs4_free_createdata(struct nfs4_createdata *data)
4436{
David Quigley14c43f72013-05-22 12:50:43 -04004437 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004438 kfree(data);
4439}
4440
Chuck Lever4f390c12006-08-22 20:06:22 -04004441static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004442 struct page *page, unsigned int len, struct iattr *sattr,
4443 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004444{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445 struct nfs4_createdata *data;
4446 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447
Chuck Lever94a6d752006-08-22 20:06:23 -04004448 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004450
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004451 status = -ENOMEM;
4452 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4453 if (data == NULL)
4454 goto out;
4455
4456 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4457 data->arg.u.symlink.pages = &page;
4458 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004459 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004461 status = nfs4_do_create(dir, dentry, data);
4462
4463 nfs4_free_createdata(data);
4464out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004465 return status;
4466}
4467
Chuck Lever4f390c12006-08-22 20:06:22 -04004468static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004469 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
4471 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004472 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004474
4475 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4476
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004478 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4479 trace_nfs4_symlink(dir, &dentry->d_name, err);
4480 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 &exception);
4482 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004483
4484 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 return err;
4486}
4487
4488static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004489 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004490{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004491 struct nfs4_createdata *data;
4492 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004493
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004494 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4495 if (data == NULL)
4496 goto out;
4497
David Quigley1775fd32013-05-22 12:50:42 -04004498 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004499 status = nfs4_do_create(dir, dentry, data);
4500
4501 nfs4_free_createdata(data);
4502out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 return status;
4504}
4505
4506static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4507 struct iattr *sattr)
4508{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004509 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004511 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004512 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004513
David Quigleyaa9c2662013-05-22 12:50:44 -04004514 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4515
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004516 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4517 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004518 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004519 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4520 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4521 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 &exception);
4523 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004524 nfs4_label_release_security(label);
4525
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 return err;
4527}
4528
4529static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004530 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004531{
David Howells2b0143b2015-03-17 22:25:59 +00004532 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004533 struct nfs4_readdir_arg args = {
4534 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004535 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 .pgbase = 0,
4537 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004538 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004539 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 };
4541 struct nfs4_readdir_res res;
4542 struct rpc_message msg = {
4543 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4544 .rpc_argp = &args,
4545 .rpc_resp = &res,
4546 .rpc_cred = cred,
4547 };
4548 int status;
4549
Al Viro6de14722013-09-16 10:53:17 -04004550 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4551 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004552 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004553 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004555 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 -05004556 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004557 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004558 status += args.pgbase;
4559 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004560
4561 nfs_invalidate_atime(dir);
4562
Harvey Harrison3110ff82008-05-02 13:42:44 -07004563 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004564 return status;
4565}
4566
4567static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004568 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569{
4570 struct nfs4_exception exception = { };
4571 int err;
4572 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004573 err = _nfs4_proc_readdir(dentry, cred, cookie,
4574 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004575 trace_nfs4_readdir(d_inode(dentry), err);
4576 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 &exception);
4578 } while (exception.retry);
4579 return err;
4580}
4581
4582static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004583 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004585 struct nfs4_createdata *data;
4586 int mode = sattr->ia_mode;
4587 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004589 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4590 if (data == NULL)
4591 goto out;
4592
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004594 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004596 data->arg.ftype = NF4BLK;
4597 data->arg.u.device.specdata1 = MAJOR(rdev);
4598 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 }
4600 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004601 data->arg.ftype = NF4CHR;
4602 data->arg.u.device.specdata1 = MAJOR(rdev);
4603 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004604 } else if (!S_ISSOCK(mode)) {
4605 status = -EINVAL;
4606 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004607 }
David Quigley1775fd32013-05-22 12:50:42 -04004608
David Quigleyaa9c2662013-05-22 12:50:44 -04004609 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004610 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004611out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004612 nfs4_free_createdata(data);
4613out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614 return status;
4615}
4616
4617static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4618 struct iattr *sattr, dev_t rdev)
4619{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004620 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004622 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004623 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004624
David Quigleyaa9c2662013-05-22 12:50:44 -04004625 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4626
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004627 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4628 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004630 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4631 trace_nfs4_mknod(dir, &dentry->d_name, err);
4632 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 &exception);
4634 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004635
4636 nfs4_label_release_security(label);
4637
Linus Torvalds1da177e2005-04-16 15:20:36 -07004638 return err;
4639}
4640
4641static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4642 struct nfs_fsstat *fsstat)
4643{
4644 struct nfs4_statfs_arg args = {
4645 .fh = fhandle,
4646 .bitmask = server->attr_bitmask,
4647 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004648 struct nfs4_statfs_res res = {
4649 .fsstat = fsstat,
4650 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004651 struct rpc_message msg = {
4652 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4653 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004654 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 };
4656
Trond Myklebust0e574af2005-10-27 22:12:38 -04004657 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004658 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659}
4660
4661static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4662{
4663 struct nfs4_exception exception = { };
4664 int err;
4665 do {
4666 err = nfs4_handle_exception(server,
4667 _nfs4_proc_statfs(server, fhandle, fsstat),
4668 &exception);
4669 } while (exception.retry);
4670 return err;
4671}
4672
4673static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4674 struct nfs_fsinfo *fsinfo)
4675{
4676 struct nfs4_fsinfo_arg args = {
4677 .fh = fhandle,
4678 .bitmask = server->attr_bitmask,
4679 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004680 struct nfs4_fsinfo_res res = {
4681 .fsinfo = fsinfo,
4682 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004683 struct rpc_message msg = {
4684 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4685 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004686 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 };
4688
Bryan Schumaker7c513052011-03-24 17:12:24 +00004689 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004690}
4691
4692static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4693{
4694 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004695 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004696 int err;
4697
4698 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004699 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004700 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004701 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004702 nfs4_set_lease_period(server->nfs_client,
4703 fsinfo->lease_time * HZ,
4704 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004705 break;
4706 }
4707 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004708 } while (exception.retry);
4709 return err;
4710}
4711
4712static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4713{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004714 int error;
4715
Trond Myklebust0e574af2005-10-27 22:12:38 -04004716 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004717 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004718 if (error == 0) {
4719 /* block layout checks this! */
4720 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004721 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004722 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004723
4724 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004725}
4726
4727static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4728 struct nfs_pathconf *pathconf)
4729{
4730 struct nfs4_pathconf_arg args = {
4731 .fh = fhandle,
4732 .bitmask = server->attr_bitmask,
4733 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004734 struct nfs4_pathconf_res res = {
4735 .pathconf = pathconf,
4736 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004737 struct rpc_message msg = {
4738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4739 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004740 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 };
4742
4743 /* None of the pathconf attributes are mandatory to implement */
4744 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4745 memset(pathconf, 0, sizeof(*pathconf));
4746 return 0;
4747 }
4748
Trond Myklebust0e574af2005-10-27 22:12:38 -04004749 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004750 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
4753static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4754 struct nfs_pathconf *pathconf)
4755{
4756 struct nfs4_exception exception = { };
4757 int err;
4758
4759 do {
4760 err = nfs4_handle_exception(server,
4761 _nfs4_proc_pathconf(server, fhandle, pathconf),
4762 &exception);
4763 } while (exception.retry);
4764 return err;
4765}
4766
Trond Myklebust5521abf2013-03-16 20:54:34 -04004767int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004768 const struct nfs_open_context *ctx,
4769 const struct nfs_lock_context *l_ctx,
4770 fmode_t fmode)
4771{
NeilBrown17393472016-10-13 15:26:47 +11004772 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004773}
4774EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4775
Trond Myklebust5521abf2013-03-16 20:54:34 -04004776static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4777 const struct nfs_open_context *ctx,
4778 const struct nfs_lock_context *l_ctx,
4779 fmode_t fmode)
4780{
4781 nfs4_stateid current_stateid;
4782
Trond Myklebuste1253be2014-03-05 08:44:23 -05004783 /* If the current stateid represents a lost lock, then exit */
4784 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4785 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004786 return nfs4_stateid_match(stateid, &current_stateid);
4787}
4788
4789static bool nfs4_error_stateid_expired(int err)
4790{
4791 switch (err) {
4792 case -NFS4ERR_DELEG_REVOKED:
4793 case -NFS4ERR_ADMIN_REVOKED:
4794 case -NFS4ERR_BAD_STATEID:
4795 case -NFS4ERR_STALE_STATEID:
4796 case -NFS4ERR_OLD_STATEID:
4797 case -NFS4ERR_OPENMODE:
4798 case -NFS4ERR_EXPIRED:
4799 return true;
4800 }
4801 return false;
4802}
4803
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004804static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004805{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004806 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004807
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004808 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004809 if (task->tk_status < 0) {
4810 struct nfs4_exception exception = {
4811 .inode = hdr->inode,
4812 .state = hdr->args.context->state,
4813 .stateid = &hdr->args.stateid,
4814 };
4815 task->tk_status = nfs4_async_handle_exception(task,
4816 server, task->tk_status, &exception);
4817 if (exception.retry) {
4818 rpc_restart_call_prepare(task);
4819 return -EAGAIN;
4820 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004821 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004822
Linus Torvalds1da177e2005-04-16 15:20:36 -07004823 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004824 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004825 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004826}
4827
Trond Myklebust5521abf2013-03-16 20:54:34 -04004828static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004829 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004830{
4831
4832 if (!nfs4_error_stateid_expired(task->tk_status) ||
4833 nfs4_stateid_is_current(&args->stateid,
4834 args->context,
4835 args->lock_context,
4836 FMODE_READ))
4837 return false;
4838 rpc_restart_call_prepare(task);
4839 return true;
4840}
4841
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004842static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004843{
4844
4845 dprintk("--> %s\n", __func__);
4846
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004847 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004848 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004849 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004850 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004851 if (task->tk_status > 0)
4852 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004853 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4854 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004855}
4856
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004857static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4858 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004859{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004860 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004861 if (!hdr->pgio_done_cb)
4862 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004863 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004864 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004865}
4866
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004867static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4868 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004869{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004870 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004871 &hdr->args.seq_args,
4872 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004873 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004874 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004875 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4876 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004877 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004878 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004879 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004880 return -EIO;
4881 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004882}
4883
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004884static int nfs4_write_done_cb(struct rpc_task *task,
4885 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004888
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004889 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004890 if (task->tk_status < 0) {
4891 struct nfs4_exception exception = {
4892 .inode = hdr->inode,
4893 .state = hdr->args.context->state,
4894 .stateid = &hdr->args.stateid,
4895 };
4896 task->tk_status = nfs4_async_handle_exception(task,
4897 NFS_SERVER(inode), task->tk_status,
4898 &exception);
4899 if (exception.retry) {
4900 rpc_restart_call_prepare(task);
4901 return -EAGAIN;
4902 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004903 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004904 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004905 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004906 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004907 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004908 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004909}
4910
Trond Myklebust5521abf2013-03-16 20:54:34 -04004911static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004912 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004913{
4914
4915 if (!nfs4_error_stateid_expired(task->tk_status) ||
4916 nfs4_stateid_is_current(&args->stateid,
4917 args->context,
4918 args->lock_context,
4919 FMODE_WRITE))
4920 return false;
4921 rpc_restart_call_prepare(task);
4922 return true;
4923}
4924
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004925static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004926{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004927 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004928 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004929 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004930 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004931 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4932 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004933}
4934
Trond Myklebust5a37f852012-04-28 14:55:16 -04004935static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004936bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004937{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004938 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004939 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004940 return false;
4941 /* Otherwise, request attributes if and only if we don't hold
4942 * a delegation
4943 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004944 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004945}
Fred Isamana69aef12011-03-03 15:13:47 +00004946
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004947static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4948 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004949{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004950 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004951
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004952 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4953 hdr->args.bitmask = NULL;
4954 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004955 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004956 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004957
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004958 if (!hdr->pgio_done_cb)
4959 hdr->pgio_done_cb = nfs4_write_done_cb;
4960 hdr->res.server = server;
4961 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004962
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004963 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004964 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004965}
4966
Fred Isaman0b7c0152012-04-20 14:47:39 -04004967static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4968{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004969 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004970 &data->args.seq_args,
4971 &data->res.seq_res,
4972 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004973}
4974
Fred Isaman0b7c0152012-04-20 14:47:39 -04004975static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004978
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004979 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004980 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4981 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004982 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004983 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004985 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004986}
4987
Fred Isaman0b7c0152012-04-20 14:47:39 -04004988static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004989{
4990 if (!nfs4_sequence_done(task, &data->res.seq_res))
4991 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004992 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004993}
4994
Fred Isaman0b7c0152012-04-20 14:47:39 -04004995static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004996{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004997 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004998
Fred Isaman0b7c0152012-04-20 14:47:39 -04004999 if (data->commit_done_cb == NULL)
5000 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005001 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005002 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04005003 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004}
5005
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005006struct nfs4_renewdata {
5007 struct nfs_client *client;
5008 unsigned long timestamp;
5009};
5010
Linus Torvalds1da177e2005-04-16 15:20:36 -07005011/*
5012 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5013 * standalone procedure for queueing an asynchronous RENEW.
5014 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005015static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005016{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005017 struct nfs4_renewdata *data = calldata;
5018 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005019
Elena Reshetova212bf412017-10-20 12:53:38 +03005020 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005021 nfs4_schedule_state_renewal(clp);
5022 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005023 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005024}
5025
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005026static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005027{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005028 struct nfs4_renewdata *data = calldata;
5029 struct nfs_client *clp = data->client;
5030 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005032 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005033 switch (task->tk_status) {
5034 case 0:
5035 break;
5036 case -NFS4ERR_LEASE_MOVED:
5037 nfs4_schedule_lease_moved_recovery(clp);
5038 break;
5039 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005040 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005041 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5042 return;
5043 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005044 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005045 return;
5046 }
5047 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005048 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005049 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005050}
5051
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005052static const struct rpc_call_ops nfs4_renew_ops = {
5053 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005054 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005055};
5056
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005057static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005058{
5059 struct rpc_message msg = {
5060 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5061 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005062 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005064 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005066 if (renew_flags == 0)
5067 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005068 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005069 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005070 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005071 if (data == NULL) {
5072 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005073 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005074 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005075 data->client = clp;
5076 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005077 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005078 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005079}
5080
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005081static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005082{
5083 struct rpc_message msg = {
5084 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5085 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005086 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005087 };
5088 unsigned long now = jiffies;
5089 int status;
5090
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005091 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005092 if (status < 0)
5093 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005094 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095 return 0;
5096}
5097
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005098static inline int nfs4_server_supports_acls(struct nfs_server *server)
5099{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005100 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005101}
5102
Trond Myklebust21f498c2012-08-24 10:59:25 -04005103/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5104 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005105 * the stack.
5106 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005107#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005108
Neil Hormane9e3d722011-03-04 19:26:03 -05005109static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005110 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005111{
5112 struct page *newpage, **spages;
5113 int rc = 0;
5114 size_t len;
5115 spages = pages;
5116
5117 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005118 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005119 newpage = alloc_page(GFP_KERNEL);
5120
5121 if (newpage == NULL)
5122 goto unwind;
5123 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005124 buf += len;
5125 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005126 *pages++ = newpage;
5127 rc++;
5128 } while (buflen != 0);
5129
5130 return rc;
5131
5132unwind:
5133 for(; rc > 0; rc--)
5134 __free_page(spages[rc-1]);
5135 return -ENOMEM;
5136}
5137
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005138struct nfs4_cached_acl {
5139 int cached;
5140 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005141 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005142};
5143
5144static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005145{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005146 struct nfs_inode *nfsi = NFS_I(inode);
5147
5148 spin_lock(&inode->i_lock);
5149 kfree(nfsi->nfs4_acl);
5150 nfsi->nfs4_acl = acl;
5151 spin_unlock(&inode->i_lock);
5152}
5153
5154static void nfs4_zap_acl_attr(struct inode *inode)
5155{
5156 nfs4_set_cached_acl(inode, NULL);
5157}
5158
5159static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5160{
5161 struct nfs_inode *nfsi = NFS_I(inode);
5162 struct nfs4_cached_acl *acl;
5163 int ret = -ENOENT;
5164
5165 spin_lock(&inode->i_lock);
5166 acl = nfsi->nfs4_acl;
5167 if (acl == NULL)
5168 goto out;
5169 if (buf == NULL) /* user is just asking for length */
5170 goto out_len;
5171 if (acl->cached == 0)
5172 goto out;
5173 ret = -ERANGE; /* see getxattr(2) man page */
5174 if (acl->len > buflen)
5175 goto out;
5176 memcpy(buf, acl->data, acl->len);
5177out_len:
5178 ret = acl->len;
5179out:
5180 spin_unlock(&inode->i_lock);
5181 return ret;
5182}
5183
Sachin Prabhu5794d212012-04-17 14:36:40 +01005184static 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 +00005185{
5186 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005187 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005188
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005189 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005190 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005191 if (acl == NULL)
5192 goto out;
5193 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005194 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005195 } else {
5196 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5197 if (acl == NULL)
5198 goto out;
5199 acl->cached = 0;
5200 }
5201 acl->len = acl_len;
5202out:
5203 nfs4_set_cached_acl(inode, acl);
5204}
5205
Andy Adamsonbf118a32011-12-07 11:55:27 -05005206/*
5207 * The getxattr API returns the required buffer length when called with a
5208 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5209 * the required buf. On a NULL buf, we send a page of data to the server
5210 * guessing that the ACL request can be serviced by a page. If so, we cache
5211 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5212 * the cache. If not so, we throw away the page, and cache the required
5213 * length. The next getxattr call will then produce another round trip to
5214 * the server, this time with the input buf of the required size.
5215 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005216static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005217{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005218 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005219 struct nfs_getaclargs args = {
5220 .fh = NFS_FH(inode),
5221 .acl_pages = pages,
5222 .acl_len = buflen,
5223 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005224 struct nfs_getaclres res = {
5225 .acl_len = buflen,
5226 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005227 struct rpc_message msg = {
5228 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5229 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005230 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005231 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005232 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005233 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005234
Trond Myklebust21f498c2012-08-24 10:59:25 -04005235 if (npages > ARRAY_SIZE(pages))
5236 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005237
Andy Adamsonbf118a32011-12-07 11:55:27 -05005238 for (i = 0; i < npages; i++) {
5239 pages[i] = alloc_page(GFP_KERNEL);
5240 if (!pages[i])
5241 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005242 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005243
5244 /* for decoding across pages */
5245 res.acl_scratch = alloc_page(GFP_KERNEL);
5246 if (!res.acl_scratch)
5247 goto out_free;
5248
Andy Adamsonbf118a32011-12-07 11:55:27 -05005249 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005250
Peng Taode040be2012-01-10 22:42:47 +08005251 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005252 __func__, buf, buflen, npages, args.acl_len);
5253 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5254 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005255 if (ret)
5256 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005257
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005258 /* Handle the case where the passed-in buffer is too short */
5259 if (res.acl_flags & NFS4_ACL_TRUNC) {
5260 /* Did the user only issue a request for the acl length? */
5261 if (buf == NULL)
5262 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005263 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005264 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005265 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005266 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005267 if (buf) {
5268 if (res.acl_len > buflen) {
5269 ret = -ERANGE;
5270 goto out_free;
5271 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005272 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005273 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005274out_ok:
5275 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005276out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005277 for (i = 0; i < npages; i++)
5278 if (pages[i])
5279 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005280 if (res.acl_scratch)
5281 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005282 return ret;
5283}
5284
Trond Myklebust16b42892006-08-24 12:27:15 -04005285static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5286{
5287 struct nfs4_exception exception = { };
5288 ssize_t ret;
5289 do {
5290 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005291 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005292 if (ret >= 0)
5293 break;
5294 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5295 } while (exception.retry);
5296 return ret;
5297}
5298
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005299static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5300{
5301 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005302 int ret;
5303
5304 if (!nfs4_server_supports_acls(server))
5305 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005306 ret = nfs_revalidate_inode(server, inode);
5307 if (ret < 0)
5308 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005309 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5310 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005311 ret = nfs4_read_cached_acl(inode, buf, buflen);
5312 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005313 /* -ENOENT is returned if there is no ACL or if there is an ACL
5314 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005315 return ret;
5316 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005317}
5318
Trond Myklebust16b42892006-08-24 12:27:15 -04005319static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005320{
5321 struct nfs_server *server = NFS_SERVER(inode);
5322 struct page *pages[NFS4ACL_MAXPAGES];
5323 struct nfs_setaclargs arg = {
5324 .fh = NFS_FH(inode),
5325 .acl_pages = pages,
5326 .acl_len = buflen,
5327 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005328 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005329 struct rpc_message msg = {
5330 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5331 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005332 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005333 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005334 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005335 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005336
5337 if (!nfs4_server_supports_acls(server))
5338 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005339 if (npages > ARRAY_SIZE(pages))
5340 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005341 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005342 if (i < 0)
5343 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005344 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005345 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005346
5347 /*
5348 * Free each page after tx, so the only ref left is
5349 * held by the network stack
5350 */
5351 for (; i > 0; i--)
5352 put_page(pages[i-1]);
5353
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005354 /*
5355 * Acl update can result in inode attribute update.
5356 * so mark the attribute cache invalid.
5357 */
5358 spin_lock(&inode->i_lock);
5359 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5360 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005361 nfs_access_zap_cache(inode);
5362 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005363 return ret;
5364}
5365
Trond Myklebust16b42892006-08-24 12:27:15 -04005366static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5367{
5368 struct nfs4_exception exception = { };
5369 int err;
5370 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005371 err = __nfs4_proc_set_acl(inode, buf, buflen);
5372 trace_nfs4_set_acl(inode, err);
5373 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005374 &exception);
5375 } while (exception.retry);
5376 return err;
5377}
5378
David Quigleyaa9c2662013-05-22 12:50:44 -04005379#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5380static int _nfs4_get_security_label(struct inode *inode, void *buf,
5381 size_t buflen)
5382{
5383 struct nfs_server *server = NFS_SERVER(inode);
5384 struct nfs_fattr fattr;
5385 struct nfs4_label label = {0, 0, buflen, buf};
5386
5387 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005388 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005389 .fh = NFS_FH(inode),
5390 .bitmask = bitmask,
5391 };
5392 struct nfs4_getattr_res res = {
5393 .fattr = &fattr,
5394 .label = &label,
5395 .server = server,
5396 };
5397 struct rpc_message msg = {
5398 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005399 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005400 .rpc_resp = &res,
5401 };
5402 int ret;
5403
5404 nfs_fattr_init(&fattr);
5405
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005406 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005407 if (ret)
5408 return ret;
5409 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5410 return -ENOENT;
5411 if (buflen < label.len)
5412 return -ERANGE;
5413 return 0;
5414}
5415
5416static int nfs4_get_security_label(struct inode *inode, void *buf,
5417 size_t buflen)
5418{
5419 struct nfs4_exception exception = { };
5420 int err;
5421
5422 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5423 return -EOPNOTSUPP;
5424
5425 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005426 err = _nfs4_get_security_label(inode, buf, buflen);
5427 trace_nfs4_get_security_label(inode, err);
5428 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005429 &exception);
5430 } while (exception.retry);
5431 return err;
5432}
5433
5434static int _nfs4_do_set_security_label(struct inode *inode,
5435 struct nfs4_label *ilabel,
5436 struct nfs_fattr *fattr,
5437 struct nfs4_label *olabel)
5438{
5439
5440 struct iattr sattr = {0};
5441 struct nfs_server *server = NFS_SERVER(inode);
5442 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005443 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005444 .fh = NFS_FH(inode),
5445 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005446 .server = server,
5447 .bitmask = bitmask,
5448 .label = ilabel,
5449 };
5450 struct nfs_setattrres res = {
5451 .fattr = fattr,
5452 .label = olabel,
5453 .server = server,
5454 };
5455 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005456 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5457 .rpc_argp = &arg,
5458 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005459 };
5460 int status;
5461
Jeff Layton12207f62013-11-01 10:49:32 -04005462 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005463
Jeff Layton12207f62013-11-01 10:49:32 -04005464 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005465 if (status)
5466 dprintk("%s failed: %d\n", __func__, status);
5467
5468 return status;
5469}
5470
5471static int nfs4_do_set_security_label(struct inode *inode,
5472 struct nfs4_label *ilabel,
5473 struct nfs_fattr *fattr,
5474 struct nfs4_label *olabel)
5475{
5476 struct nfs4_exception exception = { };
5477 int err;
5478
5479 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005480 err = _nfs4_do_set_security_label(inode, ilabel,
5481 fattr, olabel);
5482 trace_nfs4_set_security_label(inode, err);
5483 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005484 &exception);
5485 } while (exception.retry);
5486 return err;
5487}
5488
5489static int
Al Viro59301222016-05-27 10:19:30 -04005490nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005491{
5492 struct nfs4_label ilabel, *olabel = NULL;
5493 struct nfs_fattr fattr;
5494 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005495 int status;
5496
5497 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5498 return -EOPNOTSUPP;
5499
5500 nfs_fattr_init(&fattr);
5501
5502 ilabel.pi = 0;
5503 ilabel.lfs = 0;
5504 ilabel.label = (char *)buf;
5505 ilabel.len = buflen;
5506
5507 cred = rpc_lookup_cred();
5508 if (IS_ERR(cred))
5509 return PTR_ERR(cred);
5510
5511 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5512 if (IS_ERR(olabel)) {
5513 status = -PTR_ERR(olabel);
5514 goto out;
5515 }
5516
5517 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5518 if (status == 0)
5519 nfs_setsecurity(inode, &fattr, olabel);
5520
5521 nfs4_label_free(olabel);
5522out:
5523 put_rpccred(cred);
5524 return status;
5525}
5526#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5527
5528
Chuck Leverf0920752012-05-21 22:45:41 -04005529static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5530 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005531{
5532 __be32 verf[2];
5533
Chuck Lever2c820d92012-05-21 22:45:33 -04005534 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5535 /* An impossible timestamp guarantees this value
5536 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005537 verf[0] = cpu_to_be32(U32_MAX);
5538 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005539 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005540 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005541 u64 ns = ktime_to_ns(nn->boot_time);
5542
5543 verf[0] = cpu_to_be32(ns >> 32);
5544 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005545 }
Chuck Levercd937102012-03-02 17:14:31 -05005546 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5547}
5548
Jeff Laytona3192682015-06-09 19:43:59 -04005549static int
5550nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005551{
Jeff Laytona3192682015-06-09 19:43:59 -04005552 size_t len;
5553 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005554
Trond Myklebustceb3a162015-01-03 15:16:04 -05005555 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005556 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005557
Jeff Laytona3192682015-06-09 19:43:59 -04005558 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005559 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005560 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5561 1 +
5562 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5563 1;
5564 rcu_read_unlock();
5565
5566 if (len > NFS4_OPAQUE_LIMIT + 1)
5567 return -EINVAL;
5568
5569 /*
5570 * Since this string is allocated at mount time, and held until the
5571 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5572 * about a memory-reclaim deadlock.
5573 */
5574 str = kmalloc(len, GFP_KERNEL);
5575 if (!str)
5576 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005577
Chuck Levere984a552012-09-14 17:24:21 -04005578 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005579 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005580 clp->cl_ipaddr,
5581 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5582 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005583 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005584
Jeff Laytona3192682015-06-09 19:43:59 -04005585 clp->cl_owner_id = str;
5586 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005587}
5588
Jeff Layton873e3852015-06-09 19:44:00 -04005589static int
5590nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005591{
Jeff Layton873e3852015-06-09 19:44:00 -04005592 size_t len;
5593 char *str;
5594
5595 len = 10 + 10 + 1 + 10 + 1 +
5596 strlen(nfs4_client_id_uniquifier) + 1 +
5597 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5598
5599 if (len > NFS4_OPAQUE_LIMIT + 1)
5600 return -EINVAL;
5601
5602 /*
5603 * Since this string is allocated at mount time, and held until the
5604 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5605 * about a memory-reclaim deadlock.
5606 */
5607 str = kmalloc(len, GFP_KERNEL);
5608 if (!str)
5609 return -ENOMEM;
5610
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005611 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005612 clp->rpc_ops->version, clp->cl_minorversion,
5613 nfs4_client_id_uniquifier,
5614 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005615 clp->cl_owner_id = str;
5616 return 0;
5617}
5618
5619static int
5620nfs4_init_uniform_client_string(struct nfs_client *clp)
5621{
Jeff Layton873e3852015-06-09 19:44:00 -04005622 size_t len;
5623 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005624
5625 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005626 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005627
5628 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005629 return nfs4_init_uniquifier_client_string(clp);
5630
5631 len = 10 + 10 + 1 + 10 + 1 +
5632 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5633
5634 if (len > NFS4_OPAQUE_LIMIT + 1)
5635 return -EINVAL;
5636
5637 /*
5638 * Since this string is allocated at mount time, and held until the
5639 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5640 * about a memory-reclaim deadlock.
5641 */
5642 str = kmalloc(len, GFP_KERNEL);
5643 if (!str)
5644 return -ENOMEM;
5645
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005646 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005647 clp->rpc_ops->version, clp->cl_minorversion,
5648 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005649 clp->cl_owner_id = str;
5650 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005651}
5652
Chuck Lever706cb8d2014-03-12 12:51:47 -04005653/*
5654 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5655 * services. Advertise one based on the address family of the
5656 * clientaddr.
5657 */
5658static unsigned int
5659nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5660{
5661 if (strchr(clp->cl_ipaddr, ':') != NULL)
5662 return scnprintf(buf, len, "tcp6");
5663 else
5664 return scnprintf(buf, len, "tcp");
5665}
5666
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005667static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5668{
5669 struct nfs4_setclientid *sc = calldata;
5670
5671 if (task->tk_status == 0)
5672 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5673}
5674
5675static const struct rpc_call_ops nfs4_setclientid_ops = {
5676 .rpc_call_done = nfs4_setclientid_done,
5677};
5678
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005679/**
5680 * nfs4_proc_setclientid - Negotiate client ID
5681 * @clp: state data structure
5682 * @program: RPC program for NFSv4 callback service
5683 * @port: IP port number for NFS4 callback service
5684 * @cred: RPC credential to use for this call
5685 * @res: where to place the result
5686 *
5687 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5688 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005689int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5690 unsigned short port, struct rpc_cred *cred,
5691 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005692{
5693 nfs4_verifier sc_verifier;
5694 struct nfs4_setclientid setclientid = {
5695 .sc_verifier = &sc_verifier,
5696 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005697 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005698 };
5699 struct rpc_message msg = {
5700 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5701 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005702 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005703 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005704 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005705 struct rpc_task *task;
5706 struct rpc_task_setup task_setup_data = {
5707 .rpc_client = clp->cl_rpcclient,
5708 .rpc_message = &msg,
5709 .callback_ops = &nfs4_setclientid_ops,
5710 .callback_data = &setclientid,
5711 .flags = RPC_TASK_TIMEOUT,
5712 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005713 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005714
Chuck Leverde734832012-07-11 16:30:50 -04005715 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005716 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005717
5718 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5719 status = nfs4_init_uniform_client_string(clp);
5720 else
Jeff Laytona3192682015-06-09 19:43:59 -04005721 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005722
5723 if (status)
5724 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005725
Chuck Leverde734832012-07-11 16:30:50 -04005726 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005727 setclientid.sc_netid_len =
5728 nfs4_init_callback_netid(clp,
5729 setclientid.sc_netid,
5730 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005731 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005732 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005733 clp->cl_ipaddr, port >> 8, port & 255);
5734
Jeff Layton3a6bb732015-06-09 19:43:57 -04005735 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005736 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005737 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005738 task = rpc_run_task(&task_setup_data);
5739 if (IS_ERR(task)) {
5740 status = PTR_ERR(task);
5741 goto out;
5742 }
5743 status = task->tk_status;
5744 if (setclientid.sc_cred) {
5745 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5746 put_rpccred(setclientid.sc_cred);
5747 }
5748 rpc_put_task(task);
5749out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005750 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005751 dprintk("NFS reply setclientid: %d\n", status);
5752 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005753}
5754
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005755/**
5756 * nfs4_proc_setclientid_confirm - Confirm client ID
5757 * @clp: state data structure
5758 * @res: result of a previous SETCLIENTID
5759 * @cred: RPC credential to use for this call
5760 *
5761 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5762 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005763int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005764 struct nfs4_setclientid_res *arg,
5765 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005766{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005767 struct rpc_message msg = {
5768 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005769 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005770 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005772 int status;
5773
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005774 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5775 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5776 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005777 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005778 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005779 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005780 return status;
5781}
5782
Trond Myklebustfe650402006-01-03 09:55:18 +01005783struct nfs4_delegreturndata {
5784 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005785 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005786 struct nfs_fh fh;
5787 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005788 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005789 struct {
5790 struct nfs4_layoutreturn_args arg;
5791 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005792 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005793 u32 roc_barrier;
5794 bool roc;
5795 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005796 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005797 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005798 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005799};
5800
Trond Myklebustfe650402006-01-03 09:55:18 +01005801static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5802{
5803 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005804 struct nfs4_exception exception = {
5805 .inode = data->inode,
5806 .stateid = &data->stateid,
5807 };
Andy Adamson938e1012009-04-01 09:22:28 -04005808
Trond Myklebust14516c32010-07-31 14:29:06 -04005809 if (!nfs4_sequence_done(task, &data->res.seq_res))
5810 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005811
Trond Myklebustca8acf82013-08-13 10:36:56 -04005812 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005813
5814 /* Handle Layoutreturn errors */
5815 if (data->args.lr_args && task->tk_status != 0) {
5816 switch(data->res.lr_ret) {
5817 default:
5818 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5819 break;
5820 case 0:
5821 data->args.lr_args = NULL;
5822 data->res.lr_res = NULL;
5823 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005824 case -NFS4ERR_OLD_STATEID:
5825 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005826 data->inode))
5827 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005828 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005829 case -NFS4ERR_ADMIN_REVOKED:
5830 case -NFS4ERR_DELEG_REVOKED:
5831 case -NFS4ERR_EXPIRED:
5832 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005833 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5834 case -NFS4ERR_WRONG_CRED:
5835 data->args.lr_args = NULL;
5836 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005837 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005838 }
5839 }
5840
Ricardo Labiaga79708862009-12-07 09:23:21 -05005841 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005842 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005843 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005844 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005845 case -NFS4ERR_ADMIN_REVOKED:
5846 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005847 case -NFS4ERR_EXPIRED:
5848 nfs4_free_revoked_stateid(data->res.server,
5849 data->args.stateid,
5850 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005851 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005852 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005853 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005854 task->tk_status = 0;
5855 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005856 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005857 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5858 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005859 task->tk_status = 0;
5860 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005861 case -NFS4ERR_ACCESS:
5862 if (data->args.bitmask) {
5863 data->args.bitmask = NULL;
5864 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005865 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005866 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005867 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005868 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005869 task->tk_status = nfs4_async_handle_exception(task,
5870 data->res.server, task->tk_status,
5871 &exception);
5872 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005873 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005874 }
5875 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005876 return;
5877lr_restart:
5878 data->res.lr_ret = 0;
5879out_restart:
5880 task->tk_status = 0;
5881 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005882}
5883
5884static void nfs4_delegreturn_release(void *calldata)
5885{
Peng Tao039b7562014-07-03 13:05:02 +08005886 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005887 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005888
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005889 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005890 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005891 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5892 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005893 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005894 nfs_iput_and_deactive(inode);
5895 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005896 kfree(calldata);
5897}
5898
Andy Adamson938e1012009-04-01 09:22:28 -04005899static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5900{
5901 struct nfs4_delegreturndata *d_data;
5902
5903 d_data = (struct nfs4_delegreturndata *)data;
5904
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005905 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005906 return;
5907
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005908 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005909 &d_data->args.seq_args,
5910 &d_data->res.seq_res,
5911 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005912}
Andy Adamson938e1012009-04-01 09:22:28 -04005913
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005914static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005915 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005916 .rpc_call_done = nfs4_delegreturn_done,
5917 .rpc_release = nfs4_delegreturn_release,
5918};
5919
Trond Myklebuste6f81072008-01-24 18:14:34 -05005920static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005921{
5922 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005923 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005924 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005925 struct rpc_message msg = {
5926 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5927 .rpc_cred = cred,
5928 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005929 struct rpc_task_setup task_setup_data = {
5930 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005931 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005932 .callback_ops = &nfs4_delegreturn_ops,
5933 .flags = RPC_TASK_ASYNC,
5934 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005935 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005936
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005937 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005938 if (data == NULL)
5939 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005940 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005941
5942 nfs4_state_protect(server->nfs_client,
5943 NFS_SP4_MACH_CRED_CLEANUP,
5944 &task_setup_data.rpc_client, &msg);
5945
Trond Myklebustfe650402006-01-03 09:55:18 +01005946 data->args.fhandle = &data->fh;
5947 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005948 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005949 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005950 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005951 data->res.fattr = &data->fattr;
5952 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005953 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005954 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005955 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005956 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005957 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005958 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005959 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005960 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005961 if (data->lr.roc) {
5962 data->args.lr_args = &data->lr.arg;
5963 data->res.lr_res = &data->lr.res;
5964 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005965 } else if (data->lr.roc) {
5966 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5967 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005968 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005969
Trond Myklebustc970aa82007-07-14 15:39:59 -04005970 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005971 msg.rpc_argp = &data->args;
5972 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005973 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005974 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005975 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005976 if (!issync)
5977 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005978 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005979 if (status != 0)
5980 goto out;
5981 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005982out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005983 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005984 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005985}
5986
Trond Myklebuste6f81072008-01-24 18:14:34 -05005987int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005988{
5989 struct nfs_server *server = NFS_SERVER(inode);
5990 struct nfs4_exception exception = { };
5991 int err;
5992 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005993 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005994 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005995 switch (err) {
5996 case -NFS4ERR_STALE_STATEID:
5997 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005998 case 0:
5999 return 0;
6000 }
6001 err = nfs4_handle_exception(server, err, &exception);
6002 } while (exception.retry);
6003 return err;
6004}
6005
Linus Torvalds1da177e2005-04-16 15:20:36 -07006006static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6007{
6008 struct inode *inode = state->inode;
6009 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006010 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006011 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006012 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006013 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006014 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006015 struct nfs_lockt_res res = {
6016 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017 };
6018 struct rpc_message msg = {
6019 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006020 .rpc_argp = &arg,
6021 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022 .rpc_cred = state->owner->so_cred,
6023 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006024 struct nfs4_lock_state *lsp;
6025 int status;
6026
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006027 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006028 status = nfs4_set_lock_state(state, request);
6029 if (status != 0)
6030 goto out;
6031 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006032 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006033 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006034 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006035 switch (status) {
6036 case 0:
6037 request->fl_type = F_UNLCK;
6038 break;
6039 case -NFS4ERR_DENIED:
6040 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006041 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006042 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006043 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006044out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 return status;
6046}
6047
6048static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6049{
6050 struct nfs4_exception exception = { };
6051 int err;
6052
6053 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006054 err = _nfs4_proc_getlk(state, cmd, request);
6055 trace_nfs4_get_lock(request, state, cmd, err);
6056 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006057 &exception);
6058 } while (exception.retry);
6059 return err;
6060}
6061
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006062struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006063 struct nfs_locku_args arg;
6064 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006065 struct nfs4_lock_state *lsp;
6066 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006067 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006068 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006069 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006070 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006071};
6072
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006073static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6074 struct nfs_open_context *ctx,
6075 struct nfs4_lock_state *lsp,
6076 struct nfs_seqid *seqid)
6077{
6078 struct nfs4_unlockdata *p;
6079 struct inode *inode = lsp->ls_state->inode;
6080
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006081 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006082 if (p == NULL)
6083 return NULL;
6084 p->arg.fh = NFS_FH(inode);
6085 p->arg.fl = &p->fl;
6086 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006087 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006088 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006089 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006090 /* Ensure we don't close file until we're done freeing locks! */
6091 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006092 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006093 memcpy(&p->fl, fl, sizeof(p->fl));
6094 p->server = NFS_SERVER(inode);
6095 return p;
6096}
6097
Trond Myklebust06f814a2006-01-03 09:55:07 +01006098static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006099{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006100 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006101 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006102 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006103 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006104 put_nfs_open_context(calldata->ctx);
6105 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006106}
6107
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006108static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006109{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006110 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006111 struct nfs4_exception exception = {
6112 .inode = calldata->lsp->ls_state->inode,
6113 .stateid = &calldata->arg.stateid,
6114 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006115
Trond Myklebust14516c32010-07-31 14:29:06 -04006116 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6117 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006118 switch (task->tk_status) {
6119 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006120 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006121 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006122 if (nfs4_update_lock_stateid(calldata->lsp,
6123 &calldata->res.stateid))
6124 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006125 case -NFS4ERR_ADMIN_REVOKED:
6126 case -NFS4ERR_EXPIRED:
6127 nfs4_free_revoked_stateid(calldata->server,
6128 &calldata->arg.stateid,
6129 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006130 case -NFS4ERR_BAD_STATEID:
6131 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006132 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006133 if (!nfs4_stateid_match(&calldata->arg.stateid,
6134 &calldata->lsp->ls_stateid))
6135 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006136 break;
6137 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006138 task->tk_status = nfs4_async_handle_exception(task,
6139 calldata->server, task->tk_status,
6140 &exception);
6141 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006142 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006143 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006144 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006145}
6146
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006147static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006148{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006149 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006150
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006151 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6152 nfs_async_iocounter_wait(task, calldata->l_ctx))
6153 return;
6154
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006155 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006156 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006157 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006158 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006159 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006160 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006161 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006162 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006163 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006164 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006165 &calldata->res.seq_res,
6166 task) != 0)
6167 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006168 return;
6169out_no_action:
6170 task->tk_action = NULL;
6171out_wait:
6172 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006173}
6174
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006175static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006176 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006177 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006178 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006179};
6180
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006181static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6182 struct nfs_open_context *ctx,
6183 struct nfs4_lock_state *lsp,
6184 struct nfs_seqid *seqid)
6185{
6186 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006187 struct rpc_message msg = {
6188 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6189 .rpc_cred = ctx->cred,
6190 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006191 struct rpc_task_setup task_setup_data = {
6192 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006193 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006194 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006195 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006196 .flags = RPC_TASK_ASYNC,
6197 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006198
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006199 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6200 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6201
Frank Filz137d6ac2007-07-09 15:32:29 -07006202 /* Ensure this is an unlock - when canceling a lock, the
6203 * canceled lock is passed in, and it won't be an unlock.
6204 */
6205 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006206 if (fl->fl_flags & FL_CLOSE)
6207 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006208
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006209 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6210 if (data == NULL) {
6211 nfs_free_seqid(seqid);
6212 return ERR_PTR(-ENOMEM);
6213 }
6214
Chuck Levera9c92d62013-08-09 12:48:18 -04006215 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006216 msg.rpc_argp = &data->arg;
6217 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006218 task_setup_data.callback_data = data;
6219 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006220}
6221
Linus Torvalds1da177e2005-04-16 15:20:36 -07006222static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6223{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006224 struct inode *inode = state->inode;
6225 struct nfs4_state_owner *sp = state->owner;
6226 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006227 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006228 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006229 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006230 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006231 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006232 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006233
Trond Myklebust9b073572006-06-29 16:38:34 -04006234 status = nfs4_set_lock_state(state, request);
6235 /* Unlock _before_ we do the RPC call */
6236 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006237 /* Exclude nfs_delegation_claim_locks() */
6238 mutex_lock(&sp->so_delegreturn_mutex);
6239 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006240 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006241 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006242 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006243 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006244 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006245 }
6246 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006247 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006248 if (status != 0)
6249 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006250 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006251 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006252 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6253 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006254 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6255 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006256 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006257 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006258 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006259 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006260 status = PTR_ERR(task);
6261 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006262 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006263 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006264 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006265out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006266 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006267 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006268 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006269}
6270
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006271struct nfs4_lockdata {
6272 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006273 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006274 struct nfs4_lock_state *lsp;
6275 struct nfs_open_context *ctx;
6276 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006277 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006278 int rpc_status;
6279 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006280 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006281};
6282
6283static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006284 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6285 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006286{
6287 struct nfs4_lockdata *p;
6288 struct inode *inode = lsp->ls_state->inode;
6289 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006290 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006291
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006292 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006293 if (p == NULL)
6294 return NULL;
6295
6296 p->arg.fh = NFS_FH(inode);
6297 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006298 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006299 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006300 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006301 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6302 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006303 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006304 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006305 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006306 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006307 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006308 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006309 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006310 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006311 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006312 p->ctx = get_nfs_open_context(ctx);
6313 memcpy(&p->fl, fl, sizeof(p->fl));
6314 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006315out_free_seqid:
6316 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006317out_free:
6318 kfree(p);
6319 return NULL;
6320}
6321
6322static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6323{
6324 struct nfs4_lockdata *data = calldata;
6325 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006326
Harvey Harrison3110ff82008-05-02 13:42:44 -07006327 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006328 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006329 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006330 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006331 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006332 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006333 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006334 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006335 nfs4_stateid_copy(&data->arg.open_stateid,
6336 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006337 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006338 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006339 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006340 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006341 nfs4_stateid_copy(&data->arg.lock_stateid,
6342 &data->lsp->ls_stateid);
6343 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006344 if (!nfs4_valid_open_stateid(state)) {
6345 data->rpc_status = -EBADF;
6346 task->tk_action = NULL;
6347 goto out_release_open_seqid;
6348 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006349 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006350 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006351 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006352 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006353 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006354 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006355out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006356 nfs_release_seqid(data->arg.open_seqid);
6357out_release_lock_seqid:
6358 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006359out_wait:
6360 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006361 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006362}
6363
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006364static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6365{
6366 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006367 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006368
Harvey Harrison3110ff82008-05-02 13:42:44 -07006369 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006370
Trond Myklebust14516c32010-07-31 14:29:06 -04006371 if (!nfs4_sequence_done(task, &data->res.seq_res))
6372 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006373
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006374 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006375 switch (task->tk_status) {
6376 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006377 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006378 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006379 if (data->arg.new_lock) {
6380 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006381 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006382 rpc_restart_call_prepare(task);
6383 break;
6384 }
6385 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006386 if (data->arg.new_lock_owner != 0) {
6387 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6388 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6389 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6390 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6391 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006392 break;
6393 case -NFS4ERR_BAD_STATEID:
6394 case -NFS4ERR_OLD_STATEID:
6395 case -NFS4ERR_STALE_STATEID:
6396 case -NFS4ERR_EXPIRED:
6397 if (data->arg.new_lock_owner != 0) {
6398 if (!nfs4_stateid_match(&data->arg.open_stateid,
6399 &lsp->ls_state->open_stateid))
6400 rpc_restart_call_prepare(task);
6401 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6402 &lsp->ls_stateid))
6403 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006404 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006405 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006406}
6407
6408static void nfs4_lock_release(void *calldata)
6409{
6410 struct nfs4_lockdata *data = calldata;
6411
Harvey Harrison3110ff82008-05-02 13:42:44 -07006412 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006413 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006414 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006415 struct rpc_task *task;
6416 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6417 data->arg.lock_seqid);
6418 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006419 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006420 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006421 } else
6422 nfs_free_seqid(data->arg.lock_seqid);
6423 nfs4_put_lock_state(data->lsp);
6424 put_nfs_open_context(data->ctx);
6425 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006426 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006427}
6428
6429static const struct rpc_call_ops nfs4_lock_ops = {
6430 .rpc_call_prepare = nfs4_lock_prepare,
6431 .rpc_call_done = nfs4_lock_done,
6432 .rpc_release = nfs4_lock_release,
6433};
6434
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006435static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6436{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006437 switch (error) {
6438 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006439 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006440 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006441 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006442 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006443 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006444 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006445 break;
6446 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006447 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006448 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006449 };
6450}
6451
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006452static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006453{
6454 struct nfs4_lockdata *data;
6455 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006456 struct rpc_message msg = {
6457 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6458 .rpc_cred = state->owner->so_cred,
6459 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006460 struct rpc_task_setup task_setup_data = {
6461 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006462 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006463 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006464 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006465 .flags = RPC_TASK_ASYNC,
6466 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006467 int ret;
6468
Harvey Harrison3110ff82008-05-02 13:42:44 -07006469 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006470 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006471 fl->fl_u.nfs4_fl.owner,
6472 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006473 if (data == NULL)
6474 return -ENOMEM;
6475 if (IS_SETLKW(cmd))
6476 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006477 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006478 msg.rpc_argp = &data->arg;
6479 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006480 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006481 if (recovery_type > NFS_LOCK_NEW) {
6482 if (recovery_type == NFS_LOCK_RECLAIM)
6483 data->arg.reclaim = NFS_LOCK_RECLAIM;
6484 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006485 } else
6486 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006487 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006488 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006489 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006490 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006491 if (ret == 0) {
6492 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006493 if (ret)
6494 nfs4_handle_setlk_error(data->server, data->lsp,
6495 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006496 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006497 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006498 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006499 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006500 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006501 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006502}
6503
6504static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6505{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006506 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006507 struct nfs4_exception exception = {
6508 .inode = state->inode,
6509 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006510 int err;
6511
6512 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006513 /* Cache the lock if possible... */
6514 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6515 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006516 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006517 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006518 break;
6519 nfs4_handle_exception(server, err, &exception);
6520 } while (exception.retry);
6521 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006522}
6523
6524static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6525{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006526 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006527 struct nfs4_exception exception = {
6528 .inode = state->inode,
6529 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006530 int err;
6531
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006532 err = nfs4_set_lock_state(state, request);
6533 if (err != 0)
6534 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006535 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006536 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6537 return 0;
6538 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006539 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006540 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6541 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006542 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006543 switch (err) {
6544 default:
6545 goto out;
6546 case -NFS4ERR_GRACE:
6547 case -NFS4ERR_DELAY:
6548 nfs4_handle_exception(server, err, &exception);
6549 err = 0;
6550 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006551 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006552out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006553 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006554}
6555
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006556#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006557static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6558{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006559 struct nfs4_lock_state *lsp;
6560 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006561
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006562 status = nfs4_set_lock_state(state, request);
6563 if (status != 0)
6564 return status;
6565 lsp = request->fl_u.nfs4_fl.owner;
6566 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6567 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6568 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006569 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006570}
6571#endif
6572
Linus Torvalds1da177e2005-04-16 15:20:36 -07006573static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6574{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006575 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006576 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006577 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006578 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579
Trond Myklebust01c3b862006-06-29 16:38:39 -04006580 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006581 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006582 if (status < 0)
6583 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006584 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006585 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006586 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006587 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006588 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006589 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006590 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006591 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006592 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006593 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006594 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006595 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006596 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006597 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006598out:
6599 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006600 return status;
6601}
6602
6603static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6604{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006605 struct nfs4_exception exception = {
6606 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006607 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006608 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609 int err;
6610
6611 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006612 err = _nfs4_proc_setlk(state, cmd, request);
6613 if (err == -NFS4ERR_DENIED)
6614 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006616 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006617 } while (exception.retry);
6618 return err;
6619}
6620
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006621#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6622#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6623
6624static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006625nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6626 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006627{
6628 int status = -ERESTARTSYS;
6629 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6630
6631 while(!signalled()) {
6632 status = nfs4_proc_setlk(state, cmd, request);
6633 if ((status != -EAGAIN) || IS_SETLK(cmd))
6634 break;
6635 freezable_schedule_timeout_interruptible(timeout);
6636 timeout *= 2;
6637 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6638 status = -ERESTARTSYS;
6639 }
6640 return status;
6641}
6642
Jeff Laytona1d617d82016-09-17 18:17:39 -04006643#ifdef CONFIG_NFS_V4_1
6644struct nfs4_lock_waiter {
6645 struct task_struct *task;
6646 struct inode *inode;
6647 struct nfs_lowner *owner;
6648 bool notified;
6649};
6650
6651static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006652nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006653{
6654 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006655 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006656
Jeff Layton57174592018-03-18 08:37:03 -04006657 /* NULL key means to wake up everyone */
6658 if (key) {
6659 struct cb_notify_lock_args *cbnl = key;
6660 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6661 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006662
Jeff Layton57174592018-03-18 08:37:03 -04006663 /* Only wake if the callback was for the same owner. */
6664 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6665 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006666
Jeff Layton57174592018-03-18 08:37:03 -04006667 /* Make sure it's for the right inode */
6668 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6669 return 0;
6670
6671 waiter->notified = true;
6672 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006673
6674 /* override "private" so we can use default_wake_function */
6675 wait->private = waiter->task;
6676 ret = autoremove_wake_function(wait, mode, flags, key);
6677 wait->private = waiter;
6678 return ret;
6679}
6680
6681static int
6682nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6683{
6684 int status = -ERESTARTSYS;
6685 unsigned long flags;
6686 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6687 struct nfs_server *server = NFS_SERVER(state->inode);
6688 struct nfs_client *clp = server->nfs_client;
6689 wait_queue_head_t *q = &clp->cl_lock_waitq;
6690 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6691 .id = lsp->ls_seqid.owner_id,
6692 .s_dev = server->s_dev };
6693 struct nfs4_lock_waiter waiter = { .task = current,
6694 .inode = state->inode,
6695 .owner = &owner,
6696 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006697 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006698
6699 /* Don't bother with waitqueue if we don't expect a callback */
6700 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6701 return nfs4_retry_setlk_simple(state, cmd, request);
6702
6703 init_wait(&wait);
6704 wait.private = &waiter;
6705 wait.func = nfs4_wake_lock_waiter;
6706 add_wait_queue(q, &wait);
6707
6708 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006709 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006710 status = nfs4_proc_setlk(state, cmd, request);
6711 if ((status != -EAGAIN) || IS_SETLK(cmd))
6712 break;
6713
6714 status = -ERESTARTSYS;
6715 spin_lock_irqsave(&q->lock, flags);
6716 if (waiter.notified) {
6717 spin_unlock_irqrestore(&q->lock, flags);
6718 continue;
6719 }
6720 set_current_state(TASK_INTERRUPTIBLE);
6721 spin_unlock_irqrestore(&q->lock, flags);
6722
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006723 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006724 }
6725
6726 finish_wait(q, &wait);
6727 return status;
6728}
6729#else /* !CONFIG_NFS_V4_1 */
6730static inline int
6731nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6732{
6733 return nfs4_retry_setlk_simple(state, cmd, request);
6734}
6735#endif
6736
Linus Torvalds1da177e2005-04-16 15:20:36 -07006737static int
6738nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6739{
6740 struct nfs_open_context *ctx;
6741 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006742 int status;
6743
6744 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006745 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 state = ctx->state;
6747
Trond Myklebustd9531262009-07-21 19:22:38 -04006748 if (IS_GETLK(cmd)) {
6749 if (state != NULL)
6750 return nfs4_proc_getlk(state, F_GETLK, request);
6751 return 0;
6752 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006753
6754 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6755 return -EINVAL;
6756
Trond Myklebustd9531262009-07-21 19:22:38 -04006757 if (request->fl_type == F_UNLCK) {
6758 if (state != NULL)
6759 return nfs4_proc_unlck(state, cmd, request);
6760 return 0;
6761 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006762
Trond Myklebustd9531262009-07-21 19:22:38 -04006763 if (state == NULL)
6764 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006765
6766 if ((request->fl_flags & FL_POSIX) &&
6767 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6768 return -ENOLCK;
6769
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006770 /*
6771 * Don't rely on the VFS having checked the file open mode,
6772 * since it won't do this for flock() locks.
6773 */
6774 switch (request->fl_type) {
6775 case F_RDLCK:
6776 if (!(filp->f_mode & FMODE_READ))
6777 return -EBADF;
6778 break;
6779 case F_WRLCK:
6780 if (!(filp->f_mode & FMODE_WRITE))
6781 return -EBADF;
6782 }
6783
Jeff Layton1ea67db2016-09-17 18:17:37 -04006784 status = nfs4_set_lock_state(state, request);
6785 if (status != 0)
6786 return status;
6787
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006788 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789}
6790
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006791int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006792{
6793 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006794 int err;
6795
6796 err = nfs4_set_lock_state(state, fl);
6797 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006798 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006799 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006800 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006801}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006802
Trond Myklebustcf470c32012-03-07 13:49:12 -05006803struct nfs_release_lockowner_data {
6804 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006805 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006806 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006807 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006808 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006809};
6810
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006811static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6812{
6813 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006814 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006815 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6816 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006817 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006818 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006819}
6820
6821static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6822{
6823 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006824 struct nfs_server *server = data->server;
6825
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006826 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006827
6828 switch (task->tk_status) {
6829 case 0:
6830 renew_lease(server, data->timestamp);
6831 break;
6832 case -NFS4ERR_STALE_CLIENTID:
6833 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006834 nfs4_schedule_lease_recovery(server->nfs_client);
6835 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006836 case -NFS4ERR_LEASE_MOVED:
6837 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006838 if (nfs4_async_handle_error(task, server,
6839 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006840 rpc_restart_call_prepare(task);
6841 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006842}
6843
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006844static void nfs4_release_lockowner_release(void *calldata)
6845{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006846 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006847 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006848 kfree(calldata);
6849}
6850
Trond Myklebust17280172012-03-11 13:11:00 -04006851static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006852 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6853 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006854 .rpc_release = nfs4_release_lockowner_release,
6855};
6856
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006857static void
6858nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006859{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006860 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006861 struct rpc_message msg = {
6862 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6863 };
6864
6865 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006866 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006867
Trond Myklebustcf470c32012-03-07 13:49:12 -05006868 data = kmalloc(sizeof(*data), GFP_NOFS);
6869 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006870 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006871 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006872 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006873 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6874 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6875 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006876
Trond Myklebustcf470c32012-03-07 13:49:12 -05006877 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006878 msg.rpc_resp = &data->res;
6879 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006880 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006881}
6882
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006883#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6884
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006885static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006886 struct dentry *unused, struct inode *inode,
6887 const char *key, const void *buf,
6888 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006889{
Al Viro59301222016-05-27 10:19:30 -04006890 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006891}
6892
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006893static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006894 struct dentry *unused, struct inode *inode,
6895 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006896{
Al Virob2968212016-04-10 20:48:24 -04006897 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006898}
6899
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006900static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006901{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006902 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006903}
6904
David Quigleyc9bccef2013-05-22 12:50:45 -04006905#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006906
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006907static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006908 struct dentry *unused, struct inode *inode,
6909 const char *key, const void *buf,
6910 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006911{
6912 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006913 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006914
6915 return -EOPNOTSUPP;
6916}
6917
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006918static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006919 struct dentry *unused, struct inode *inode,
6920 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006921{
6922 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006923 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006924 return -EOPNOTSUPP;
6925}
6926
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006927static ssize_t
6928nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006929{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006930 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006931
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006932 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6933 len = security_inode_listsecurity(inode, list, list_len);
6934 if (list_len && len > list_len)
6935 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006936 }
6937 return len;
6938}
6939
6940static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6941 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006942 .get = nfs4_xattr_get_nfs4_label,
6943 .set = nfs4_xattr_set_nfs4_label,
6944};
David Quigleyc9bccef2013-05-22 12:50:45 -04006945
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006946#else
6947
6948static ssize_t
6949nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6950{
6951 return 0;
6952}
6953
6954#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006955
Andy Adamson533eb462011-06-13 18:25:56 -04006956/*
6957 * nfs_fhget will use either the mounted_on_fileid or the fileid
6958 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006959static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6960{
Andy Adamson533eb462011-06-13 18:25:56 -04006961 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6962 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6963 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006964 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006965 return;
6966
6967 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006968 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006969 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6970 fattr->nlink = 2;
6971}
6972
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006973static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6974 const struct qstr *name,
6975 struct nfs4_fs_locations *fs_locations,
6976 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006977{
6978 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006979 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006980 struct nfs4_fs_locations_arg args = {
6981 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006982 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006983 .page = page,
6984 .bitmask = bitmask,
6985 };
Benny Halevy22958462009-04-01 09:22:02 -04006986 struct nfs4_fs_locations_res res = {
6987 .fs_locations = fs_locations,
6988 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006989 struct rpc_message msg = {
6990 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6991 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006992 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006993 };
6994 int status;
6995
Harvey Harrison3110ff82008-05-02 13:42:44 -07006996 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006997
Chuck Leverc05cefc2017-11-05 15:45:22 -05006998 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6999 bitmask[1] = nfs4_fattr_bitmap[1];
7000
Andy Adamson533eb462011-06-13 18:25:56 -04007001 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7002 * is not supported */
7003 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007004 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007005 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007006 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007007
Trond Myklebustc228fd32007-01-13 02:28:11 -05007008 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007009 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007010 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007011 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007012 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007013 return status;
7014}
7015
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007016int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7017 const struct qstr *name,
7018 struct nfs4_fs_locations *fs_locations,
7019 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007020{
7021 struct nfs4_exception exception = { };
7022 int err;
7023 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007024 err = _nfs4_proc_fs_locations(client, dir, name,
7025 fs_locations, page);
7026 trace_nfs4_get_fs_locations(dir, name, err);
7027 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007028 &exception);
7029 } while (exception.retry);
7030 return err;
7031}
7032
Chuck Leverb03d7352013-10-17 14:12:50 -04007033/*
7034 * This operation also signals the server that this client is
7035 * performing migration recovery. The server can stop returning
7036 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7037 * appended to this compound to identify the client ID which is
7038 * performing recovery.
7039 */
7040static int _nfs40_proc_get_locations(struct inode *inode,
7041 struct nfs4_fs_locations *locations,
7042 struct page *page, struct rpc_cred *cred)
7043{
7044 struct nfs_server *server = NFS_SERVER(inode);
7045 struct rpc_clnt *clnt = server->client;
7046 u32 bitmask[2] = {
7047 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7048 };
7049 struct nfs4_fs_locations_arg args = {
7050 .clientid = server->nfs_client->cl_clientid,
7051 .fh = NFS_FH(inode),
7052 .page = page,
7053 .bitmask = bitmask,
7054 .migration = 1, /* skip LOOKUP */
7055 .renew = 1, /* append RENEW */
7056 };
7057 struct nfs4_fs_locations_res res = {
7058 .fs_locations = locations,
7059 .migration = 1,
7060 .renew = 1,
7061 };
7062 struct rpc_message msg = {
7063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7064 .rpc_argp = &args,
7065 .rpc_resp = &res,
7066 .rpc_cred = cred,
7067 };
7068 unsigned long now = jiffies;
7069 int status;
7070
7071 nfs_fattr_init(&locations->fattr);
7072 locations->server = server;
7073 locations->nlocations = 0;
7074
7075 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7076 nfs4_set_sequence_privileged(&args.seq_args);
7077 status = nfs4_call_sync_sequence(clnt, server, &msg,
7078 &args.seq_args, &res.seq_res);
7079 if (status)
7080 return status;
7081
7082 renew_lease(server, now);
7083 return 0;
7084}
7085
7086#ifdef CONFIG_NFS_V4_1
7087
7088/*
7089 * This operation also signals the server that this client is
7090 * performing migration recovery. The server can stop asserting
7091 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7092 * performing this operation is identified in the SEQUENCE
7093 * operation in this compound.
7094 *
7095 * When the client supports GETATTR(fs_locations_info), it can
7096 * be plumbed in here.
7097 */
7098static int _nfs41_proc_get_locations(struct inode *inode,
7099 struct nfs4_fs_locations *locations,
7100 struct page *page, struct rpc_cred *cred)
7101{
7102 struct nfs_server *server = NFS_SERVER(inode);
7103 struct rpc_clnt *clnt = server->client;
7104 u32 bitmask[2] = {
7105 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7106 };
7107 struct nfs4_fs_locations_arg args = {
7108 .fh = NFS_FH(inode),
7109 .page = page,
7110 .bitmask = bitmask,
7111 .migration = 1, /* skip LOOKUP */
7112 };
7113 struct nfs4_fs_locations_res res = {
7114 .fs_locations = locations,
7115 .migration = 1,
7116 };
7117 struct rpc_message msg = {
7118 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7119 .rpc_argp = &args,
7120 .rpc_resp = &res,
7121 .rpc_cred = cred,
7122 };
7123 int status;
7124
7125 nfs_fattr_init(&locations->fattr);
7126 locations->server = server;
7127 locations->nlocations = 0;
7128
7129 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7130 nfs4_set_sequence_privileged(&args.seq_args);
7131 status = nfs4_call_sync_sequence(clnt, server, &msg,
7132 &args.seq_args, &res.seq_res);
7133 if (status == NFS4_OK &&
7134 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7135 status = -NFS4ERR_LEASE_MOVED;
7136 return status;
7137}
7138
7139#endif /* CONFIG_NFS_V4_1 */
7140
7141/**
7142 * nfs4_proc_get_locations - discover locations for a migrated FSID
7143 * @inode: inode on FSID that is migrating
7144 * @locations: result of query
7145 * @page: buffer
7146 * @cred: credential to use for this operation
7147 *
7148 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7149 * operation failed, or a negative errno if a local error occurred.
7150 *
7151 * On success, "locations" is filled in, but if the server has
7152 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7153 * asserted.
7154 *
7155 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7156 * from this client that require migration recovery.
7157 */
7158int nfs4_proc_get_locations(struct inode *inode,
7159 struct nfs4_fs_locations *locations,
7160 struct page *page, struct rpc_cred *cred)
7161{
7162 struct nfs_server *server = NFS_SERVER(inode);
7163 struct nfs_client *clp = server->nfs_client;
7164 const struct nfs4_mig_recovery_ops *ops =
7165 clp->cl_mvops->mig_recovery_ops;
7166 struct nfs4_exception exception = { };
7167 int status;
7168
7169 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7170 (unsigned long long)server->fsid.major,
7171 (unsigned long long)server->fsid.minor,
7172 clp->cl_hostname);
7173 nfs_display_fhandle(NFS_FH(inode), __func__);
7174
7175 do {
7176 status = ops->get_locations(inode, locations, page, cred);
7177 if (status != -NFS4ERR_DELAY)
7178 break;
7179 nfs4_handle_exception(server, status, &exception);
7180 } while (exception.retry);
7181 return status;
7182}
7183
Chuck Lever44c99932013-10-17 14:13:30 -04007184/*
7185 * This operation also signals the server that this client is
7186 * performing "lease moved" recovery. The server can stop
7187 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7188 * is appended to this compound to identify the client ID which is
7189 * performing recovery.
7190 */
7191static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7192{
7193 struct nfs_server *server = NFS_SERVER(inode);
7194 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7195 struct rpc_clnt *clnt = server->client;
7196 struct nfs4_fsid_present_arg args = {
7197 .fh = NFS_FH(inode),
7198 .clientid = clp->cl_clientid,
7199 .renew = 1, /* append RENEW */
7200 };
7201 struct nfs4_fsid_present_res res = {
7202 .renew = 1,
7203 };
7204 struct rpc_message msg = {
7205 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7206 .rpc_argp = &args,
7207 .rpc_resp = &res,
7208 .rpc_cred = cred,
7209 };
7210 unsigned long now = jiffies;
7211 int status;
7212
7213 res.fh = nfs_alloc_fhandle();
7214 if (res.fh == NULL)
7215 return -ENOMEM;
7216
7217 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7218 nfs4_set_sequence_privileged(&args.seq_args);
7219 status = nfs4_call_sync_sequence(clnt, server, &msg,
7220 &args.seq_args, &res.seq_res);
7221 nfs_free_fhandle(res.fh);
7222 if (status)
7223 return status;
7224
7225 do_renew_lease(clp, now);
7226 return 0;
7227}
7228
7229#ifdef CONFIG_NFS_V4_1
7230
7231/*
7232 * This operation also signals the server that this client is
7233 * performing "lease moved" recovery. The server can stop asserting
7234 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7235 * this operation is identified in the SEQUENCE operation in this
7236 * compound.
7237 */
7238static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7239{
7240 struct nfs_server *server = NFS_SERVER(inode);
7241 struct rpc_clnt *clnt = server->client;
7242 struct nfs4_fsid_present_arg args = {
7243 .fh = NFS_FH(inode),
7244 };
7245 struct nfs4_fsid_present_res res = {
7246 };
7247 struct rpc_message msg = {
7248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7249 .rpc_argp = &args,
7250 .rpc_resp = &res,
7251 .rpc_cred = cred,
7252 };
7253 int status;
7254
7255 res.fh = nfs_alloc_fhandle();
7256 if (res.fh == NULL)
7257 return -ENOMEM;
7258
7259 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7260 nfs4_set_sequence_privileged(&args.seq_args);
7261 status = nfs4_call_sync_sequence(clnt, server, &msg,
7262 &args.seq_args, &res.seq_res);
7263 nfs_free_fhandle(res.fh);
7264 if (status == NFS4_OK &&
7265 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7266 status = -NFS4ERR_LEASE_MOVED;
7267 return status;
7268}
7269
7270#endif /* CONFIG_NFS_V4_1 */
7271
7272/**
7273 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7274 * @inode: inode on FSID to check
7275 * @cred: credential to use for this operation
7276 *
7277 * Server indicates whether the FSID is present, moved, or not
7278 * recognized. This operation is necessary to clear a LEASE_MOVED
7279 * condition for this client ID.
7280 *
7281 * Returns NFS4_OK if the FSID is present on this server,
7282 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7283 * NFS4ERR code if some error occurred on the server, or a
7284 * negative errno if a local failure occurred.
7285 */
7286int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7287{
7288 struct nfs_server *server = NFS_SERVER(inode);
7289 struct nfs_client *clp = server->nfs_client;
7290 const struct nfs4_mig_recovery_ops *ops =
7291 clp->cl_mvops->mig_recovery_ops;
7292 struct nfs4_exception exception = { };
7293 int status;
7294
7295 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7296 (unsigned long long)server->fsid.major,
7297 (unsigned long long)server->fsid.minor,
7298 clp->cl_hostname);
7299 nfs_display_fhandle(NFS_FH(inode), __func__);
7300
7301 do {
7302 status = ops->fsid_present(inode, cred);
7303 if (status != -NFS4ERR_DELAY)
7304 break;
7305 nfs4_handle_exception(server, status, &exception);
7306 } while (exception.retry);
7307 return status;
7308}
7309
Andy Adamson5ec16a82013-08-08 10:57:55 -04007310/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007311 * If 'use_integrity' is true and the state managment nfs_client
7312 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7313 * and the machine credential as per RFC3530bis and RFC5661 Security
7314 * Considerations sections. Otherwise, just use the user cred with the
7315 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007316 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007317static 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 +00007318{
7319 int status;
7320 struct nfs4_secinfo_arg args = {
7321 .dir_fh = NFS_FH(dir),
7322 .name = name,
7323 };
7324 struct nfs4_secinfo_res res = {
7325 .flavors = flavors,
7326 };
7327 struct rpc_message msg = {
7328 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7329 .rpc_argp = &args,
7330 .rpc_resp = &res,
7331 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007332 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007333 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007334
7335 if (use_integrity) {
7336 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007337 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7338 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007339 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007340
7341 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007342
7343 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7344 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7345
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007346 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7347 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007348 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007349
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007350 if (cred)
7351 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007352
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007353 return status;
7354}
7355
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007356int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7357 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007358{
7359 struct nfs4_exception exception = { };
7360 int err;
7361 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007362 err = -NFS4ERR_WRONGSEC;
7363
7364 /* try to use integrity protection with machine cred */
7365 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7366 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7367
7368 /*
7369 * if unable to use integrity protection, or SECINFO with
7370 * integrity protection returns NFS4ERR_WRONGSEC (which is
7371 * disallowed by spec, but exists in deployed servers) use
7372 * the current filesystem's rpc_client and the user cred.
7373 */
7374 if (err == -NFS4ERR_WRONGSEC)
7375 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7376
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007377 trace_nfs4_secinfo(dir, name, err);
7378 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007379 &exception);
7380 } while (exception.retry);
7381 return err;
7382}
7383
Andy Adamson557134a2009-04-01 09:21:53 -04007384#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007385/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007386 * Check the exchange flags returned by the server for invalid flags, having
7387 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7388 * DS flags set.
7389 */
7390static int nfs4_check_cl_exchange_flags(u32 flags)
7391{
7392 if (flags & ~EXCHGID4_FLAG_MASK_R)
7393 goto out_inval;
7394 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7395 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7396 goto out_inval;
7397 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7398 goto out_inval;
7399 return NFS_OK;
7400out_inval:
7401 return -NFS4ERR_INVAL;
7402}
7403
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007404static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007405nfs41_same_server_scope(struct nfs41_server_scope *a,
7406 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007407{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007408 if (a->server_scope_sz != b->server_scope_sz)
7409 return false;
7410 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007411}
7412
Andy Adamson02a95de2016-02-05 16:08:37 -05007413static void
7414nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7415{
7416}
7417
7418static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7419 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7420};
7421
Andy Adamson357f54d2010-12-14 10:11:57 -05007422/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007423 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007424 *
7425 * The 4.1 client currently uses the same TCP connection for the
7426 * fore and backchannel.
7427 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007428static
7429int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7430 struct rpc_xprt *xprt,
7431 struct nfs_client *clp,
7432 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007433{
7434 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007435 struct nfs41_bind_conn_to_session_args args = {
7436 .client = clp,
7437 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7438 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007439 struct nfs41_bind_conn_to_session_res res;
7440 struct rpc_message msg = {
7441 .rpc_proc =
7442 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007443 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007444 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007445 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007446 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007447 struct rpc_task_setup task_setup_data = {
7448 .rpc_client = clnt,
7449 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007450 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007451 .rpc_message = &msg,
7452 .flags = RPC_TASK_TIMEOUT,
7453 };
7454 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007455
Trond Myklebust71a097c2015-02-18 09:27:18 -08007456 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7457 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7458 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007459
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007460 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7461 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7462 args.dir = NFS4_CDFC4_FORE;
7463
7464 task = rpc_run_task(&task_setup_data);
7465 if (!IS_ERR(task)) {
7466 status = task->tk_status;
7467 rpc_put_task(task);
7468 } else
7469 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007470 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007471 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007472 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007473 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7474 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007475 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007476 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007477 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007478 dprintk("NFS: %s: Unexpected direction from server\n",
7479 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007480 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007481 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007482 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007483 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7484 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007485 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007486 }
7487 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007488
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007489 return status;
7490}
7491
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007492struct rpc_bind_conn_calldata {
7493 struct nfs_client *clp;
7494 struct rpc_cred *cred;
7495};
7496
7497static int
7498nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7499 struct rpc_xprt *xprt,
7500 void *calldata)
7501{
7502 struct rpc_bind_conn_calldata *p = calldata;
7503
7504 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7505}
7506
7507int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7508{
7509 struct rpc_bind_conn_calldata data = {
7510 .clp = clp,
7511 .cred = cred,
7512 };
7513 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7514 nfs4_proc_bind_conn_to_session_callback, &data);
7515}
7516
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007517/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007518 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7519 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007520 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007521static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7522 .how = SP4_MACH_CRED,
7523 .enforce.u.words = {
7524 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7525 1 << (OP_EXCHANGE_ID - 32) |
7526 1 << (OP_CREATE_SESSION - 32) |
7527 1 << (OP_DESTROY_SESSION - 32) |
7528 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007529 },
7530 .allow.u.words = {
7531 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007532 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007533 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007534 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007535 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007536 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007537 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007538 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007539 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007540 1 << (OP_FREE_STATEID - 32) |
7541 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007542 }
7543};
7544
7545/*
7546 * Select the state protection mode for client `clp' given the server results
7547 * from exchange_id in `sp'.
7548 *
7549 * Returns 0 on success, negative errno otherwise.
7550 */
7551static int nfs4_sp4_select_mode(struct nfs_client *clp,
7552 struct nfs41_state_protection *sp)
7553{
7554 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7555 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7556 1 << (OP_EXCHANGE_ID - 32) |
7557 1 << (OP_CREATE_SESSION - 32) |
7558 1 << (OP_DESTROY_SESSION - 32) |
7559 1 << (OP_DESTROY_CLIENTID - 32)
7560 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007561 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007562 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007563 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007564
7565 if (sp->how == SP4_MACH_CRED) {
7566 /* Print state protect result */
7567 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7568 for (i = 0; i <= LAST_NFS4_OP; i++) {
7569 if (test_bit(i, sp->enforce.u.longs))
7570 dfprintk(MOUNT, " enforce op %d\n", i);
7571 if (test_bit(i, sp->allow.u.longs))
7572 dfprintk(MOUNT, " allow op %d\n", i);
7573 }
7574
7575 /* make sure nothing is on enforce list that isn't supported */
7576 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7577 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7578 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007579 ret = -EINVAL;
7580 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007581 }
7582 }
7583
7584 /*
7585 * Minimal mode - state operations are allowed to use machine
7586 * credential. Note this already happens by default, so the
7587 * client doesn't have to do anything more than the negotiation.
7588 *
7589 * NOTE: we don't care if EXCHANGE_ID is in the list -
7590 * we're already using the machine cred for exchange_id
7591 * and will never use a different cred.
7592 */
7593 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7594 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7595 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7596 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7597 dfprintk(MOUNT, "sp4_mach_cred:\n");
7598 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007599 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007600 } else {
7601 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007602 ret = -EINVAL;
7603 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007604 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007605
7606 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007607 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7608 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007609 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7610 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007611 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007612 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007613
Andrew Elble99ade3c2015-12-02 09:39:51 -05007614 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7615 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007616 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007617 }
7618
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007619 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7620 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7621 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007622 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007623 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007624
7625 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7626 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7627 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007628 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007629 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007630
7631 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7632 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007633 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007634 }
7635
7636 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7637 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007638 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007639 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007640 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007641out:
7642 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007643 return 0;
7644}
7645
Andy Adamson8d89bd72016-09-09 09:22:18 -04007646struct nfs41_exchange_id_data {
7647 struct nfs41_exchange_id_res res;
7648 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007649};
7650
Andy Adamson8d89bd72016-09-09 09:22:18 -04007651static void nfs4_exchange_id_release(void *data)
7652{
7653 struct nfs41_exchange_id_data *cdata =
7654 (struct nfs41_exchange_id_data *)data;
7655
Olga Kornievskaia63513232017-03-13 10:36:19 -04007656 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007657 kfree(cdata->res.impl_id);
7658 kfree(cdata->res.server_scope);
7659 kfree(cdata->res.server_owner);
7660 kfree(cdata);
7661}
7662
7663static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007664 .rpc_release = nfs4_exchange_id_release,
7665};
7666
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007667/*
7668 * _nfs4_proc_exchange_id()
7669 *
7670 * Wrapper for EXCHANGE_ID operation.
7671 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007672static struct rpc_task *
7673nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007674 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007675{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007676 struct rpc_message msg = {
7677 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007678 .rpc_cred = cred,
7679 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007680 struct rpc_task_setup task_setup_data = {
7681 .rpc_client = clp->cl_rpcclient,
7682 .callback_ops = &nfs4_exchange_id_call_ops,
7683 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007684 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007685 };
7686 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007687 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007688
Elena Reshetova212bf412017-10-20 12:53:38 +03007689 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007690 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007691
Trond Myklebust9c760d12017-07-31 18:38:50 -04007692 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007693 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007694 if (!calldata)
7695 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007696
Trond Myklebustfd405592017-08-01 16:02:47 -04007697 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007698
7699 status = nfs4_init_uniform_client_string(clp);
7700 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007701 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007702
Andy Adamson8d89bd72016-09-09 09:22:18 -04007703 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7704 GFP_NOFS);
7705 status = -ENOMEM;
7706 if (unlikely(calldata->res.server_owner == NULL))
7707 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007708
Andy Adamson8d89bd72016-09-09 09:22:18 -04007709 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007710 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007711 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007712 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007713
Andy Adamson8d89bd72016-09-09 09:22:18 -04007714 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7715 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007716 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007717
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007718 switch (sp4_how) {
7719 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007720 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007721 break;
7722
7723 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007724 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007725 break;
7726
7727 default:
7728 /* unsupported! */
7729 WARN_ON_ONCE(1);
7730 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007731 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007732 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007733 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007734 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007735 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007736 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7737 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007738 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007739 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007740 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7741 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007742#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007743 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007744#endif
7745 msg.rpc_argp = &calldata->args;
7746 msg.rpc_resp = &calldata->res;
7747 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007748
Trond Myklebust9c760d12017-07-31 18:38:50 -04007749 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007750
7751out_impl_id:
7752 kfree(calldata->res.impl_id);
7753out_server_scope:
7754 kfree(calldata->res.server_scope);
7755out_server_owner:
7756 kfree(calldata->res.server_owner);
7757out_calldata:
7758 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007759out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007760 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007761 return ERR_PTR(status);
7762}
7763
7764/*
7765 * _nfs4_proc_exchange_id()
7766 *
7767 * Wrapper for EXCHANGE_ID operation.
7768 */
7769static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7770 u32 sp4_how)
7771{
7772 struct rpc_task *task;
7773 struct nfs41_exchange_id_args *argp;
7774 struct nfs41_exchange_id_res *resp;
7775 int status;
7776
7777 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7778 if (IS_ERR(task))
7779 return PTR_ERR(task);
7780
7781 argp = task->tk_msg.rpc_argp;
7782 resp = task->tk_msg.rpc_resp;
7783 status = task->tk_status;
7784 if (status != 0)
7785 goto out;
7786
7787 status = nfs4_check_cl_exchange_flags(resp->flags);
7788 if (status != 0)
7789 goto out;
7790
7791 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7792 if (status != 0)
7793 goto out;
7794
7795 clp->cl_clientid = resp->clientid;
7796 clp->cl_exchange_flags = resp->flags;
7797 clp->cl_seqid = resp->seqid;
7798 /* Client ID is not confirmed */
7799 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7800 clear_bit(NFS4_SESSION_ESTABLISHED,
7801 &clp->cl_session->session_state);
7802
7803 if (clp->cl_serverscope != NULL &&
7804 !nfs41_same_server_scope(clp->cl_serverscope,
7805 resp->server_scope)) {
7806 dprintk("%s: server_scope mismatch detected\n",
7807 __func__);
7808 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7809 }
7810
7811 swap(clp->cl_serverowner, resp->server_owner);
7812 swap(clp->cl_serverscope, resp->server_scope);
7813 swap(clp->cl_implid, resp->impl_id);
7814
7815 /* Save the EXCHANGE_ID verifier session trunk tests */
7816 memcpy(clp->cl_confirm.data, argp->verifier.data,
7817 sizeof(clp->cl_confirm.data));
7818out:
7819 trace_nfs4_exchange_id(clp, status);
7820 rpc_put_task(task);
7821 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007822}
7823
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007824/*
7825 * nfs4_proc_exchange_id()
7826 *
7827 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7828 *
7829 * Since the clientid has expired, all compounds using sessions
7830 * associated with the stale clientid will be returning
7831 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7832 * be in some phase of session reset.
7833 *
7834 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7835 */
7836int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7837{
7838 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7839 int status;
7840
7841 /* try SP4_MACH_CRED if krb5i/p */
7842 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7843 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007844 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007845 if (!status)
7846 return 0;
7847 }
7848
7849 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007850 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007851}
7852
Andy Adamson04fa2c62016-09-09 09:22:29 -04007853/**
7854 * nfs4_test_session_trunk
7855 *
7856 * This is an add_xprt_test() test function called from
7857 * rpc_clnt_setup_test_and_add_xprt.
7858 *
7859 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7860 * and is dereferrenced in nfs4_exchange_id_release
7861 *
7862 * Upon success, add the new transport to the rpc_clnt
7863 *
7864 * @clnt: struct rpc_clnt to get new transport
7865 * @xprt: the rpc_xprt to test
7866 * @data: call data for _nfs4_proc_exchange_id.
7867 */
7868int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7869 void *data)
7870{
7871 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007872 struct rpc_task *task;
7873 int status;
7874
Andy Adamson04fa2c62016-09-09 09:22:29 -04007875 u32 sp4_how;
7876
7877 dprintk("--> %s try %s\n", __func__,
7878 xprt->address_strings[RPC_DISPLAY_ADDR]);
7879
7880 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7881
7882 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007883 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7884 if (IS_ERR(task))
7885 return PTR_ERR(task);
7886
7887 status = task->tk_status;
7888 if (status == 0)
7889 status = nfs4_detect_session_trunking(adata->clp,
7890 task->tk_msg.rpc_resp, xprt);
7891
7892 rpc_put_task(task);
7893 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007894}
7895EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7896
Trond Myklebust66245532012-05-25 17:18:09 -04007897static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7898 struct rpc_cred *cred)
7899{
7900 struct rpc_message msg = {
7901 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7902 .rpc_argp = clp,
7903 .rpc_cred = cred,
7904 };
7905 int status;
7906
7907 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007908 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007909 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007910 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007911 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7912 return status;
7913}
7914
7915static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7916 struct rpc_cred *cred)
7917{
7918 unsigned int loop;
7919 int ret;
7920
7921 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7922 ret = _nfs4_proc_destroy_clientid(clp, cred);
7923 switch (ret) {
7924 case -NFS4ERR_DELAY:
7925 case -NFS4ERR_CLIENTID_BUSY:
7926 ssleep(1);
7927 break;
7928 default:
7929 return ret;
7930 }
7931 }
7932 return 0;
7933}
7934
7935int nfs4_destroy_clientid(struct nfs_client *clp)
7936{
7937 struct rpc_cred *cred;
7938 int ret = 0;
7939
7940 if (clp->cl_mvops->minor_version < 1)
7941 goto out;
7942 if (clp->cl_exchange_flags == 0)
7943 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007944 if (clp->cl_preserve_clid)
7945 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007946 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007947 ret = nfs4_proc_destroy_clientid(clp, cred);
7948 if (cred)
7949 put_rpccred(cred);
7950 switch (ret) {
7951 case 0:
7952 case -NFS4ERR_STALE_CLIENTID:
7953 clp->cl_exchange_flags = 0;
7954 }
7955out:
7956 return ret;
7957}
7958
Andy Adamson2050f0c2009-04-01 09:22:30 -04007959struct nfs4_get_lease_time_data {
7960 struct nfs4_get_lease_time_args *args;
7961 struct nfs4_get_lease_time_res *res;
7962 struct nfs_client *clp;
7963};
7964
7965static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7966 void *calldata)
7967{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007968 struct nfs4_get_lease_time_data *data =
7969 (struct nfs4_get_lease_time_data *)calldata;
7970
7971 dprintk("--> %s\n", __func__);
7972 /* just setup sequence, do not trigger session recovery
7973 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007974 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007975 &data->args->la_seq_args,
7976 &data->res->lr_seq_res,
7977 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007978 dprintk("<-- %s\n", __func__);
7979}
7980
7981/*
7982 * Called from nfs4_state_manager thread for session setup, so don't recover
7983 * from sequence operation or clientid errors.
7984 */
7985static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7986{
7987 struct nfs4_get_lease_time_data *data =
7988 (struct nfs4_get_lease_time_data *)calldata;
7989
7990 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007991 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7992 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007993 switch (task->tk_status) {
7994 case -NFS4ERR_DELAY:
7995 case -NFS4ERR_GRACE:
7996 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7997 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7998 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007999 /* fall through */
8000 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008001 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008002 return;
8003 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008004 dprintk("<-- %s\n", __func__);
8005}
8006
Trond Myklebust17280172012-03-11 13:11:00 -04008007static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008008 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8009 .rpc_call_done = nfs4_get_lease_time_done,
8010};
8011
8012int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8013{
8014 struct rpc_task *task;
8015 struct nfs4_get_lease_time_args args;
8016 struct nfs4_get_lease_time_res res = {
8017 .lr_fsinfo = fsinfo,
8018 };
8019 struct nfs4_get_lease_time_data data = {
8020 .args = &args,
8021 .res = &res,
8022 .clp = clp,
8023 };
8024 struct rpc_message msg = {
8025 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8026 .rpc_argp = &args,
8027 .rpc_resp = &res,
8028 };
8029 struct rpc_task_setup task_setup = {
8030 .rpc_client = clp->cl_rpcclient,
8031 .rpc_message = &msg,
8032 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008033 .callback_data = &data,
8034 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008035 };
8036 int status;
8037
Chuck Levera9c92d62013-08-09 12:48:18 -04008038 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008039 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008040 task = rpc_run_task(&task_setup);
8041
8042 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008043 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008044
Anna Schumakerf6148712017-04-07 14:15:23 -04008045 status = task->tk_status;
8046 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008047 return status;
8048}
8049
Andy Adamsonfc931582009-04-01 09:22:31 -04008050/*
8051 * Initialize the values to be used by the client in CREATE_SESSION
8052 * If nfs4_init_session set the fore channel request and response sizes,
8053 * use them.
8054 *
8055 * Set the back channel max_resp_sz_cached to zero to force the client to
8056 * always set csa_cachethis to FALSE because the current implementation
8057 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8058 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008059static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8060 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008061{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008062 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008063 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008064
Andy Adamson18aad3d2013-06-26 12:21:49 -04008065 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8066 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8067
Andy Adamsonfc931582009-04-01 09:22:31 -04008068 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008069 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8070 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008071 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008072 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008073
8074 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008075 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008076 __func__,
8077 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008078 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008079
8080 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008081 args->bc_attrs.max_rqst_sz = max_bc_payload;
8082 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008083 args->bc_attrs.max_resp_sz_cached = 0;
8084 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008085 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008086
8087 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8088 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8089 __func__,
8090 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8091 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8092 args->bc_attrs.max_reqs);
8093}
8094
Trond Myklebust79969dd2015-02-18 11:30:18 -08008095static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8096 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008097{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008098 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008099 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008100
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008101 if (rcvd->max_resp_sz > sent->max_resp_sz)
8102 return -EINVAL;
8103 /*
8104 * Our requested max_ops is the minimum we need; we're not
8105 * prepared to break up compounds into smaller pieces than that.
8106 * So, no point even trying to continue if the server won't
8107 * cooperate:
8108 */
8109 if (rcvd->max_ops < sent->max_ops)
8110 return -EINVAL;
8111 if (rcvd->max_reqs == 0)
8112 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008113 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8114 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008115 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008116}
8117
Trond Myklebust79969dd2015-02-18 11:30:18 -08008118static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8119 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008120{
8121 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008122 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008123
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008124 if (!(res->flags & SESSION4_BACK_CHAN))
8125 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008126 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8127 return -EINVAL;
8128 if (rcvd->max_resp_sz < sent->max_resp_sz)
8129 return -EINVAL;
8130 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8131 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008132 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008133 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008134 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008135 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008136out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008137 return 0;
8138}
Andy Adamson8d353012009-04-01 09:22:32 -04008139
Andy Adamson8d353012009-04-01 09:22:32 -04008140static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008141 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008142{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008143 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008144
Trond Myklebust79969dd2015-02-18 11:30:18 -08008145 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008146 if (ret)
8147 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008148 return nfs4_verify_back_channel_attrs(args, res);
8149}
8150
8151static void nfs4_update_session(struct nfs4_session *session,
8152 struct nfs41_create_session_res *res)
8153{
8154 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008155 /* Mark client id and session as being confirmed */
8156 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8157 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008158 session->flags = res->flags;
8159 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008160 if (res->flags & SESSION4_BACK_CHAN)
8161 memcpy(&session->bc_attrs, &res->bc_attrs,
8162 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008163}
8164
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008165static int _nfs4_proc_create_session(struct nfs_client *clp,
8166 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008167{
8168 struct nfs4_session *session = clp->cl_session;
8169 struct nfs41_create_session_args args = {
8170 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008171 .clientid = clp->cl_clientid,
8172 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008173 .cb_program = NFS4_CALLBACK,
8174 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008175 struct nfs41_create_session_res res;
8176
Andy Adamsonfc931582009-04-01 09:22:31 -04008177 struct rpc_message msg = {
8178 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8179 .rpc_argp = &args,
8180 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008181 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008182 };
8183 int status;
8184
Chuck Lever6b26cc82016-05-02 14:40:40 -04008185 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008186 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008187
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008188 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008189 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008190
Trond Myklebustb519d402016-09-11 14:50:01 -04008191 switch (status) {
8192 case -NFS4ERR_STALE_CLIENTID:
8193 case -NFS4ERR_DELAY:
8194 case -ETIMEDOUT:
8195 case -EACCES:
8196 case -EAGAIN:
8197 goto out;
8198 };
8199
8200 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008201 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008202 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008203 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008204 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008205 if (status)
8206 goto out;
8207 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008208 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008209out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008210 return status;
8211}
8212
8213/*
8214 * Issues a CREATE_SESSION operation to the server.
8215 * It is the responsibility of the caller to verify the session is
8216 * expired before calling this routine.
8217 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008218int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008219{
8220 int status;
8221 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008222 struct nfs4_session *session = clp->cl_session;
8223
8224 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8225
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008226 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008227 if (status)
8228 goto out;
8229
Andy Adamsonaacd5532011-11-09 13:58:21 -05008230 /* Init or reset the session slot tables */
8231 status = nfs4_setup_session_slot_tables(session);
8232 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008233 if (status)
8234 goto out;
8235
8236 ptr = (unsigned *)&session->sess_id.data[0];
8237 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8238 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008239out:
8240 dprintk("<-- %s\n", __func__);
8241 return status;
8242}
8243
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008244/*
8245 * Issue the over-the-wire RPC DESTROY_SESSION.
8246 * The caller must serialize access to this routine.
8247 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008248int nfs4_proc_destroy_session(struct nfs4_session *session,
8249 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008250{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008251 struct rpc_message msg = {
8252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8253 .rpc_argp = session,
8254 .rpc_cred = cred,
8255 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008256 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008257
8258 dprintk("--> nfs4_proc_destroy_session\n");
8259
8260 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008261 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8262 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008263
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008264 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008265 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008266
8267 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008268 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008269 "Session has been destroyed regardless...\n", status);
8270
8271 dprintk("<-- nfs4_proc_destroy_session\n");
8272 return status;
8273}
8274
Trond Myklebust7b38c362012-05-23 13:23:31 -04008275/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008276 * Renew the cl_session lease.
8277 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008278struct nfs4_sequence_data {
8279 struct nfs_client *clp;
8280 struct nfs4_sequence_args args;
8281 struct nfs4_sequence_res res;
8282};
8283
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008284static void nfs41_sequence_release(void *data)
8285{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008286 struct nfs4_sequence_data *calldata = data;
8287 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008288
Elena Reshetova212bf412017-10-20 12:53:38 +03008289 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008290 nfs4_schedule_state_renewal(clp);
8291 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008292 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008293}
8294
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008295static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8296{
8297 switch(task->tk_status) {
8298 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008299 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8300 return -EAGAIN;
8301 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008302 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008303 }
8304 return 0;
8305}
8306
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008307static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008308{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008309 struct nfs4_sequence_data *calldata = data;
8310 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008311
Trond Myklebust14516c32010-07-31 14:29:06 -04008312 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8313 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008314
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008315 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008316 if (task->tk_status < 0) {
8317 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008318 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008319 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008320
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008321 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8322 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008323 return;
8324 }
8325 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008326 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008327out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008328 dprintk("<-- %s\n", __func__);
8329}
8330
8331static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8332{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008333 struct nfs4_sequence_data *calldata = data;
8334 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008335 struct nfs4_sequence_args *args;
8336 struct nfs4_sequence_res *res;
8337
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008338 args = task->tk_msg.rpc_argp;
8339 res = task->tk_msg.rpc_resp;
8340
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008341 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008342}
8343
8344static const struct rpc_call_ops nfs41_sequence_ops = {
8345 .rpc_call_done = nfs41_sequence_call_done,
8346 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008347 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008348};
8349
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008350static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8351 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008352 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008353 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008354{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008355 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008356 struct rpc_message msg = {
8357 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8358 .rpc_cred = cred,
8359 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008360 struct rpc_task_setup task_setup_data = {
8361 .rpc_client = clp->cl_rpcclient,
8362 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008363 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008364 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008365 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008366 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008367
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008368 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008369 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008370 goto out_err;
8371
8372 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008373 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008374 if (calldata == NULL)
8375 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008376 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008377 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008378 if (is_privileged)
8379 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008380 msg.rpc_argp = &calldata->args;
8381 msg.rpc_resp = &calldata->res;
8382 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008383 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008384
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008385 ret = rpc_run_task(&task_setup_data);
8386 if (IS_ERR(ret))
8387 goto out_err;
8388 return ret;
8389out_put_clp:
8390 nfs_put_client(clp);
8391out_err:
8392 nfs41_release_slot(slot);
8393 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008394}
8395
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008396static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008397{
8398 struct rpc_task *task;
8399 int ret = 0;
8400
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008401 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008402 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008403 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008404 if (IS_ERR(task))
8405 ret = PTR_ERR(task);
8406 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008407 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008408 dprintk("<-- %s status=%d\n", __func__, ret);
8409 return ret;
8410}
8411
8412static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8413{
8414 struct rpc_task *task;
8415 int ret;
8416
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008417 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008418 if (IS_ERR(task)) {
8419 ret = PTR_ERR(task);
8420 goto out;
8421 }
8422 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008423 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008424 ret = task->tk_status;
8425 rpc_put_task(task);
8426out:
8427 dprintk("<-- %s status=%d\n", __func__, ret);
8428 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008429}
8430
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008431struct nfs4_reclaim_complete_data {
8432 struct nfs_client *clp;
8433 struct nfs41_reclaim_complete_args arg;
8434 struct nfs41_reclaim_complete_res res;
8435};
8436
8437static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8438{
8439 struct nfs4_reclaim_complete_data *calldata = data;
8440
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008441 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008442 &calldata->arg.seq_args,
8443 &calldata->res.seq_res,
8444 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008445}
8446
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008447static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8448{
8449 switch(task->tk_status) {
8450 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008451 wake_up_all(&clp->cl_lock_waitq);
8452 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008453 case -NFS4ERR_COMPLETE_ALREADY:
8454 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8455 break;
8456 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008457 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008458 /* fall through */
8459 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008460 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008461 case -NFS4ERR_BADSESSION:
8462 case -NFS4ERR_DEADSESSION:
8463 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8464 nfs4_schedule_session_recovery(clp->cl_session,
8465 task->tk_status);
8466 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008467 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008468 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008469 }
8470 return 0;
8471}
8472
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008473static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8474{
8475 struct nfs4_reclaim_complete_data *calldata = data;
8476 struct nfs_client *clp = calldata->clp;
8477 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8478
8479 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008480 if (!nfs41_sequence_done(task, res))
8481 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008482
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008483 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008484 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8485 rpc_restart_call_prepare(task);
8486 return;
8487 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008488 dprintk("<-- %s\n", __func__);
8489}
8490
8491static void nfs4_free_reclaim_complete_data(void *data)
8492{
8493 struct nfs4_reclaim_complete_data *calldata = data;
8494
8495 kfree(calldata);
8496}
8497
8498static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8499 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8500 .rpc_call_done = nfs4_reclaim_complete_done,
8501 .rpc_release = nfs4_free_reclaim_complete_data,
8502};
8503
8504/*
8505 * Issue a global reclaim complete.
8506 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008507static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8508 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008509{
8510 struct nfs4_reclaim_complete_data *calldata;
8511 struct rpc_task *task;
8512 struct rpc_message msg = {
8513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008514 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008515 };
8516 struct rpc_task_setup task_setup_data = {
8517 .rpc_client = clp->cl_rpcclient,
8518 .rpc_message = &msg,
8519 .callback_ops = &nfs4_reclaim_complete_call_ops,
8520 .flags = RPC_TASK_ASYNC,
8521 };
8522 int status = -ENOMEM;
8523
8524 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008525 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008526 if (calldata == NULL)
8527 goto out;
8528 calldata->clp = clp;
8529 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008530
Chuck Levera9c92d62013-08-09 12:48:18 -04008531 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008532 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008533 msg.rpc_argp = &calldata->arg;
8534 msg.rpc_resp = &calldata->res;
8535 task_setup_data.callback_data = calldata;
8536 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008537 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008538 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008539 goto out;
8540 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008541 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008542 if (status == 0)
8543 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008544 rpc_put_task(task);
8545out:
8546 dprintk("<-- %s status=%d\n", __func__, status);
8547 return status;
8548}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008549
8550static void
8551nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8552{
8553 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008554 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008555
8556 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008557 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008558 &lgp->res.seq_res, task);
8559 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008560}
8561
8562static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8563{
8564 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008565
8566 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008567 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008568 dprintk("<-- %s\n", __func__);
8569}
8570
8571static int
8572nfs4_layoutget_handle_exception(struct rpc_task *task,
8573 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8574{
Trond Myklebustee314c22012-10-01 17:25:48 -07008575 struct inode *inode = lgp->args.inode;
8576 struct nfs_server *server = NFS_SERVER(inode);
8577 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008578 int nfs4err = task->tk_status;
8579 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008580 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008581
Boaz Harroshed7e5422014-01-22 20:34:54 +02008582 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008583
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008584 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008585 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008586 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008587
8588 /*
8589 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8590 * on the file. set tk_status to -ENODATA to tell upper layer to
8591 * retry go inband.
8592 */
8593 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008594 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008595 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008596 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008597 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8598 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8599 */
8600 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008601 status = -EOVERFLOW;
8602 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008603 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008604 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008605 * (or clients) writing to the same RAID stripe except when
8606 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008607 *
8608 * Treat it like we would RECALLCONFLICT -- we retry for a little
8609 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008610 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008611 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008612 if (lgp->args.minlength == 0) {
8613 status = -EOVERFLOW;
8614 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008615 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008616 status = -EBUSY;
8617 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008618 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008619 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008620 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008621 case -NFS4ERR_DELEG_REVOKED:
8622 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008623 case -NFS4ERR_EXPIRED:
8624 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008625 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008626 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008627 lo = NFS_I(inode)->layout;
8628 /* If the open stateid was bad, then recover it. */
8629 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008630 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008631 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008632 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008633 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008634 break;
8635 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008636
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008637 /*
8638 * Mark the bad layout state as invalid, then retry
8639 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008640 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008641 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008642 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008643 pnfs_free_lseg_list(&head);
8644 status = -EAGAIN;
8645 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008646 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008647
Trond Myklebust8ac09252017-01-23 22:44:12 -05008648 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008649 err = nfs4_handle_exception(server, nfs4err, exception);
8650 if (!status) {
8651 if (exception->retry)
8652 status = -EAGAIN;
8653 else
8654 status = err;
8655 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008656out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008657 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008658 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008659}
8660
Idan Kedar85541162012-08-02 11:47:10 +03008661static size_t max_response_pages(struct nfs_server *server)
8662{
8663 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8664 return nfs_page_array_len(0, max_resp_sz);
8665}
8666
8667static void nfs4_free_pages(struct page **pages, size_t size)
8668{
8669 int i;
8670
8671 if (!pages)
8672 return;
8673
8674 for (i = 0; i < size; i++) {
8675 if (!pages[i])
8676 break;
8677 __free_page(pages[i]);
8678 }
8679 kfree(pages);
8680}
8681
8682static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8683{
8684 struct page **pages;
8685 int i;
8686
8687 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8688 if (!pages) {
8689 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8690 return NULL;
8691 }
8692
8693 for (i = 0; i < size; i++) {
8694 pages[i] = alloc_page(gfp_flags);
8695 if (!pages[i]) {
8696 dprintk("%s: failed to allocate page\n", __func__);
8697 nfs4_free_pages(pages, size);
8698 return NULL;
8699 }
8700 }
8701
8702 return pages;
8703}
8704
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008705static void nfs4_layoutget_release(void *calldata)
8706{
8707 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008708 struct inode *inode = lgp->args.inode;
8709 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008710 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008711
8712 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008713 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008714 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008715 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008716 put_nfs_open_context(lgp->args.ctx);
8717 kfree(calldata);
8718 dprintk("<-- %s\n", __func__);
8719}
8720
8721static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8722 .rpc_call_prepare = nfs4_layoutget_prepare,
8723 .rpc_call_done = nfs4_layoutget_done,
8724 .rpc_release = nfs4_layoutget_release,
8725};
8726
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008727struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008728nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008729{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008730 struct inode *inode = lgp->args.inode;
8731 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008732 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008733 struct rpc_task *task;
8734 struct rpc_message msg = {
8735 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8736 .rpc_argp = &lgp->args,
8737 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008738 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008739 };
8740 struct rpc_task_setup task_setup_data = {
8741 .rpc_client = server->client,
8742 .rpc_message = &msg,
8743 .callback_ops = &nfs4_layoutget_call_ops,
8744 .callback_data = lgp,
8745 .flags = RPC_TASK_ASYNC,
8746 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008747 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008748 struct nfs4_exception exception = {
8749 .inode = inode,
8750 .timeout = *timeout,
8751 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008752 int status = 0;
8753
8754 dprintk("--> %s\n", __func__);
8755
Peng Tao4bd5a982014-11-17 11:05:17 +08008756 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8757 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8758
Idan Kedar85541162012-08-02 11:47:10 +03008759 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8760 if (!lgp->args.layout.pages) {
8761 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008762 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008763 }
8764 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8765
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008766 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008767 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008768 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008769
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008770 task = rpc_run_task(&task_setup_data);
8771 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008772 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008773 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008774 if (status == 0) {
8775 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8776 *timeout = exception.timeout;
8777 }
8778
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008779 trace_nfs4_layoutget(lgp->args.ctx,
8780 &lgp->args.range,
8781 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008782 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008783 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008784
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008785 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8786 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008787 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008788 rpc_put_task(task);
8789 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008790 if (status)
8791 return ERR_PTR(status);
8792 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008793}
8794
Benny Halevycbe82602011-05-22 19:52:37 +03008795static void
8796nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8797{
8798 struct nfs4_layoutreturn *lrp = calldata;
8799
8800 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008801 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008802 &lrp->args.seq_args,
8803 &lrp->res.seq_res,
8804 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008805}
8806
8807static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8808{
8809 struct nfs4_layoutreturn *lrp = calldata;
8810 struct nfs_server *server;
8811
8812 dprintk("--> %s\n", __func__);
8813
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008814 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008815 return;
8816
8817 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008818 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008819 case -NFS4ERR_OLD_STATEID:
8820 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8821 lrp->args.inode))
8822 goto out_restart;
8823 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008824 default:
8825 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008826 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008827 case 0:
8828 break;
8829 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008830 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008831 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008832 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008833 }
Benny Halevycbe82602011-05-22 19:52:37 +03008834 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008835 return;
8836out_restart:
8837 task->tk_status = 0;
8838 nfs4_sequence_free_slot(&lrp->res.seq_res);
8839 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008840}
8841
8842static void nfs4_layoutreturn_release(void *calldata)
8843{
8844 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008845 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008846
8847 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008848 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008849 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008850 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008851 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8852 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008853 pnfs_put_layout_hdr(lrp->args.layout);
8854 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008855 kfree(calldata);
8856 dprintk("<-- %s\n", __func__);
8857}
8858
8859static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8860 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8861 .rpc_call_done = nfs4_layoutreturn_done,
8862 .rpc_release = nfs4_layoutreturn_release,
8863};
8864
Peng Tao6c166052014-11-17 09:30:40 +08008865int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008866{
8867 struct rpc_task *task;
8868 struct rpc_message msg = {
8869 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8870 .rpc_argp = &lrp->args,
8871 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008872 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008873 };
8874 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008875 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008876 .rpc_message = &msg,
8877 .callback_ops = &nfs4_layoutreturn_call_ops,
8878 .callback_data = lrp,
8879 };
Peng Tao6c166052014-11-17 09:30:40 +08008880 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008881
Andrew Elble99ade3c2015-12-02 09:39:51 -05008882 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8883 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8884 &task_setup_data.rpc_client, &msg);
8885
Benny Halevycbe82602011-05-22 19:52:37 +03008886 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008887 if (!sync) {
8888 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8889 if (!lrp->inode) {
8890 nfs4_layoutreturn_release(lrp);
8891 return -EAGAIN;
8892 }
8893 task_setup_data.flags |= RPC_TASK_ASYNC;
8894 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008895 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008896 task = rpc_run_task(&task_setup_data);
8897 if (IS_ERR(task))
8898 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008899 if (sync)
8900 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008901 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008902 dprintk("<-- %s status=%d\n", __func__, status);
8903 rpc_put_task(task);
8904 return status;
8905}
8906
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008907static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008908_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8909 struct pnfs_device *pdev,
8910 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008911{
8912 struct nfs4_getdeviceinfo_args args = {
8913 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008914 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8915 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008916 };
8917 struct nfs4_getdeviceinfo_res res = {
8918 .pdev = pdev,
8919 };
8920 struct rpc_message msg = {
8921 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8922 .rpc_argp = &args,
8923 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008924 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008925 };
8926 int status;
8927
8928 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008929 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008930 if (res.notification & ~args.notify_types)
8931 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008932 if (res.notification != args.notify_types)
8933 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008934
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008935 dprintk("<-- %s status=%d\n", __func__, status);
8936
8937 return status;
8938}
8939
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008940int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8941 struct pnfs_device *pdev,
8942 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008943{
8944 struct nfs4_exception exception = { };
8945 int err;
8946
8947 do {
8948 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008949 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008950 &exception);
8951 } while (exception.retry);
8952 return err;
8953}
8954EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8955
Andy Adamson863a3c62011-03-23 13:27:54 +00008956static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8957{
8958 struct nfs4_layoutcommit_data *data = calldata;
8959 struct nfs_server *server = NFS_SERVER(data->args.inode);
8960
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008961 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008962 &data->args.seq_args,
8963 &data->res.seq_res,
8964 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008965}
8966
8967static void
8968nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8969{
8970 struct nfs4_layoutcommit_data *data = calldata;
8971 struct nfs_server *server = NFS_SERVER(data->args.inode);
8972
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008973 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008974 return;
8975
8976 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008977 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8978 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8979 case -NFS4ERR_BADLAYOUT: /* no layout */
8980 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008981 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008982 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008983 break;
8984 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008985 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008986 rpc_restart_call_prepare(task);
8987 return;
8988 }
8989 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008990}
8991
8992static void nfs4_layoutcommit_release(void *calldata)
8993{
8994 struct nfs4_layoutcommit_data *data = calldata;
8995
Andy Adamsondb29c082011-07-30 20:52:38 -04008996 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008997 nfs_post_op_update_inode_force_wcc(data->args.inode,
8998 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008999 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009000 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009001 kfree(data);
9002}
9003
9004static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9005 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9006 .rpc_call_done = nfs4_layoutcommit_done,
9007 .rpc_release = nfs4_layoutcommit_release,
9008};
9009
9010int
Andy Adamsonef311532011-03-12 02:58:10 -05009011nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009012{
9013 struct rpc_message msg = {
9014 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9015 .rpc_argp = &data->args,
9016 .rpc_resp = &data->res,
9017 .rpc_cred = data->cred,
9018 };
9019 struct rpc_task_setup task_setup_data = {
9020 .task = &data->task,
9021 .rpc_client = NFS_CLIENT(data->args.inode),
9022 .rpc_message = &msg,
9023 .callback_ops = &nfs4_layoutcommit_ops,
9024 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009025 };
9026 struct rpc_task *task;
9027 int status = 0;
9028
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009029 dprintk("NFS: initiating layoutcommit call. sync %d "
9030 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009031 data->args.lastbytewritten,
9032 data->args.inode->i_ino);
9033
Trond Myklebust472e2592015-02-05 16:50:30 -05009034 if (!sync) {
9035 data->inode = nfs_igrab_and_active(data->args.inode);
9036 if (data->inode == NULL) {
9037 nfs4_layoutcommit_release(data);
9038 return -EAGAIN;
9039 }
9040 task_setup_data.flags = RPC_TASK_ASYNC;
9041 }
Chuck Levera9c92d62013-08-09 12:48:18 -04009042 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00009043 task = rpc_run_task(&task_setup_data);
9044 if (IS_ERR(task))
9045 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009046 if (sync)
9047 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009048 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009049 dprintk("%s: status %d\n", __func__, status);
9050 rpc_put_task(task);
9051 return status;
9052}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009053
Andy Adamson97431202013-08-08 10:57:56 -04009054/**
9055 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9056 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9057 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009058static int
9059_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009060 struct nfs_fsinfo *info,
9061 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009062{
9063 struct nfs41_secinfo_no_name_args args = {
9064 .style = SECINFO_STYLE_CURRENT_FH,
9065 };
9066 struct nfs4_secinfo_res res = {
9067 .flavors = flavors,
9068 };
9069 struct rpc_message msg = {
9070 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9071 .rpc_argp = &args,
9072 .rpc_resp = &res,
9073 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009074 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009075 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009076 int status;
9077
9078 if (use_integrity) {
9079 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009080 cred = nfs4_get_clid_cred(server->nfs_client);
9081 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009082 }
9083
9084 dprintk("--> %s\n", __func__);
9085 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9086 &res.seq_res, 0);
9087 dprintk("<-- %s status=%d\n", __func__, status);
9088
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009089 if (cred)
9090 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009091
9092 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009093}
9094
9095static int
9096nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9097 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9098{
9099 struct nfs4_exception exception = { };
9100 int err;
9101 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009102 /* first try using integrity protection */
9103 err = -NFS4ERR_WRONGSEC;
9104
9105 /* try to use integrity protection with machine cred */
9106 if (_nfs4_is_integrity_protected(server->nfs_client))
9107 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9108 flavors, true);
9109
9110 /*
9111 * if unable to use integrity protection, or SECINFO with
9112 * integrity protection returns NFS4ERR_WRONGSEC (which is
9113 * disallowed by spec, but exists in deployed servers) use
9114 * the current filesystem's rpc_client and the user cred.
9115 */
9116 if (err == -NFS4ERR_WRONGSEC)
9117 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9118 flavors, false);
9119
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009120 switch (err) {
9121 case 0:
9122 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009123 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009124 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009125 default:
9126 err = nfs4_handle_exception(server, err, &exception);
9127 }
9128 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009129out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009130 return err;
9131}
9132
9133static int
9134nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9135 struct nfs_fsinfo *info)
9136{
9137 int err;
9138 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009139 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009140 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009141 struct nfs4_secinfo4 *secinfo;
9142 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009143
9144 page = alloc_page(GFP_KERNEL);
9145 if (!page) {
9146 err = -ENOMEM;
9147 goto out;
9148 }
9149
9150 flavors = page_address(page);
9151 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9152
9153 /*
9154 * Fall back on "guess and check" method if
9155 * the server doesn't support SECINFO_NO_NAME
9156 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009157 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009158 err = nfs4_find_root_sec(server, fhandle, info);
9159 goto out_freepage;
9160 }
9161 if (err)
9162 goto out_freepage;
9163
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009164 for (i = 0; i < flavors->num_flavors; i++) {
9165 secinfo = &flavors->flavors[i];
9166
9167 switch (secinfo->flavor) {
9168 case RPC_AUTH_NULL:
9169 case RPC_AUTH_UNIX:
9170 case RPC_AUTH_GSS:
9171 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9172 &secinfo->flavor_info);
9173 break;
9174 default:
9175 flavor = RPC_AUTH_MAXFLAVOR;
9176 break;
9177 }
9178
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009179 if (!nfs_auth_info_match(&server->auth_info, flavor))
9180 flavor = RPC_AUTH_MAXFLAVOR;
9181
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009182 if (flavor != RPC_AUTH_MAXFLAVOR) {
9183 err = nfs4_lookup_root_sec(server, fhandle,
9184 info, flavor);
9185 if (!err)
9186 break;
9187 }
9188 }
9189
9190 if (flavor == RPC_AUTH_MAXFLAVOR)
9191 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009192
9193out_freepage:
9194 put_page(page);
9195 if (err == -EACCES)
9196 return -EPERM;
9197out:
9198 return err;
9199}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009200
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009201static int _nfs41_test_stateid(struct nfs_server *server,
9202 nfs4_stateid *stateid,
9203 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009204{
9205 int status;
9206 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009207 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009208 };
9209 struct nfs41_test_stateid_res res;
9210 struct rpc_message msg = {
9211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9212 .rpc_argp = &args,
9213 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009214 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009215 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009216 struct rpc_clnt *rpc_client = server->client;
9217
9218 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9219 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009220
Chuck Lever38527b12012-07-11 16:30:23 -04009221 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009222 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009223 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009224 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009225 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009226 if (status != NFS_OK) {
9227 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009228 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009229 }
9230 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009231 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009232}
9233
Trond Myklebust43912bb2016-09-22 13:38:56 -04009234static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9235 int err, struct nfs4_exception *exception)
9236{
9237 exception->retry = 0;
9238 switch(err) {
9239 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009240 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009241 nfs4_handle_exception(server, err, exception);
9242 break;
9243 case -NFS4ERR_BADSESSION:
9244 case -NFS4ERR_BADSLOT:
9245 case -NFS4ERR_BAD_HIGH_SLOT:
9246 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9247 case -NFS4ERR_DEADSESSION:
9248 nfs4_do_handle_exception(server, err, exception);
9249 }
9250}
9251
Chuck Lever38527b12012-07-11 16:30:23 -04009252/**
9253 * nfs41_test_stateid - perform a TEST_STATEID operation
9254 *
9255 * @server: server / transport on which to perform the operation
9256 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009257 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009258 *
9259 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9260 * Otherwise a negative NFS4ERR value is returned if the operation
9261 * failed or the state ID is not currently valid.
9262 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009263static int nfs41_test_stateid(struct nfs_server *server,
9264 nfs4_stateid *stateid,
9265 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009266{
9267 struct nfs4_exception exception = { };
9268 int err;
9269 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009270 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009271 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009272 } while (exception.retry);
9273 return err;
9274}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009275
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009276struct nfs_free_stateid_data {
9277 struct nfs_server *server;
9278 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009279 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009280};
9281
9282static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9283{
9284 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009285 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009286 &data->args.seq_args,
9287 &data->res.seq_res,
9288 task);
9289}
9290
9291static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9292{
9293 struct nfs_free_stateid_data *data = calldata;
9294
9295 nfs41_sequence_done(task, &data->res.seq_res);
9296
9297 switch (task->tk_status) {
9298 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009299 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009300 rpc_restart_call_prepare(task);
9301 }
9302}
9303
9304static void nfs41_free_stateid_release(void *calldata)
9305{
9306 kfree(calldata);
9307}
9308
Trond Myklebust17f26b12013-08-21 15:48:42 -04009309static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009310 .rpc_call_prepare = nfs41_free_stateid_prepare,
9311 .rpc_call_done = nfs41_free_stateid_done,
9312 .rpc_release = nfs41_free_stateid_release,
9313};
9314
9315static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009316 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009317 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009318 bool privileged)
9319{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009320 struct rpc_message msg = {
9321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009322 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009323 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009324 struct rpc_task_setup task_setup = {
9325 .rpc_client = server->client,
9326 .rpc_message = &msg,
9327 .callback_ops = &nfs41_free_stateid_ops,
9328 .flags = RPC_TASK_ASYNC,
9329 };
9330 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009331
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009332 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9333 &task_setup.rpc_client, &msg);
9334
Chuck Lever38527b12012-07-11 16:30:23 -04009335 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009336 data = kmalloc(sizeof(*data), GFP_NOFS);
9337 if (!data)
9338 return ERR_PTR(-ENOMEM);
9339 data->server = server;
9340 nfs4_stateid_copy(&data->args.stateid, stateid);
9341
9342 task_setup.callback_data = data;
9343
9344 msg.rpc_argp = &data->args;
9345 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009346 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009347 if (privileged)
9348 nfs4_set_sequence_privileged(&data->args.seq_args);
9349
9350 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009351}
9352
Chuck Lever38527b12012-07-11 16:30:23 -04009353/**
9354 * nfs41_free_stateid - perform a FREE_STATEID operation
9355 *
9356 * @server: server / transport on which to perform the operation
9357 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009358 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009359 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009360 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009361 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009362 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009363static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009364 const nfs4_stateid *stateid,
9365 struct rpc_cred *cred,
9366 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009367{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009368 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009369
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009370 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009371 if (IS_ERR(task))
9372 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009373 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009374 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009375}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009376
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009377static void
9378nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009379{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009380 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009381
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009382 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009383 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009384}
9385
Trond Myklebust36281ca2012-03-04 18:13:56 -05009386static bool nfs41_match_stateid(const nfs4_stateid *s1,
9387 const nfs4_stateid *s2)
9388{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009389 if (s1->type != s2->type)
9390 return false;
9391
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009392 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009393 return false;
9394
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009395 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009396 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009397
Anna Schumaker045c5512017-01-11 16:59:48 -05009398 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009399}
9400
Andy Adamson557134a2009-04-01 09:21:53 -04009401#endif /* CONFIG_NFS_V4_1 */
9402
Trond Myklebust36281ca2012-03-04 18:13:56 -05009403static bool nfs4_match_stateid(const nfs4_stateid *s1,
9404 const nfs4_stateid *s2)
9405{
Trond Myklebustf597c532012-03-04 18:13:56 -05009406 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009407}
9408
9409
Trond Myklebust17280172012-03-11 13:11:00 -04009410static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009411 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009412 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009413 .recover_open = nfs4_open_reclaim,
9414 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009415 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009416 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417};
9418
Andy Adamson591d71c2009-04-01 09:22:47 -04009419#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009420static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009421 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9422 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9423 .recover_open = nfs4_open_reclaim,
9424 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009425 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009426 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009427 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009428};
9429#endif /* CONFIG_NFS_V4_1 */
9430
Trond Myklebust17280172012-03-11 13:11:00 -04009431static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009432 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009433 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009434 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009435 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009436 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009437};
9438
Andy Adamson591d71c2009-04-01 09:22:47 -04009439#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009440static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009441 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9442 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009443 .recover_open = nfs41_open_expired,
9444 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009445 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009446};
9447#endif /* CONFIG_NFS_V4_1 */
9448
Trond Myklebust17280172012-03-11 13:11:00 -04009449static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009450 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009451 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009452 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009453};
9454
9455#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009456static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009457 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009458 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009459 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009460};
9461#endif
9462
Chuck Leverec011fe2013-10-17 14:12:39 -04009463static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009464 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009465 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009466};
9467
9468#if defined(CONFIG_NFS_V4_1)
9469static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009470 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009471 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009472};
9473#endif /* CONFIG_NFS_V4_1 */
9474
Trond Myklebust97dc1352010-06-16 09:52:26 -04009475static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9476 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009477 .init_caps = NFS_CAP_READDIRPLUS
9478 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009479 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009480 .init_client = nfs40_init_client,
9481 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009482 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009483 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009484 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009485 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009486 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009487 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009488 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9489 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9490 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009491 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009492};
9493
9494#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009495static struct nfs_seqid *
9496nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9497{
9498 return NULL;
9499}
9500
Trond Myklebust97dc1352010-06-16 09:52:26 -04009501static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9502 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009503 .init_caps = NFS_CAP_READDIRPLUS
9504 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009505 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009506 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009507 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009508 .init_client = nfs41_init_client,
9509 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009510 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009511 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009512 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009513 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009514 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009515 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009516 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009517 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9518 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9519 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009520 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009521};
9522#endif
9523
Steve Dickson42c2c422013-05-22 12:50:38 -04009524#if defined(CONFIG_NFS_V4_2)
9525static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9526 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009527 .init_caps = NFS_CAP_READDIRPLUS
9528 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009529 | NFS_CAP_POSIX_LOCK
9530 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009531 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009532 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009533 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009534 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009535 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009536 | NFS_CAP_LAYOUTSTATS
9537 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009538 .init_client = nfs41_init_client,
9539 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009540 .match_stateid = nfs41_match_stateid,
9541 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009542 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009543 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009544 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009545 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009546 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009547 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9548 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9549 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009550 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009551};
9552#endif
9553
Trond Myklebust97dc1352010-06-16 09:52:26 -04009554const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9555 [0] = &nfs_v4_0_minor_ops,
9556#if defined(CONFIG_NFS_V4_1)
9557 [1] = &nfs_v4_1_minor_ops,
9558#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009559#if defined(CONFIG_NFS_V4_2)
9560 [2] = &nfs_v4_2_minor_ops,
9561#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009562};
9563
Trond Myklebust13997822016-07-24 17:10:52 -04009564static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009565{
9566 ssize_t error, error2;
9567
9568 error = generic_listxattr(dentry, list, size);
9569 if (error < 0)
9570 return error;
9571 if (list) {
9572 list += error;
9573 size -= error;
9574 }
9575
9576 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9577 if (error2 < 0)
9578 return error2;
9579 return error + error2;
9580}
9581
Trond Myklebust17f26b12013-08-21 15:48:42 -04009582static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009583 .create = nfs_create,
9584 .lookup = nfs_lookup,
9585 .atomic_open = nfs_atomic_open,
9586 .link = nfs_link,
9587 .unlink = nfs_unlink,
9588 .symlink = nfs_symlink,
9589 .mkdir = nfs_mkdir,
9590 .rmdir = nfs_rmdir,
9591 .mknod = nfs_mknod,
9592 .rename = nfs_rename,
9593 .permission = nfs_permission,
9594 .getattr = nfs_getattr,
9595 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009596 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009597};
9598
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009599static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009600 .permission = nfs_permission,
9601 .getattr = nfs_getattr,
9602 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009603 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009604};
9605
David Howells509de812006-08-22 20:06:11 -04009606const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009607 .version = 4, /* protocol version */
9608 .dentry_ops = &nfs4_dentry_operations,
9609 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009610 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009611 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009612 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009613 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009614 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009615 .getattr = nfs4_proc_getattr,
9616 .setattr = nfs4_proc_setattr,
9617 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009618 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619 .access = nfs4_proc_access,
9620 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009621 .create = nfs4_proc_create,
9622 .remove = nfs4_proc_remove,
9623 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009624 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009625 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009626 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009627 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009628 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629 .link = nfs4_proc_link,
9630 .symlink = nfs4_proc_symlink,
9631 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009632 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009633 .readdir = nfs4_proc_readdir,
9634 .mknod = nfs4_proc_mknod,
9635 .statfs = nfs4_proc_statfs,
9636 .fsinfo = nfs4_proc_fsinfo,
9637 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009638 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009639 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009640 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009641 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009642 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009643 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009644 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009645 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009646 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009647 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009648 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009649 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009650 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009651 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009652 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009653 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009654 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009655 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009656 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009657 .create_server = nfs4_create_server,
9658 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009659};
9660
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009661static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009662 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009663 .list = nfs4_xattr_list_nfs4_acl,
9664 .get = nfs4_xattr_get_nfs4_acl,
9665 .set = nfs4_xattr_set_nfs4_acl,
9666};
9667
9668const struct xattr_handler *nfs4_xattr_handlers[] = {
9669 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009670#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9671 &nfs4_xattr_nfs4_label_handler,
9672#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009673 NULL
9674};
9675
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676/*
9677 * Local variables:
9678 * c-basic-offset: 8
9679 * End:
9680 */