blob: 71f2916f93ff567111b2060e303b3f0c71a073bf [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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust6926afd12012-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 Myklebust62ab460c2009-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 Myklebust62ab460c2009-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 Myklebust62ab460c2009-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
Al Virobeffb8f2016-07-20 16:34:42 -04004203static int nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204{
4205 struct nfs4_exception exception = { };
4206 int err;
4207 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004208 err = _nfs4_proc_remove(dir, name);
4209 trace_nfs4_remove(dir, name, err);
4210 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004211 &exception);
4212 } while (exception.retry);
4213 return err;
4214}
4215
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004216static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004217{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004218 struct nfs_server *server = NFS_SERVER(dir);
4219 struct nfs_removeargs *args = msg->rpc_argp;
4220 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004222 res->server = server;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004224 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004225
4226 nfs_fattr_init(res->dir_attr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004227}
4228
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004229static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4230{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004231 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004232 &data->args.seq_args,
4233 &data->res.seq_res,
4234 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004235}
4236
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004237static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004238{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004239 struct nfs_unlinkdata *data = task->tk_calldata;
4240 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004241
Trond Myklebust14516c32010-07-31 14:29:06 -04004242 if (!nfs4_sequence_done(task, &res->seq_res))
4243 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004244 if (nfs4_async_handle_error(task, res->server, NULL,
4245 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004246 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004247 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004248 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004249 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250}
4251
Jeff Laytond3d41522010-09-17 17:31:57 -04004252static void nfs4_proc_rename_setup(struct rpc_message *msg, struct inode *dir)
4253{
4254 struct nfs_server *server = NFS_SERVER(dir);
4255 struct nfs_renameargs *arg = msg->rpc_argp;
4256 struct nfs_renameres *res = msg->rpc_resp;
4257
4258 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Jeff Laytond3d41522010-09-17 17:31:57 -04004259 res->server = server;
Chuck Levera9c92d62013-08-09 12:48:18 -04004260 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004261}
4262
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004263static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4264{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004265 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004266 &data->args.seq_args,
4267 &data->res.seq_res,
4268 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004269}
4270
4271static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4272 struct inode *new_dir)
4273{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004274 struct nfs_renamedata *data = task->tk_calldata;
4275 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004276
4277 if (!nfs4_sequence_done(task, &res->seq_res))
4278 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004279 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004280 return 0;
4281
Trond Myklebustc733c492017-01-11 12:32:26 -05004282 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004283 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004284 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004285 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004286 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004287 return 1;
4288}
4289
Al Virobeffb8f2016-07-20 16:34:42 -04004290static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004291{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004292 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293 struct nfs4_link_arg arg = {
4294 .fh = NFS_FH(inode),
4295 .dir_fh = NFS_FH(dir),
4296 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004297 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004298 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004299 struct nfs4_link_res res = {
4300 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004301 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004302 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004303 struct rpc_message msg = {
4304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4305 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004306 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004307 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004308 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004309
Trond Myklebust136f2622010-04-16 16:22:49 -04004310 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004311 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004312 goto out;
4313
David Quigley14c43f72013-05-22 12:50:43 -04004314 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4315 if (IS_ERR(res.label)) {
4316 status = PTR_ERR(res.label);
4317 goto out;
4318 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004319 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004320
Bryan Schumaker7c513052011-03-24 17:12:24 +00004321 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004322 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004323 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004324 status = nfs_post_op_update_inode(inode, res.fattr);
4325 if (!status)
4326 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004327 }
David Quigley14c43f72013-05-22 12:50:43 -04004328
4329
4330 nfs4_label_free(res.label);
4331
Trond Myklebust136f2622010-04-16 16:22:49 -04004332out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004333 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004334 return status;
4335}
4336
Al Virobeffb8f2016-07-20 16:34:42 -04004337static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004338{
4339 struct nfs4_exception exception = { };
4340 int err;
4341 do {
4342 err = nfs4_handle_exception(NFS_SERVER(inode),
4343 _nfs4_proc_link(inode, dir, name),
4344 &exception);
4345 } while (exception.retry);
4346 return err;
4347}
4348
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004349struct nfs4_createdata {
4350 struct rpc_message msg;
4351 struct nfs4_create_arg arg;
4352 struct nfs4_create_res res;
4353 struct nfs_fh fh;
4354 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004355 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004356};
4357
4358static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004359 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004360{
4361 struct nfs4_createdata *data;
4362
4363 data = kzalloc(sizeof(*data), GFP_KERNEL);
4364 if (data != NULL) {
4365 struct nfs_server *server = NFS_SERVER(dir);
4366
David Quigley14c43f72013-05-22 12:50:43 -04004367 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4368 if (IS_ERR(data->label))
4369 goto out_free;
4370
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004371 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4372 data->msg.rpc_argp = &data->arg;
4373 data->msg.rpc_resp = &data->res;
4374 data->arg.dir_fh = NFS_FH(dir);
4375 data->arg.server = server;
4376 data->arg.name = name;
4377 data->arg.attrs = sattr;
4378 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004379 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004380 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004381 data->res.server = server;
4382 data->res.fh = &data->fh;
4383 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004384 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004385 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004386 }
4387 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004388out_free:
4389 kfree(data);
4390 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004391}
4392
4393static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4394{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004395 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004396 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004397 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004398 update_changeattr(dir, &data->res.dir_cinfo,
4399 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004400 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004401 }
4402 return status;
4403}
4404
4405static void nfs4_free_createdata(struct nfs4_createdata *data)
4406{
David Quigley14c43f72013-05-22 12:50:43 -04004407 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004408 kfree(data);
4409}
4410
Chuck Lever4f390c12006-08-22 20:06:22 -04004411static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004412 struct page *page, unsigned int len, struct iattr *sattr,
4413 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004414{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004415 struct nfs4_createdata *data;
4416 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004417
Chuck Lever94a6d752006-08-22 20:06:23 -04004418 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004420
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004421 status = -ENOMEM;
4422 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4423 if (data == NULL)
4424 goto out;
4425
4426 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4427 data->arg.u.symlink.pages = &page;
4428 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004429 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004430
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004431 status = nfs4_do_create(dir, dentry, data);
4432
4433 nfs4_free_createdata(data);
4434out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004435 return status;
4436}
4437
Chuck Lever4f390c12006-08-22 20:06:22 -04004438static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004439 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004440{
4441 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004442 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004443 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004444
4445 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4446
Linus Torvalds1da177e2005-04-16 15:20:36 -07004447 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004448 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4449 trace_nfs4_symlink(dir, &dentry->d_name, err);
4450 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451 &exception);
4452 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004453
4454 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004455 return err;
4456}
4457
4458static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004459 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004460{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004461 struct nfs4_createdata *data;
4462 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004463
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004464 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4465 if (data == NULL)
4466 goto out;
4467
David Quigley1775fd32013-05-22 12:50:42 -04004468 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004469 status = nfs4_do_create(dir, dentry, data);
4470
4471 nfs4_free_createdata(data);
4472out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473 return status;
4474}
4475
4476static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4477 struct iattr *sattr)
4478{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004479 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004480 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004481 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004482 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004483
David Quigleyaa9c2662013-05-22 12:50:44 -04004484 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4485
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004486 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4487 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004488 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004489 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4490 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4491 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004492 &exception);
4493 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004494 nfs4_label_release_security(label);
4495
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496 return err;
4497}
4498
4499static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004500 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004501{
David Howells2b0143b2015-03-17 22:25:59 +00004502 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 struct nfs4_readdir_arg args = {
4504 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004505 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004506 .pgbase = 0,
4507 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004508 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004509 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004510 };
4511 struct nfs4_readdir_res res;
4512 struct rpc_message msg = {
4513 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4514 .rpc_argp = &args,
4515 .rpc_resp = &res,
4516 .rpc_cred = cred,
4517 };
4518 int status;
4519
Al Viro6de14722013-09-16 10:53:17 -04004520 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4521 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004522 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004523 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004525 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 -05004526 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004527 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004528 status += args.pgbase;
4529 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004530
4531 nfs_invalidate_atime(dir);
4532
Harvey Harrison3110ff82008-05-02 13:42:44 -07004533 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004534 return status;
4535}
4536
4537static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004538 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004539{
4540 struct nfs4_exception exception = { };
4541 int err;
4542 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004543 err = _nfs4_proc_readdir(dentry, cred, cookie,
4544 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004545 trace_nfs4_readdir(d_inode(dentry), err);
4546 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004547 &exception);
4548 } while (exception.retry);
4549 return err;
4550}
4551
4552static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004553 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004554{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004555 struct nfs4_createdata *data;
4556 int mode = sattr->ia_mode;
4557 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004559 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4560 if (data == NULL)
4561 goto out;
4562
Linus Torvalds1da177e2005-04-16 15:20:36 -07004563 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004564 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004566 data->arg.ftype = NF4BLK;
4567 data->arg.u.device.specdata1 = MAJOR(rdev);
4568 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004569 }
4570 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004571 data->arg.ftype = NF4CHR;
4572 data->arg.u.device.specdata1 = MAJOR(rdev);
4573 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004574 } else if (!S_ISSOCK(mode)) {
4575 status = -EINVAL;
4576 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004577 }
David Quigley1775fd32013-05-22 12:50:42 -04004578
David Quigleyaa9c2662013-05-22 12:50:44 -04004579 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004580 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004581out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004582 nfs4_free_createdata(data);
4583out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004584 return status;
4585}
4586
4587static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4588 struct iattr *sattr, dev_t rdev)
4589{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004590 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004591 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004592 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004593 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004594
David Quigleyaa9c2662013-05-22 12:50:44 -04004595 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4596
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004597 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4598 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004600 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4601 trace_nfs4_mknod(dir, &dentry->d_name, err);
4602 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 &exception);
4604 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004605
4606 nfs4_label_release_security(label);
4607
Linus Torvalds1da177e2005-04-16 15:20:36 -07004608 return err;
4609}
4610
4611static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4612 struct nfs_fsstat *fsstat)
4613{
4614 struct nfs4_statfs_arg args = {
4615 .fh = fhandle,
4616 .bitmask = server->attr_bitmask,
4617 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004618 struct nfs4_statfs_res res = {
4619 .fsstat = fsstat,
4620 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 struct rpc_message msg = {
4622 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4623 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004624 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 };
4626
Trond Myklebust0e574af2005-10-27 22:12:38 -04004627 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004628 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629}
4630
4631static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4632{
4633 struct nfs4_exception exception = { };
4634 int err;
4635 do {
4636 err = nfs4_handle_exception(server,
4637 _nfs4_proc_statfs(server, fhandle, fsstat),
4638 &exception);
4639 } while (exception.retry);
4640 return err;
4641}
4642
4643static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4644 struct nfs_fsinfo *fsinfo)
4645{
4646 struct nfs4_fsinfo_arg args = {
4647 .fh = fhandle,
4648 .bitmask = server->attr_bitmask,
4649 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004650 struct nfs4_fsinfo_res res = {
4651 .fsinfo = fsinfo,
4652 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004653 struct rpc_message msg = {
4654 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4655 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004656 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 };
4658
Bryan Schumaker7c513052011-03-24 17:12:24 +00004659 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004660}
4661
4662static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4663{
4664 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004665 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004666 int err;
4667
4668 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004669 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004670 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004671 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004672 nfs4_set_lease_period(server->nfs_client,
4673 fsinfo->lease_time * HZ,
4674 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004675 break;
4676 }
4677 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004678 } while (exception.retry);
4679 return err;
4680}
4681
4682static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4683{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004684 int error;
4685
Trond Myklebust0e574af2005-10-27 22:12:38 -04004686 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004687 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004688 if (error == 0) {
4689 /* block layout checks this! */
4690 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004691 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004692 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004693
4694 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004695}
4696
4697static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4698 struct nfs_pathconf *pathconf)
4699{
4700 struct nfs4_pathconf_arg args = {
4701 .fh = fhandle,
4702 .bitmask = server->attr_bitmask,
4703 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004704 struct nfs4_pathconf_res res = {
4705 .pathconf = pathconf,
4706 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004707 struct rpc_message msg = {
4708 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4709 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004710 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004711 };
4712
4713 /* None of the pathconf attributes are mandatory to implement */
4714 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4715 memset(pathconf, 0, sizeof(*pathconf));
4716 return 0;
4717 }
4718
Trond Myklebust0e574af2005-10-27 22:12:38 -04004719 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004720 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721}
4722
4723static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4724 struct nfs_pathconf *pathconf)
4725{
4726 struct nfs4_exception exception = { };
4727 int err;
4728
4729 do {
4730 err = nfs4_handle_exception(server,
4731 _nfs4_proc_pathconf(server, fhandle, pathconf),
4732 &exception);
4733 } while (exception.retry);
4734 return err;
4735}
4736
Trond Myklebust5521abf2013-03-16 20:54:34 -04004737int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004738 const struct nfs_open_context *ctx,
4739 const struct nfs_lock_context *l_ctx,
4740 fmode_t fmode)
4741{
NeilBrown17393472016-10-13 15:26:47 +11004742 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004743}
4744EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4745
Trond Myklebust5521abf2013-03-16 20:54:34 -04004746static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4747 const struct nfs_open_context *ctx,
4748 const struct nfs_lock_context *l_ctx,
4749 fmode_t fmode)
4750{
4751 nfs4_stateid current_stateid;
4752
Trond Myklebuste1253be2014-03-05 08:44:23 -05004753 /* If the current stateid represents a lost lock, then exit */
4754 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4755 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004756 return nfs4_stateid_match(stateid, &current_stateid);
4757}
4758
4759static bool nfs4_error_stateid_expired(int err)
4760{
4761 switch (err) {
4762 case -NFS4ERR_DELEG_REVOKED:
4763 case -NFS4ERR_ADMIN_REVOKED:
4764 case -NFS4ERR_BAD_STATEID:
4765 case -NFS4ERR_STALE_STATEID:
4766 case -NFS4ERR_OLD_STATEID:
4767 case -NFS4ERR_OPENMODE:
4768 case -NFS4ERR_EXPIRED:
4769 return true;
4770 }
4771 return false;
4772}
4773
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004774static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004776 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004778 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004779 if (task->tk_status < 0) {
4780 struct nfs4_exception exception = {
4781 .inode = hdr->inode,
4782 .state = hdr->args.context->state,
4783 .stateid = &hdr->args.stateid,
4784 };
4785 task->tk_status = nfs4_async_handle_exception(task,
4786 server, task->tk_status, &exception);
4787 if (exception.retry) {
4788 rpc_restart_call_prepare(task);
4789 return -EAGAIN;
4790 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004791 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004792
Linus Torvalds1da177e2005-04-16 15:20:36 -07004793 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004794 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004795 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004796}
4797
Trond Myklebust5521abf2013-03-16 20:54:34 -04004798static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004799 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004800{
4801
4802 if (!nfs4_error_stateid_expired(task->tk_status) ||
4803 nfs4_stateid_is_current(&args->stateid,
4804 args->context,
4805 args->lock_context,
4806 FMODE_READ))
4807 return false;
4808 rpc_restart_call_prepare(task);
4809 return true;
4810}
4811
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004812static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004813{
4814
4815 dprintk("--> %s\n", __func__);
4816
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004817 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004818 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004819 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004820 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004821 if (task->tk_status > 0)
4822 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004823 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4824 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004825}
4826
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004827static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4828 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004829{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004830 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004831 if (!hdr->pgio_done_cb)
4832 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004833 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004834 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004835}
4836
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004837static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4838 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004839{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004840 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004841 &hdr->args.seq_args,
4842 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004843 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004844 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004845 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4846 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004847 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004848 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004849 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004850 return -EIO;
4851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004854static int nfs4_write_done_cb(struct rpc_task *task,
4855 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004856{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004857 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004858
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004859 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004860 if (task->tk_status < 0) {
4861 struct nfs4_exception exception = {
4862 .inode = hdr->inode,
4863 .state = hdr->args.context->state,
4864 .stateid = &hdr->args.stateid,
4865 };
4866 task->tk_status = nfs4_async_handle_exception(task,
4867 NFS_SERVER(inode), task->tk_status,
4868 &exception);
4869 if (exception.retry) {
4870 rpc_restart_call_prepare(task);
4871 return -EAGAIN;
4872 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004873 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004874 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004875 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004876 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004877 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004878 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004879}
4880
Trond Myklebust5521abf2013-03-16 20:54:34 -04004881static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004882 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004883{
4884
4885 if (!nfs4_error_stateid_expired(task->tk_status) ||
4886 nfs4_stateid_is_current(&args->stateid,
4887 args->context,
4888 args->lock_context,
4889 FMODE_WRITE))
4890 return false;
4891 rpc_restart_call_prepare(task);
4892 return true;
4893}
4894
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004895static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004896{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004897 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004898 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004899 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004900 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004901 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4902 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004903}
4904
Trond Myklebust5a37f852012-04-28 14:55:16 -04004905static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004906bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004907{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004908 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004909 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004910 return false;
4911 /* Otherwise, request attributes if and only if we don't hold
4912 * a delegation
4913 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004914 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004915}
Fred Isamana69aef12011-03-03 15:13:47 +00004916
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004917static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4918 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004919{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004920 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004921
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004922 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4923 hdr->args.bitmask = NULL;
4924 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004925 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004926 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004927
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004928 if (!hdr->pgio_done_cb)
4929 hdr->pgio_done_cb = nfs4_write_done_cb;
4930 hdr->res.server = server;
4931 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004932
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004933 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004934 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
Fred Isaman0b7c0152012-04-20 14:47:39 -04004937static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4938{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004939 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004940 &data->args.seq_args,
4941 &data->res.seq_res,
4942 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943}
4944
Fred Isaman0b7c0152012-04-20 14:47:39 -04004945static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004946{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004947 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004948
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004949 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004950 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4951 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004952 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004953 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004954 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004955 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004956}
4957
Fred Isaman0b7c0152012-04-20 14:47:39 -04004958static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004959{
4960 if (!nfs4_sequence_done(task, &data->res.seq_res))
4961 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004962 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004963}
4964
Fred Isaman0b7c0152012-04-20 14:47:39 -04004965static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966{
Trond Myklebust788e7a82006-03-20 13:44:27 -05004967 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00004968
Fred Isaman0b7c0152012-04-20 14:47:39 -04004969 if (data->commit_done_cb == NULL)
4970 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004971 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004972 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04004973 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004974}
4975
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004976struct nfs4_renewdata {
4977 struct nfs_client *client;
4978 unsigned long timestamp;
4979};
4980
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981/*
4982 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
4983 * standalone procedure for queueing an asynchronous RENEW.
4984 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004985static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004986{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004987 struct nfs4_renewdata *data = calldata;
4988 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004989
Elena Reshetova212bf412017-10-20 12:53:38 +03004990 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08004991 nfs4_schedule_state_renewal(clp);
4992 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004993 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08004994}
4995
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004996static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04004998 struct nfs4_renewdata *data = calldata;
4999 struct nfs_client *clp = data->client;
5000 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005002 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005003 switch (task->tk_status) {
5004 case 0:
5005 break;
5006 case -NFS4ERR_LEASE_MOVED:
5007 nfs4_schedule_lease_moved_recovery(clp);
5008 break;
5009 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005010 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005011 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5012 return;
5013 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005014 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005015 return;
5016 }
5017 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005018 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005019 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020}
5021
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005022static const struct rpc_call_ops nfs4_renew_ops = {
5023 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005024 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005025};
5026
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005027static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005028{
5029 struct rpc_message msg = {
5030 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5031 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005032 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005033 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005034 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005036 if (renew_flags == 0)
5037 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005038 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005039 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005040 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005041 if (data == NULL) {
5042 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005043 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005044 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005045 data->client = clp;
5046 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005047 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005048 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005049}
5050
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005051static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052{
5053 struct rpc_message msg = {
5054 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5055 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005056 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057 };
5058 unsigned long now = jiffies;
5059 int status;
5060
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005061 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062 if (status < 0)
5063 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005064 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005065 return 0;
5066}
5067
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005068static inline int nfs4_server_supports_acls(struct nfs_server *server)
5069{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005070 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005071}
5072
Trond Myklebust21f498c2012-08-24 10:59:25 -04005073/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5074 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005075 * the stack.
5076 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005077#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005078
Neil Hormane9e3d722011-03-04 19:26:03 -05005079static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005080 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005081{
5082 struct page *newpage, **spages;
5083 int rc = 0;
5084 size_t len;
5085 spages = pages;
5086
5087 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005088 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005089 newpage = alloc_page(GFP_KERNEL);
5090
5091 if (newpage == NULL)
5092 goto unwind;
5093 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005094 buf += len;
5095 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005096 *pages++ = newpage;
5097 rc++;
5098 } while (buflen != 0);
5099
5100 return rc;
5101
5102unwind:
5103 for(; rc > 0; rc--)
5104 __free_page(spages[rc-1]);
5105 return -ENOMEM;
5106}
5107
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005108struct nfs4_cached_acl {
5109 int cached;
5110 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005111 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005112};
5113
5114static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005115{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005116 struct nfs_inode *nfsi = NFS_I(inode);
5117
5118 spin_lock(&inode->i_lock);
5119 kfree(nfsi->nfs4_acl);
5120 nfsi->nfs4_acl = acl;
5121 spin_unlock(&inode->i_lock);
5122}
5123
5124static void nfs4_zap_acl_attr(struct inode *inode)
5125{
5126 nfs4_set_cached_acl(inode, NULL);
5127}
5128
5129static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5130{
5131 struct nfs_inode *nfsi = NFS_I(inode);
5132 struct nfs4_cached_acl *acl;
5133 int ret = -ENOENT;
5134
5135 spin_lock(&inode->i_lock);
5136 acl = nfsi->nfs4_acl;
5137 if (acl == NULL)
5138 goto out;
5139 if (buf == NULL) /* user is just asking for length */
5140 goto out_len;
5141 if (acl->cached == 0)
5142 goto out;
5143 ret = -ERANGE; /* see getxattr(2) man page */
5144 if (acl->len > buflen)
5145 goto out;
5146 memcpy(buf, acl->data, acl->len);
5147out_len:
5148 ret = acl->len;
5149out:
5150 spin_unlock(&inode->i_lock);
5151 return ret;
5152}
5153
Sachin Prabhu5794d212012-04-17 14:36:40 +01005154static 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 +00005155{
5156 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005157 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005158
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005159 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005160 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005161 if (acl == NULL)
5162 goto out;
5163 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005164 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005165 } else {
5166 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5167 if (acl == NULL)
5168 goto out;
5169 acl->cached = 0;
5170 }
5171 acl->len = acl_len;
5172out:
5173 nfs4_set_cached_acl(inode, acl);
5174}
5175
Andy Adamsonbf118a32011-12-07 11:55:27 -05005176/*
5177 * The getxattr API returns the required buffer length when called with a
5178 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5179 * the required buf. On a NULL buf, we send a page of data to the server
5180 * guessing that the ACL request can be serviced by a page. If so, we cache
5181 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5182 * the cache. If not so, we throw away the page, and cache the required
5183 * length. The next getxattr call will then produce another round trip to
5184 * the server, this time with the input buf of the required size.
5185 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005186static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005187{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005188 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005189 struct nfs_getaclargs args = {
5190 .fh = NFS_FH(inode),
5191 .acl_pages = pages,
5192 .acl_len = buflen,
5193 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005194 struct nfs_getaclres res = {
5195 .acl_len = buflen,
5196 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005197 struct rpc_message msg = {
5198 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5199 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005200 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005201 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005202 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005203 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005204
Trond Myklebust21f498c2012-08-24 10:59:25 -04005205 if (npages > ARRAY_SIZE(pages))
5206 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005207
Andy Adamsonbf118a32011-12-07 11:55:27 -05005208 for (i = 0; i < npages; i++) {
5209 pages[i] = alloc_page(GFP_KERNEL);
5210 if (!pages[i])
5211 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005212 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005213
5214 /* for decoding across pages */
5215 res.acl_scratch = alloc_page(GFP_KERNEL);
5216 if (!res.acl_scratch)
5217 goto out_free;
5218
Andy Adamsonbf118a32011-12-07 11:55:27 -05005219 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005220
Peng Taode040be2012-01-10 22:42:47 +08005221 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005222 __func__, buf, buflen, npages, args.acl_len);
5223 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5224 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005225 if (ret)
5226 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005227
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005228 /* Handle the case where the passed-in buffer is too short */
5229 if (res.acl_flags & NFS4_ACL_TRUNC) {
5230 /* Did the user only issue a request for the acl length? */
5231 if (buf == NULL)
5232 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005233 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005234 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005235 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005236 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005237 if (buf) {
5238 if (res.acl_len > buflen) {
5239 ret = -ERANGE;
5240 goto out_free;
5241 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005242 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005243 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005244out_ok:
5245 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005246out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005247 for (i = 0; i < npages; i++)
5248 if (pages[i])
5249 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005250 if (res.acl_scratch)
5251 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005252 return ret;
5253}
5254
Trond Myklebust16b42892006-08-24 12:27:15 -04005255static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5256{
5257 struct nfs4_exception exception = { };
5258 ssize_t ret;
5259 do {
5260 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005261 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005262 if (ret >= 0)
5263 break;
5264 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5265 } while (exception.retry);
5266 return ret;
5267}
5268
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005269static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5270{
5271 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005272 int ret;
5273
5274 if (!nfs4_server_supports_acls(server))
5275 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005276 ret = nfs_revalidate_inode(server, inode);
5277 if (ret < 0)
5278 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005279 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5280 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005281 ret = nfs4_read_cached_acl(inode, buf, buflen);
5282 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005283 /* -ENOENT is returned if there is no ACL or if there is an ACL
5284 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005285 return ret;
5286 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005287}
5288
Trond Myklebust16b42892006-08-24 12:27:15 -04005289static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005290{
5291 struct nfs_server *server = NFS_SERVER(inode);
5292 struct page *pages[NFS4ACL_MAXPAGES];
5293 struct nfs_setaclargs arg = {
5294 .fh = NFS_FH(inode),
5295 .acl_pages = pages,
5296 .acl_len = buflen,
5297 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005298 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005299 struct rpc_message msg = {
5300 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5301 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005302 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005303 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005304 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005305 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005306
5307 if (!nfs4_server_supports_acls(server))
5308 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005309 if (npages > ARRAY_SIZE(pages))
5310 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005311 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005312 if (i < 0)
5313 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005314 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005315 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005316
5317 /*
5318 * Free each page after tx, so the only ref left is
5319 * held by the network stack
5320 */
5321 for (; i > 0; i--)
5322 put_page(pages[i-1]);
5323
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005324 /*
5325 * Acl update can result in inode attribute update.
5326 * so mark the attribute cache invalid.
5327 */
5328 spin_lock(&inode->i_lock);
5329 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5330 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005331 nfs_access_zap_cache(inode);
5332 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005333 return ret;
5334}
5335
Trond Myklebust16b42892006-08-24 12:27:15 -04005336static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5337{
5338 struct nfs4_exception exception = { };
5339 int err;
5340 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005341 err = __nfs4_proc_set_acl(inode, buf, buflen);
5342 trace_nfs4_set_acl(inode, err);
5343 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005344 &exception);
5345 } while (exception.retry);
5346 return err;
5347}
5348
David Quigleyaa9c2662013-05-22 12:50:44 -04005349#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5350static int _nfs4_get_security_label(struct inode *inode, void *buf,
5351 size_t buflen)
5352{
5353 struct nfs_server *server = NFS_SERVER(inode);
5354 struct nfs_fattr fattr;
5355 struct nfs4_label label = {0, 0, buflen, buf};
5356
5357 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005358 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005359 .fh = NFS_FH(inode),
5360 .bitmask = bitmask,
5361 };
5362 struct nfs4_getattr_res res = {
5363 .fattr = &fattr,
5364 .label = &label,
5365 .server = server,
5366 };
5367 struct rpc_message msg = {
5368 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005369 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005370 .rpc_resp = &res,
5371 };
5372 int ret;
5373
5374 nfs_fattr_init(&fattr);
5375
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005376 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005377 if (ret)
5378 return ret;
5379 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5380 return -ENOENT;
5381 if (buflen < label.len)
5382 return -ERANGE;
5383 return 0;
5384}
5385
5386static int nfs4_get_security_label(struct inode *inode, void *buf,
5387 size_t buflen)
5388{
5389 struct nfs4_exception exception = { };
5390 int err;
5391
5392 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5393 return -EOPNOTSUPP;
5394
5395 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005396 err = _nfs4_get_security_label(inode, buf, buflen);
5397 trace_nfs4_get_security_label(inode, err);
5398 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005399 &exception);
5400 } while (exception.retry);
5401 return err;
5402}
5403
5404static int _nfs4_do_set_security_label(struct inode *inode,
5405 struct nfs4_label *ilabel,
5406 struct nfs_fattr *fattr,
5407 struct nfs4_label *olabel)
5408{
5409
5410 struct iattr sattr = {0};
5411 struct nfs_server *server = NFS_SERVER(inode);
5412 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005413 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005414 .fh = NFS_FH(inode),
5415 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005416 .server = server,
5417 .bitmask = bitmask,
5418 .label = ilabel,
5419 };
5420 struct nfs_setattrres res = {
5421 .fattr = fattr,
5422 .label = olabel,
5423 .server = server,
5424 };
5425 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005426 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5427 .rpc_argp = &arg,
5428 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005429 };
5430 int status;
5431
Jeff Layton12207f62013-11-01 10:49:32 -04005432 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005433
Jeff Layton12207f62013-11-01 10:49:32 -04005434 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005435 if (status)
5436 dprintk("%s failed: %d\n", __func__, status);
5437
5438 return status;
5439}
5440
5441static int nfs4_do_set_security_label(struct inode *inode,
5442 struct nfs4_label *ilabel,
5443 struct nfs_fattr *fattr,
5444 struct nfs4_label *olabel)
5445{
5446 struct nfs4_exception exception = { };
5447 int err;
5448
5449 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005450 err = _nfs4_do_set_security_label(inode, ilabel,
5451 fattr, olabel);
5452 trace_nfs4_set_security_label(inode, err);
5453 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005454 &exception);
5455 } while (exception.retry);
5456 return err;
5457}
5458
5459static int
Al Viro59301222016-05-27 10:19:30 -04005460nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005461{
5462 struct nfs4_label ilabel, *olabel = NULL;
5463 struct nfs_fattr fattr;
5464 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005465 int status;
5466
5467 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5468 return -EOPNOTSUPP;
5469
5470 nfs_fattr_init(&fattr);
5471
5472 ilabel.pi = 0;
5473 ilabel.lfs = 0;
5474 ilabel.label = (char *)buf;
5475 ilabel.len = buflen;
5476
5477 cred = rpc_lookup_cred();
5478 if (IS_ERR(cred))
5479 return PTR_ERR(cred);
5480
5481 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5482 if (IS_ERR(olabel)) {
5483 status = -PTR_ERR(olabel);
5484 goto out;
5485 }
5486
5487 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5488 if (status == 0)
5489 nfs_setsecurity(inode, &fattr, olabel);
5490
5491 nfs4_label_free(olabel);
5492out:
5493 put_rpccred(cred);
5494 return status;
5495}
5496#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5497
5498
Chuck Leverf0920752012-05-21 22:45:41 -04005499static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5500 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005501{
5502 __be32 verf[2];
5503
Chuck Lever2c820d92012-05-21 22:45:33 -04005504 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5505 /* An impossible timestamp guarantees this value
5506 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005507 verf[0] = cpu_to_be32(U32_MAX);
5508 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005509 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005510 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005511 u64 ns = ktime_to_ns(nn->boot_time);
5512
5513 verf[0] = cpu_to_be32(ns >> 32);
5514 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005515 }
Chuck Levercd937102012-03-02 17:14:31 -05005516 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5517}
5518
Jeff Laytona3192682015-06-09 19:43:59 -04005519static int
5520nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005521{
Jeff Laytona3192682015-06-09 19:43:59 -04005522 size_t len;
5523 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005524
Trond Myklebustceb3a162015-01-03 15:16:04 -05005525 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005526 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005527
Jeff Laytona3192682015-06-09 19:43:59 -04005528 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005529 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005530 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5531 1 +
5532 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5533 1;
5534 rcu_read_unlock();
5535
5536 if (len > NFS4_OPAQUE_LIMIT + 1)
5537 return -EINVAL;
5538
5539 /*
5540 * Since this string is allocated at mount time, and held until the
5541 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5542 * about a memory-reclaim deadlock.
5543 */
5544 str = kmalloc(len, GFP_KERNEL);
5545 if (!str)
5546 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005547
Chuck Levere984a552012-09-14 17:24:21 -04005548 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005549 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005550 clp->cl_ipaddr,
5551 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5552 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005553 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005554
Jeff Laytona3192682015-06-09 19:43:59 -04005555 clp->cl_owner_id = str;
5556 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005557}
5558
Jeff Layton873e3852015-06-09 19:44:00 -04005559static int
5560nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005561{
Jeff Layton873e3852015-06-09 19:44:00 -04005562 size_t len;
5563 char *str;
5564
5565 len = 10 + 10 + 1 + 10 + 1 +
5566 strlen(nfs4_client_id_uniquifier) + 1 +
5567 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5568
5569 if (len > NFS4_OPAQUE_LIMIT + 1)
5570 return -EINVAL;
5571
5572 /*
5573 * Since this string is allocated at mount time, and held until the
5574 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5575 * about a memory-reclaim deadlock.
5576 */
5577 str = kmalloc(len, GFP_KERNEL);
5578 if (!str)
5579 return -ENOMEM;
5580
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005581 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005582 clp->rpc_ops->version, clp->cl_minorversion,
5583 nfs4_client_id_uniquifier,
5584 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005585 clp->cl_owner_id = str;
5586 return 0;
5587}
5588
5589static int
5590nfs4_init_uniform_client_string(struct nfs_client *clp)
5591{
Jeff Layton873e3852015-06-09 19:44:00 -04005592 size_t len;
5593 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005594
5595 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005596 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005597
5598 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005599 return nfs4_init_uniquifier_client_string(clp);
5600
5601 len = 10 + 10 + 1 + 10 + 1 +
5602 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5603
5604 if (len > NFS4_OPAQUE_LIMIT + 1)
5605 return -EINVAL;
5606
5607 /*
5608 * Since this string is allocated at mount time, and held until the
5609 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5610 * about a memory-reclaim deadlock.
5611 */
5612 str = kmalloc(len, GFP_KERNEL);
5613 if (!str)
5614 return -ENOMEM;
5615
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005616 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005617 clp->rpc_ops->version, clp->cl_minorversion,
5618 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005619 clp->cl_owner_id = str;
5620 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005621}
5622
Chuck Lever706cb8d2014-03-12 12:51:47 -04005623/*
5624 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5625 * services. Advertise one based on the address family of the
5626 * clientaddr.
5627 */
5628static unsigned int
5629nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5630{
5631 if (strchr(clp->cl_ipaddr, ':') != NULL)
5632 return scnprintf(buf, len, "tcp6");
5633 else
5634 return scnprintf(buf, len, "tcp");
5635}
5636
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005637static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5638{
5639 struct nfs4_setclientid *sc = calldata;
5640
5641 if (task->tk_status == 0)
5642 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5643}
5644
5645static const struct rpc_call_ops nfs4_setclientid_ops = {
5646 .rpc_call_done = nfs4_setclientid_done,
5647};
5648
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005649/**
5650 * nfs4_proc_setclientid - Negotiate client ID
5651 * @clp: state data structure
5652 * @program: RPC program for NFSv4 callback service
5653 * @port: IP port number for NFS4 callback service
5654 * @cred: RPC credential to use for this call
5655 * @res: where to place the result
5656 *
5657 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5658 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005659int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5660 unsigned short port, struct rpc_cred *cred,
5661 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005662{
5663 nfs4_verifier sc_verifier;
5664 struct nfs4_setclientid setclientid = {
5665 .sc_verifier = &sc_verifier,
5666 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005667 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005668 };
5669 struct rpc_message msg = {
5670 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5671 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005672 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005673 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005674 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005675 struct rpc_task *task;
5676 struct rpc_task_setup task_setup_data = {
5677 .rpc_client = clp->cl_rpcclient,
5678 .rpc_message = &msg,
5679 .callback_ops = &nfs4_setclientid_ops,
5680 .callback_data = &setclientid,
5681 .flags = RPC_TASK_TIMEOUT,
5682 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005683 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005684
Chuck Leverde734832012-07-11 16:30:50 -04005685 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005686 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005687
5688 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5689 status = nfs4_init_uniform_client_string(clp);
5690 else
Jeff Laytona3192682015-06-09 19:43:59 -04005691 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005692
5693 if (status)
5694 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005695
Chuck Leverde734832012-07-11 16:30:50 -04005696 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005697 setclientid.sc_netid_len =
5698 nfs4_init_callback_netid(clp,
5699 setclientid.sc_netid,
5700 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005701 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005702 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005703 clp->cl_ipaddr, port >> 8, port & 255);
5704
Jeff Layton3a6bb732015-06-09 19:43:57 -04005705 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005706 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005707 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005708 task = rpc_run_task(&task_setup_data);
5709 if (IS_ERR(task)) {
5710 status = PTR_ERR(task);
5711 goto out;
5712 }
5713 status = task->tk_status;
5714 if (setclientid.sc_cred) {
5715 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5716 put_rpccred(setclientid.sc_cred);
5717 }
5718 rpc_put_task(task);
5719out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005720 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005721 dprintk("NFS reply setclientid: %d\n", status);
5722 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005723}
5724
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005725/**
5726 * nfs4_proc_setclientid_confirm - Confirm client ID
5727 * @clp: state data structure
5728 * @res: result of a previous SETCLIENTID
5729 * @cred: RPC credential to use for this call
5730 *
5731 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5732 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005733int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005734 struct nfs4_setclientid_res *arg,
5735 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 struct rpc_message msg = {
5738 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005739 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005740 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005742 int status;
5743
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005744 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5745 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5746 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005747 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005748 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005749 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005750 return status;
5751}
5752
Trond Myklebustfe650402006-01-03 09:55:18 +01005753struct nfs4_delegreturndata {
5754 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005755 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005756 struct nfs_fh fh;
5757 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005758 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005759 struct {
5760 struct nfs4_layoutreturn_args arg;
5761 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005762 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005763 u32 roc_barrier;
5764 bool roc;
5765 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005766 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005767 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005768 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005769};
5770
Trond Myklebustfe650402006-01-03 09:55:18 +01005771static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5772{
5773 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005774 struct nfs4_exception exception = {
5775 .inode = data->inode,
5776 .stateid = &data->stateid,
5777 };
Andy Adamson938e1012009-04-01 09:22:28 -04005778
Trond Myklebust14516c32010-07-31 14:29:06 -04005779 if (!nfs4_sequence_done(task, &data->res.seq_res))
5780 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005781
Trond Myklebustca8acf82013-08-13 10:36:56 -04005782 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005783
5784 /* Handle Layoutreturn errors */
5785 if (data->args.lr_args && task->tk_status != 0) {
5786 switch(data->res.lr_ret) {
5787 default:
5788 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5789 break;
5790 case 0:
5791 data->args.lr_args = NULL;
5792 data->res.lr_res = NULL;
5793 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005794 case -NFS4ERR_OLD_STATEID:
5795 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005796 data->inode))
5797 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005798 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005799 case -NFS4ERR_ADMIN_REVOKED:
5800 case -NFS4ERR_DELEG_REVOKED:
5801 case -NFS4ERR_EXPIRED:
5802 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005803 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5804 case -NFS4ERR_WRONG_CRED:
5805 data->args.lr_args = NULL;
5806 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005807 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005808 }
5809 }
5810
Ricardo Labiaga79708862009-12-07 09:23:21 -05005811 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005812 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005813 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005814 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005815 case -NFS4ERR_ADMIN_REVOKED:
5816 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005817 case -NFS4ERR_EXPIRED:
5818 nfs4_free_revoked_stateid(data->res.server,
5819 data->args.stateid,
5820 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005821 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005822 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005823 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005824 task->tk_status = 0;
5825 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005826 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005827 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5828 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005829 task->tk_status = 0;
5830 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005831 case -NFS4ERR_ACCESS:
5832 if (data->args.bitmask) {
5833 data->args.bitmask = NULL;
5834 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005835 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005836 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005837 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005838 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005839 task->tk_status = nfs4_async_handle_exception(task,
5840 data->res.server, task->tk_status,
5841 &exception);
5842 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005843 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005844 }
5845 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005846 return;
5847lr_restart:
5848 data->res.lr_ret = 0;
5849out_restart:
5850 task->tk_status = 0;
5851 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005852}
5853
5854static void nfs4_delegreturn_release(void *calldata)
5855{
Peng Tao039b7562014-07-03 13:05:02 +08005856 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005857 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005858
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005859 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005860 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005861 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5862 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005863 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005864 nfs_iput_and_deactive(inode);
5865 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005866 kfree(calldata);
5867}
5868
Andy Adamson938e1012009-04-01 09:22:28 -04005869static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5870{
5871 struct nfs4_delegreturndata *d_data;
5872
5873 d_data = (struct nfs4_delegreturndata *)data;
5874
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005875 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005876 return;
5877
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005878 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005879 &d_data->args.seq_args,
5880 &d_data->res.seq_res,
5881 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005882}
Andy Adamson938e1012009-04-01 09:22:28 -04005883
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005884static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005885 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005886 .rpc_call_done = nfs4_delegreturn_done,
5887 .rpc_release = nfs4_delegreturn_release,
5888};
5889
Trond Myklebuste6f81072008-01-24 18:14:34 -05005890static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005891{
5892 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005893 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005894 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005895 struct rpc_message msg = {
5896 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5897 .rpc_cred = cred,
5898 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005899 struct rpc_task_setup task_setup_data = {
5900 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005901 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005902 .callback_ops = &nfs4_delegreturn_ops,
5903 .flags = RPC_TASK_ASYNC,
5904 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005905 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005906
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005907 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005908 if (data == NULL)
5909 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005910 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005911
5912 nfs4_state_protect(server->nfs_client,
5913 NFS_SP4_MACH_CRED_CLEANUP,
5914 &task_setup_data.rpc_client, &msg);
5915
Trond Myklebustfe650402006-01-03 09:55:18 +01005916 data->args.fhandle = &data->fh;
5917 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005918 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005919 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005920 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005921 data->res.fattr = &data->fattr;
5922 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005923 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005924 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005925 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005926 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005927 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005928 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005929 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005930 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005931 if (data->lr.roc) {
5932 data->args.lr_args = &data->lr.arg;
5933 data->res.lr_res = &data->lr.res;
5934 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005935 } else if (data->lr.roc) {
5936 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5937 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005938 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005939
Trond Myklebustc970aa82007-07-14 15:39:59 -04005940 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005941 msg.rpc_argp = &data->args;
5942 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005943 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005944 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005945 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005946 if (!issync)
5947 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005948 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005949 if (status != 0)
5950 goto out;
5951 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005952out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005953 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005954 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005955}
5956
Trond Myklebuste6f81072008-01-24 18:14:34 -05005957int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005958{
5959 struct nfs_server *server = NFS_SERVER(inode);
5960 struct nfs4_exception exception = { };
5961 int err;
5962 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005963 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005964 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005965 switch (err) {
5966 case -NFS4ERR_STALE_STATEID:
5967 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07005968 case 0:
5969 return 0;
5970 }
5971 err = nfs4_handle_exception(server, err, &exception);
5972 } while (exception.retry);
5973 return err;
5974}
5975
Linus Torvalds1da177e2005-04-16 15:20:36 -07005976static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
5977{
5978 struct inode *inode = state->inode;
5979 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04005980 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005981 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07005982 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005983 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005984 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005985 struct nfs_lockt_res res = {
5986 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005987 };
5988 struct rpc_message msg = {
5989 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005990 .rpc_argp = &arg,
5991 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992 .rpc_cred = state->owner->so_cred,
5993 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005994 struct nfs4_lock_state *lsp;
5995 int status;
5996
Trond Myklebust911d1aa2006-01-03 09:55:16 +01005997 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00005998 status = nfs4_set_lock_state(state, request);
5999 if (status != 0)
6000 goto out;
6001 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006002 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006003 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006004 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006005 switch (status) {
6006 case 0:
6007 request->fl_type = F_UNLCK;
6008 break;
6009 case -NFS4ERR_DENIED:
6010 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006011 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006012 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006013 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006014out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006015 return status;
6016}
6017
6018static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6019{
6020 struct nfs4_exception exception = { };
6021 int err;
6022
6023 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006024 err = _nfs4_proc_getlk(state, cmd, request);
6025 trace_nfs4_get_lock(request, state, cmd, err);
6026 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 &exception);
6028 } while (exception.retry);
6029 return err;
6030}
6031
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006032struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006033 struct nfs_locku_args arg;
6034 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006035 struct nfs4_lock_state *lsp;
6036 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006037 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006038 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006039 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006040 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006041};
6042
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006043static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6044 struct nfs_open_context *ctx,
6045 struct nfs4_lock_state *lsp,
6046 struct nfs_seqid *seqid)
6047{
6048 struct nfs4_unlockdata *p;
6049 struct inode *inode = lsp->ls_state->inode;
6050
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006051 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006052 if (p == NULL)
6053 return NULL;
6054 p->arg.fh = NFS_FH(inode);
6055 p->arg.fl = &p->fl;
6056 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006057 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006058 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006059 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006060 /* Ensure we don't close file until we're done freeing locks! */
6061 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006062 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006063 memcpy(&p->fl, fl, sizeof(p->fl));
6064 p->server = NFS_SERVER(inode);
6065 return p;
6066}
6067
Trond Myklebust06f814a2006-01-03 09:55:07 +01006068static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006069{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006070 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006071 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006072 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006073 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006074 put_nfs_open_context(calldata->ctx);
6075 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006076}
6077
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006078static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006079{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006080 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006081 struct nfs4_exception exception = {
6082 .inode = calldata->lsp->ls_state->inode,
6083 .stateid = &calldata->arg.stateid,
6084 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006085
Trond Myklebust14516c32010-07-31 14:29:06 -04006086 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6087 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006088 switch (task->tk_status) {
6089 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006090 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006091 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006092 if (nfs4_update_lock_stateid(calldata->lsp,
6093 &calldata->res.stateid))
6094 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006095 case -NFS4ERR_ADMIN_REVOKED:
6096 case -NFS4ERR_EXPIRED:
6097 nfs4_free_revoked_stateid(calldata->server,
6098 &calldata->arg.stateid,
6099 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006100 case -NFS4ERR_BAD_STATEID:
6101 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006102 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006103 if (!nfs4_stateid_match(&calldata->arg.stateid,
6104 &calldata->lsp->ls_stateid))
6105 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006106 break;
6107 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006108 task->tk_status = nfs4_async_handle_exception(task,
6109 calldata->server, task->tk_status,
6110 &exception);
6111 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006112 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006113 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006114 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006115}
6116
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006117static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006118{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006119 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006120
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006121 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6122 nfs_async_iocounter_wait(task, calldata->l_ctx))
6123 return;
6124
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006125 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006126 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006127 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006128 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006129 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006130 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006131 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006132 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006133 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006134 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006135 &calldata->res.seq_res,
6136 task) != 0)
6137 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006138 return;
6139out_no_action:
6140 task->tk_action = NULL;
6141out_wait:
6142 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006143}
6144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006145static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006146 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006147 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006148 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006149};
6150
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006151static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6152 struct nfs_open_context *ctx,
6153 struct nfs4_lock_state *lsp,
6154 struct nfs_seqid *seqid)
6155{
6156 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006157 struct rpc_message msg = {
6158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6159 .rpc_cred = ctx->cred,
6160 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006161 struct rpc_task_setup task_setup_data = {
6162 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006163 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006164 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006165 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006166 .flags = RPC_TASK_ASYNC,
6167 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006168
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006169 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6170 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6171
Frank Filz137d6ac2007-07-09 15:32:29 -07006172 /* Ensure this is an unlock - when canceling a lock, the
6173 * canceled lock is passed in, and it won't be an unlock.
6174 */
6175 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006176 if (fl->fl_flags & FL_CLOSE)
6177 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006178
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006179 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6180 if (data == NULL) {
6181 nfs_free_seqid(seqid);
6182 return ERR_PTR(-ENOMEM);
6183 }
6184
Chuck Levera9c92d62013-08-09 12:48:18 -04006185 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006186 msg.rpc_argp = &data->arg;
6187 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006188 task_setup_data.callback_data = data;
6189 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006190}
6191
Linus Torvalds1da177e2005-04-16 15:20:36 -07006192static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6193{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006194 struct inode *inode = state->inode;
6195 struct nfs4_state_owner *sp = state->owner;
6196 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006197 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006198 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006199 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006200 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006201 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006202 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006203
Trond Myklebust9b073572006-06-29 16:38:34 -04006204 status = nfs4_set_lock_state(state, request);
6205 /* Unlock _before_ we do the RPC call */
6206 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006207 /* Exclude nfs_delegation_claim_locks() */
6208 mutex_lock(&sp->so_delegreturn_mutex);
6209 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006210 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006211 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006212 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006213 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006214 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006215 }
6216 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006217 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006218 if (status != 0)
6219 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006220 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006221 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006222 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6223 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006224 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6225 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006226 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006227 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006228 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006229 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006230 status = PTR_ERR(task);
6231 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006232 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006233 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006234 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006235out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006236 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006237 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006238 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006239}
6240
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241struct nfs4_lockdata {
6242 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006243 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006244 struct nfs4_lock_state *lsp;
6245 struct nfs_open_context *ctx;
6246 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006247 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006248 int rpc_status;
6249 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006250 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006251};
6252
6253static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006254 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6255 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006256{
6257 struct nfs4_lockdata *p;
6258 struct inode *inode = lsp->ls_state->inode;
6259 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006260 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006261
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006262 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006263 if (p == NULL)
6264 return NULL;
6265
6266 p->arg.fh = NFS_FH(inode);
6267 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006268 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006269 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006270 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006271 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6272 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006273 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006274 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006275 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006276 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006277 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006278 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006279 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006280 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006281 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006282 p->ctx = get_nfs_open_context(ctx);
6283 memcpy(&p->fl, fl, sizeof(p->fl));
6284 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006285out_free_seqid:
6286 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006287out_free:
6288 kfree(p);
6289 return NULL;
6290}
6291
6292static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6293{
6294 struct nfs4_lockdata *data = calldata;
6295 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006296
Harvey Harrison3110ff82008-05-02 13:42:44 -07006297 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006298 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006299 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006300 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006301 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006302 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006303 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006304 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006305 nfs4_stateid_copy(&data->arg.open_stateid,
6306 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006307 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006308 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006309 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006310 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006311 nfs4_stateid_copy(&data->arg.lock_stateid,
6312 &data->lsp->ls_stateid);
6313 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006314 if (!nfs4_valid_open_stateid(state)) {
6315 data->rpc_status = -EBADF;
6316 task->tk_action = NULL;
6317 goto out_release_open_seqid;
6318 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006319 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006320 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006321 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006322 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006323 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006324 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006325out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006326 nfs_release_seqid(data->arg.open_seqid);
6327out_release_lock_seqid:
6328 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006329out_wait:
6330 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006331 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006332}
6333
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006334static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6335{
6336 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006337 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006338
Harvey Harrison3110ff82008-05-02 13:42:44 -07006339 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006340
Trond Myklebust14516c32010-07-31 14:29:06 -04006341 if (!nfs4_sequence_done(task, &data->res.seq_res))
6342 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006343
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006344 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006345 switch (task->tk_status) {
6346 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006347 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006348 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006349 if (data->arg.new_lock) {
6350 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006351 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006352 rpc_restart_call_prepare(task);
6353 break;
6354 }
6355 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006356 if (data->arg.new_lock_owner != 0) {
6357 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6358 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6359 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6360 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6361 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006362 break;
6363 case -NFS4ERR_BAD_STATEID:
6364 case -NFS4ERR_OLD_STATEID:
6365 case -NFS4ERR_STALE_STATEID:
6366 case -NFS4ERR_EXPIRED:
6367 if (data->arg.new_lock_owner != 0) {
6368 if (!nfs4_stateid_match(&data->arg.open_stateid,
6369 &lsp->ls_state->open_stateid))
6370 rpc_restart_call_prepare(task);
6371 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6372 &lsp->ls_stateid))
6373 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006374 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006375 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006376}
6377
6378static void nfs4_lock_release(void *calldata)
6379{
6380 struct nfs4_lockdata *data = calldata;
6381
Harvey Harrison3110ff82008-05-02 13:42:44 -07006382 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006383 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006384 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006385 struct rpc_task *task;
6386 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6387 data->arg.lock_seqid);
6388 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006389 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006390 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006391 } else
6392 nfs_free_seqid(data->arg.lock_seqid);
6393 nfs4_put_lock_state(data->lsp);
6394 put_nfs_open_context(data->ctx);
6395 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006396 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006397}
6398
6399static const struct rpc_call_ops nfs4_lock_ops = {
6400 .rpc_call_prepare = nfs4_lock_prepare,
6401 .rpc_call_done = nfs4_lock_done,
6402 .rpc_release = nfs4_lock_release,
6403};
6404
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006405static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6406{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006407 switch (error) {
6408 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006409 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006410 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006411 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006412 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006413 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006414 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006415 break;
6416 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006417 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006418 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006419 };
6420}
6421
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006422static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006423{
6424 struct nfs4_lockdata *data;
6425 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006426 struct rpc_message msg = {
6427 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6428 .rpc_cred = state->owner->so_cred,
6429 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006430 struct rpc_task_setup task_setup_data = {
6431 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006432 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006433 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006434 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006435 .flags = RPC_TASK_ASYNC,
6436 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006437 int ret;
6438
Harvey Harrison3110ff82008-05-02 13:42:44 -07006439 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006440 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006441 fl->fl_u.nfs4_fl.owner,
6442 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006443 if (data == NULL)
6444 return -ENOMEM;
6445 if (IS_SETLKW(cmd))
6446 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006447 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006448 msg.rpc_argp = &data->arg;
6449 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006450 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006451 if (recovery_type > NFS_LOCK_NEW) {
6452 if (recovery_type == NFS_LOCK_RECLAIM)
6453 data->arg.reclaim = NFS_LOCK_RECLAIM;
6454 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006455 } else
6456 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006457 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006458 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006459 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006460 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006461 if (ret == 0) {
6462 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006463 if (ret)
6464 nfs4_handle_setlk_error(data->server, data->lsp,
6465 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006466 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006467 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006468 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006469 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006470 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006471 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006472}
6473
6474static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6475{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006476 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006477 struct nfs4_exception exception = {
6478 .inode = state->inode,
6479 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006480 int err;
6481
6482 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006483 /* Cache the lock if possible... */
6484 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6485 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006486 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006487 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006488 break;
6489 nfs4_handle_exception(server, err, &exception);
6490 } while (exception.retry);
6491 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006492}
6493
6494static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6495{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006496 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006497 struct nfs4_exception exception = {
6498 .inode = state->inode,
6499 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006500 int err;
6501
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006502 err = nfs4_set_lock_state(state, request);
6503 if (err != 0)
6504 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006505 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006506 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6507 return 0;
6508 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006509 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006510 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6511 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006512 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006513 switch (err) {
6514 default:
6515 goto out;
6516 case -NFS4ERR_GRACE:
6517 case -NFS4ERR_DELAY:
6518 nfs4_handle_exception(server, err, &exception);
6519 err = 0;
6520 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006521 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006522out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006523 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006524}
6525
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006526#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006527static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6528{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006529 struct nfs4_lock_state *lsp;
6530 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006531
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006532 status = nfs4_set_lock_state(state, request);
6533 if (status != 0)
6534 return status;
6535 lsp = request->fl_u.nfs4_fl.owner;
6536 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6537 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6538 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006539 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006540}
6541#endif
6542
Linus Torvalds1da177e2005-04-16 15:20:36 -07006543static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6544{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006545 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006546 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006547 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006548 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006549
Trond Myklebust01c3b862006-06-29 16:38:39 -04006550 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006551 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006552 if (status < 0)
6553 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006554 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006555 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006556 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006557 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006558 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006559 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006560 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006561 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006562 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006563 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006564 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006565 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006566 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006567 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006568out:
6569 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006570 return status;
6571}
6572
6573static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6574{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006575 struct nfs4_exception exception = {
6576 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006577 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006578 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006579 int err;
6580
6581 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006582 err = _nfs4_proc_setlk(state, cmd, request);
6583 if (err == -NFS4ERR_DENIED)
6584 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006585 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006586 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006587 } while (exception.retry);
6588 return err;
6589}
6590
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006591#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6592#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6593
6594static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006595nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6596 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006597{
6598 int status = -ERESTARTSYS;
6599 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6600
6601 while(!signalled()) {
6602 status = nfs4_proc_setlk(state, cmd, request);
6603 if ((status != -EAGAIN) || IS_SETLK(cmd))
6604 break;
6605 freezable_schedule_timeout_interruptible(timeout);
6606 timeout *= 2;
6607 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6608 status = -ERESTARTSYS;
6609 }
6610 return status;
6611}
6612
Jeff Laytona1d617d82016-09-17 18:17:39 -04006613#ifdef CONFIG_NFS_V4_1
6614struct nfs4_lock_waiter {
6615 struct task_struct *task;
6616 struct inode *inode;
6617 struct nfs_lowner *owner;
6618 bool notified;
6619};
6620
6621static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006622nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006623{
6624 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006625 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006626
Jeff Layton57174592018-03-18 08:37:03 -04006627 /* NULL key means to wake up everyone */
6628 if (key) {
6629 struct cb_notify_lock_args *cbnl = key;
6630 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6631 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006632
Jeff Layton57174592018-03-18 08:37:03 -04006633 /* Only wake if the callback was for the same owner. */
6634 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6635 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006636
Jeff Layton57174592018-03-18 08:37:03 -04006637 /* Make sure it's for the right inode */
6638 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6639 return 0;
6640
6641 waiter->notified = true;
6642 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006643
6644 /* override "private" so we can use default_wake_function */
6645 wait->private = waiter->task;
6646 ret = autoremove_wake_function(wait, mode, flags, key);
6647 wait->private = waiter;
6648 return ret;
6649}
6650
6651static int
6652nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6653{
6654 int status = -ERESTARTSYS;
6655 unsigned long flags;
6656 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6657 struct nfs_server *server = NFS_SERVER(state->inode);
6658 struct nfs_client *clp = server->nfs_client;
6659 wait_queue_head_t *q = &clp->cl_lock_waitq;
6660 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6661 .id = lsp->ls_seqid.owner_id,
6662 .s_dev = server->s_dev };
6663 struct nfs4_lock_waiter waiter = { .task = current,
6664 .inode = state->inode,
6665 .owner = &owner,
6666 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006667 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006668
6669 /* Don't bother with waitqueue if we don't expect a callback */
6670 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6671 return nfs4_retry_setlk_simple(state, cmd, request);
6672
6673 init_wait(&wait);
6674 wait.private = &waiter;
6675 wait.func = nfs4_wake_lock_waiter;
6676 add_wait_queue(q, &wait);
6677
6678 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006679 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006680 status = nfs4_proc_setlk(state, cmd, request);
6681 if ((status != -EAGAIN) || IS_SETLK(cmd))
6682 break;
6683
6684 status = -ERESTARTSYS;
6685 spin_lock_irqsave(&q->lock, flags);
6686 if (waiter.notified) {
6687 spin_unlock_irqrestore(&q->lock, flags);
6688 continue;
6689 }
6690 set_current_state(TASK_INTERRUPTIBLE);
6691 spin_unlock_irqrestore(&q->lock, flags);
6692
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006693 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006694 }
6695
6696 finish_wait(q, &wait);
6697 return status;
6698}
6699#else /* !CONFIG_NFS_V4_1 */
6700static inline int
6701nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6702{
6703 return nfs4_retry_setlk_simple(state, cmd, request);
6704}
6705#endif
6706
Linus Torvalds1da177e2005-04-16 15:20:36 -07006707static int
6708nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6709{
6710 struct nfs_open_context *ctx;
6711 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006712 int status;
6713
6714 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006715 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006716 state = ctx->state;
6717
Trond Myklebustd9531262009-07-21 19:22:38 -04006718 if (IS_GETLK(cmd)) {
6719 if (state != NULL)
6720 return nfs4_proc_getlk(state, F_GETLK, request);
6721 return 0;
6722 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006723
6724 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6725 return -EINVAL;
6726
Trond Myklebustd9531262009-07-21 19:22:38 -04006727 if (request->fl_type == F_UNLCK) {
6728 if (state != NULL)
6729 return nfs4_proc_unlck(state, cmd, request);
6730 return 0;
6731 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006732
Trond Myklebustd9531262009-07-21 19:22:38 -04006733 if (state == NULL)
6734 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006735
6736 if ((request->fl_flags & FL_POSIX) &&
6737 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6738 return -ENOLCK;
6739
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006740 /*
6741 * Don't rely on the VFS having checked the file open mode,
6742 * since it won't do this for flock() locks.
6743 */
6744 switch (request->fl_type) {
6745 case F_RDLCK:
6746 if (!(filp->f_mode & FMODE_READ))
6747 return -EBADF;
6748 break;
6749 case F_WRLCK:
6750 if (!(filp->f_mode & FMODE_WRITE))
6751 return -EBADF;
6752 }
6753
Jeff Layton1ea67db2016-09-17 18:17:37 -04006754 status = nfs4_set_lock_state(state, request);
6755 if (status != 0)
6756 return status;
6757
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006758 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006759}
6760
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006761int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006762{
6763 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006764 int err;
6765
6766 err = nfs4_set_lock_state(state, fl);
6767 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006768 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006769 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006770 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006771}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006772
Trond Myklebustcf470c32012-03-07 13:49:12 -05006773struct nfs_release_lockowner_data {
6774 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006775 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006776 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006777 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006778 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006779};
6780
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006781static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6782{
6783 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006784 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006785 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6786 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006787 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006788 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006789}
6790
6791static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6792{
6793 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006794 struct nfs_server *server = data->server;
6795
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006796 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006797
6798 switch (task->tk_status) {
6799 case 0:
6800 renew_lease(server, data->timestamp);
6801 break;
6802 case -NFS4ERR_STALE_CLIENTID:
6803 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006804 nfs4_schedule_lease_recovery(server->nfs_client);
6805 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006806 case -NFS4ERR_LEASE_MOVED:
6807 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006808 if (nfs4_async_handle_error(task, server,
6809 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006810 rpc_restart_call_prepare(task);
6811 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006812}
6813
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006814static void nfs4_release_lockowner_release(void *calldata)
6815{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006816 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006817 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006818 kfree(calldata);
6819}
6820
Trond Myklebust17280172012-03-11 13:11:00 -04006821static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006822 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6823 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006824 .rpc_release = nfs4_release_lockowner_release,
6825};
6826
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006827static void
6828nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006829{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006830 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006831 struct rpc_message msg = {
6832 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6833 };
6834
6835 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006836 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006837
Trond Myklebustcf470c32012-03-07 13:49:12 -05006838 data = kmalloc(sizeof(*data), GFP_NOFS);
6839 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006840 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006841 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006842 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006843 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6844 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6845 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006846
Trond Myklebustcf470c32012-03-07 13:49:12 -05006847 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006848 msg.rpc_resp = &data->res;
6849 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006850 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006851}
6852
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006853#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6854
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006855static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006856 struct dentry *unused, struct inode *inode,
6857 const char *key, const void *buf,
6858 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006859{
Al Viro59301222016-05-27 10:19:30 -04006860 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006861}
6862
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006863static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006864 struct dentry *unused, struct inode *inode,
6865 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006866{
Al Virob2968212016-04-10 20:48:24 -04006867 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006868}
6869
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006870static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006871{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006872 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006873}
6874
David Quigleyc9bccef2013-05-22 12:50:45 -04006875#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006876
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006877static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006878 struct dentry *unused, struct inode *inode,
6879 const char *key, const void *buf,
6880 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006881{
6882 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006883 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006884
6885 return -EOPNOTSUPP;
6886}
6887
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006888static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006889 struct dentry *unused, struct inode *inode,
6890 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006891{
6892 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006893 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006894 return -EOPNOTSUPP;
6895}
6896
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006897static ssize_t
6898nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006899{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006900 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006901
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006902 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6903 len = security_inode_listsecurity(inode, list, list_len);
6904 if (list_len && len > list_len)
6905 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006906 }
6907 return len;
6908}
6909
6910static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6911 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006912 .get = nfs4_xattr_get_nfs4_label,
6913 .set = nfs4_xattr_set_nfs4_label,
6914};
David Quigleyc9bccef2013-05-22 12:50:45 -04006915
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006916#else
6917
6918static ssize_t
6919nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6920{
6921 return 0;
6922}
6923
6924#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006925
Andy Adamson533eb462011-06-13 18:25:56 -04006926/*
6927 * nfs_fhget will use either the mounted_on_fileid or the fileid
6928 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006929static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6930{
Andy Adamson533eb462011-06-13 18:25:56 -04006931 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6932 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6933 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006934 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006935 return;
6936
6937 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006938 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006939 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6940 fattr->nlink = 2;
6941}
6942
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006943static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6944 const struct qstr *name,
6945 struct nfs4_fs_locations *fs_locations,
6946 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006947{
6948 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006949 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006950 struct nfs4_fs_locations_arg args = {
6951 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006952 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006953 .page = page,
6954 .bitmask = bitmask,
6955 };
Benny Halevy22958462009-04-01 09:22:02 -04006956 struct nfs4_fs_locations_res res = {
6957 .fs_locations = fs_locations,
6958 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006959 struct rpc_message msg = {
6960 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6961 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006962 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006963 };
6964 int status;
6965
Harvey Harrison3110ff82008-05-02 13:42:44 -07006966 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04006967
Chuck Leverc05cefc2017-11-05 15:45:22 -05006968 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
6969 bitmask[1] = nfs4_fattr_bitmap[1];
6970
Andy Adamson533eb462011-06-13 18:25:56 -04006971 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
6972 * is not supported */
6973 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05006974 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006975 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05006976 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04006977
Trond Myklebustc228fd32007-01-13 02:28:11 -05006978 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006979 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04006980 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006981 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006982 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04006983 return status;
6984}
6985
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006986int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6987 const struct qstr *name,
6988 struct nfs4_fs_locations *fs_locations,
6989 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006990{
6991 struct nfs4_exception exception = { };
6992 int err;
6993 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04006994 err = _nfs4_proc_fs_locations(client, dir, name,
6995 fs_locations, page);
6996 trace_nfs4_get_fs_locations(dir, name, err);
6997 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04006998 &exception);
6999 } while (exception.retry);
7000 return err;
7001}
7002
Chuck Leverb03d7352013-10-17 14:12:50 -04007003/*
7004 * This operation also signals the server that this client is
7005 * performing migration recovery. The server can stop returning
7006 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7007 * appended to this compound to identify the client ID which is
7008 * performing recovery.
7009 */
7010static int _nfs40_proc_get_locations(struct inode *inode,
7011 struct nfs4_fs_locations *locations,
7012 struct page *page, struct rpc_cred *cred)
7013{
7014 struct nfs_server *server = NFS_SERVER(inode);
7015 struct rpc_clnt *clnt = server->client;
7016 u32 bitmask[2] = {
7017 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7018 };
7019 struct nfs4_fs_locations_arg args = {
7020 .clientid = server->nfs_client->cl_clientid,
7021 .fh = NFS_FH(inode),
7022 .page = page,
7023 .bitmask = bitmask,
7024 .migration = 1, /* skip LOOKUP */
7025 .renew = 1, /* append RENEW */
7026 };
7027 struct nfs4_fs_locations_res res = {
7028 .fs_locations = locations,
7029 .migration = 1,
7030 .renew = 1,
7031 };
7032 struct rpc_message msg = {
7033 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7034 .rpc_argp = &args,
7035 .rpc_resp = &res,
7036 .rpc_cred = cred,
7037 };
7038 unsigned long now = jiffies;
7039 int status;
7040
7041 nfs_fattr_init(&locations->fattr);
7042 locations->server = server;
7043 locations->nlocations = 0;
7044
7045 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7046 nfs4_set_sequence_privileged(&args.seq_args);
7047 status = nfs4_call_sync_sequence(clnt, server, &msg,
7048 &args.seq_args, &res.seq_res);
7049 if (status)
7050 return status;
7051
7052 renew_lease(server, now);
7053 return 0;
7054}
7055
7056#ifdef CONFIG_NFS_V4_1
7057
7058/*
7059 * This operation also signals the server that this client is
7060 * performing migration recovery. The server can stop asserting
7061 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7062 * performing this operation is identified in the SEQUENCE
7063 * operation in this compound.
7064 *
7065 * When the client supports GETATTR(fs_locations_info), it can
7066 * be plumbed in here.
7067 */
7068static int _nfs41_proc_get_locations(struct inode *inode,
7069 struct nfs4_fs_locations *locations,
7070 struct page *page, struct rpc_cred *cred)
7071{
7072 struct nfs_server *server = NFS_SERVER(inode);
7073 struct rpc_clnt *clnt = server->client;
7074 u32 bitmask[2] = {
7075 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7076 };
7077 struct nfs4_fs_locations_arg args = {
7078 .fh = NFS_FH(inode),
7079 .page = page,
7080 .bitmask = bitmask,
7081 .migration = 1, /* skip LOOKUP */
7082 };
7083 struct nfs4_fs_locations_res res = {
7084 .fs_locations = locations,
7085 .migration = 1,
7086 };
7087 struct rpc_message msg = {
7088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7089 .rpc_argp = &args,
7090 .rpc_resp = &res,
7091 .rpc_cred = cred,
7092 };
7093 int status;
7094
7095 nfs_fattr_init(&locations->fattr);
7096 locations->server = server;
7097 locations->nlocations = 0;
7098
7099 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7100 nfs4_set_sequence_privileged(&args.seq_args);
7101 status = nfs4_call_sync_sequence(clnt, server, &msg,
7102 &args.seq_args, &res.seq_res);
7103 if (status == NFS4_OK &&
7104 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7105 status = -NFS4ERR_LEASE_MOVED;
7106 return status;
7107}
7108
7109#endif /* CONFIG_NFS_V4_1 */
7110
7111/**
7112 * nfs4_proc_get_locations - discover locations for a migrated FSID
7113 * @inode: inode on FSID that is migrating
7114 * @locations: result of query
7115 * @page: buffer
7116 * @cred: credential to use for this operation
7117 *
7118 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7119 * operation failed, or a negative errno if a local error occurred.
7120 *
7121 * On success, "locations" is filled in, but if the server has
7122 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7123 * asserted.
7124 *
7125 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7126 * from this client that require migration recovery.
7127 */
7128int nfs4_proc_get_locations(struct inode *inode,
7129 struct nfs4_fs_locations *locations,
7130 struct page *page, struct rpc_cred *cred)
7131{
7132 struct nfs_server *server = NFS_SERVER(inode);
7133 struct nfs_client *clp = server->nfs_client;
7134 const struct nfs4_mig_recovery_ops *ops =
7135 clp->cl_mvops->mig_recovery_ops;
7136 struct nfs4_exception exception = { };
7137 int status;
7138
7139 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7140 (unsigned long long)server->fsid.major,
7141 (unsigned long long)server->fsid.minor,
7142 clp->cl_hostname);
7143 nfs_display_fhandle(NFS_FH(inode), __func__);
7144
7145 do {
7146 status = ops->get_locations(inode, locations, page, cred);
7147 if (status != -NFS4ERR_DELAY)
7148 break;
7149 nfs4_handle_exception(server, status, &exception);
7150 } while (exception.retry);
7151 return status;
7152}
7153
Chuck Lever44c99932013-10-17 14:13:30 -04007154/*
7155 * This operation also signals the server that this client is
7156 * performing "lease moved" recovery. The server can stop
7157 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7158 * is appended to this compound to identify the client ID which is
7159 * performing recovery.
7160 */
7161static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7162{
7163 struct nfs_server *server = NFS_SERVER(inode);
7164 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7165 struct rpc_clnt *clnt = server->client;
7166 struct nfs4_fsid_present_arg args = {
7167 .fh = NFS_FH(inode),
7168 .clientid = clp->cl_clientid,
7169 .renew = 1, /* append RENEW */
7170 };
7171 struct nfs4_fsid_present_res res = {
7172 .renew = 1,
7173 };
7174 struct rpc_message msg = {
7175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7176 .rpc_argp = &args,
7177 .rpc_resp = &res,
7178 .rpc_cred = cred,
7179 };
7180 unsigned long now = jiffies;
7181 int status;
7182
7183 res.fh = nfs_alloc_fhandle();
7184 if (res.fh == NULL)
7185 return -ENOMEM;
7186
7187 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7188 nfs4_set_sequence_privileged(&args.seq_args);
7189 status = nfs4_call_sync_sequence(clnt, server, &msg,
7190 &args.seq_args, &res.seq_res);
7191 nfs_free_fhandle(res.fh);
7192 if (status)
7193 return status;
7194
7195 do_renew_lease(clp, now);
7196 return 0;
7197}
7198
7199#ifdef CONFIG_NFS_V4_1
7200
7201/*
7202 * This operation also signals the server that this client is
7203 * performing "lease moved" recovery. The server can stop asserting
7204 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7205 * this operation is identified in the SEQUENCE operation in this
7206 * compound.
7207 */
7208static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7209{
7210 struct nfs_server *server = NFS_SERVER(inode);
7211 struct rpc_clnt *clnt = server->client;
7212 struct nfs4_fsid_present_arg args = {
7213 .fh = NFS_FH(inode),
7214 };
7215 struct nfs4_fsid_present_res res = {
7216 };
7217 struct rpc_message msg = {
7218 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7219 .rpc_argp = &args,
7220 .rpc_resp = &res,
7221 .rpc_cred = cred,
7222 };
7223 int status;
7224
7225 res.fh = nfs_alloc_fhandle();
7226 if (res.fh == NULL)
7227 return -ENOMEM;
7228
7229 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7230 nfs4_set_sequence_privileged(&args.seq_args);
7231 status = nfs4_call_sync_sequence(clnt, server, &msg,
7232 &args.seq_args, &res.seq_res);
7233 nfs_free_fhandle(res.fh);
7234 if (status == NFS4_OK &&
7235 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7236 status = -NFS4ERR_LEASE_MOVED;
7237 return status;
7238}
7239
7240#endif /* CONFIG_NFS_V4_1 */
7241
7242/**
7243 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7244 * @inode: inode on FSID to check
7245 * @cred: credential to use for this operation
7246 *
7247 * Server indicates whether the FSID is present, moved, or not
7248 * recognized. This operation is necessary to clear a LEASE_MOVED
7249 * condition for this client ID.
7250 *
7251 * Returns NFS4_OK if the FSID is present on this server,
7252 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7253 * NFS4ERR code if some error occurred on the server, or a
7254 * negative errno if a local failure occurred.
7255 */
7256int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7257{
7258 struct nfs_server *server = NFS_SERVER(inode);
7259 struct nfs_client *clp = server->nfs_client;
7260 const struct nfs4_mig_recovery_ops *ops =
7261 clp->cl_mvops->mig_recovery_ops;
7262 struct nfs4_exception exception = { };
7263 int status;
7264
7265 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7266 (unsigned long long)server->fsid.major,
7267 (unsigned long long)server->fsid.minor,
7268 clp->cl_hostname);
7269 nfs_display_fhandle(NFS_FH(inode), __func__);
7270
7271 do {
7272 status = ops->fsid_present(inode, cred);
7273 if (status != -NFS4ERR_DELAY)
7274 break;
7275 nfs4_handle_exception(server, status, &exception);
7276 } while (exception.retry);
7277 return status;
7278}
7279
Andy Adamson5ec16a82013-08-08 10:57:55 -04007280/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007281 * If 'use_integrity' is true and the state managment nfs_client
7282 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7283 * and the machine credential as per RFC3530bis and RFC5661 Security
7284 * Considerations sections. Otherwise, just use the user cred with the
7285 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007286 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007287static 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 +00007288{
7289 int status;
7290 struct nfs4_secinfo_arg args = {
7291 .dir_fh = NFS_FH(dir),
7292 .name = name,
7293 };
7294 struct nfs4_secinfo_res res = {
7295 .flavors = flavors,
7296 };
7297 struct rpc_message msg = {
7298 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7299 .rpc_argp = &args,
7300 .rpc_resp = &res,
7301 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007302 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007303 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007304
7305 if (use_integrity) {
7306 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007307 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7308 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007309 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007310
7311 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007312
7313 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7314 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7315
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007316 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7317 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007318 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007319
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007320 if (cred)
7321 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007322
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007323 return status;
7324}
7325
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007326int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7327 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007328{
7329 struct nfs4_exception exception = { };
7330 int err;
7331 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007332 err = -NFS4ERR_WRONGSEC;
7333
7334 /* try to use integrity protection with machine cred */
7335 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7336 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7337
7338 /*
7339 * if unable to use integrity protection, or SECINFO with
7340 * integrity protection returns NFS4ERR_WRONGSEC (which is
7341 * disallowed by spec, but exists in deployed servers) use
7342 * the current filesystem's rpc_client and the user cred.
7343 */
7344 if (err == -NFS4ERR_WRONGSEC)
7345 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7346
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007347 trace_nfs4_secinfo(dir, name, err);
7348 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007349 &exception);
7350 } while (exception.retry);
7351 return err;
7352}
7353
Andy Adamson557134a2009-04-01 09:21:53 -04007354#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007355/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007356 * Check the exchange flags returned by the server for invalid flags, having
7357 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7358 * DS flags set.
7359 */
7360static int nfs4_check_cl_exchange_flags(u32 flags)
7361{
7362 if (flags & ~EXCHGID4_FLAG_MASK_R)
7363 goto out_inval;
7364 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7365 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7366 goto out_inval;
7367 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7368 goto out_inval;
7369 return NFS_OK;
7370out_inval:
7371 return -NFS4ERR_INVAL;
7372}
7373
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007374static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007375nfs41_same_server_scope(struct nfs41_server_scope *a,
7376 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007377{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007378 if (a->server_scope_sz != b->server_scope_sz)
7379 return false;
7380 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007381}
7382
Andy Adamson02a95de2016-02-05 16:08:37 -05007383static void
7384nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7385{
7386}
7387
7388static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7389 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7390};
7391
Andy Adamson357f54d2010-12-14 10:11:57 -05007392/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007393 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007394 *
7395 * The 4.1 client currently uses the same TCP connection for the
7396 * fore and backchannel.
7397 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007398static
7399int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7400 struct rpc_xprt *xprt,
7401 struct nfs_client *clp,
7402 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007403{
7404 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007405 struct nfs41_bind_conn_to_session_args args = {
7406 .client = clp,
7407 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7408 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007409 struct nfs41_bind_conn_to_session_res res;
7410 struct rpc_message msg = {
7411 .rpc_proc =
7412 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007413 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007414 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007415 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007416 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007417 struct rpc_task_setup task_setup_data = {
7418 .rpc_client = clnt,
7419 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007420 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007421 .rpc_message = &msg,
7422 .flags = RPC_TASK_TIMEOUT,
7423 };
7424 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007425
Trond Myklebust71a097c2015-02-18 09:27:18 -08007426 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7427 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7428 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007429
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007430 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7431 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7432 args.dir = NFS4_CDFC4_FORE;
7433
7434 task = rpc_run_task(&task_setup_data);
7435 if (!IS_ERR(task)) {
7436 status = task->tk_status;
7437 rpc_put_task(task);
7438 } else
7439 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007440 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007441 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007442 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007443 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7444 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007445 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007446 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007447 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007448 dprintk("NFS: %s: Unexpected direction from server\n",
7449 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007450 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007451 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007452 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007453 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7454 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007455 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007456 }
7457 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007458
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007459 return status;
7460}
7461
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007462struct rpc_bind_conn_calldata {
7463 struct nfs_client *clp;
7464 struct rpc_cred *cred;
7465};
7466
7467static int
7468nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7469 struct rpc_xprt *xprt,
7470 void *calldata)
7471{
7472 struct rpc_bind_conn_calldata *p = calldata;
7473
7474 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7475}
7476
7477int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7478{
7479 struct rpc_bind_conn_calldata data = {
7480 .clp = clp,
7481 .cred = cred,
7482 };
7483 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7484 nfs4_proc_bind_conn_to_session_callback, &data);
7485}
7486
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007487/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007488 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7489 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007490 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007491static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7492 .how = SP4_MACH_CRED,
7493 .enforce.u.words = {
7494 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7495 1 << (OP_EXCHANGE_ID - 32) |
7496 1 << (OP_CREATE_SESSION - 32) |
7497 1 << (OP_DESTROY_SESSION - 32) |
7498 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007499 },
7500 .allow.u.words = {
7501 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007502 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007503 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007504 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007505 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007506 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007507 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007508 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007509 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007510 1 << (OP_FREE_STATEID - 32) |
7511 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007512 }
7513};
7514
7515/*
7516 * Select the state protection mode for client `clp' given the server results
7517 * from exchange_id in `sp'.
7518 *
7519 * Returns 0 on success, negative errno otherwise.
7520 */
7521static int nfs4_sp4_select_mode(struct nfs_client *clp,
7522 struct nfs41_state_protection *sp)
7523{
7524 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7525 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7526 1 << (OP_EXCHANGE_ID - 32) |
7527 1 << (OP_CREATE_SESSION - 32) |
7528 1 << (OP_DESTROY_SESSION - 32) |
7529 1 << (OP_DESTROY_CLIENTID - 32)
7530 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007531 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007532 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007533 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007534
7535 if (sp->how == SP4_MACH_CRED) {
7536 /* Print state protect result */
7537 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7538 for (i = 0; i <= LAST_NFS4_OP; i++) {
7539 if (test_bit(i, sp->enforce.u.longs))
7540 dfprintk(MOUNT, " enforce op %d\n", i);
7541 if (test_bit(i, sp->allow.u.longs))
7542 dfprintk(MOUNT, " allow op %d\n", i);
7543 }
7544
7545 /* make sure nothing is on enforce list that isn't supported */
7546 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7547 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7548 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007549 ret = -EINVAL;
7550 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007551 }
7552 }
7553
7554 /*
7555 * Minimal mode - state operations are allowed to use machine
7556 * credential. Note this already happens by default, so the
7557 * client doesn't have to do anything more than the negotiation.
7558 *
7559 * NOTE: we don't care if EXCHANGE_ID is in the list -
7560 * we're already using the machine cred for exchange_id
7561 * and will never use a different cred.
7562 */
7563 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7564 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7565 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7566 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7567 dfprintk(MOUNT, "sp4_mach_cred:\n");
7568 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007569 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007570 } else {
7571 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007572 ret = -EINVAL;
7573 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007574 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007575
7576 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007577 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7578 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007579 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7580 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007581 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007582 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007583
Andrew Elble99ade3c2015-12-02 09:39:51 -05007584 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7585 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007586 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007587 }
7588
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007589 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7590 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7591 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007592 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007593 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007594
7595 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7596 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7597 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007598 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007599 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007600
7601 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7602 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007603 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007604 }
7605
7606 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7607 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007608 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007609 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007610 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007611out:
7612 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007613 return 0;
7614}
7615
Andy Adamson8d89bd72016-09-09 09:22:18 -04007616struct nfs41_exchange_id_data {
7617 struct nfs41_exchange_id_res res;
7618 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007619};
7620
Andy Adamson8d89bd72016-09-09 09:22:18 -04007621static void nfs4_exchange_id_release(void *data)
7622{
7623 struct nfs41_exchange_id_data *cdata =
7624 (struct nfs41_exchange_id_data *)data;
7625
Olga Kornievskaia63513232017-03-13 10:36:19 -04007626 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007627 kfree(cdata->res.impl_id);
7628 kfree(cdata->res.server_scope);
7629 kfree(cdata->res.server_owner);
7630 kfree(cdata);
7631}
7632
7633static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007634 .rpc_release = nfs4_exchange_id_release,
7635};
7636
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007637/*
7638 * _nfs4_proc_exchange_id()
7639 *
7640 * Wrapper for EXCHANGE_ID operation.
7641 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007642static struct rpc_task *
7643nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007644 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007645{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007646 struct rpc_message msg = {
7647 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007648 .rpc_cred = cred,
7649 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007650 struct rpc_task_setup task_setup_data = {
7651 .rpc_client = clp->cl_rpcclient,
7652 .callback_ops = &nfs4_exchange_id_call_ops,
7653 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007654 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007655 };
7656 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007657 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007658
Elena Reshetova212bf412017-10-20 12:53:38 +03007659 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007660 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007661
Trond Myklebust9c760d12017-07-31 18:38:50 -04007662 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007663 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007664 if (!calldata)
7665 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007666
Trond Myklebustfd405592017-08-01 16:02:47 -04007667 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007668
7669 status = nfs4_init_uniform_client_string(clp);
7670 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007671 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007672
Andy Adamson8d89bd72016-09-09 09:22:18 -04007673 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7674 GFP_NOFS);
7675 status = -ENOMEM;
7676 if (unlikely(calldata->res.server_owner == NULL))
7677 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007678
Andy Adamson8d89bd72016-09-09 09:22:18 -04007679 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007680 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007681 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007682 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007683
Andy Adamson8d89bd72016-09-09 09:22:18 -04007684 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7685 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007686 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007687
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007688 switch (sp4_how) {
7689 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007690 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007691 break;
7692
7693 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007694 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007695 break;
7696
7697 default:
7698 /* unsupported! */
7699 WARN_ON_ONCE(1);
7700 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007701 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007702 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007703 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007704 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007705 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007706 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7707 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007708 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007709 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007710 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7711 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007712#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007713 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007714#endif
7715 msg.rpc_argp = &calldata->args;
7716 msg.rpc_resp = &calldata->res;
7717 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007718
Trond Myklebust9c760d12017-07-31 18:38:50 -04007719 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007720
7721out_impl_id:
7722 kfree(calldata->res.impl_id);
7723out_server_scope:
7724 kfree(calldata->res.server_scope);
7725out_server_owner:
7726 kfree(calldata->res.server_owner);
7727out_calldata:
7728 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007729out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007730 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007731 return ERR_PTR(status);
7732}
7733
7734/*
7735 * _nfs4_proc_exchange_id()
7736 *
7737 * Wrapper for EXCHANGE_ID operation.
7738 */
7739static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7740 u32 sp4_how)
7741{
7742 struct rpc_task *task;
7743 struct nfs41_exchange_id_args *argp;
7744 struct nfs41_exchange_id_res *resp;
7745 int status;
7746
7747 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7748 if (IS_ERR(task))
7749 return PTR_ERR(task);
7750
7751 argp = task->tk_msg.rpc_argp;
7752 resp = task->tk_msg.rpc_resp;
7753 status = task->tk_status;
7754 if (status != 0)
7755 goto out;
7756
7757 status = nfs4_check_cl_exchange_flags(resp->flags);
7758 if (status != 0)
7759 goto out;
7760
7761 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7762 if (status != 0)
7763 goto out;
7764
7765 clp->cl_clientid = resp->clientid;
7766 clp->cl_exchange_flags = resp->flags;
7767 clp->cl_seqid = resp->seqid;
7768 /* Client ID is not confirmed */
7769 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7770 clear_bit(NFS4_SESSION_ESTABLISHED,
7771 &clp->cl_session->session_state);
7772
7773 if (clp->cl_serverscope != NULL &&
7774 !nfs41_same_server_scope(clp->cl_serverscope,
7775 resp->server_scope)) {
7776 dprintk("%s: server_scope mismatch detected\n",
7777 __func__);
7778 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7779 }
7780
7781 swap(clp->cl_serverowner, resp->server_owner);
7782 swap(clp->cl_serverscope, resp->server_scope);
7783 swap(clp->cl_implid, resp->impl_id);
7784
7785 /* Save the EXCHANGE_ID verifier session trunk tests */
7786 memcpy(clp->cl_confirm.data, argp->verifier.data,
7787 sizeof(clp->cl_confirm.data));
7788out:
7789 trace_nfs4_exchange_id(clp, status);
7790 rpc_put_task(task);
7791 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007792}
7793
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007794/*
7795 * nfs4_proc_exchange_id()
7796 *
7797 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7798 *
7799 * Since the clientid has expired, all compounds using sessions
7800 * associated with the stale clientid will be returning
7801 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7802 * be in some phase of session reset.
7803 *
7804 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7805 */
7806int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7807{
7808 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7809 int status;
7810
7811 /* try SP4_MACH_CRED if krb5i/p */
7812 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7813 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007814 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007815 if (!status)
7816 return 0;
7817 }
7818
7819 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007820 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007821}
7822
Andy Adamson04fa2c62016-09-09 09:22:29 -04007823/**
7824 * nfs4_test_session_trunk
7825 *
7826 * This is an add_xprt_test() test function called from
7827 * rpc_clnt_setup_test_and_add_xprt.
7828 *
7829 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7830 * and is dereferrenced in nfs4_exchange_id_release
7831 *
7832 * Upon success, add the new transport to the rpc_clnt
7833 *
7834 * @clnt: struct rpc_clnt to get new transport
7835 * @xprt: the rpc_xprt to test
7836 * @data: call data for _nfs4_proc_exchange_id.
7837 */
7838int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7839 void *data)
7840{
7841 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007842 struct rpc_task *task;
7843 int status;
7844
Andy Adamson04fa2c62016-09-09 09:22:29 -04007845 u32 sp4_how;
7846
7847 dprintk("--> %s try %s\n", __func__,
7848 xprt->address_strings[RPC_DISPLAY_ADDR]);
7849
7850 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7851
7852 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007853 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7854 if (IS_ERR(task))
7855 return PTR_ERR(task);
7856
7857 status = task->tk_status;
7858 if (status == 0)
7859 status = nfs4_detect_session_trunking(adata->clp,
7860 task->tk_msg.rpc_resp, xprt);
7861
7862 rpc_put_task(task);
7863 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007864}
7865EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7866
Trond Myklebust66245532012-05-25 17:18:09 -04007867static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7868 struct rpc_cred *cred)
7869{
7870 struct rpc_message msg = {
7871 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7872 .rpc_argp = clp,
7873 .rpc_cred = cred,
7874 };
7875 int status;
7876
7877 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007878 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007879 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007880 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007881 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7882 return status;
7883}
7884
7885static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7886 struct rpc_cred *cred)
7887{
7888 unsigned int loop;
7889 int ret;
7890
7891 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7892 ret = _nfs4_proc_destroy_clientid(clp, cred);
7893 switch (ret) {
7894 case -NFS4ERR_DELAY:
7895 case -NFS4ERR_CLIENTID_BUSY:
7896 ssleep(1);
7897 break;
7898 default:
7899 return ret;
7900 }
7901 }
7902 return 0;
7903}
7904
7905int nfs4_destroy_clientid(struct nfs_client *clp)
7906{
7907 struct rpc_cred *cred;
7908 int ret = 0;
7909
7910 if (clp->cl_mvops->minor_version < 1)
7911 goto out;
7912 if (clp->cl_exchange_flags == 0)
7913 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007914 if (clp->cl_preserve_clid)
7915 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007916 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007917 ret = nfs4_proc_destroy_clientid(clp, cred);
7918 if (cred)
7919 put_rpccred(cred);
7920 switch (ret) {
7921 case 0:
7922 case -NFS4ERR_STALE_CLIENTID:
7923 clp->cl_exchange_flags = 0;
7924 }
7925out:
7926 return ret;
7927}
7928
Andy Adamson2050f0c2009-04-01 09:22:30 -04007929struct nfs4_get_lease_time_data {
7930 struct nfs4_get_lease_time_args *args;
7931 struct nfs4_get_lease_time_res *res;
7932 struct nfs_client *clp;
7933};
7934
7935static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7936 void *calldata)
7937{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007938 struct nfs4_get_lease_time_data *data =
7939 (struct nfs4_get_lease_time_data *)calldata;
7940
7941 dprintk("--> %s\n", __func__);
7942 /* just setup sequence, do not trigger session recovery
7943 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007944 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007945 &data->args->la_seq_args,
7946 &data->res->lr_seq_res,
7947 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007948 dprintk("<-- %s\n", __func__);
7949}
7950
7951/*
7952 * Called from nfs4_state_manager thread for session setup, so don't recover
7953 * from sequence operation or clientid errors.
7954 */
7955static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7956{
7957 struct nfs4_get_lease_time_data *data =
7958 (struct nfs4_get_lease_time_data *)calldata;
7959
7960 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007961 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7962 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007963 switch (task->tk_status) {
7964 case -NFS4ERR_DELAY:
7965 case -NFS4ERR_GRACE:
7966 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
7967 rpc_delay(task, NFS4_POLL_RETRY_MIN);
7968 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04007969 /* fall through */
7970 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07007971 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007972 return;
7973 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04007974 dprintk("<-- %s\n", __func__);
7975}
7976
Trond Myklebust17280172012-03-11 13:11:00 -04007977static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04007978 .rpc_call_prepare = nfs4_get_lease_time_prepare,
7979 .rpc_call_done = nfs4_get_lease_time_done,
7980};
7981
7982int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
7983{
7984 struct rpc_task *task;
7985 struct nfs4_get_lease_time_args args;
7986 struct nfs4_get_lease_time_res res = {
7987 .lr_fsinfo = fsinfo,
7988 };
7989 struct nfs4_get_lease_time_data data = {
7990 .args = &args,
7991 .res = &res,
7992 .clp = clp,
7993 };
7994 struct rpc_message msg = {
7995 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
7996 .rpc_argp = &args,
7997 .rpc_resp = &res,
7998 };
7999 struct rpc_task_setup task_setup = {
8000 .rpc_client = clp->cl_rpcclient,
8001 .rpc_message = &msg,
8002 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008003 .callback_data = &data,
8004 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008005 };
8006 int status;
8007
Chuck Levera9c92d62013-08-09 12:48:18 -04008008 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008009 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008010 task = rpc_run_task(&task_setup);
8011
8012 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008013 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008014
Anna Schumakerf6148712017-04-07 14:15:23 -04008015 status = task->tk_status;
8016 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008017 return status;
8018}
8019
Andy Adamsonfc931582009-04-01 09:22:31 -04008020/*
8021 * Initialize the values to be used by the client in CREATE_SESSION
8022 * If nfs4_init_session set the fore channel request and response sizes,
8023 * use them.
8024 *
8025 * Set the back channel max_resp_sz_cached to zero to force the client to
8026 * always set csa_cachethis to FALSE because the current implementation
8027 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8028 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008029static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8030 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008031{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008032 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008033 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008034
Andy Adamson18aad3d2013-06-26 12:21:49 -04008035 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8036 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8037
Andy Adamsonfc931582009-04-01 09:22:31 -04008038 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008039 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8040 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008041 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008042 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008043
8044 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008045 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008046 __func__,
8047 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008048 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008049
8050 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008051 args->bc_attrs.max_rqst_sz = max_bc_payload;
8052 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008053 args->bc_attrs.max_resp_sz_cached = 0;
8054 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008055 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008056
8057 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8058 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8059 __func__,
8060 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8061 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8062 args->bc_attrs.max_reqs);
8063}
8064
Trond Myklebust79969dd12015-02-18 11:30:18 -08008065static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8066 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008067{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008068 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008069 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008070
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008071 if (rcvd->max_resp_sz > sent->max_resp_sz)
8072 return -EINVAL;
8073 /*
8074 * Our requested max_ops is the minimum we need; we're not
8075 * prepared to break up compounds into smaller pieces than that.
8076 * So, no point even trying to continue if the server won't
8077 * cooperate:
8078 */
8079 if (rcvd->max_ops < sent->max_ops)
8080 return -EINVAL;
8081 if (rcvd->max_reqs == 0)
8082 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008083 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8084 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008085 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008086}
8087
Trond Myklebust79969dd12015-02-18 11:30:18 -08008088static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8089 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008090{
8091 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008092 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008093
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008094 if (!(res->flags & SESSION4_BACK_CHAN))
8095 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008096 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8097 return -EINVAL;
8098 if (rcvd->max_resp_sz < sent->max_resp_sz)
8099 return -EINVAL;
8100 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8101 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008102 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008103 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008104 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008105 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008106out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008107 return 0;
8108}
Andy Adamson8d353012009-04-01 09:22:32 -04008109
Andy Adamson8d353012009-04-01 09:22:32 -04008110static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008111 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008112{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008113 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008114
Trond Myklebust79969dd12015-02-18 11:30:18 -08008115 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008116 if (ret)
8117 return ret;
Trond Myklebust79969dd12015-02-18 11:30:18 -08008118 return nfs4_verify_back_channel_attrs(args, res);
8119}
8120
8121static void nfs4_update_session(struct nfs4_session *session,
8122 struct nfs41_create_session_res *res)
8123{
8124 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008125 /* Mark client id and session as being confirmed */
8126 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8127 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd12015-02-18 11:30:18 -08008128 session->flags = res->flags;
8129 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008130 if (res->flags & SESSION4_BACK_CHAN)
8131 memcpy(&session->bc_attrs, &res->bc_attrs,
8132 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008133}
8134
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008135static int _nfs4_proc_create_session(struct nfs_client *clp,
8136 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008137{
8138 struct nfs4_session *session = clp->cl_session;
8139 struct nfs41_create_session_args args = {
8140 .client = clp,
Trond Myklebust79969dd12015-02-18 11:30:18 -08008141 .clientid = clp->cl_clientid,
8142 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008143 .cb_program = NFS4_CALLBACK,
8144 };
Trond Myklebust79969dd12015-02-18 11:30:18 -08008145 struct nfs41_create_session_res res;
8146
Andy Adamsonfc931582009-04-01 09:22:31 -04008147 struct rpc_message msg = {
8148 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8149 .rpc_argp = &args,
8150 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008151 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008152 };
8153 int status;
8154
Chuck Lever6b26cc82016-05-02 14:40:40 -04008155 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008156 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008157
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008158 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008159 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008160
Trond Myklebustb519d402016-09-11 14:50:01 -04008161 switch (status) {
8162 case -NFS4ERR_STALE_CLIENTID:
8163 case -NFS4ERR_DELAY:
8164 case -ETIMEDOUT:
8165 case -EACCES:
8166 case -EAGAIN:
8167 goto out;
8168 };
8169
8170 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008171 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008172 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008173 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008174 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd12015-02-18 11:30:18 -08008175 if (status)
8176 goto out;
8177 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008178 }
Trond Myklebust79969dd12015-02-18 11:30:18 -08008179out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008180 return status;
8181}
8182
8183/*
8184 * Issues a CREATE_SESSION operation to the server.
8185 * It is the responsibility of the caller to verify the session is
8186 * expired before calling this routine.
8187 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008188int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008189{
8190 int status;
8191 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008192 struct nfs4_session *session = clp->cl_session;
8193
8194 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8195
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008196 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008197 if (status)
8198 goto out;
8199
Andy Adamsonaacd5532011-11-09 13:58:21 -05008200 /* Init or reset the session slot tables */
8201 status = nfs4_setup_session_slot_tables(session);
8202 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008203 if (status)
8204 goto out;
8205
8206 ptr = (unsigned *)&session->sess_id.data[0];
8207 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8208 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008209out:
8210 dprintk("<-- %s\n", __func__);
8211 return status;
8212}
8213
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008214/*
8215 * Issue the over-the-wire RPC DESTROY_SESSION.
8216 * The caller must serialize access to this routine.
8217 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008218int nfs4_proc_destroy_session(struct nfs4_session *session,
8219 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008220{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008221 struct rpc_message msg = {
8222 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8223 .rpc_argp = session,
8224 .rpc_cred = cred,
8225 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008226 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008227
8228 dprintk("--> nfs4_proc_destroy_session\n");
8229
8230 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008231 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8232 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008233
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008234 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008235 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008236
8237 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008238 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008239 "Session has been destroyed regardless...\n", status);
8240
8241 dprintk("<-- nfs4_proc_destroy_session\n");
8242 return status;
8243}
8244
Trond Myklebust7b38c362012-05-23 13:23:31 -04008245/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008246 * Renew the cl_session lease.
8247 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008248struct nfs4_sequence_data {
8249 struct nfs_client *clp;
8250 struct nfs4_sequence_args args;
8251 struct nfs4_sequence_res res;
8252};
8253
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008254static void nfs41_sequence_release(void *data)
8255{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008256 struct nfs4_sequence_data *calldata = data;
8257 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008258
Elena Reshetova212bf412017-10-20 12:53:38 +03008259 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008260 nfs4_schedule_state_renewal(clp);
8261 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008262 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008263}
8264
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008265static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8266{
8267 switch(task->tk_status) {
8268 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008269 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8270 return -EAGAIN;
8271 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008272 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008273 }
8274 return 0;
8275}
8276
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008277static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008278{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008279 struct nfs4_sequence_data *calldata = data;
8280 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008281
Trond Myklebust14516c32010-07-31 14:29:06 -04008282 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8283 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008284
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008285 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008286 if (task->tk_status < 0) {
8287 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008288 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008289 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008290
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008291 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8292 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008293 return;
8294 }
8295 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008296 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008297out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008298 dprintk("<-- %s\n", __func__);
8299}
8300
8301static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8302{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008303 struct nfs4_sequence_data *calldata = data;
8304 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008305 struct nfs4_sequence_args *args;
8306 struct nfs4_sequence_res *res;
8307
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008308 args = task->tk_msg.rpc_argp;
8309 res = task->tk_msg.rpc_resp;
8310
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008311 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008312}
8313
8314static const struct rpc_call_ops nfs41_sequence_ops = {
8315 .rpc_call_done = nfs41_sequence_call_done,
8316 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008317 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008318};
8319
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008320static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8321 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008322 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008323 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008324{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008325 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008326 struct rpc_message msg = {
8327 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8328 .rpc_cred = cred,
8329 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008330 struct rpc_task_setup task_setup_data = {
8331 .rpc_client = clp->cl_rpcclient,
8332 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008333 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008334 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008335 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008336 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008337
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008338 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008339 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008340 goto out_err;
8341
8342 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008343 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008344 if (calldata == NULL)
8345 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008346 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008347 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008348 if (is_privileged)
8349 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008350 msg.rpc_argp = &calldata->args;
8351 msg.rpc_resp = &calldata->res;
8352 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008353 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008354
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008355 ret = rpc_run_task(&task_setup_data);
8356 if (IS_ERR(ret))
8357 goto out_err;
8358 return ret;
8359out_put_clp:
8360 nfs_put_client(clp);
8361out_err:
8362 nfs41_release_slot(slot);
8363 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008364}
8365
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008366static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008367{
8368 struct rpc_task *task;
8369 int ret = 0;
8370
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008371 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008372 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008373 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008374 if (IS_ERR(task))
8375 ret = PTR_ERR(task);
8376 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008377 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008378 dprintk("<-- %s status=%d\n", __func__, ret);
8379 return ret;
8380}
8381
8382static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8383{
8384 struct rpc_task *task;
8385 int ret;
8386
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008387 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008388 if (IS_ERR(task)) {
8389 ret = PTR_ERR(task);
8390 goto out;
8391 }
8392 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008393 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008394 ret = task->tk_status;
8395 rpc_put_task(task);
8396out:
8397 dprintk("<-- %s status=%d\n", __func__, ret);
8398 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008399}
8400
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008401struct nfs4_reclaim_complete_data {
8402 struct nfs_client *clp;
8403 struct nfs41_reclaim_complete_args arg;
8404 struct nfs41_reclaim_complete_res res;
8405};
8406
8407static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8408{
8409 struct nfs4_reclaim_complete_data *calldata = data;
8410
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008411 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008412 &calldata->arg.seq_args,
8413 &calldata->res.seq_res,
8414 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008415}
8416
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008417static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8418{
8419 switch(task->tk_status) {
8420 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008421 wake_up_all(&clp->cl_lock_waitq);
8422 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008423 case -NFS4ERR_COMPLETE_ALREADY:
8424 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8425 break;
8426 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008427 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008428 /* fall through */
8429 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008430 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008431 case -NFS4ERR_BADSESSION:
8432 case -NFS4ERR_DEADSESSION:
8433 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8434 nfs4_schedule_session_recovery(clp->cl_session,
8435 task->tk_status);
8436 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008437 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008438 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008439 }
8440 return 0;
8441}
8442
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008443static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8444{
8445 struct nfs4_reclaim_complete_data *calldata = data;
8446 struct nfs_client *clp = calldata->clp;
8447 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8448
8449 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008450 if (!nfs41_sequence_done(task, res))
8451 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008452
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008453 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008454 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8455 rpc_restart_call_prepare(task);
8456 return;
8457 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008458 dprintk("<-- %s\n", __func__);
8459}
8460
8461static void nfs4_free_reclaim_complete_data(void *data)
8462{
8463 struct nfs4_reclaim_complete_data *calldata = data;
8464
8465 kfree(calldata);
8466}
8467
8468static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8469 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8470 .rpc_call_done = nfs4_reclaim_complete_done,
8471 .rpc_release = nfs4_free_reclaim_complete_data,
8472};
8473
8474/*
8475 * Issue a global reclaim complete.
8476 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008477static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8478 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008479{
8480 struct nfs4_reclaim_complete_data *calldata;
8481 struct rpc_task *task;
8482 struct rpc_message msg = {
8483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008484 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008485 };
8486 struct rpc_task_setup task_setup_data = {
8487 .rpc_client = clp->cl_rpcclient,
8488 .rpc_message = &msg,
8489 .callback_ops = &nfs4_reclaim_complete_call_ops,
8490 .flags = RPC_TASK_ASYNC,
8491 };
8492 int status = -ENOMEM;
8493
8494 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008495 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008496 if (calldata == NULL)
8497 goto out;
8498 calldata->clp = clp;
8499 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008500
Chuck Levera9c92d62013-08-09 12:48:18 -04008501 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008502 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008503 msg.rpc_argp = &calldata->arg;
8504 msg.rpc_resp = &calldata->res;
8505 task_setup_data.callback_data = calldata;
8506 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008507 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008508 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008509 goto out;
8510 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008511 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008512 if (status == 0)
8513 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008514 rpc_put_task(task);
8515out:
8516 dprintk("<-- %s status=%d\n", __func__, status);
8517 return status;
8518}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008519
8520static void
8521nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8522{
8523 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008524 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008525
8526 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008527 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008528 &lgp->res.seq_res, task);
8529 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008530}
8531
8532static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8533{
8534 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008535
8536 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008537 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008538 dprintk("<-- %s\n", __func__);
8539}
8540
8541static int
8542nfs4_layoutget_handle_exception(struct rpc_task *task,
8543 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8544{
Trond Myklebustee314c22012-10-01 17:25:48 -07008545 struct inode *inode = lgp->args.inode;
8546 struct nfs_server *server = NFS_SERVER(inode);
8547 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008548 int nfs4err = task->tk_status;
8549 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008550 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008551
Boaz Harroshed7e5422014-01-22 20:34:54 +02008552 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008553
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008554 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008555 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008556 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008557
8558 /*
8559 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8560 * on the file. set tk_status to -ENODATA to tell upper layer to
8561 * retry go inband.
8562 */
8563 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008564 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008565 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008566 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008567 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8568 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8569 */
8570 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008571 status = -EOVERFLOW;
8572 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008573 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008574 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008575 * (or clients) writing to the same RAID stripe except when
8576 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008577 *
8578 * Treat it like we would RECALLCONFLICT -- we retry for a little
8579 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008580 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008581 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008582 if (lgp->args.minlength == 0) {
8583 status = -EOVERFLOW;
8584 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008585 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008586 status = -EBUSY;
8587 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008588 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008589 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008590 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008591 case -NFS4ERR_DELEG_REVOKED:
8592 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008593 case -NFS4ERR_EXPIRED:
8594 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008595 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008596 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008597 lo = NFS_I(inode)->layout;
8598 /* If the open stateid was bad, then recover it. */
8599 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008600 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008601 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008602 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008603 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008604 break;
8605 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008606
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008607 /*
8608 * Mark the bad layout state as invalid, then retry
8609 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008610 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008611 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008612 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008613 pnfs_free_lseg_list(&head);
8614 status = -EAGAIN;
8615 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008616 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008617
Trond Myklebust8ac09252017-01-23 22:44:12 -05008618 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008619 err = nfs4_handle_exception(server, nfs4err, exception);
8620 if (!status) {
8621 if (exception->retry)
8622 status = -EAGAIN;
8623 else
8624 status = err;
8625 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008626out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008627 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008628 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008629}
8630
Idan Kedar85541162012-08-02 11:47:10 +03008631static size_t max_response_pages(struct nfs_server *server)
8632{
8633 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8634 return nfs_page_array_len(0, max_resp_sz);
8635}
8636
8637static void nfs4_free_pages(struct page **pages, size_t size)
8638{
8639 int i;
8640
8641 if (!pages)
8642 return;
8643
8644 for (i = 0; i < size; i++) {
8645 if (!pages[i])
8646 break;
8647 __free_page(pages[i]);
8648 }
8649 kfree(pages);
8650}
8651
8652static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8653{
8654 struct page **pages;
8655 int i;
8656
8657 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8658 if (!pages) {
8659 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8660 return NULL;
8661 }
8662
8663 for (i = 0; i < size; i++) {
8664 pages[i] = alloc_page(gfp_flags);
8665 if (!pages[i]) {
8666 dprintk("%s: failed to allocate page\n", __func__);
8667 nfs4_free_pages(pages, size);
8668 return NULL;
8669 }
8670 }
8671
8672 return pages;
8673}
8674
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008675static void nfs4_layoutget_release(void *calldata)
8676{
8677 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008678 struct inode *inode = lgp->args.inode;
8679 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008680 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008681
8682 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008683 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008684 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008685 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008686 put_nfs_open_context(lgp->args.ctx);
8687 kfree(calldata);
8688 dprintk("<-- %s\n", __func__);
8689}
8690
8691static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8692 .rpc_call_prepare = nfs4_layoutget_prepare,
8693 .rpc_call_done = nfs4_layoutget_done,
8694 .rpc_release = nfs4_layoutget_release,
8695};
8696
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008697struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008698nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008699{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008700 struct inode *inode = lgp->args.inode;
8701 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008702 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008703 struct rpc_task *task;
8704 struct rpc_message msg = {
8705 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8706 .rpc_argp = &lgp->args,
8707 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008708 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008709 };
8710 struct rpc_task_setup task_setup_data = {
8711 .rpc_client = server->client,
8712 .rpc_message = &msg,
8713 .callback_ops = &nfs4_layoutget_call_ops,
8714 .callback_data = lgp,
8715 .flags = RPC_TASK_ASYNC,
8716 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008717 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008718 struct nfs4_exception exception = {
8719 .inode = inode,
8720 .timeout = *timeout,
8721 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008722 int status = 0;
8723
8724 dprintk("--> %s\n", __func__);
8725
Peng Tao4bd5a982014-11-17 11:05:17 +08008726 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8727 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8728
Idan Kedar85541162012-08-02 11:47:10 +03008729 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8730 if (!lgp->args.layout.pages) {
8731 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008732 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008733 }
8734 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8735
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008736 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008737 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008738 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008739
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008740 task = rpc_run_task(&task_setup_data);
8741 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008742 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008743 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008744 if (status == 0) {
8745 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8746 *timeout = exception.timeout;
8747 }
8748
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008749 trace_nfs4_layoutget(lgp->args.ctx,
8750 &lgp->args.range,
8751 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008752 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008753 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008754
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008755 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8756 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008757 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008758 rpc_put_task(task);
8759 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008760 if (status)
8761 return ERR_PTR(status);
8762 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008763}
8764
Benny Halevycbe82602011-05-22 19:52:37 +03008765static void
8766nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8767{
8768 struct nfs4_layoutreturn *lrp = calldata;
8769
8770 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008771 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008772 &lrp->args.seq_args,
8773 &lrp->res.seq_res,
8774 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008775}
8776
8777static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8778{
8779 struct nfs4_layoutreturn *lrp = calldata;
8780 struct nfs_server *server;
8781
8782 dprintk("--> %s\n", __func__);
8783
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008784 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008785 return;
8786
8787 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008788 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008789 case -NFS4ERR_OLD_STATEID:
8790 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8791 lrp->args.inode))
8792 goto out_restart;
8793 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008794 default:
8795 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008796 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008797 case 0:
8798 break;
8799 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008800 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008801 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008802 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008803 }
Benny Halevycbe82602011-05-22 19:52:37 +03008804 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008805 return;
8806out_restart:
8807 task->tk_status = 0;
8808 nfs4_sequence_free_slot(&lrp->res.seq_res);
8809 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008810}
8811
8812static void nfs4_layoutreturn_release(void *calldata)
8813{
8814 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008815 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008816
8817 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008818 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008819 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008820 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008821 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8822 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008823 pnfs_put_layout_hdr(lrp->args.layout);
8824 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008825 kfree(calldata);
8826 dprintk("<-- %s\n", __func__);
8827}
8828
8829static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8830 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8831 .rpc_call_done = nfs4_layoutreturn_done,
8832 .rpc_release = nfs4_layoutreturn_release,
8833};
8834
Peng Tao6c166052014-11-17 09:30:40 +08008835int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008836{
8837 struct rpc_task *task;
8838 struct rpc_message msg = {
8839 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8840 .rpc_argp = &lrp->args,
8841 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008842 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008843 };
8844 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c5772013-07-22 12:42:05 -04008845 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008846 .rpc_message = &msg,
8847 .callback_ops = &nfs4_layoutreturn_call_ops,
8848 .callback_data = lrp,
8849 };
Peng Tao6c166052014-11-17 09:30:40 +08008850 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008851
Andrew Elble99ade3c2015-12-02 09:39:51 -05008852 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8853 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8854 &task_setup_data.rpc_client, &msg);
8855
Benny Halevycbe82602011-05-22 19:52:37 +03008856 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008857 if (!sync) {
8858 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8859 if (!lrp->inode) {
8860 nfs4_layoutreturn_release(lrp);
8861 return -EAGAIN;
8862 }
8863 task_setup_data.flags |= RPC_TASK_ASYNC;
8864 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008865 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008866 task = rpc_run_task(&task_setup_data);
8867 if (IS_ERR(task))
8868 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008869 if (sync)
8870 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008871 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008872 dprintk("<-- %s status=%d\n", __func__, status);
8873 rpc_put_task(task);
8874 return status;
8875}
8876
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008877static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008878_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8879 struct pnfs_device *pdev,
8880 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008881{
8882 struct nfs4_getdeviceinfo_args args = {
8883 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008884 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8885 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008886 };
8887 struct nfs4_getdeviceinfo_res res = {
8888 .pdev = pdev,
8889 };
8890 struct rpc_message msg = {
8891 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8892 .rpc_argp = &args,
8893 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008894 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008895 };
8896 int status;
8897
8898 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008899 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008900 if (res.notification & ~args.notify_types)
8901 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008902 if (res.notification != args.notify_types)
8903 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008904
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008905 dprintk("<-- %s status=%d\n", __func__, status);
8906
8907 return status;
8908}
8909
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008910int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8911 struct pnfs_device *pdev,
8912 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008913{
8914 struct nfs4_exception exception = { };
8915 int err;
8916
8917 do {
8918 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008919 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008920 &exception);
8921 } while (exception.retry);
8922 return err;
8923}
8924EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8925
Andy Adamson863a3c62011-03-23 13:27:54 +00008926static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8927{
8928 struct nfs4_layoutcommit_data *data = calldata;
8929 struct nfs_server *server = NFS_SERVER(data->args.inode);
8930
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008931 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008932 &data->args.seq_args,
8933 &data->res.seq_res,
8934 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008935}
8936
8937static void
8938nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8939{
8940 struct nfs4_layoutcommit_data *data = calldata;
8941 struct nfs_server *server = NFS_SERVER(data->args.inode);
8942
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008943 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008944 return;
8945
8946 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008947 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8948 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8949 case -NFS4ERR_BADLAYOUT: /* no layout */
8950 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008951 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008952 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008953 break;
8954 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008955 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008956 rpc_restart_call_prepare(task);
8957 return;
8958 }
8959 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008960}
8961
8962static void nfs4_layoutcommit_release(void *calldata)
8963{
8964 struct nfs4_layoutcommit_data *data = calldata;
8965
Andy Adamsondb29c082011-07-30 20:52:38 -04008966 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008967 nfs_post_op_update_inode_force_wcc(data->args.inode,
8968 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008969 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008970 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008971 kfree(data);
8972}
8973
8974static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8975 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8976 .rpc_call_done = nfs4_layoutcommit_done,
8977 .rpc_release = nfs4_layoutcommit_release,
8978};
8979
8980int
Andy Adamsonef311532011-03-12 02:58:10 -05008981nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008982{
8983 struct rpc_message msg = {
8984 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8985 .rpc_argp = &data->args,
8986 .rpc_resp = &data->res,
8987 .rpc_cred = data->cred,
8988 };
8989 struct rpc_task_setup task_setup_data = {
8990 .task = &data->task,
8991 .rpc_client = NFS_CLIENT(data->args.inode),
8992 .rpc_message = &msg,
8993 .callback_ops = &nfs4_layoutcommit_ops,
8994 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008995 };
8996 struct rpc_task *task;
8997 int status = 0;
8998
Kinglong Meeb4839eb2015-07-01 12:00:13 +08008999 dprintk("NFS: initiating layoutcommit call. sync %d "
9000 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009001 data->args.lastbytewritten,
9002 data->args.inode->i_ino);
9003
Trond Myklebust472e2592015-02-05 16:50:30 -05009004 if (!sync) {
9005 data->inode = nfs_igrab_and_active(data->args.inode);
9006 if (data->inode == NULL) {
9007 nfs4_layoutcommit_release(data);
9008 return -EAGAIN;
9009 }
9010 task_setup_data.flags = RPC_TASK_ASYNC;
9011 }
Chuck Levera9c92d62013-08-09 12:48:18 -04009012 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00009013 task = rpc_run_task(&task_setup_data);
9014 if (IS_ERR(task))
9015 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009016 if (sync)
9017 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009018 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009019 dprintk("%s: status %d\n", __func__, status);
9020 rpc_put_task(task);
9021 return status;
9022}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009023
Andy Adamson97431202013-08-08 10:57:56 -04009024/**
9025 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9026 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9027 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009028static int
9029_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009030 struct nfs_fsinfo *info,
9031 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009032{
9033 struct nfs41_secinfo_no_name_args args = {
9034 .style = SECINFO_STYLE_CURRENT_FH,
9035 };
9036 struct nfs4_secinfo_res res = {
9037 .flavors = flavors,
9038 };
9039 struct rpc_message msg = {
9040 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9041 .rpc_argp = &args,
9042 .rpc_resp = &res,
9043 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009044 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009045 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009046 int status;
9047
9048 if (use_integrity) {
9049 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009050 cred = nfs4_get_clid_cred(server->nfs_client);
9051 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009052 }
9053
9054 dprintk("--> %s\n", __func__);
9055 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9056 &res.seq_res, 0);
9057 dprintk("<-- %s status=%d\n", __func__, status);
9058
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009059 if (cred)
9060 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009061
9062 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009063}
9064
9065static int
9066nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9067 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9068{
9069 struct nfs4_exception exception = { };
9070 int err;
9071 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009072 /* first try using integrity protection */
9073 err = -NFS4ERR_WRONGSEC;
9074
9075 /* try to use integrity protection with machine cred */
9076 if (_nfs4_is_integrity_protected(server->nfs_client))
9077 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9078 flavors, true);
9079
9080 /*
9081 * if unable to use integrity protection, or SECINFO with
9082 * integrity protection returns NFS4ERR_WRONGSEC (which is
9083 * disallowed by spec, but exists in deployed servers) use
9084 * the current filesystem's rpc_client and the user cred.
9085 */
9086 if (err == -NFS4ERR_WRONGSEC)
9087 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9088 flavors, false);
9089
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009090 switch (err) {
9091 case 0:
9092 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009093 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009094 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009095 default:
9096 err = nfs4_handle_exception(server, err, &exception);
9097 }
9098 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009099out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009100 return err;
9101}
9102
9103static int
9104nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9105 struct nfs_fsinfo *info)
9106{
9107 int err;
9108 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009109 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009110 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009111 struct nfs4_secinfo4 *secinfo;
9112 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009113
9114 page = alloc_page(GFP_KERNEL);
9115 if (!page) {
9116 err = -ENOMEM;
9117 goto out;
9118 }
9119
9120 flavors = page_address(page);
9121 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9122
9123 /*
9124 * Fall back on "guess and check" method if
9125 * the server doesn't support SECINFO_NO_NAME
9126 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009127 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009128 err = nfs4_find_root_sec(server, fhandle, info);
9129 goto out_freepage;
9130 }
9131 if (err)
9132 goto out_freepage;
9133
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009134 for (i = 0; i < flavors->num_flavors; i++) {
9135 secinfo = &flavors->flavors[i];
9136
9137 switch (secinfo->flavor) {
9138 case RPC_AUTH_NULL:
9139 case RPC_AUTH_UNIX:
9140 case RPC_AUTH_GSS:
9141 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9142 &secinfo->flavor_info);
9143 break;
9144 default:
9145 flavor = RPC_AUTH_MAXFLAVOR;
9146 break;
9147 }
9148
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009149 if (!nfs_auth_info_match(&server->auth_info, flavor))
9150 flavor = RPC_AUTH_MAXFLAVOR;
9151
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009152 if (flavor != RPC_AUTH_MAXFLAVOR) {
9153 err = nfs4_lookup_root_sec(server, fhandle,
9154 info, flavor);
9155 if (!err)
9156 break;
9157 }
9158 }
9159
9160 if (flavor == RPC_AUTH_MAXFLAVOR)
9161 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009162
9163out_freepage:
9164 put_page(page);
9165 if (err == -EACCES)
9166 return -EPERM;
9167out:
9168 return err;
9169}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009170
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009171static int _nfs41_test_stateid(struct nfs_server *server,
9172 nfs4_stateid *stateid,
9173 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009174{
9175 int status;
9176 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009177 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009178 };
9179 struct nfs41_test_stateid_res res;
9180 struct rpc_message msg = {
9181 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9182 .rpc_argp = &args,
9183 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009184 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009185 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009186 struct rpc_clnt *rpc_client = server->client;
9187
9188 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9189 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009190
Chuck Lever38527b12012-07-11 16:30:23 -04009191 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009192 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009193 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009194 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009195 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009196 if (status != NFS_OK) {
9197 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009198 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009199 }
9200 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009201 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009202}
9203
Trond Myklebust43912bb2016-09-22 13:38:56 -04009204static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9205 int err, struct nfs4_exception *exception)
9206{
9207 exception->retry = 0;
9208 switch(err) {
9209 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009210 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009211 nfs4_handle_exception(server, err, exception);
9212 break;
9213 case -NFS4ERR_BADSESSION:
9214 case -NFS4ERR_BADSLOT:
9215 case -NFS4ERR_BAD_HIGH_SLOT:
9216 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9217 case -NFS4ERR_DEADSESSION:
9218 nfs4_do_handle_exception(server, err, exception);
9219 }
9220}
9221
Chuck Lever38527b12012-07-11 16:30:23 -04009222/**
9223 * nfs41_test_stateid - perform a TEST_STATEID operation
9224 *
9225 * @server: server / transport on which to perform the operation
9226 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009227 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009228 *
9229 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9230 * Otherwise a negative NFS4ERR value is returned if the operation
9231 * failed or the state ID is not currently valid.
9232 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009233static int nfs41_test_stateid(struct nfs_server *server,
9234 nfs4_stateid *stateid,
9235 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009236{
9237 struct nfs4_exception exception = { };
9238 int err;
9239 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009240 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009241 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009242 } while (exception.retry);
9243 return err;
9244}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009245
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009246struct nfs_free_stateid_data {
9247 struct nfs_server *server;
9248 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009249 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009250};
9251
9252static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9253{
9254 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009255 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009256 &data->args.seq_args,
9257 &data->res.seq_res,
9258 task);
9259}
9260
9261static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9262{
9263 struct nfs_free_stateid_data *data = calldata;
9264
9265 nfs41_sequence_done(task, &data->res.seq_res);
9266
9267 switch (task->tk_status) {
9268 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009269 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009270 rpc_restart_call_prepare(task);
9271 }
9272}
9273
9274static void nfs41_free_stateid_release(void *calldata)
9275{
9276 kfree(calldata);
9277}
9278
Trond Myklebust17f26b12013-08-21 15:48:42 -04009279static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009280 .rpc_call_prepare = nfs41_free_stateid_prepare,
9281 .rpc_call_done = nfs41_free_stateid_done,
9282 .rpc_release = nfs41_free_stateid_release,
9283};
9284
9285static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009286 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009287 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009288 bool privileged)
9289{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009290 struct rpc_message msg = {
9291 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009292 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009293 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009294 struct rpc_task_setup task_setup = {
9295 .rpc_client = server->client,
9296 .rpc_message = &msg,
9297 .callback_ops = &nfs41_free_stateid_ops,
9298 .flags = RPC_TASK_ASYNC,
9299 };
9300 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009301
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009302 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9303 &task_setup.rpc_client, &msg);
9304
Chuck Lever38527b12012-07-11 16:30:23 -04009305 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009306 data = kmalloc(sizeof(*data), GFP_NOFS);
9307 if (!data)
9308 return ERR_PTR(-ENOMEM);
9309 data->server = server;
9310 nfs4_stateid_copy(&data->args.stateid, stateid);
9311
9312 task_setup.callback_data = data;
9313
9314 msg.rpc_argp = &data->args;
9315 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009316 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009317 if (privileged)
9318 nfs4_set_sequence_privileged(&data->args.seq_args);
9319
9320 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009321}
9322
Chuck Lever38527b12012-07-11 16:30:23 -04009323/**
9324 * nfs41_free_stateid - perform a FREE_STATEID operation
9325 *
9326 * @server: server / transport on which to perform the operation
9327 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009328 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009329 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009330 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009331 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009332 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009333static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009334 const nfs4_stateid *stateid,
9335 struct rpc_cred *cred,
9336 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009337{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009338 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009339
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009340 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009341 if (IS_ERR(task))
9342 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009343 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009344 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009345}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009346
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009347static void
9348nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009349{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009350 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009351
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009352 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009353 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009354}
9355
Trond Myklebust36281ca2012-03-04 18:13:56 -05009356static bool nfs41_match_stateid(const nfs4_stateid *s1,
9357 const nfs4_stateid *s2)
9358{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009359 if (s1->type != s2->type)
9360 return false;
9361
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009362 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009363 return false;
9364
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009365 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009366 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009367
Anna Schumaker045c5512017-01-11 16:59:48 -05009368 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009369}
9370
Andy Adamson557134a2009-04-01 09:21:53 -04009371#endif /* CONFIG_NFS_V4_1 */
9372
Trond Myklebust36281ca2012-03-04 18:13:56 -05009373static bool nfs4_match_stateid(const nfs4_stateid *s1,
9374 const nfs4_stateid *s2)
9375{
Trond Myklebustf597c532012-03-04 18:13:56 -05009376 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009377}
9378
9379
Trond Myklebust17280172012-03-11 13:11:00 -04009380static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009381 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009382 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009383 .recover_open = nfs4_open_reclaim,
9384 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009385 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009386 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009387};
9388
Andy Adamson591d71c2009-04-01 09:22:47 -04009389#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009390static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009391 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9392 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9393 .recover_open = nfs4_open_reclaim,
9394 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009395 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009396 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009397 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009398};
9399#endif /* CONFIG_NFS_V4_1 */
9400
Trond Myklebust17280172012-03-11 13:11:00 -04009401static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009402 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009403 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009404 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009405 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009406 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009407};
9408
Andy Adamson591d71c2009-04-01 09:22:47 -04009409#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009410static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009411 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9412 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009413 .recover_open = nfs41_open_expired,
9414 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009415 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009416};
9417#endif /* CONFIG_NFS_V4_1 */
9418
Trond Myklebust17280172012-03-11 13:11:00 -04009419static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009420 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009421 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009422 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009423};
9424
9425#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009426static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009427 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009428 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009429 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009430};
9431#endif
9432
Chuck Leverec011fe2013-10-17 14:12:39 -04009433static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009434 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009435 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009436};
9437
9438#if defined(CONFIG_NFS_V4_1)
9439static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009440 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009441 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009442};
9443#endif /* CONFIG_NFS_V4_1 */
9444
Trond Myklebust97dc1352010-06-16 09:52:26 -04009445static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9446 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009447 .init_caps = NFS_CAP_READDIRPLUS
9448 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009449 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009450 .init_client = nfs40_init_client,
9451 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009452 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009453 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009454 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009455 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009456 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009457 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009458 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9459 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9460 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009461 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009462};
9463
9464#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009465static struct nfs_seqid *
9466nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9467{
9468 return NULL;
9469}
9470
Trond Myklebust97dc1352010-06-16 09:52:26 -04009471static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9472 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009473 .init_caps = NFS_CAP_READDIRPLUS
9474 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009475 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009476 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009477 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009478 .init_client = nfs41_init_client,
9479 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009480 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009481 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009482 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009483 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009484 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009485 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009486 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009487 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9488 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9489 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009490 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009491};
9492#endif
9493
Steve Dickson42c2c422013-05-22 12:50:38 -04009494#if defined(CONFIG_NFS_V4_2)
9495static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9496 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009497 .init_caps = NFS_CAP_READDIRPLUS
9498 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009499 | NFS_CAP_POSIX_LOCK
9500 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009501 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009502 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009503 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009504 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009505 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009506 | NFS_CAP_LAYOUTSTATS
9507 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009508 .init_client = nfs41_init_client,
9509 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009510 .match_stateid = nfs41_match_stateid,
9511 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009512 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009513 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009514 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009515 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009516 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009517 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9518 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9519 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009520 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009521};
9522#endif
9523
Trond Myklebust97dc1352010-06-16 09:52:26 -04009524const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9525 [0] = &nfs_v4_0_minor_ops,
9526#if defined(CONFIG_NFS_V4_1)
9527 [1] = &nfs_v4_1_minor_ops,
9528#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009529#if defined(CONFIG_NFS_V4_2)
9530 [2] = &nfs_v4_2_minor_ops,
9531#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009532};
9533
Trond Myklebust13997822016-07-24 17:10:52 -04009534static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009535{
9536 ssize_t error, error2;
9537
9538 error = generic_listxattr(dentry, list, size);
9539 if (error < 0)
9540 return error;
9541 if (list) {
9542 list += error;
9543 size -= error;
9544 }
9545
9546 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9547 if (error2 < 0)
9548 return error2;
9549 return error + error2;
9550}
9551
Trond Myklebust17f26b12013-08-21 15:48:42 -04009552static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009553 .create = nfs_create,
9554 .lookup = nfs_lookup,
9555 .atomic_open = nfs_atomic_open,
9556 .link = nfs_link,
9557 .unlink = nfs_unlink,
9558 .symlink = nfs_symlink,
9559 .mkdir = nfs_mkdir,
9560 .rmdir = nfs_rmdir,
9561 .mknod = nfs_mknod,
9562 .rename = nfs_rename,
9563 .permission = nfs_permission,
9564 .getattr = nfs_getattr,
9565 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009566 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009567};
9568
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009569static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009570 .permission = nfs_permission,
9571 .getattr = nfs_getattr,
9572 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009573 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009574};
9575
David Howells509de812006-08-22 20:06:11 -04009576const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009577 .version = 4, /* protocol version */
9578 .dentry_ops = &nfs4_dentry_operations,
9579 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009580 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009581 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009583 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009584 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009585 .getattr = nfs4_proc_getattr,
9586 .setattr = nfs4_proc_setattr,
9587 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009588 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009589 .access = nfs4_proc_access,
9590 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009591 .create = nfs4_proc_create,
9592 .remove = nfs4_proc_remove,
9593 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009594 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009595 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009596 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009597 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009598 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009599 .link = nfs4_proc_link,
9600 .symlink = nfs4_proc_symlink,
9601 .mkdir = nfs4_proc_mkdir,
9602 .rmdir = nfs4_proc_remove,
9603 .readdir = nfs4_proc_readdir,
9604 .mknod = nfs4_proc_mknod,
9605 .statfs = nfs4_proc_statfs,
9606 .fsinfo = nfs4_proc_fsinfo,
9607 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009608 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009609 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009610 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009612 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009613 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009614 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009615 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009616 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009617 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009618 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009619 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009620 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009621 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009622 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009623 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009624 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009625 .init_client = nfs4_init_client,
Bryan Schumakercdb7eced2012-06-20 15:53:45 -04009626 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009627 .create_server = nfs4_create_server,
9628 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629};
9630
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009631static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009632 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009633 .list = nfs4_xattr_list_nfs4_acl,
9634 .get = nfs4_xattr_get_nfs4_acl,
9635 .set = nfs4_xattr_set_nfs4_acl,
9636};
9637
9638const struct xattr_handler *nfs4_xattr_handlers[] = {
9639 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009640#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9641 &nfs4_xattr_nfs4_label_handler,
9642#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009643 NULL
9644};
9645
Linus Torvalds1da177e2005-04-16 15:20:36 -07009646/*
9647 * Local variables:
9648 * c-basic-offset: 8
9649 * End:
9650 */