blob: 05454cbc473df7950ae8b650263165a67156879c [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;
Fred Isaman2409a972016-10-06 12:11:21 -040089static void nfs4_layoutget_release(void *calldata);
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 Myklebustcf61eb22018-05-29 22:06:08 -0400410 case -NFS4ERR_BADHANDLE:
411 case -ESTALE:
412 if (inode != NULL && S_ISREG(inode->i_mode))
413 pnfs_destroy_layout(NFS_I(inode));
414 break;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400415 case -NFS4ERR_DELEG_REVOKED:
416 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400417 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400418 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400419 if (inode != NULL && stateid != NULL) {
420 nfs_inode_find_state_and_recover(inode,
421 stateid);
422 goto wait_on_recovery;
423 }
424 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400425 if (inode) {
426 int err;
427
428 err = nfs_async_inode_return_delegation(inode,
429 stateid);
430 if (err == 0)
431 goto wait_on_recovery;
432 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
433 exception->retry = 1;
434 break;
435 }
436 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500437 if (state == NULL)
438 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400439 ret = nfs4_schedule_stateid_recovery(server, state);
440 if (ret < 0)
441 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500442 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500443 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500444 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500445 nfs4_schedule_lease_recovery(clp);
446 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400447 case -NFS4ERR_MOVED:
448 ret = nfs4_schedule_migration_recovery(server);
449 if (ret < 0)
450 break;
451 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400452 case -NFS4ERR_LEASE_MOVED:
453 nfs4_schedule_lease_moved_recovery(clp);
454 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500455#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400456 case -NFS4ERR_BADSESSION:
457 case -NFS4ERR_BADSLOT:
458 case -NFS4ERR_BAD_HIGH_SLOT:
459 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
460 case -NFS4ERR_DEADSESSION:
461 case -NFS4ERR_SEQ_FALSE_RETRY:
462 case -NFS4ERR_SEQ_MISORDERED:
463 dprintk("%s ERROR: %d Reset session\n", __func__,
464 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400465 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400466 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500467#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500468 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500469 if (exception->timeout > HZ) {
470 /* We have retried a decent amount, time to
471 * fail
472 */
473 ret = -EBUSY;
474 break;
475 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500476 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400477 nfs_inc_server_stats(server, NFSIOS_DELAY);
478 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400479 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400480 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400481 exception->delay = 1;
482 return 0;
483
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400484 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500485 case -NFS4ERR_OLD_STATEID:
486 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800487 break;
488 case -NFS4ERR_BADOWNER:
489 /* The following works around a Linux server bug! */
490 case -NFS4ERR_BADNAME:
491 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
492 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
493 exception->retry = 1;
494 printk(KERN_WARNING "NFS: v4 server %s "
495 "does not accept raw "
496 "uid/gids. "
497 "Reenabling the idmapper.\n",
498 server->nfs_client->cl_hostname);
499 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500500 }
501 /* We failed to handle the error */
502 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500503wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400504 exception->recovering = 1;
505 return 0;
506}
507
508/* This is the error handling routine for processes that are allowed
509 * to sleep.
510 */
511int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
512{
513 struct nfs_client *clp = server->nfs_client;
514 int ret;
515
516 ret = nfs4_do_handle_exception(server, errorcode, exception);
517 if (exception->delay) {
518 ret = nfs4_delay(server->client, &exception->timeout);
519 goto out_retry;
520 }
521 if (exception->recovering) {
522 ret = nfs4_wait_clnt_recover(clp);
523 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
524 return -EIO;
525 goto out_retry;
526 }
527 return ret;
528out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500529 if (ret == 0)
530 exception->retry = 1;
531 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500532}
533
Trond Myklebust037fc982015-09-20 15:51:00 -0400534static int
535nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
536 int errorcode, struct nfs4_exception *exception)
537{
538 struct nfs_client *clp = server->nfs_client;
539 int ret;
540
541 ret = nfs4_do_handle_exception(server, errorcode, exception);
542 if (exception->delay) {
543 rpc_delay(task, nfs4_update_delay(&exception->timeout));
544 goto out_retry;
545 }
546 if (exception->recovering) {
547 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
548 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
549 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
550 goto out_retry;
551 }
552 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
553 ret = -EIO;
554 return ret;
555out_retry:
556 if (ret == 0)
557 exception->retry = 1;
558 return ret;
559}
560
561static int
562nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
563 struct nfs4_state *state, long *timeout)
564{
565 struct nfs4_exception exception = {
566 .state = state,
567 };
568
569 if (task->tk_status >= 0)
570 return 0;
571 if (timeout)
572 exception.timeout = *timeout;
573 task->tk_status = nfs4_async_handle_exception(task, server,
574 task->tk_status,
575 &exception);
576 if (exception.delay && timeout)
577 *timeout = exception.timeout;
578 if (exception.retry)
579 return -EAGAIN;
580 return 0;
581}
582
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400583/*
584 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
585 * or 'false' otherwise.
586 */
587static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
588{
589 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500590 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400591}
Trond Myklebust65de8722008-12-23 15:21:44 -0500592
Trond Myklebust452e9352010-07-31 14:29:06 -0400593static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700594{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 spin_lock(&clp->cl_lock);
596 if (time_before(clp->cl_last_renewal,timestamp))
597 clp->cl_last_renewal = timestamp;
598 spin_unlock(&clp->cl_lock);
599}
600
Trond Myklebust452e9352010-07-31 14:29:06 -0400601static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
602{
Trond Myklebustbe824162015-07-05 14:50:46 -0400603 struct nfs_client *clp = server->nfs_client;
604
605 if (!nfs4_has_session(clp))
606 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400607}
608
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400609struct nfs4_call_sync_data {
610 const struct nfs_server *seq_server;
611 struct nfs4_sequence_args *seq_args;
612 struct nfs4_sequence_res *seq_res;
613};
614
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800615void nfs4_init_sequence(struct nfs4_sequence_args *args,
Anna Schumakerfba83f32018-05-04 16:22:50 -0400616 struct nfs4_sequence_res *res, int cache_reply,
617 int privileged)
Chuck Levera9c92d62013-08-09 12:48:18 -0400618{
619 args->sa_slot = NULL;
620 args->sa_cache_this = cache_reply;
Anna Schumakerfba83f32018-05-04 16:22:50 -0400621 args->sa_privileged = privileged;
Chuck Levera9c92d62013-08-09 12:48:18 -0400622
623 res->sr_slot = NULL;
624}
625
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400626static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400627{
628 struct nfs4_slot *slot = res->sr_slot;
629 struct nfs4_slot_table *tbl;
630
Chuck Lever3bd23842013-08-09 12:49:19 -0400631 tbl = slot->table;
632 spin_lock(&tbl->slot_tbl_lock);
633 if (!nfs41_wake_and_assign_slot(tbl, slot))
634 nfs4_free_slot(tbl, slot);
635 spin_unlock(&tbl->slot_tbl_lock);
636
637 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400638}
639
640static int nfs40_sequence_done(struct rpc_task *task,
641 struct nfs4_sequence_res *res)
642{
643 if (res->sr_slot != NULL)
644 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400645 return 1;
646}
647
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400648#if defined(CONFIG_NFS_V4_1)
649
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400650static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400651{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500652 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400653 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500654 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400655
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400656 if (!slot)
657 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500658 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500659 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500660
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400661 /* Bump the slot sequence number */
662 if (slot->seq_done)
663 slot->seq_nr++;
664 slot->seq_done = 0;
665
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500666 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500667 /* Be nice to the server: try to ensure that the last transmitted
668 * value for highest_user_slotid <= target_highest_slotid
669 */
670 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
671 send_new_highest_used_slotid = true;
672
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500673 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500674 send_new_highest_used_slotid = false;
675 goto out_unlock;
676 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500677 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500678
679 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
680 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500681out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500682 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500683 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400684 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400685 if (waitqueue_active(&tbl->slot_waitq))
686 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400687}
688
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400689static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
690{
691 nfs41_release_slot(res->sr_slot);
692 res->sr_slot = NULL;
693}
694
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400695static int nfs41_sequence_process(struct rpc_task *task,
696 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400697{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500698 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500699 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400700 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500701 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500702 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400703
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500704 if (slot == NULL)
705 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400706 /* don't increment the sequence number if the task wasn't sent */
707 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400708 goto out;
709
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500710 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500711
Trond Myklebustac20d162012-12-15 15:36:07 -0500712 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400713 if (res->sr_status != -NFS4ERR_DELAY)
714 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500715 interrupted = true;
716 }
717
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400718 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500719 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400720 switch (res->sr_status) {
721 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400722 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400723 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500724 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500725 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500726 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400727 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
728 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500729 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400730 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500731 case 1:
732 /*
733 * sr_status remains 1 if an RPC level error occurred.
734 * The server may or may not have processed the sequence
735 * operation..
736 * Mark the slot as having hosted an interrupted RPC call.
737 */
738 slot->interrupted = 1;
739 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400740 case -NFS4ERR_DELAY:
741 /* The server detected a resend of the RPC call and
742 * returned NFS4ERR_DELAY as per Section 2.10.6.2
743 * of RFC5661.
744 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500745 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400746 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500747 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500748 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400749 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500750 case -NFS4ERR_BADSLOT:
751 /*
752 * The slot id we used was probably retired. Try again
753 * using a different slot id.
754 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400755 if (slot->seq_nr < slot->table->target_highest_slotid)
756 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500757 goto retry_nowait;
758 case -NFS4ERR_SEQ_MISORDERED:
759 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500760 * Was the last operation on this sequence interrupted?
761 * If so, retry after bumping the sequence number.
762 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400763 if (interrupted)
764 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500765 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500766 * Could this slot have been previously retired?
767 * If so, then the server may be expecting seq_nr = 1!
768 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500769 if (slot->seq_nr != 1) {
770 slot->seq_nr = 1;
771 goto retry_nowait;
772 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400773 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500774 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400775 if (interrupted)
776 goto retry_new_seq;
777 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400778 default:
779 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400780 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400781 }
782out:
783 /* The session may be reset by one of the error handlers. */
784 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500785out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500786 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400787session_recover:
788 nfs4_schedule_session_recovery(session, res->sr_status);
789 goto retry_nowait;
790retry_new_seq:
791 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500792retry_nowait:
793 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400794 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500795 task->tk_status = 0;
796 ret = 0;
797 }
798 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400799out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400800 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400801 goto out;
802 rpc_delay(task, NFS4_POLL_RETRY_MAX);
803 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400804}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400805
806int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
807{
808 if (!nfs41_sequence_process(task, res))
809 return 0;
810 if (res->sr_slot != NULL)
811 nfs41_sequence_free_slot(res);
812 return 1;
813
814}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500815EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400816
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400817static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
818{
819 if (res->sr_slot == NULL)
820 return 1;
821 if (res->sr_slot->table->session != NULL)
822 return nfs41_sequence_process(task, res);
823 return nfs40_sequence_done(task, res);
824}
825
826static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
827{
828 if (res->sr_slot != NULL) {
829 if (res->sr_slot->table->session != NULL)
830 nfs41_sequence_free_slot(res);
831 else
832 nfs40_sequence_free_slot(res);
833 }
834}
835
Peng Tao2c4b1312014-06-11 05:24:16 +0800836int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400837{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500838 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400839 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400840 if (!res->sr_slot->table->session)
841 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400842 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400843}
Peng Tao2c4b1312014-06-11 05:24:16 +0800844EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400845
Andy Adamsonce5039c2009-04-01 09:22:13 -0400846static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
847{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400848 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400849
Trond Myklebust035168ab2010-06-16 09:52:26 -0400850 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
851
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500852 nfs4_setup_sequence(data->seq_server->nfs_client,
853 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400854}
855
Andy Adamson69ab40c2009-04-01 09:22:19 -0400856static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
857{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400858 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400859
Trond Myklebust14516c32010-07-31 14:29:06 -0400860 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400861}
862
Trond Myklebust17280172012-03-11 13:11:00 -0400863static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400864 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400865 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400866};
867
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400868static void
869nfs4_sequence_process_interrupted(struct nfs_client *client,
870 struct nfs4_slot *slot, struct rpc_cred *cred)
871{
872 struct rpc_task *task;
873
874 task = _nfs41_proc_sequence(client, cred, slot, true);
875 if (!IS_ERR(task))
876 rpc_put_task_async(task);
877}
878
Chuck Lever3bd23842013-08-09 12:49:19 -0400879#else /* !CONFIG_NFS_V4_1 */
880
Fred Isaman2409a972016-10-06 12:11:21 -0400881static void nfs4_layoutget_release(void *calldata)
882{
883}
884
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400885static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
886{
887 return nfs40_sequence_done(task, res);
888}
889
890static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
891{
892 if (res->sr_slot != NULL)
893 nfs40_sequence_free_slot(res);
894}
895
Peng Tao2c4b1312014-06-11 05:24:16 +0800896int nfs4_sequence_done(struct rpc_task *task,
897 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400898{
Chuck Lever3bd23842013-08-09 12:49:19 -0400899 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400900}
Peng Tao2c4b1312014-06-11 05:24:16 +0800901EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400902
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400903static void
904nfs4_sequence_process_interrupted(struct nfs_client *client,
905 struct nfs4_slot *slot, struct rpc_cred *cred)
906{
907 WARN_ON_ONCE(1);
908 slot->interrupted = 0;
909}
910
Chuck Lever3bd23842013-08-09 12:49:19 -0400911#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400912
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400913static
914void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
915 struct nfs4_sequence_res *res,
916 struct nfs4_slot *slot)
917{
918 if (!slot)
919 return;
920 slot->privileged = args->sa_privileged ? 1 : 0;
921 args->sa_slot = slot;
922
923 res->sr_slot = slot;
924 res->sr_timestamp = jiffies;
925 res->sr_status_flags = 0;
926 res->sr_status = 1;
927
928}
929
930int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500931 struct nfs4_sequence_args *args,
932 struct nfs4_sequence_res *res,
933 struct rpc_task *task)
934{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500935 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500936 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500937 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500938
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500939 /* slot already allocated? */
940 if (res->sr_slot != NULL)
941 goto out_start;
942
Anna Schumaker76ee0352017-01-10 16:49:31 -0500943 if (session) {
944 tbl = &session->fc_slot_table;
945 task->tk_timeout = 0;
946 }
947
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400948 for (;;) {
949 spin_lock(&tbl->slot_tbl_lock);
950 /* The state manager will wait until the slot table is empty */
951 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
952 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500953
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400954 slot = nfs4_alloc_slot(tbl);
955 if (IS_ERR(slot)) {
956 /* Try again in 1/4 second */
957 if (slot == ERR_PTR(-ENOMEM))
958 task->tk_timeout = HZ >> 2;
959 goto out_sleep;
960 }
961 spin_unlock(&tbl->slot_tbl_lock);
962
963 if (likely(!slot->interrupted))
964 break;
965 nfs4_sequence_process_interrupted(client,
966 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500967 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500968
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400969 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500970
Anna Schumakerad05cc02017-01-11 13:37:06 -0500971 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500972out_start:
973 rpc_call_start(task);
974 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500975
976out_sleep:
977 if (args->sa_privileged)
978 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
979 NULL, RPC_PRIORITY_PRIVILEGED);
980 else
981 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
982 spin_unlock(&tbl->slot_tbl_lock);
983 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500984}
985EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
986
Chuck Lever9915ea72013-08-09 12:48:27 -0400987static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
988{
989 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500990 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400991 data->seq_args, data->seq_res, task);
992}
993
994static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
995{
996 struct nfs4_call_sync_data *data = calldata;
997 nfs4_sequence_done(task, data->seq_res);
998}
999
1000static const struct rpc_call_ops nfs40_call_sync_ops = {
1001 .rpc_call_prepare = nfs40_call_sync_prepare,
1002 .rpc_call_done = nfs40_call_sync_done,
1003};
1004
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001005static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001006 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001007 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001008 struct nfs4_sequence_args *args,
1009 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001010{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001011 int ret;
1012 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001013 struct nfs_client *clp = server->nfs_client;
1014 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001015 .seq_server = server,
1016 .seq_args = args,
1017 .seq_res = res,
1018 };
1019 struct rpc_task_setup task_setup = {
1020 .rpc_client = clnt,
1021 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001022 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001023 .callback_data = &data
1024 };
1025
1026 task = rpc_run_task(&task_setup);
1027 if (IS_ERR(task))
1028 ret = PTR_ERR(task);
1029 else {
1030 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001031 rpc_put_task(task);
1032 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001033 return ret;
1034}
1035
Bryan Schumaker7c513052011-03-24 17:12:24 +00001036int nfs4_call_sync(struct rpc_clnt *clnt,
1037 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001038 struct rpc_message *msg,
1039 struct nfs4_sequence_args *args,
1040 struct nfs4_sequence_res *res,
1041 int cache_reply)
1042{
Anna Schumakerfba83f32018-05-04 16:22:50 -04001043 nfs4_init_sequence(args, res, cache_reply, 0);
Chuck Lever9915ea72013-08-09 12:48:27 -04001044 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001045}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001047static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1048 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049{
1050 struct nfs_inode *nfsi = NFS_I(dir);
1051
1052 spin_lock(&dir->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04001053 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1054 | NFS_INO_INVALID_MTIME
1055 | NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001056 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001057 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1058 nfsi->attrtimeo_timestamp = jiffies;
1059 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001061 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001062 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1063 NFS_INO_INVALID_ACL;
1064 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001065 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001066 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001067 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001068 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001069 spin_unlock(&dir->i_lock);
1070}
1071
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001072struct nfs4_open_createattrs {
1073 struct nfs4_label *label;
1074 struct iattr *sattr;
1075 const __u32 verf[2];
1076};
1077
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001078static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1079 int err, struct nfs4_exception *exception)
1080{
1081 if (err != -EINVAL)
1082 return false;
1083 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1084 return false;
1085 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1086 exception->retry = 1;
1087 return true;
1088}
1089
Trond Myklebust6ae37332015-01-30 14:21:14 -05001090static u32
1091nfs4_map_atomic_open_share(struct nfs_server *server,
1092 fmode_t fmode, int openflags)
1093{
1094 u32 res = 0;
1095
1096 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1097 case FMODE_READ:
1098 res = NFS4_SHARE_ACCESS_READ;
1099 break;
1100 case FMODE_WRITE:
1101 res = NFS4_SHARE_ACCESS_WRITE;
1102 break;
1103 case FMODE_READ|FMODE_WRITE:
1104 res = NFS4_SHARE_ACCESS_BOTH;
1105 }
1106 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1107 goto out;
1108 /* Want no delegation if we're using O_DIRECT */
1109 if (openflags & O_DIRECT)
1110 res |= NFS4_SHARE_WANT_NO_DELEG;
1111out:
1112 return res;
1113}
1114
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001115static enum open_claim_type4
1116nfs4_map_atomic_open_claim(struct nfs_server *server,
1117 enum open_claim_type4 claim)
1118{
1119 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1120 return claim;
1121 switch (claim) {
1122 default:
1123 return claim;
1124 case NFS4_OPEN_CLAIM_FH:
1125 return NFS4_OPEN_CLAIM_NULL;
1126 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1127 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1128 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1129 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1130 }
1131}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132
1133static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001134{
1135 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001136 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001137 p->o_res.seqid = p->o_arg.seqid;
1138 p->c_res.seqid = p->c_arg.seqid;
1139 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001140 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001141 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001142 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001143}
1144
Al Viro82a2c1b2011-06-22 18:30:55 -04001145static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001146 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001147 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001148 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001149 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001150{
Al Viro82a2c1b2011-06-22 18:30:55 -04001151 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001152 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001154 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001155 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 struct nfs4_opendata *p;
1157
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001158 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001159 if (p == NULL)
1160 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001161
1162 p->f_label = nfs4_label_alloc(server, gfp_mask);
1163 if (IS_ERR(p->f_label))
1164 goto err_free_p;
1165
Kinglong Meea49c2692015-07-27 15:31:38 +08001166 p->a_label = nfs4_label_alloc(server, gfp_mask);
1167 if (IS_ERR(p->a_label))
1168 goto err_free_f;
1169
Trond Myklebust63f5f792015-01-23 19:19:25 -05001170 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1171 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001172 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001173 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001174 nfs_sb_active(dentry->d_sb);
1175 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 p->dir = parent;
1177 p->owner = sp;
1178 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001179 p->o_arg.open_flags = flags;
1180 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001181 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001182 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001183 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1184 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001185 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1186 * will return permission denied for all bits until close */
1187 if (!(flags & O_EXCL)) {
1188 /* ask server to check for all possible rights as results
1189 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001190 switch (p->o_arg.claim) {
1191 default:
1192 break;
1193 case NFS4_OPEN_CLAIM_NULL:
1194 case NFS4_OPEN_CLAIM_FH:
1195 p->o_arg.access = NFS4_ACCESS_READ |
1196 NFS4_ACCESS_MODIFY |
1197 NFS4_ACCESS_EXTEND |
1198 NFS4_ACCESS_EXECUTE;
1199 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001200 }
David Howells7539bba2006-08-22 20:06:09 -04001201 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001202 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1203 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001204 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001205 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001206 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001207 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001208 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001209 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001210 case NFS4_OPEN_CLAIM_NULL:
1211 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1212 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1213 p->o_arg.fh = NFS_FH(dir);
1214 break;
1215 case NFS4_OPEN_CLAIM_PREVIOUS:
1216 case NFS4_OPEN_CLAIM_FH:
1217 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1218 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001219 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001220 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001221 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001223 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001224
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001225 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001226 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001227 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001228 p->c_arg.fh = &p->o_res.fh;
1229 p->c_arg.stateid = &p->o_res.stateid;
1230 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001231 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001232 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001233 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001234
1235err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001236 nfs4_label_free(p->a_label);
1237err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001238 nfs4_label_free(p->f_label);
1239err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001240 kfree(p);
1241err:
1242 dput(parent);
1243 return NULL;
1244}
1245
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001246static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001247{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001248 struct nfs4_opendata *p = container_of(kref,
1249 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001250 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251
Fred Isaman2409a972016-10-06 12:11:21 -04001252 if (p->lgp)
1253 nfs4_layoutget_release(p->lgp);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001254 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001255 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001256 if (p->state != NULL)
1257 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001258 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001259
Kinglong Meea49c2692015-07-27 15:31:38 +08001260 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001261 nfs4_label_free(p->f_label);
1262
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001264 dput(p->dentry);
1265 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001266 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001267 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001268 kfree(p);
1269}
1270
1271static void nfs4_opendata_put(struct nfs4_opendata *p)
1272{
1273 if (p != NULL)
1274 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001275}
1276
Trond Myklebust24311f82015-09-20 10:50:17 -04001277static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1278 fmode_t fmode)
1279{
1280 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1281 case FMODE_READ|FMODE_WRITE:
1282 return state->n_rdwr != 0;
1283 case FMODE_WRITE:
1284 return state->n_wronly != 0;
1285 case FMODE_READ:
1286 return state->n_rdonly != 0;
1287 }
1288 WARN_ON_ONCE(1);
1289 return false;
1290}
1291
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001292static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001293{
1294 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001295
Trond Myklebust536e43d2012-01-17 22:04:26 -05001296 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001297 goto out;
1298 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001299 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001300 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1301 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001302 break;
1303 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001304 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1305 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001306 break;
1307 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001308 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1309 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001311out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001312 return ret;
1313}
1314
Trond Myklebust2a606182015-08-19 22:30:00 -05001315static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1316 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001317{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001318 if (delegation == NULL)
1319 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001320 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001321 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001322 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1323 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001324 switch (claim) {
1325 case NFS4_OPEN_CLAIM_NULL:
1326 case NFS4_OPEN_CLAIM_FH:
1327 break;
1328 case NFS4_OPEN_CLAIM_PREVIOUS:
1329 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1330 break;
1331 default:
1332 return 0;
1333 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001334 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001335 return 1;
1336}
1337
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001338static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001339{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001340 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001341 case FMODE_WRITE:
1342 state->n_wronly++;
1343 break;
1344 case FMODE_READ:
1345 state->n_rdonly++;
1346 break;
1347 case FMODE_READ|FMODE_WRITE:
1348 state->n_rdwr++;
1349 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001350 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001351}
1352
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001353#ifdef CONFIG_NFS_V4_1
1354static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1355{
1356 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1357 return true;
1358 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1359 return true;
1360 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1361 return true;
1362 return false;
1363}
1364#endif /* CONFIG_NFS_V4_1 */
1365
Trond Myklebustc9399f22017-11-06 15:28:01 -05001366static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1367{
1368 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1369 wake_up_all(&state->waitq);
1370}
1371
1372static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1373 const nfs4_stateid *stateid)
1374{
1375 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1376 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1377
1378 if (stateid_seqid == state_seqid + 1U ||
1379 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1380 nfs_state_log_update_open_stateid(state);
1381 else
1382 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1383}
1384
Trond Myklebust4f14c192014-02-12 19:15:06 -05001385static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001386{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001387 struct nfs_client *clp = state->owner->so_server->nfs_client;
1388 bool need_recover = false;
1389
1390 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1391 need_recover = true;
1392 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1393 need_recover = true;
1394 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1395 need_recover = true;
1396 if (need_recover)
1397 nfs4_state_mark_reclaim_nograce(clp, state);
1398}
1399
Trond Myklebustc9399f22017-11-06 15:28:01 -05001400/*
1401 * Check for whether or not the caller may update the open stateid
1402 * to the value passed in by stateid.
1403 *
1404 * Note: This function relies heavily on the server implementing
1405 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1406 * correctly.
1407 * i.e. The stateid seqids have to be initialised to 1, and
1408 * are then incremented on every state transition.
1409 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001410static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001411 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001412{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001413 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1414 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1415 if (stateid->seqid == cpu_to_be32(1))
1416 nfs_state_log_update_open_stateid(state);
1417 else
1418 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001419 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001420 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001421
1422 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1423 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001424 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001425 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001426 return false;
1427}
1428
Trond Myklebustf95549c2015-01-23 18:06:09 -05001429static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1430{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001431 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1432 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001433 if (state->n_wronly)
1434 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1435 if (state->n_rdonly)
1436 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1437 if (state->n_rdwr)
1438 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001439 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001440}
1441
Trond Myklebust226056c2014-02-11 10:41:07 -05001442static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1443 nfs4_stateid *stateid, fmode_t fmode)
1444{
1445 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1446 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1447 case FMODE_WRITE:
1448 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1449 break;
1450 case FMODE_READ:
1451 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1452 break;
1453 case 0:
1454 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1455 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1456 clear_bit(NFS_OPEN_STATE, &state->flags);
1457 }
1458 if (stateid == NULL)
1459 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001460 /* Handle OPEN+OPEN_DOWNGRADE races */
1461 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1462 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001463 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001464 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001465 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001466 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001467 nfs4_stateid_copy(&state->stateid, stateid);
1468 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001469 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001470out:
1471 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001472}
1473
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001474static void nfs_clear_open_stateid(struct nfs4_state *state,
1475 nfs4_stateid *arg_stateid,
1476 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001477{
1478 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001479 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1480 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1481 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001482 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001483 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1484 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001485}
1486
Trond Myklebust1393d962016-09-22 13:39:13 -04001487static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001488 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001489{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001490 DEFINE_WAIT(wait);
1491 int status = 0;
1492 for (;;) {
1493
1494 if (!nfs_need_update_open_stateid(state, stateid))
1495 return;
1496 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001497 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001498 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001499 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001500 /* Rely on seqids for serialisation with NFSv4.0 */
1501 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1502 break;
1503
1504 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1505 /*
1506 * Ensure we process the state changes in the same order
1507 * in which the server processed them by delaying the
1508 * update of the stateid until we are in sequence.
1509 */
1510 write_sequnlock(&state->seqlock);
1511 spin_unlock(&state->owner->so_lock);
1512 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001513 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001514 if (!signal_pending(current)) {
1515 if (schedule_timeout(5*HZ) == 0)
1516 status = -EAGAIN;
1517 else
1518 status = 0;
1519 } else
1520 status = -EINTR;
1521 finish_wait(&state->waitq, &wait);
1522 rcu_read_lock();
1523 spin_lock(&state->owner->so_lock);
1524 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001525 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001526
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001527 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1528 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001529 nfs4_stateid_copy(freeme, &state->open_stateid);
1530 nfs_test_and_clear_all_open_stateid(state);
1531 }
1532
Trond Myklebuste999e802014-02-10 18:20:47 -05001533 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1534 nfs4_stateid_copy(&state->stateid, stateid);
1535 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001536 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001537 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001538}
1539
Trond Myklebustc9399f22017-11-06 15:28:01 -05001540static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001541 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001542 fmode_t fmode,
1543 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001545 /*
1546 * Protect the call to nfs4_state_set_mode_locked and
1547 * serialise the stateid update
1548 */
1549 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001550 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1551 switch (fmode) {
1552 case FMODE_READ:
1553 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1554 break;
1555 case FMODE_WRITE:
1556 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1557 break;
1558 case FMODE_READ|FMODE_WRITE:
1559 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001560 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001561 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001562 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001563}
1564
1565static void nfs_state_set_delegation(struct nfs4_state *state,
1566 const nfs4_stateid *deleg_stateid,
1567 fmode_t fmode)
1568{
1569 /*
1570 * Protect the call to nfs4_state_set_mode_locked and
1571 * serialise the stateid update
1572 */
1573 write_seqlock(&state->seqlock);
1574 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1575 set_bit(NFS_DELEGATED_STATE, &state->flags);
1576 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577}
1578
Trond Myklebust1393d962016-09-22 13:39:13 -04001579static int update_open_stateid(struct nfs4_state *state,
1580 const nfs4_stateid *open_stateid,
1581 const nfs4_stateid *delegation,
1582 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001583{
Trond Myklebust1393d962016-09-22 13:39:13 -04001584 struct nfs_server *server = NFS_SERVER(state->inode);
1585 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001586 struct nfs_inode *nfsi = NFS_I(state->inode);
1587 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001588 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001589 int ret = 0;
1590
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001591 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001592
1593 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001594 spin_lock(&state->owner->so_lock);
1595 if (open_stateid != NULL) {
1596 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1597 ret = 1;
1598 }
1599
Trond Myklebust34310432008-12-23 15:21:38 -05001600 deleg_cur = rcu_dereference(nfsi->delegation);
1601 if (deleg_cur == NULL)
1602 goto no_delegation;
1603
1604 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001605 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001606 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001607 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001608 goto no_delegation_unlock;
1609
1610 if (delegation == NULL)
1611 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001612 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001613 goto no_delegation_unlock;
1614
Trond Myklebustb7391f42008-12-23 15:21:52 -05001615 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001616 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001617 ret = 1;
1618no_delegation_unlock:
1619 spin_unlock(&deleg_cur->lock);
1620no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001621 if (ret)
1622 update_open_stateflags(state, fmode);
1623 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001624 rcu_read_unlock();
1625
Trond Myklebust4f14c192014-02-12 19:15:06 -05001626 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001627 nfs4_schedule_state_manager(clp);
1628 if (freeme.type != 0)
1629 nfs4_test_and_free_stateid(server, &freeme,
1630 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001631
1632 return ret;
1633}
1634
Trond Myklebust39071e62015-01-24 15:07:56 -05001635static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1636 const nfs4_stateid *stateid)
1637{
1638 struct nfs4_state *state = lsp->ls_state;
1639 bool ret = false;
1640
1641 spin_lock(&state->state_lock);
1642 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1643 goto out_noupdate;
1644 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1645 goto out_noupdate;
1646 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1647 ret = true;
1648out_noupdate:
1649 spin_unlock(&state->state_lock);
1650 return ret;
1651}
Trond Myklebust34310432008-12-23 15:21:38 -05001652
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001653static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001654{
1655 struct nfs_delegation *delegation;
1656
Trond Myklebustf5086242018-03-20 16:43:13 -04001657 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001658 rcu_read_lock();
1659 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001660 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001661 rcu_read_unlock();
1662 return;
1663 }
1664 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001665 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001666}
1667
Trond Myklebust6ee41262007-07-08 14:11:36 -04001668static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669{
1670 struct nfs4_state *state = opendata->state;
1671 struct nfs_inode *nfsi = NFS_I(state->inode);
1672 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001673 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001674 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001675 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 nfs4_stateid stateid;
1677 int ret = -EAGAIN;
1678
Trond Myklebustaac00a82007-07-05 19:02:21 -04001679 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001680 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001681 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001682 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001683 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001684 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001685 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001686 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001687 rcu_read_lock();
1688 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001689 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001690 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001691 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001692 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001693 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001694 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001695 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001696 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001697 if (!opendata->is_recover) {
1698 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1699 if (ret != 0)
1700 goto out;
1701 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001702 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001703
1704 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001705 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001706 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001707 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708out:
1709 return ERR_PTR(ret);
1710out_return_state:
1711 atomic_inc(&state->count);
1712 return state;
1713}
1714
Andy Adamsone23008e2012-10-02 21:07:32 -04001715static void
1716nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1717{
1718 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1719 struct nfs_delegation *delegation;
1720 int delegation_flags = 0;
1721
1722 rcu_read_lock();
1723 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1724 if (delegation)
1725 delegation_flags = delegation->flags;
1726 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001727 switch (data->o_arg.claim) {
1728 default:
1729 break;
1730 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1731 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001732 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1733 "returning a delegation for "
1734 "OPEN(CLAIM_DELEGATE_CUR)\n",
1735 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001736 return;
1737 }
1738 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001739 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001740 data->owner->so_cred,
1741 data->o_res.delegation_type,
1742 &data->o_res.delegation,
1743 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001744 else
1745 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001746 data->owner->so_cred,
1747 data->o_res.delegation_type,
1748 &data->o_res.delegation,
1749 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001750}
1751
1752/*
1753 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1754 * and update the nfs4_state.
1755 */
1756static struct nfs4_state *
1757_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1758{
1759 struct inode *inode = data->state->inode;
1760 struct nfs4_state *state = data->state;
1761 int ret;
1762
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001763 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001764 if (data->rpc_status)
1765 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001766 /* cached opens have already been processed */
1767 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001768 }
1769
Andy Adamsone23008e2012-10-02 21:07:32 -04001770 ret = nfs_refresh_inode(inode, &data->f_attr);
1771 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001772 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001773
1774 if (data->o_res.delegation_type != 0)
1775 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001776update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001777 update_open_stateid(state, &data->o_res.stateid, NULL,
1778 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001779 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001780
1781 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001782}
1783
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001784static struct inode *
1785nfs4_opendata_get_inode(struct nfs4_opendata *data)
1786{
1787 struct inode *inode;
1788
1789 switch (data->o_arg.claim) {
1790 case NFS4_OPEN_CLAIM_NULL:
1791 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1792 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1793 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1794 return ERR_PTR(-EAGAIN);
1795 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1796 &data->f_attr, data->f_label);
1797 break;
1798 default:
1799 inode = d_inode(data->dentry);
1800 ihold(inode);
1801 nfs_refresh_inode(inode, &data->f_attr);
1802 }
1803 return inode;
1804}
1805
Andy Adamsone23008e2012-10-02 21:07:32 -04001806static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001807nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1808{
1809 struct nfs4_state *state;
1810 struct inode *inode;
1811
1812 inode = nfs4_opendata_get_inode(data);
1813 if (IS_ERR(inode))
1814 return ERR_CAST(inode);
1815 if (data->state != NULL && data->state->inode == inode) {
1816 state = data->state;
1817 atomic_inc(&state->count);
1818 } else
1819 state = nfs4_get_open_state(inode, data->owner);
1820 iput(inode);
1821 if (state == NULL)
1822 state = ERR_PTR(-ENOMEM);
1823 return state;
1824}
1825
Andy Adamsone23008e2012-10-02 21:07:32 -04001826static struct nfs4_state *
1827_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001828{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001829 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001830
Trond Myklebustaac00a82007-07-05 19:02:21 -04001831 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001832 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001833 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001834 goto out;
1835 }
1836
Trond Myklebust75e8c482017-08-08 10:38:07 -04001837 state = nfs4_opendata_find_nfs4_state(data);
1838 if (IS_ERR(state))
1839 goto out;
1840
Andy Adamsone23008e2012-10-02 21:07:32 -04001841 if (data->o_res.delegation_type != 0)
1842 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001843 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001844 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001846 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847 return state;
1848}
1849
Andy Adamsone23008e2012-10-02 21:07:32 -04001850static struct nfs4_state *
1851nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1852{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001853 struct nfs4_state *ret;
1854
Andy Adamsone23008e2012-10-02 21:07:32 -04001855 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001856 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1857 else
1858 ret = _nfs4_opendata_to_nfs4_state(data);
1859 nfs4_sequence_free_slot(&data->o_res.seq_res);
1860 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001861}
1862
Trond Myklebust864472e2006-01-03 09:55:15 +01001863static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1864{
1865 struct nfs_inode *nfsi = NFS_I(state->inode);
1866 struct nfs_open_context *ctx;
1867
1868 spin_lock(&state->inode->i_lock);
1869 list_for_each_entry(ctx, &nfsi->open_files, list) {
1870 if (ctx->state != state)
1871 continue;
1872 get_nfs_open_context(ctx);
1873 spin_unlock(&state->inode->i_lock);
1874 return ctx;
1875 }
1876 spin_unlock(&state->inode->i_lock);
1877 return ERR_PTR(-ENOENT);
1878}
1879
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001880static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1881 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001882{
1883 struct nfs4_opendata *opendata;
1884
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001885 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001886 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001887 if (opendata == NULL)
1888 return ERR_PTR(-ENOMEM);
1889 opendata->state = state;
1890 atomic_inc(&state->count);
1891 return opendata;
1892}
1893
Trond Myklebust24311f82015-09-20 10:50:17 -04001894static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1895 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001896{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001897 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001898 int ret;
1899
Trond Myklebust24311f82015-09-20 10:50:17 -04001900 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001901 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001902 opendata->o_arg.open_flags = 0;
1903 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001904 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1905 NFS_SB(opendata->dentry->d_sb),
1906 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001907 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1908 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1909 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001910 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001911 if (ret != 0)
1912 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001913 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001914 if (IS_ERR(newstate))
1915 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001916 if (newstate != opendata->state)
1917 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001918 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001919 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001920}
1921
1922static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1923{
Trond Myklebust864472e2006-01-03 09:55:15 +01001924 int ret;
1925
Trond Myklebust4f14c192014-02-12 19:15:06 -05001926 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1927 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1928 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1929 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001930 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001931 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001932 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001933 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001934 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1935 if (ret != 0)
1936 return ret;
1937 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1938 if (ret != 0)
1939 return ret;
1940 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1941 if (ret != 0)
1942 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001943 /*
1944 * We may have performed cached opens for all three recoveries.
1945 * Check if we need to update the current stateid.
1946 */
1947 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001948 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001949 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001950 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001951 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001952 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001953 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001954 return 0;
1955}
1956
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957/*
1958 * OPEN_RECLAIM:
1959 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001960 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001961static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001963 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001964 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001965 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966 int status;
1967
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001968 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1969 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001970 if (IS_ERR(opendata))
1971 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001972 rcu_read_lock();
1973 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001974 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001975 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001976 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001977 opendata->o_arg.u.delegation_type = delegation_type;
1978 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001979 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 return status;
1981}
1982
Trond Myklebust539cd032007-06-05 11:46:42 -04001983static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984{
1985 struct nfs_server *server = NFS_SERVER(state->inode);
1986 struct nfs4_exception exception = { };
1987 int err;
1988 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04001989 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04001990 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001991 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
1992 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04001993 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00001994 break;
1995 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 } while (exception.retry);
1997 return err;
1998}
1999
Trond Myklebust864472e2006-01-03 09:55:15 +01002000static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2001{
2002 struct nfs_open_context *ctx;
2003 int ret;
2004
2005 ctx = nfs4_state_find_open_context(state);
2006 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002007 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002008 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002009 put_nfs_open_context(ctx);
2010 return ret;
2011}
2012
NeilBrowndce26302017-12-13 09:57:09 +11002013static 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 -07002014{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002015 switch (err) {
2016 default:
2017 printk(KERN_ERR "NFS: %s: unhandled error "
2018 "%d.\n", __func__, err);
2019 case 0:
2020 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002021 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002022 case -ESTALE:
2023 break;
2024 case -NFS4ERR_BADSESSION:
2025 case -NFS4ERR_BADSLOT:
2026 case -NFS4ERR_BAD_HIGH_SLOT:
2027 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2028 case -NFS4ERR_DEADSESSION:
2029 set_bit(NFS_DELEGATED_STATE, &state->flags);
2030 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2031 return -EAGAIN;
2032 case -NFS4ERR_STALE_CLIENTID:
2033 case -NFS4ERR_STALE_STATEID:
2034 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002035 /* Don't recall a delegation if it was lost */
2036 nfs4_schedule_lease_recovery(server->nfs_client);
2037 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002038 case -NFS4ERR_MOVED:
2039 nfs4_schedule_migration_recovery(server);
2040 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002041 case -NFS4ERR_LEASE_MOVED:
2042 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2043 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002044 case -NFS4ERR_DELEG_REVOKED:
2045 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002046 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002047 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002048 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002049 nfs_inode_find_state_and_recover(state->inode,
2050 stateid);
2051 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002052 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002053 case -NFS4ERR_DELAY:
2054 case -NFS4ERR_GRACE:
2055 set_bit(NFS_DELEGATED_STATE, &state->flags);
2056 ssleep(1);
2057 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002058 case -ENOMEM:
2059 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002060 if (fl) {
2061 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2062 if (lsp)
2063 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2064 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002065 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002066 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 return err;
2068}
2069
Trond Myklebust24311f82015-09-20 10:50:17 -04002070int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2071 struct nfs4_state *state, const nfs4_stateid *stateid,
2072 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002073{
2074 struct nfs_server *server = NFS_SERVER(state->inode);
2075 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002076 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002077
2078 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2079 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2080 if (IS_ERR(opendata))
2081 return PTR_ERR(opendata);
2082 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002083 write_seqlock(&state->seqlock);
2084 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2085 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002086 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2087 switch (type & (FMODE_READ|FMODE_WRITE)) {
2088 case FMODE_READ|FMODE_WRITE:
2089 case FMODE_WRITE:
2090 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2091 if (err)
2092 break;
2093 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2094 if (err)
2095 break;
2096 case FMODE_READ:
2097 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2098 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002099 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002100 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002101}
2102
Chuck Leverbe05c862013-08-09 12:49:47 -04002103static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2104{
2105 struct nfs4_opendata *data = calldata;
2106
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002107 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2108 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002109}
2110
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002111static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2112{
2113 struct nfs4_opendata *data = calldata;
2114
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002115 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002116
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002117 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002118 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002119 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002120 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002121 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002122 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002123 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002124}
2125
2126static void nfs4_open_confirm_release(void *calldata)
2127{
2128 struct nfs4_opendata *data = calldata;
2129 struct nfs4_state *state = NULL;
2130
2131 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002132 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002133 goto out_free;
2134 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002135 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002136 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002137 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002138 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002139 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002140out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002141 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002142}
2143
2144static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002145 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002146 .rpc_call_done = nfs4_open_confirm_done,
2147 .rpc_release = nfs4_open_confirm_release,
2148};
2149
2150/*
2151 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2152 */
2153static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2154{
David Howells2b0143b2015-03-17 22:25:59 +00002155 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002156 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002157 struct rpc_message msg = {
2158 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2159 .rpc_argp = &data->c_arg,
2160 .rpc_resp = &data->c_res,
2161 .rpc_cred = data->owner->so_cred,
2162 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002163 struct rpc_task_setup task_setup_data = {
2164 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002165 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002166 .callback_ops = &nfs4_open_confirm_ops,
2167 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002168 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002169 .flags = RPC_TASK_ASYNC,
2170 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002171 int status;
2172
Anna Schumakerfba83f32018-05-04 16:22:50 -04002173 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1,
2174 data->is_recover);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002175 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002176 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002177 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002178 data->timestamp = jiffies;
Trond Myklebustc970aa82007-07-14 15:39:59 -04002179 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002180 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002181 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002182 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002183 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002184 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002185 smp_wmb();
2186 } else
2187 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002188 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002189 return status;
2190}
2191
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002192static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002193{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002194 struct nfs4_opendata *data = calldata;
2195 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002196 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002197 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002198
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002199 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002200 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002201 /*
2202 * Check if we still need to send an OPEN call, or if we can use
2203 * a delegation instead.
2204 */
2205 if (data->state != NULL) {
2206 struct nfs_delegation *delegation;
2207
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002208 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002209 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002210 rcu_read_lock();
2211 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002212 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002213 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002214 rcu_read_unlock();
2215 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002216 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002217 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002218 switch (claim) {
2219 default:
2220 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002221 case NFS4_OPEN_CLAIM_PREVIOUS:
2222 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2223 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002224 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002225 case NFS4_OPEN_CLAIM_FH:
2226 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002227 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002228 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002229 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002230 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002231 &data->o_res.seq_res,
2232 task) != 0)
2233 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002234
2235 /* Set the create mode (note dependency on the session type) */
2236 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2237 if (data->o_arg.open_flags & O_EXCL) {
2238 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2239 if (nfs4_has_persistent_session(clp))
2240 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2241 else if (clp->cl_mvops->minor_version > 0)
2242 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2243 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002244 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002245unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002246 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002247 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002248out_no_action:
2249 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002250out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002251 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002252}
2253
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002254static void nfs4_open_done(struct rpc_task *task, void *calldata)
2255{
2256 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002257
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002258 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002259
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002260 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002261 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002262
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002263 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002264 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2265 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002266 case S_IFREG:
2267 break;
2268 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002269 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002270 break;
2271 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002272 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002273 break;
2274 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002275 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002276 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002277 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002278 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002279 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2280 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002281 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002282 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002283}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002284
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002285static void nfs4_open_release(void *calldata)
2286{
2287 struct nfs4_opendata *data = calldata;
2288 struct nfs4_state *state = NULL;
2289
2290 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002291 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002292 goto out_free;
2293 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002294 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295 goto out_free;
2296 /* In case we need an open_confirm, no cleanup! */
2297 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2298 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002299 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002300 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002301 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002302out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002303 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002304}
2305
2306static const struct rpc_call_ops nfs4_open_ops = {
2307 .rpc_call_prepare = nfs4_open_prepare,
2308 .rpc_call_done = nfs4_open_done,
2309 .rpc_release = nfs4_open_release,
2310};
2311
Fred Isaman3b65a302016-09-19 10:06:49 -04002312static int nfs4_run_open_task(struct nfs4_opendata *data,
2313 struct nfs_open_context *ctx)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002314{
David Howells2b0143b2015-03-17 22:25:59 +00002315 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002316 struct nfs_server *server = NFS_SERVER(dir);
2317 struct nfs_openargs *o_arg = &data->o_arg;
2318 struct nfs_openres *o_res = &data->o_res;
2319 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002320 struct rpc_message msg = {
2321 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2322 .rpc_argp = o_arg,
2323 .rpc_resp = o_res,
2324 .rpc_cred = data->owner->so_cred,
2325 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002326 struct rpc_task_setup task_setup_data = {
2327 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002328 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002329 .callback_ops = &nfs4_open_ops,
2330 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002331 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002332 .flags = RPC_TASK_ASYNC,
2333 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002334 int status;
2335
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002336 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002337 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002338 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002339 data->cancelled = false;
2340 data->is_recover = false;
Fred Isaman3b65a302016-09-19 10:06:49 -04002341 if (!ctx) {
2342 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 1);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002343 data->is_recover = true;
Fred Isaman2409a972016-10-06 12:11:21 -04002344 } else {
Fred Isaman3b65a302016-09-19 10:06:49 -04002345 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1, 0);
Fred Isaman2409a972016-10-06 12:11:21 -04002346 pnfs_lgopen_prepare(data, ctx);
2347 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002348 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002349 if (IS_ERR(task))
2350 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002351 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002352 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002353 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002354 smp_wmb();
2355 } else
2356 status = data->rpc_status;
2357 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002358
2359 return status;
2360}
2361
2362static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2363{
David Howells2b0143b2015-03-17 22:25:59 +00002364 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002365 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002366 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002367
Fred Isaman3b65a302016-09-19 10:06:49 -04002368 status = nfs4_run_open_task(data, NULL);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002369 if (status != 0 || !data->rpc_done)
2370 return status;
2371
Trond Myklebust6926afd12012-01-07 13:22:46 -05002372 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2373
Anna Schumakerd7e98252017-01-11 16:13:29 -05002374 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002375 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002376
2377 return status;
2378}
2379
Trond Myklebustf3792d62014-07-10 08:54:32 -04002380/*
2381 * Additional permission checks in order to distinguish between an
2382 * open for read, and an open for execute. This works around the
2383 * fact that NFSv4 OPEN treats read and execute permissions as being
2384 * the same.
2385 * Note that in the non-execute case, we want to turn off permission
2386 * checking if we just created a new file (POSIX open() semantics).
2387 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002388static int nfs4_opendata_access(struct rpc_cred *cred,
2389 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002390 struct nfs4_state *state, fmode_t fmode,
2391 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002392{
2393 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002394 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002395
2396 /* access call failed or for some reason the server doesn't
2397 * support any access modes -- defer access call until later */
2398 if (opendata->o_res.access_supported == 0)
2399 return 0;
2400
2401 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002402 /*
2403 * Use openflags to check for exec, because fmode won't
2404 * always have FMODE_EXEC set when file open for exec.
2405 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002406 if (openflags & __FMODE_EXEC) {
2407 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002408 if (S_ISDIR(state->inode->i_mode))
2409 mask = NFS4_ACCESS_LOOKUP;
2410 else
2411 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002412 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002413 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002414
2415 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002416 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2417 nfs_access_add_cache(state->inode, &cache);
2418
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002419 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2420 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002421 return 0;
2422
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002423 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002424}
2425
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002426/*
2427 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2428 */
Fred Isaman3b65a302016-09-19 10:06:49 -04002429static int _nfs4_proc_open(struct nfs4_opendata *data,
2430 struct nfs_open_context *ctx)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002431{
David Howells2b0143b2015-03-17 22:25:59 +00002432 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002433 struct nfs_server *server = NFS_SERVER(dir);
2434 struct nfs_openargs *o_arg = &data->o_arg;
2435 struct nfs_openres *o_res = &data->o_res;
2436 int status;
2437
Fred Isaman3b65a302016-09-19 10:06:49 -04002438 status = nfs4_run_open_task(data, ctx);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002439 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002440 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002441 if (status != 0) {
2442 if (status == -NFS4ERR_BADNAME &&
2443 !(o_arg->open_flags & O_CREAT))
2444 return -ENOENT;
2445 return status;
2446 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002447
Trond Myklebust6926afd12012-01-07 13:22:46 -05002448 nfs_fattr_map_and_free_names(server, &data->f_attr);
2449
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002450 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002451 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002452 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002453 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002454 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002455 if (data->file_created ||
2456 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002457 update_changeattr(dir, &o_res->cinfo,
2458 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002459 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002460 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2461 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002462 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002463 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002464 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002465 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002466 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002467 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2468 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002469 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002470 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002471 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472}
2473
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474/*
2475 * OPEN_EXPIRED:
2476 * reclaim state on the server after a network partition.
2477 * Assumes caller holds the appropriate lock
2478 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002479static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002481 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002482 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002483
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002484 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002485 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002486 if (IS_ERR(opendata))
2487 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002488 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002489 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002490 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002491 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002492 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002493}
2494
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002495static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002496{
Trond Myklebust539cd032007-06-05 11:46:42 -04002497 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002498 struct nfs4_exception exception = { };
2499 int err;
2500
2501 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002502 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002503 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002504 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2505 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002506 switch (err) {
2507 default:
2508 goto out;
2509 case -NFS4ERR_GRACE:
2510 case -NFS4ERR_DELAY:
2511 nfs4_handle_exception(server, err, &exception);
2512 err = 0;
2513 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002514 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002515out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002516 return err;
2517}
2518
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2520{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002522 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002523
Trond Myklebust864472e2006-01-03 09:55:15 +01002524 ctx = nfs4_state_find_open_context(state);
2525 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002526 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002527 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002528 put_nfs_open_context(ctx);
2529 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002530}
2531
Trond Myklebust41020b62016-09-22 13:38:58 -04002532static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2533 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002534{
Trond Myklebust41020b62016-09-22 13:38:58 -04002535 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002536 write_seqlock(&state->seqlock);
2537 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2538 write_sequnlock(&state->seqlock);
2539 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2540}
2541
2542static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2543{
2544 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002545 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002546}
2547
2548static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2549{
2550 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2551 nfs40_clear_delegation_stateid(state);
2552 return nfs4_open_expired(sp, state);
2553}
2554
Trond Myklebust45870d62016-09-22 13:38:59 -04002555static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2556 nfs4_stateid *stateid,
2557 struct rpc_cred *cred)
2558{
2559 return -NFS4ERR_BAD_STATEID;
2560}
2561
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002562#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002563static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2564 nfs4_stateid *stateid,
2565 struct rpc_cred *cred)
2566{
2567 int status;
2568
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002569 switch (stateid->type) {
2570 default:
2571 break;
2572 case NFS4_INVALID_STATEID_TYPE:
2573 case NFS4_SPECIAL_STATEID_TYPE:
2574 return -NFS4ERR_BAD_STATEID;
2575 case NFS4_REVOKED_STATEID_TYPE:
2576 goto out_free;
2577 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002578
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002579 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002580 switch (status) {
2581 case -NFS4ERR_EXPIRED:
2582 case -NFS4ERR_ADMIN_REVOKED:
2583 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002584 break;
2585 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002586 return status;
2587 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002588out_free:
2589 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002590 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002591 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002592}
2593
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002594static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002595{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002596 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002597 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002598 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002599 struct rpc_cred *cred;
2600 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002601
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002602 /* Get the delegation credential for use by test/free_stateid */
2603 rcu_read_lock();
2604 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002605 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002606 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002607 return;
2608 }
2609
2610 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002611 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2612 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2613 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002614 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002615 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002616 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002617 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002618
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002619 cred = get_rpccred(delegation->cred);
2620 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002621 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002622 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002623 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002624 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002625
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002626 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002627}
2628
2629/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002630 * nfs41_check_expired_locks - possibly free a lock stateid
2631 *
2632 * @state: NFSv4 state for an inode
2633 *
2634 * Returns NFS_OK if recovery for this stateid is now finished.
2635 * Otherwise a negative NFS4ERR value is returned.
2636 */
2637static int nfs41_check_expired_locks(struct nfs4_state *state)
2638{
2639 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002640 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002641 struct nfs_server *server = NFS_SERVER(state->inode);
2642
2643 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2644 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002645
2646 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002647 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2648 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2649 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2650
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002651 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002652 spin_unlock(&state->state_lock);
2653
2654 nfs4_put_lock_state(prev);
2655 prev = lsp;
2656
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002657 status = nfs41_test_and_free_expired_stateid(server,
2658 &lsp->ls_stateid,
2659 cred);
2660 trace_nfs4_test_lock_stateid(state, lsp, status);
2661 if (status == -NFS4ERR_EXPIRED ||
2662 status == -NFS4ERR_BAD_STATEID) {
2663 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002664 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002665 if (!recover_lost_locks)
2666 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2667 } else if (status != NFS_OK) {
2668 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002669 nfs4_put_lock_state(prev);
2670 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002671 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002672 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002673 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002674 }
2675 spin_unlock(&state->state_lock);
2676 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002677out:
2678 return ret;
2679}
2680
2681/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002682 * nfs41_check_open_stateid - possibly free an open stateid
2683 *
2684 * @state: NFSv4 state for an inode
2685 *
2686 * Returns NFS_OK if recovery for this stateid is now finished.
2687 * Otherwise a negative NFS4ERR value is returned.
2688 */
2689static int nfs41_check_open_stateid(struct nfs4_state *state)
2690{
2691 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002692 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002693 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002694 int status;
2695
Trond Myklebustb134fc42016-09-22 13:39:16 -04002696 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002697 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2698 if (nfs4_have_delegation(state->inode, state->state))
2699 return NFS_OK;
2700 return -NFS4ERR_OPENMODE;
2701 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002702 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002703 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002704 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002705 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002706 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002707 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2708 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2709 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002710 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002711 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002712 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002713 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002714 if (nfs_open_stateid_recover_openmode(state))
2715 return -NFS4ERR_OPENMODE;
2716 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002717}
2718
2719static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2720{
Chuck Levereb64cf92012-07-11 16:30:05 -04002721 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002722
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002723 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002724 status = nfs41_check_expired_locks(state);
2725 if (status != NFS_OK)
2726 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002727 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002728 if (status != NFS_OK)
2729 status = nfs4_open_expired(sp, state);
2730 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002731}
2732#endif
2733
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002735 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2736 * fields corresponding to attributes that were used to store the verifier.
2737 * Make sure we clobber those fields in the later setattr call
2738 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002739static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002740 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002741{
Trond Myklebust609339c2018-03-28 16:18:17 -04002742 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2743 __u32 attrset[3];
2744 unsigned ret;
2745 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002746
Trond Myklebust609339c2018-03-28 16:18:17 -04002747 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2748 attrset[i] = opendata->o_res.attrset[i];
2749 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2750 attrset[i] &= ~bitmask[i];
2751 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002752
Trond Myklebust609339c2018-03-28 16:18:17 -04002753 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2754 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002755
Trond Myklebust609339c2018-03-28 16:18:17 -04002756 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2757 if (sattr->ia_valid & ATTR_ATIME_SET)
2758 ret |= ATTR_ATIME_SET;
2759 else
2760 ret |= ATTR_ATIME;
2761 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002762
Trond Myklebust609339c2018-03-28 16:18:17 -04002763 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2764 if (sattr->ia_valid & ATTR_MTIME_SET)
2765 ret |= ATTR_MTIME_SET;
2766 else
2767 ret |= ATTR_MTIME;
2768 }
2769
2770 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002771 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002772 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002773}
2774
Trond Myklebustc21443c2013-02-07 14:26:21 -05002775static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2776 fmode_t fmode,
2777 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002778 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002779{
2780 struct nfs4_state_owner *sp = opendata->owner;
2781 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002782 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002783 struct nfs4_state *state;
2784 unsigned int seq;
2785 int ret;
2786
2787 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2788
Fred Isaman3b65a302016-09-19 10:06:49 -04002789 ret = _nfs4_proc_open(opendata, ctx);
Trond Myklebustdca780012014-10-23 19:23:03 +03002790 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002791 goto out;
2792
2793 state = nfs4_opendata_to_nfs4_state(opendata);
2794 ret = PTR_ERR(state);
2795 if (IS_ERR(state))
2796 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002797 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002798 if (server->caps & NFS_CAP_POSIX_LOCK)
2799 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002800 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2801 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002802
Trond Myklebust275bb302013-05-29 13:11:28 -04002803 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002804 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002805 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002806 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002807 alias = d_exact_alias(dentry, state->inode);
2808 if (!alias)
2809 alias = d_splice_alias(igrab(state->inode), dentry);
2810 /* d_splice_alias() can't fail here - it's a non-directory */
2811 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002812 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002813 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002814 }
2815 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002816 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002817 }
2818
Trond Myklebustc21443c2013-02-07 14:26:21 -05002819 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2820 if (ret != 0)
2821 goto out;
2822
David Howells2b0143b2015-03-17 22:25:59 +00002823 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002824 nfs_inode_attach_open_context(ctx);
2825 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2826 nfs4_schedule_stateid_recovery(server, state);
Fred Isaman2409a972016-10-06 12:11:21 -04002827 else
2828 pnfs_parse_lgopen(state->inode, opendata->lgp, ctx);
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002829 }
Fred Isaman2409a972016-10-06 12:11:21 -04002830
Trond Myklebustc21443c2013-02-07 14:26:21 -05002831out:
2832 return ret;
2833}
2834
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002835/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002836 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002837 */
Andy Adamson82be4172012-05-23 05:02:35 -04002838static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002839 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002840 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002841 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002842 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843{
2844 struct nfs4_state_owner *sp;
2845 struct nfs4_state *state = NULL;
2846 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002847 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002848 struct dentry *dentry = ctx->dentry;
2849 struct rpc_cred *cred = ctx->cred;
2850 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2851 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002852 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002853 struct iattr *sattr = c->sattr;
2854 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002855 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002856 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002857
2858 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002859 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002860 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2861 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002862 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2863 goto out_err;
2864 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002865 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002866 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002867 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002868 if (d_really_is_positive(dentry))
2869 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002870 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002871 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002872 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002873 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2874 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002875 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002876 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002877
David Quigley14c43f72013-05-22 12:50:43 -04002878 if (label) {
2879 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2880 if (IS_ERR(olabel)) {
2881 status = PTR_ERR(olabel);
2882 goto err_opendata_put;
2883 }
2884 }
2885
Trond Myklebuste911b812014-03-26 13:24:37 -07002886 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2887 if (!opendata->f_attr.mdsthreshold) {
2888 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2889 if (!opendata->f_attr.mdsthreshold)
2890 goto err_free_label;
2891 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002892 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002893 }
David Howells2b0143b2015-03-17 22:25:59 +00002894 if (d_really_is_positive(dentry))
2895 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002896
Trond Myklebust3efb9722013-05-29 13:17:04 -04002897 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002898 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002899 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002900 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002901
NeilBrownefcbc042015-07-30 13:00:56 +10002902 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002903 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002904 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002905 /*
2906 * send create attributes which was not set by open
2907 * with an extra setattr.
2908 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002909 if (attrs || label) {
2910 unsigned ia_old = sattr->ia_valid;
2911
2912 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002913 nfs_fattr_init(opendata->o_res.f_attr);
2914 status = nfs4_do_setattr(state->inode, cred,
2915 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002916 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002917 if (status == 0) {
2918 nfs_setattr_update_inode(state->inode, sattr,
2919 opendata->o_res.f_attr);
2920 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2921 }
Trond Myklebust609339c2018-03-28 16:18:17 -04002922 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04002923 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002924 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002925 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002926 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002927
Trond Myklebuste911b812014-03-26 13:24:37 -07002928 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002929 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002930 opendata->f_attr.mdsthreshold = NULL;
2931 }
Andy Adamson82be4172012-05-23 05:02:35 -04002932
David Quigley14c43f72013-05-22 12:50:43 -04002933 nfs4_label_free(olabel);
2934
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002935 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002936 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002937 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002938err_free_label:
2939 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002940err_opendata_put:
2941 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002942err_put_state_owner:
2943 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002944out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 return status;
2946}
2947
2948
Andy Adamson82be4172012-05-23 05:02:35 -04002949static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002950 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002951 int flags,
2952 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002953 struct nfs4_label *label,
2954 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002956 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002957 struct nfs4_exception exception = { };
2958 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002959 struct nfs4_open_createattrs c = {
2960 .label = label,
2961 .sattr = sattr,
2962 .verf = {
2963 [0] = (__u32)jiffies,
2964 [1] = (__u32)current->pid,
2965 },
2966 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002967 int status;
2968
2969 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002970 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002971 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002972 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002973 if (status == 0)
2974 break;
2975 /* NOTE: BAD_SEQID means the server and client disagree about the
2976 * book-keeping w.r.t. state-changing operations
2977 * (OPEN/CLOSE/LOCK/LOCKU...)
2978 * It is actually a sign of a bug on the client or on the server.
2979 *
2980 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002981 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002982 * have unhashed the old state_owner for us, and that we can
2983 * therefore safely retry using a new one. We should still warn
2984 * the user though...
2985 */
2986 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002987 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002988 " returned a bad sequence-id error!\n",
2989 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002990 exception.retry = 1;
2991 continue;
2992 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002993 /*
2994 * BAD_STATEID on OPEN means that the server cancelled our
2995 * state before it received the OPEN_CONFIRM.
2996 * Recover by retrying the request as per the discussion
2997 * on Page 181 of RFC3530.
2998 */
2999 if (status == -NFS4ERR_BAD_STATEID) {
3000 exception.retry = 1;
3001 continue;
3002 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003003 if (status == -EAGAIN) {
3004 /* We must have found a delegation */
3005 exception.retry = 1;
3006 continue;
3007 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003008 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3009 continue;
3010 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003011 status, &exception));
3012 } while (exception.retry);
3013 return res;
3014}
3015
Trond Myklebust8487c472016-06-26 08:44:35 -04003016static int _nfs4_do_setattr(struct inode *inode,
3017 struct nfs_setattrargs *arg,
3018 struct nfs_setattrres *res,
3019 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003020 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003021{
3022 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003023 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003024 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3025 .rpc_argp = arg,
3026 .rpc_resp = res,
3027 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003028 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003029 struct rpc_cred *delegation_cred = NULL;
3030 unsigned long timestamp = jiffies;
Trond Myklebust8487c472016-06-26 08:44:35 -04003031 bool truncate;
3032 int status;
3033
3034 nfs_fattr_init(res->fattr);
3035
3036 /* Servers should only apply open mode checks for file size changes */
3037 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003038 if (!truncate)
3039 goto zero_stateid;
Trond Myklebust8487c472016-06-26 08:44:35 -04003040
Trond Myklebust991eedb2018-04-09 11:15:30 -04003041 if (nfs4_copy_delegation_stateid(inode, FMODE_WRITE, &arg->stateid, &delegation_cred)) {
Trond Myklebust8487c472016-06-26 08:44:35 -04003042 /* Use that stateid */
Trond Myklebust991eedb2018-04-09 11:15:30 -04003043 } else if (ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003044 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003045 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003046 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003047 l_ctx = nfs_get_lock_context(ctx);
3048 if (IS_ERR(l_ctx))
3049 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003050 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3051 &arg->stateid, &delegation_cred);
3052 nfs_put_lock_context(l_ctx);
3053 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003054 return -EBADF;
Trond Myklebust991eedb2018-04-09 11:15:30 -04003055 } else {
3056zero_stateid:
Trond Myklebust8487c472016-06-26 08:44:35 -04003057 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
Trond Myklebust991eedb2018-04-09 11:15:30 -04003058 }
Trond Myklebust8487c472016-06-26 08:44:35 -04003059 if (delegation_cred)
3060 msg.rpc_cred = delegation_cred;
3061
3062 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3063
3064 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003065 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003066 renew_lease(server, timestamp);
3067 trace_nfs4_setattr(inode, &arg->stateid, status);
3068 return status;
3069}
3070
3071static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3072 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003073 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003074 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003075{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003076 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003077 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003078 struct nfs_setattrargs arg = {
3079 .fh = NFS_FH(inode),
3080 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003081 .server = server,
3082 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003083 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003084 };
3085 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003086 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003087 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003088 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003089 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003090 struct nfs4_exception exception = {
3091 .state = state,
3092 .inode = inode,
3093 .stateid = &arg.stateid,
3094 };
3095 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003096
David Quigleyaa9c2662013-05-22 12:50:44 -04003097 arg.bitmask = nfs4_bitmask(server, ilabel);
3098 if (ilabel)
3099 arg.bitmask = nfs4_bitmask(server, olabel);
3100
Linus Torvalds1da177e2005-04-16 15:20:36 -07003101 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003102 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003103 switch (err) {
3104 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003105 if (!(sattr->ia_valid & ATTR_SIZE)) {
3106 pr_warn_once("NFSv4: server %s is incorrectly "
3107 "applying open mode checks to "
3108 "a SETATTR that is not "
3109 "changing file size.\n",
3110 server->nfs_client->cl_hostname);
3111 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003112 if (state && !(state->state & FMODE_WRITE)) {
3113 err = -EBADF;
3114 if (sattr->ia_valid & ATTR_OPEN)
3115 err = -EACCES;
3116 goto out;
3117 }
3118 }
3119 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003121out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003122 return err;
3123}
3124
Peng Tao500d7012015-09-22 11:35:22 +08003125static bool
3126nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3127{
3128 if (inode == NULL || !nfs_have_layout(inode))
3129 return false;
3130
3131 return pnfs_wait_on_layoutreturn(inode, task);
3132}
3133
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134struct nfs4_closedata {
3135 struct inode *inode;
3136 struct nfs4_state *state;
3137 struct nfs_closeargs arg;
3138 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003139 struct {
3140 struct nfs4_layoutreturn_args arg;
3141 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003142 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003143 u32 roc_barrier;
3144 bool roc;
3145 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003146 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003147 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003148};
3149
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003150static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003151{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003152 struct nfs4_closedata *calldata = data;
3153 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003154 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003155
Trond Myklebustcf805162016-11-15 14:56:07 -05003156 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003157 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3158 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003159 nfs4_put_open_state(calldata->state);
3160 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003161 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003162 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003163 kfree(calldata);
3164}
3165
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003166static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003167{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003168 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003169 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003170 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003171 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003172 struct nfs4_exception exception = {
3173 .state = state,
3174 .inode = calldata->inode,
3175 .stateid = &calldata->arg.stateid,
3176 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003177
Chuck Levera3ca5652012-03-01 17:00:40 -05003178 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003179 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3180 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003181 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003182
3183 /* Handle Layoutreturn errors */
3184 if (calldata->arg.lr_args && task->tk_status != 0) {
3185 switch (calldata->res.lr_ret) {
3186 default:
3187 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3188 break;
3189 case 0:
3190 calldata->arg.lr_args = NULL;
3191 calldata->res.lr_res = NULL;
3192 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003193 case -NFS4ERR_OLD_STATEID:
3194 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003195 calldata->inode))
3196 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003197 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003198 case -NFS4ERR_ADMIN_REVOKED:
3199 case -NFS4ERR_DELEG_REVOKED:
3200 case -NFS4ERR_EXPIRED:
3201 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003202 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3203 case -NFS4ERR_WRONG_CRED:
3204 calldata->arg.lr_args = NULL;
3205 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003206 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003207 }
3208 }
3209
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003210 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003211 * the state_owner. we keep this around to process errors
3212 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003213 switch (task->tk_status) {
3214 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003215 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003216 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003217 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003218 case -NFS4ERR_ACCESS:
3219 if (calldata->arg.bitmask != NULL) {
3220 calldata->arg.bitmask = NULL;
3221 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003222 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003223
3224 }
3225 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003226 case -NFS4ERR_OLD_STATEID:
3227 /* Did we race with OPEN? */
3228 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003229 state))
3230 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003231 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003232 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003233 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003234 case -NFS4ERR_EXPIRED:
3235 nfs4_free_revoked_stateid(server,
3236 &calldata->arg.stateid,
3237 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003238 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003239 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003240 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003241 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003242 task->tk_status = nfs4_async_handle_exception(task,
3243 server, task->tk_status, &exception);
3244 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003245 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003246 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003247 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3248 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003249out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003250 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003251 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003252 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003253 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003254 return;
3255lr_restart:
3256 calldata->res.lr_ret = 0;
3257out_restart:
3258 task->tk_status = 0;
3259 rpc_restart_call_prepare(task);
3260 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003261}
3262
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003263static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003264{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003265 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003266 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003267 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003268 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003269 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003270
Chuck Levera3ca5652012-03-01 17:00:40 -05003271 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003272 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003273 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003274
Trond Myklebust88069f72009-12-08 08:33:16 -05003275 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003276 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003277 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3278 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3279 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003280 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003281 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003282 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003283 if (state->n_rdonly == 0)
3284 call_close |= is_rdonly;
3285 else if (is_rdonly)
3286 calldata->arg.fmode |= FMODE_READ;
3287 if (state->n_wronly == 0)
3288 call_close |= is_wronly;
3289 else if (is_wronly)
3290 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003291 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3292 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003293 } else if (is_rdwr)
3294 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3295
Trond Myklebust5cc78612016-11-14 11:19:56 -05003296 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003297 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003298 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003299 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003300
3301 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003302 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003303 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003304 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003305
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003306 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003307 nfs_release_seqid(calldata->arg.seqid);
3308 goto out_wait;
3309 }
3310
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003311 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003312 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003313
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003314 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003315 /* Close-to-open cache consistency revalidation */
3316 if (!nfs4_have_delegation(inode, FMODE_READ))
3317 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3318 else
3319 calldata->arg.bitmask = NULL;
3320 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003321
Trond Myklebust6ae37332015-01-30 14:21:14 -05003322 calldata->arg.share_access =
3323 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3324 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003325
Trond Myklebustd8d84982016-12-19 12:14:44 -05003326 if (calldata->res.fattr == NULL)
3327 calldata->arg.bitmask = NULL;
3328 else if (calldata->arg.bitmask == NULL)
3329 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003330 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003331 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003332 &calldata->arg.seq_args,
3333 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003334 task) != 0)
3335 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003336 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003337 return;
3338out_no_action:
3339 task->tk_action = NULL;
3340out_wait:
3341 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003342}
3343
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003344static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003345 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003346 .rpc_call_done = nfs4_close_done,
3347 .rpc_release = nfs4_free_closedata,
3348};
3349
Linus Torvalds1da177e2005-04-16 15:20:36 -07003350/*
3351 * It is possible for data to be read/written from a mem-mapped file
3352 * after the sys_close call (which hits the vfs layer as a flush).
3353 * This means that we can't safely call nfsv4 close on a file until
3354 * the inode is cleared. This in turn means that we are not good
3355 * NFSv4 citizens - we do not indicate to the server to update the file's
3356 * share state even when we are done with one of the three share
3357 * stateid's in the inode.
3358 *
3359 * NOTE: Caller must be holding the sp->so_owner semaphore!
3360 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003361int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003362{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003363 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003364 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003365 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003366 struct nfs4_state_owner *sp = state->owner;
3367 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003368 struct rpc_message msg = {
3369 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3370 .rpc_cred = state->owner->so_cred,
3371 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003372 struct rpc_task_setup task_setup_data = {
3373 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003374 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003375 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003376 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003377 .flags = RPC_TASK_ASYNC,
3378 };
Trond Myklebust95121352005-10-18 14:20:12 -07003379 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003380
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003381 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3382 &task_setup_data.rpc_client, &msg);
3383
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003384 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003385 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003386 goto out;
Anna Schumakerfba83f32018-05-04 16:22:50 -04003387 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1, 0);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003388 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003390 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003391 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3392 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003393 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003394 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3395 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003396 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003397 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003398 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003399 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003400 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003401 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003402 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003403 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003404 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003405 calldata->lr.roc = pnfs_roc(state->inode,
3406 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3407 if (calldata->lr.roc) {
3408 calldata->arg.lr_args = &calldata->lr.arg;
3409 calldata->res.lr_res = &calldata->lr.res;
3410 }
Al Viro643168c2011-06-22 18:20:23 -04003411 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003412
Trond Myklebust1174dd12010-12-21 10:52:24 -05003413 msg.rpc_argp = &calldata->arg;
3414 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003415 task_setup_data.callback_data = calldata;
3416 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003417 if (IS_ERR(task))
3418 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003419 status = 0;
3420 if (wait)
3421 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003422 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003423 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003424out_free_calldata:
3425 kfree(calldata);
3426out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003427 nfs4_put_open_state(state);
3428 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003429 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003430}
3431
Trond Myklebust2b484292010-09-17 10:56:51 -04003432static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003433nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3434 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003436 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003437 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3438
3439 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003440
Trond Myklebust565277f2007-10-15 18:17:53 -04003441 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003442 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003443
3444 nfs4_label_release_security(label);
3445
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003446 if (IS_ERR(state))
3447 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003448 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449}
3450
Trond Myklebust1185a552009-12-03 15:54:02 -05003451static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003452{
3453 if (ctx->state == NULL)
3454 return;
3455 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003456 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003457 else
Al Viro643168c2011-06-22 18:20:23 -04003458 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003459}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003460
Trond Myklebustb944dba2013-11-04 15:20:20 -05003461#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3462#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003463#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003464
Linus Torvalds1da177e2005-04-16 15:20:36 -07003465static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3466{
Kinglong Mee8c612822015-08-26 21:12:58 +08003467 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003468 struct nfs4_server_caps_arg args = {
3469 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003470 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003471 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003472 struct nfs4_server_caps_res res = {};
3473 struct rpc_message msg = {
3474 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003475 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 .rpc_resp = &res,
3477 };
3478 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003479 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003480
Kinglong Mee8c612822015-08-26 21:12:58 +08003481 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3482 FATTR4_WORD0_FH_EXPIRE_TYPE |
3483 FATTR4_WORD0_LINK_SUPPORT |
3484 FATTR4_WORD0_SYMLINK_SUPPORT |
3485 FATTR4_WORD0_ACLSUPPORT;
3486 if (minorversion)
3487 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3488
Bryan Schumaker7c513052011-03-24 17:12:24 +00003489 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003490 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003491 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003492 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003493 case 0:
3494 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3495 res.attr_bitmask[2] = 0;
3496 break;
3497 case 1:
3498 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3499 break;
3500 case 2:
3501 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3502 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003503 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003504 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3505 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3506 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3507 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003508 NFS_CAP_CTIME|NFS_CAP_MTIME|
3509 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003510 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3511 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 server->caps |= NFS_CAP_ACLS;
3513 if (res.has_links != 0)
3514 server->caps |= NFS_CAP_HARDLINKS;
3515 if (res.has_symlinks != 0)
3516 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003517 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3518 server->caps |= NFS_CAP_FILEID;
3519 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3520 server->caps |= NFS_CAP_MODE;
3521 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3522 server->caps |= NFS_CAP_NLINK;
3523 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3524 server->caps |= NFS_CAP_OWNER;
3525 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3526 server->caps |= NFS_CAP_OWNER_GROUP;
3527 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3528 server->caps |= NFS_CAP_ATIME;
3529 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3530 server->caps |= NFS_CAP_CTIME;
3531 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3532 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003533#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3534 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3535 server->caps |= NFS_CAP_SECURITY_LABEL;
3536#endif
3537 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3538 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003539 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003540
Trond Myklebusta65318b2009-03-11 14:10:28 -04003541 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3542 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3543 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003544 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003545
3546 /* Avoid a regression due to buggy server */
3547 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3548 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003549 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3550 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003551
Linus Torvalds1da177e2005-04-16 15:20:36 -07003552 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003553 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003554 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003555
Linus Torvalds1da177e2005-04-16 15:20:36 -07003556 return status;
3557}
3558
Trond Myklebust55a97592006-06-09 09:34:19 -04003559int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003560{
3561 struct nfs4_exception exception = { };
3562 int err;
3563 do {
3564 err = nfs4_handle_exception(server,
3565 _nfs4_server_capabilities(server, fhandle),
3566 &exception);
3567 } while (exception.retry);
3568 return err;
3569}
3570
3571static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3572 struct nfs_fsinfo *info)
3573{
David Quigleyaa9c2662013-05-22 12:50:44 -04003574 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003575 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003576 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003577 };
3578 struct nfs4_lookup_res res = {
3579 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003580 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003581 .fh = fhandle,
3582 };
3583 struct rpc_message msg = {
3584 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3585 .rpc_argp = &args,
3586 .rpc_resp = &res,
3587 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003588
David Quigleyaa9c2662013-05-22 12:50:44 -04003589 bitmask[0] = nfs4_fattr_bitmap[0];
3590 bitmask[1] = nfs4_fattr_bitmap[1];
3591 /*
3592 * Process the label in the upcoming getfattr
3593 */
3594 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3595
Trond Myklebust0e574af2005-10-27 22:12:38 -04003596 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003597 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003598}
3599
3600static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3601 struct nfs_fsinfo *info)
3602{
3603 struct nfs4_exception exception = { };
3604 int err;
3605 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003606 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003607 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003608 switch (err) {
3609 case 0:
3610 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003611 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003612 default:
3613 err = nfs4_handle_exception(server, err, &exception);
3614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003615 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003616out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003617 return err;
3618}
3619
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003620static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3621 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3622{
Trond Myklebustc2190662013-08-26 19:23:04 -04003623 struct rpc_auth_create_args auth_args = {
3624 .pseudoflavor = flavor,
3625 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003626 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003627
Trond Myklebustc2190662013-08-26 19:23:04 -04003628 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003629 if (IS_ERR(auth))
3630 return -EACCES;
3631 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003632}
3633
Chuck Lever9a744ba2013-03-16 15:56:02 -04003634/*
3635 * Retry pseudoroot lookup with various security flavors. We do this when:
3636 *
3637 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3638 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3639 *
3640 * Returns zero on success, or a negative NFS4ERR value, or a
3641 * negative errno value.
3642 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003643static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003644 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003645{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003646 /* Per 3530bis 15.33.5 */
3647 static const rpc_authflavor_t flav_array[] = {
3648 RPC_AUTH_GSS_KRB5P,
3649 RPC_AUTH_GSS_KRB5I,
3650 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003651 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003652 RPC_AUTH_NULL,
3653 };
3654 int status = -EPERM;
3655 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003656
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003657 if (server->auth_info.flavor_len > 0) {
3658 /* try each flavor specified by user */
3659 for (i = 0; i < server->auth_info.flavor_len; i++) {
3660 status = nfs4_lookup_root_sec(server, fhandle, info,
3661 server->auth_info.flavors[i]);
3662 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3663 continue;
3664 break;
3665 }
3666 } else {
3667 /* no flavors specified by user, try default list */
3668 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3669 status = nfs4_lookup_root_sec(server, fhandle, info,
3670 flav_array[i]);
3671 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3672 continue;
3673 break;
3674 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003675 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003676
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003677 /*
3678 * -EACCESS could mean that the user doesn't have correct permissions
3679 * to access the mount. It could also mean that we tried to mount
3680 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3681 * existing mount programs don't handle -EACCES very well so it should
3682 * be mapped to -EPERM instead.
3683 */
3684 if (status == -EACCES)
3685 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003686 return status;
3687}
3688
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003689/**
3690 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3691 * @server: initialized nfs_server handle
3692 * @fhandle: we fill in the pseudo-fs root file handle
3693 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003694 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003695 *
3696 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003697 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003698int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003699 struct nfs_fsinfo *info,
3700 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003701{
Andre Przywarac7757072015-04-23 17:17:40 +01003702 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003703
Andre Przywarac7757072015-04-23 17:17:40 +01003704 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003705 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003706
3707 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003708 status = server->nfs_client->cl_mvops->find_root_sec(server,
3709 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003710
Linus Torvalds1da177e2005-04-16 15:20:36 -07003711 if (status == 0)
3712 status = nfs4_server_capabilities(server, fhandle);
3713 if (status == 0)
3714 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003715
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003716 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003717}
3718
Bryan Schumakerbae36242012-05-10 15:07:31 -04003719static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3720 struct nfs_fsinfo *info)
3721{
3722 int error;
3723 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003724 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003725
3726 error = nfs4_server_capabilities(server, mntfh);
3727 if (error < 0) {
3728 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3729 return error;
3730 }
3731
David Quigley14c43f72013-05-22 12:50:43 -04003732 label = nfs4_label_alloc(server, GFP_KERNEL);
3733 if (IS_ERR(label))
3734 return PTR_ERR(label);
3735
David Quigley1775fd32013-05-22 12:50:42 -04003736 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003737 if (error < 0) {
3738 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003739 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003740 }
3741
3742 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3743 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3744 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3745
David Quigley14c43f72013-05-22 12:50:43 -04003746err_free_label:
3747 nfs4_label_free(label);
3748
Bryan Schumakerbae36242012-05-10 15:07:31 -04003749 return error;
3750}
3751
Manoj Naik6b97fd32006-06-09 09:34:29 -04003752/*
3753 * Get locations and (maybe) other attributes of a referral.
3754 * Note that we'll actually follow the referral later when
3755 * we detect fsid mismatch in inode revalidation
3756 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003757static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3758 const struct qstr *name, struct nfs_fattr *fattr,
3759 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003760{
3761 int status = -ENOMEM;
3762 struct page *page = NULL;
3763 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003764
3765 page = alloc_page(GFP_KERNEL);
3766 if (page == NULL)
3767 goto out;
3768 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3769 if (locations == NULL)
3770 goto out;
3771
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003772 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003773 if (status != 0)
3774 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003775
3776 /*
3777 * If the fsid didn't change, this is a migration event, not a
3778 * referral. Cause us to drop into the exception handler, which
3779 * will kick off migration recovery.
3780 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003781 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003782 dprintk("%s: server did not return a different fsid for"
3783 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003784 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003785 goto out;
3786 }
Andy Adamson533eb462011-06-13 18:25:56 -04003787 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3788 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003789
Andy Adamson533eb462011-06-13 18:25:56 -04003790 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003791 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003792 memset(fhandle, 0, sizeof(struct nfs_fh));
3793out:
3794 if (page)
3795 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003796 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003797 return status;
3798}
3799
David Quigley1775fd32013-05-22 12:50:42 -04003800static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3801 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003802{
3803 struct nfs4_getattr_arg args = {
3804 .fh = fhandle,
3805 .bitmask = server->attr_bitmask,
3806 };
3807 struct nfs4_getattr_res res = {
3808 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003809 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003810 .server = server,
3811 };
3812 struct rpc_message msg = {
3813 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3814 .rpc_argp = &args,
3815 .rpc_resp = &res,
3816 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003817
3818 args.bitmask = nfs4_bitmask(server, label);
3819
Trond Myklebust0e574af2005-10-27 22:12:38 -04003820 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003821 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003822}
3823
David Quigley1775fd32013-05-22 12:50:42 -04003824static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3825 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003826{
3827 struct nfs4_exception exception = { };
3828 int err;
3829 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003830 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3831 trace_nfs4_getattr(server, fhandle, fattr, err);
3832 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003833 &exception);
3834 } while (exception.retry);
3835 return err;
3836}
3837
3838/*
3839 * The file is not closed if it is opened due to the a request to change
3840 * the size of the file. The open call will not be needed once the
3841 * VFS layer lookup-intents are implemented.
3842 *
3843 * Close is called when the inode is destroyed.
3844 * If we haven't opened the file for O_WRONLY, we
3845 * need to in the size_change case to obtain a stateid.
3846 *
3847 * Got race?
3848 * Because OPEN is always done by name in nfsv4, it is
3849 * possible that we opened a different file by the same
3850 * name. We can recognize this race condition, but we
3851 * can't do anything about it besides returning an error.
3852 *
3853 * This will be fixed with VFS changes (lookup-intent).
3854 */
3855static int
3856nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3857 struct iattr *sattr)
3858{
David Howells2b0143b2015-03-17 22:25:59 +00003859 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003860 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003861 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003862 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003863 int status;
3864
Peng Tao88ac8152014-09-12 11:04:10 +08003865 if (pnfs_ld_layoutret_on_setattr(inode) &&
3866 sattr->ia_valid & ATTR_SIZE &&
3867 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003868 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003869
Trond Myklebust0e574af2005-10-27 22:12:38 -04003870 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003871
Andy Adamson26699402012-05-30 16:12:24 -04003872 /* Deal with open(O_TRUNC) */
3873 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003874 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003875
3876 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003877 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003878 return 0;
3879
Trond Myklebustd5308382005-11-04 15:33:38 -05003880 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003881 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003882
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003883 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003884 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003885 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003886 }
3887
David Quigley14c43f72013-05-22 12:50:43 -04003888 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3889 if (IS_ERR(label))
3890 return PTR_ERR(label);
3891
Trond Myklebust199366f2018-03-20 16:43:18 -04003892 /* Return any delegations if we're going to change ACLs */
3893 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003894 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003895
NeilBrown29b59f92016-10-13 15:26:47 +11003896 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003897 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003898 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003899 nfs_setsecurity(inode, fattr, label);
3900 }
David Quigley14c43f72013-05-22 12:50:43 -04003901 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003902 return status;
3903}
3904
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003905static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3906 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003907 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003908{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003909 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003910 int status;
3911 struct nfs4_lookup_arg args = {
3912 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003913 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003914 .name = name,
3915 };
3916 struct nfs4_lookup_res res = {
3917 .server = server,
3918 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003919 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003920 .fh = fhandle,
3921 };
3922 struct rpc_message msg = {
3923 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3924 .rpc_argp = &args,
3925 .rpc_resp = &res,
3926 };
3927
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 args.bitmask = nfs4_bitmask(server, label);
3929
David Howells2b3de442006-08-22 20:06:09 -04003930 nfs_fattr_init(fattr);
3931
Linus Torvalds1da177e2005-04-16 15:20:36 -07003932 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003933 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003934 dprintk("NFS reply lookup: %d\n", status);
3935 return status;
3936}
3937
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003938static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003939{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003940 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003941 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003942 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3943 fattr->nlink = 2;
3944}
3945
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003946static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003947 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003948 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003949{
3950 struct nfs4_exception exception = { };
3951 struct rpc_clnt *client = *clnt;
3952 int err;
3953 do {
David Quigley1775fd32013-05-22 12:50:42 -04003954 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003955 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003956 switch (err) {
3957 case -NFS4ERR_BADNAME:
3958 err = -ENOENT;
3959 goto out;
3960 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003961 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003962 if (err == -NFS4ERR_MOVED)
3963 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003964 goto out;
3965 case -NFS4ERR_WRONGSEC:
3966 err = -EPERM;
3967 if (client != *clnt)
3968 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003969 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003970 if (IS_ERR(client))
3971 return PTR_ERR(client);
3972
3973 exception.retry = 1;
3974 break;
3975 default:
3976 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3977 }
3978 } while (exception.retry);
3979
3980out:
3981 if (err == 0)
3982 *clnt = client;
3983 else if (client != *clnt)
3984 rpc_shutdown_client(client);
3985
3986 return err;
3987}
3988
Al Virobeffb8f2016-07-20 16:34:42 -04003989static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003990 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3991 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003992{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003993 int status;
3994 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003995
David Quigley1775fd32013-05-22 12:50:42 -04003996 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003997 if (client != NFS_CLIENT(dir)) {
3998 rpc_shutdown_client(client);
3999 nfs_fixup_secinfo_attributes(fattr);
4000 }
4001 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004002}
4003
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004004struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004005nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004006 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4007{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004008 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004009 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004010
David Quigley1775fd32013-05-22 12:50:42 -04004011 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004012 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004013 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004014 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004015}
4016
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004017static int _nfs4_proc_lookupp(struct inode *inode,
4018 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4019 struct nfs4_label *label)
4020{
4021 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4022 struct nfs_server *server = NFS_SERVER(inode);
4023 int status;
4024 struct nfs4_lookupp_arg args = {
4025 .bitmask = server->attr_bitmask,
4026 .fh = NFS_FH(inode),
4027 };
4028 struct nfs4_lookupp_res res = {
4029 .server = server,
4030 .fattr = fattr,
4031 .label = label,
4032 .fh = fhandle,
4033 };
4034 struct rpc_message msg = {
4035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4036 .rpc_argp = &args,
4037 .rpc_resp = &res,
4038 };
4039
4040 args.bitmask = nfs4_bitmask(server, label);
4041
4042 nfs_fattr_init(fattr);
4043
4044 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4045 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4046 &res.seq_res, 0);
4047 dprintk("NFS reply lookupp: %d\n", status);
4048 return status;
4049}
4050
4051static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4052 struct nfs_fattr *fattr, struct nfs4_label *label)
4053{
4054 struct nfs4_exception exception = { };
4055 int err;
4056 do {
4057 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4058 trace_nfs4_lookupp(inode, err);
4059 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4060 &exception);
4061 } while (exception.retry);
4062 return err;
4063}
4064
Linus Torvalds1da177e2005-04-16 15:20:36 -07004065static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4066{
Trond Myklebust76b32992007-08-10 17:45:11 -04004067 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004068 struct nfs4_accessargs args = {
4069 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004070 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004071 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004072 struct nfs4_accessres res = {
4073 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004074 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 struct rpc_message msg = {
4076 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4077 .rpc_argp = &args,
4078 .rpc_resp = &res,
4079 .rpc_cred = entry->cred,
4080 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004081 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004082
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004083 if (!nfs_have_delegated_attributes(inode)) {
4084 res.fattr = nfs_alloc_fattr();
4085 if (res.fattr == NULL)
4086 return -ENOMEM;
4087 args.bitmask = server->cache_consistency_bitmask;
4088 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004089
Bryan Schumaker7c513052011-03-24 17:12:24 +00004090 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004092 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004093 if (res.fattr)
4094 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004095 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004096 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004097 return status;
4098}
4099
4100static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4101{
4102 struct nfs4_exception exception = { };
4103 int err;
4104 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004105 err = _nfs4_proc_access(inode, entry);
4106 trace_nfs4_access(inode, err);
4107 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004108 &exception);
4109 } while (exception.retry);
4110 return err;
4111}
4112
4113/*
4114 * TODO: For the time being, we don't try to get any attributes
4115 * along with any of the zero-copy operations READ, READDIR,
4116 * READLINK, WRITE.
4117 *
4118 * In the case of the first three, we want to put the GETATTR
4119 * after the read-type operation -- this is because it is hard
4120 * to predict the length of a GETATTR response in v4, and thus
4121 * align the READ data correctly. This means that the GETATTR
4122 * may end up partially falling into the page cache, and we should
4123 * shift it into the 'tail' of the xdr_buf before processing.
4124 * To do this efficiently, we need to know the total length
4125 * of data received, which doesn't seem to be available outside
4126 * of the RPC layer.
4127 *
4128 * In the case of WRITE, we also want to put the GETATTR after
4129 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004130 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004131 *
4132 * Both of these changes to the XDR layer would in fact be quite
4133 * minor, but I decided to leave them for a subsequent patch.
4134 */
4135static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4136 unsigned int pgbase, unsigned int pglen)
4137{
4138 struct nfs4_readlink args = {
4139 .fh = NFS_FH(inode),
4140 .pgbase = pgbase,
4141 .pglen = pglen,
4142 .pages = &page,
4143 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004144 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004145 struct rpc_message msg = {
4146 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4147 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004148 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004149 };
4150
Bryan Schumaker7c513052011-03-24 17:12:24 +00004151 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 -07004152}
4153
4154static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4155 unsigned int pgbase, unsigned int pglen)
4156{
4157 struct nfs4_exception exception = { };
4158 int err;
4159 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004160 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4161 trace_nfs4_readlink(inode, err);
4162 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004163 &exception);
4164 } while (exception.retry);
4165 return err;
4166}
4167
Linus Torvalds1da177e2005-04-16 15:20:36 -07004168/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004169 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004170 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004171static int
4172nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004173 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004174{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004175 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004176 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004177 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004178 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 int status = 0;
4180
NeilBrown532d4de2016-10-13 15:26:47 +11004181 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004182 if (IS_ERR(ctx))
4183 return PTR_ERR(ctx);
4184
David Quigleyaa9c2662013-05-22 12:50:44 -04004185 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4186
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004187 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4188 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004189 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004190 if (IS_ERR(state)) {
4191 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004192 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004194out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004195 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004196 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 return status;
4198}
4199
Al Virobeffb8f2016-07-20 16:34:42 -04004200static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004201{
Trond Myklebust16e42952005-10-27 22:12:44 -04004202 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004203 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004205 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004207 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004208 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004209 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004211 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4212 .rpc_argp = &args,
4213 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004214 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004215 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004216 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004217
Bryan Schumaker7c513052011-03-24 17:12:24 +00004218 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004219 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004220 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004221 return status;
4222}
4223
Trond Myklebust912678d2018-03-20 16:43:15 -04004224static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4225{
4226 struct nfs4_exception exception = { };
4227 struct inode *inode = d_inode(dentry);
4228 int err;
4229
Trond Myklebustc01d3642018-03-20 16:43:20 -04004230 if (inode) {
4231 if (inode->i_nlink == 1)
4232 nfs4_inode_return_delegation(inode);
4233 else
4234 nfs4_inode_make_writeable(inode);
4235 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004236 do {
4237 err = _nfs4_proc_remove(dir, &dentry->d_name);
4238 trace_nfs4_remove(dir, &dentry->d_name, err);
4239 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4240 &exception);
4241 } while (exception.retry);
4242 return err;
4243}
4244
4245static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004246{
4247 struct nfs4_exception exception = { };
4248 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004249
Linus Torvalds1da177e2005-04-16 15:20:36 -07004250 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004251 err = _nfs4_proc_remove(dir, name);
4252 trace_nfs4_remove(dir, name, err);
4253 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004254 &exception);
4255 } while (exception.retry);
4256 return err;
4257}
4258
Trond Myklebusted7e9ad2018-05-30 16:11:52 -04004259static void nfs4_proc_unlink_setup(struct rpc_message *msg,
4260 struct dentry *dentry,
4261 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004262{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004263 struct nfs_removeargs *args = msg->rpc_argp;
4264 struct nfs_removeres *res = msg->rpc_resp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004265
Trond Myklebust977fcc22018-03-20 16:43:17 -04004266 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004267 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004268 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04004269
4270 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004271
4272 if (inode)
4273 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004274}
4275
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004276static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4277{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004278 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004279 &data->args.seq_args,
4280 &data->res.seq_res,
4281 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004284static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004285{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004286 struct nfs_unlinkdata *data = task->tk_calldata;
4287 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004288
Trond Myklebust14516c32010-07-31 14:29:06 -04004289 if (!nfs4_sequence_done(task, &res->seq_res))
4290 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004291 if (nfs4_async_handle_error(task, res->server, NULL,
4292 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004293 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004294 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004295 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004296 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004297}
4298
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004299static void nfs4_proc_rename_setup(struct rpc_message *msg,
4300 struct dentry *old_dentry,
4301 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004302{
Jeff Laytond3d41522010-09-17 17:31:57 -04004303 struct nfs_renameargs *arg = msg->rpc_argp;
4304 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004305 struct inode *old_inode = d_inode(old_dentry);
4306 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004307
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004308 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004309 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004310 if (new_inode)
4311 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004312 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004313 res->server = NFS_SB(old_dentry->d_sb);
Anna Schumakerfba83f32018-05-04 16:22:50 -04004314 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1, 0);
Jeff Laytond3d41522010-09-17 17:31:57 -04004315}
4316
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004317static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4318{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004319 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004320 &data->args.seq_args,
4321 &data->res.seq_res,
4322 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004323}
4324
4325static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4326 struct inode *new_dir)
4327{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004328 struct nfs_renamedata *data = task->tk_calldata;
4329 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004330
4331 if (!nfs4_sequence_done(task, &res->seq_res))
4332 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004333 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004334 return 0;
4335
Trond Myklebustc733c492017-01-11 12:32:26 -05004336 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004337 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004338 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004339 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004340 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004341 return 1;
4342}
4343
Al Virobeffb8f2016-07-20 16:34:42 -04004344static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004345{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004346 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004347 struct nfs4_link_arg arg = {
4348 .fh = NFS_FH(inode),
4349 .dir_fh = NFS_FH(dir),
4350 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004351 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004352 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004353 struct nfs4_link_res res = {
4354 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004355 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004356 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004357 struct rpc_message msg = {
4358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4359 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004360 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004361 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004362 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004363
Trond Myklebust136f2622010-04-16 16:22:49 -04004364 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004365 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004366 goto out;
4367
David Quigley14c43f72013-05-22 12:50:43 -04004368 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4369 if (IS_ERR(res.label)) {
4370 status = PTR_ERR(res.label);
4371 goto out;
4372 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004373 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004374
Trond Myklebustc01d3642018-03-20 16:43:20 -04004375 nfs4_inode_make_writeable(inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004376
Bryan Schumaker7c513052011-03-24 17:12:24 +00004377 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004378 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004379 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004380 status = nfs_post_op_update_inode(inode, res.fattr);
4381 if (!status)
4382 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004383 }
David Quigley14c43f72013-05-22 12:50:43 -04004384
4385
4386 nfs4_label_free(res.label);
4387
Trond Myklebust136f2622010-04-16 16:22:49 -04004388out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004389 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004390 return status;
4391}
4392
Al Virobeffb8f2016-07-20 16:34:42 -04004393static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004394{
4395 struct nfs4_exception exception = { };
4396 int err;
4397 do {
4398 err = nfs4_handle_exception(NFS_SERVER(inode),
4399 _nfs4_proc_link(inode, dir, name),
4400 &exception);
4401 } while (exception.retry);
4402 return err;
4403}
4404
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004405struct nfs4_createdata {
4406 struct rpc_message msg;
4407 struct nfs4_create_arg arg;
4408 struct nfs4_create_res res;
4409 struct nfs_fh fh;
4410 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004411 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004412};
4413
4414static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004415 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004416{
4417 struct nfs4_createdata *data;
4418
4419 data = kzalloc(sizeof(*data), GFP_KERNEL);
4420 if (data != NULL) {
4421 struct nfs_server *server = NFS_SERVER(dir);
4422
David Quigley14c43f72013-05-22 12:50:43 -04004423 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4424 if (IS_ERR(data->label))
4425 goto out_free;
4426
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004427 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4428 data->msg.rpc_argp = &data->arg;
4429 data->msg.rpc_resp = &data->res;
4430 data->arg.dir_fh = NFS_FH(dir);
4431 data->arg.server = server;
4432 data->arg.name = name;
4433 data->arg.attrs = sattr;
4434 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004435 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004436 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004437 data->res.server = server;
4438 data->res.fh = &data->fh;
4439 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004440 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004441 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004442 }
4443 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004444out_free:
4445 kfree(data);
4446 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004447}
4448
4449static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4450{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004451 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004452 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004453 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004454 update_changeattr(dir, &data->res.dir_cinfo,
4455 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004456 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004457 }
4458 return status;
4459}
4460
4461static void nfs4_free_createdata(struct nfs4_createdata *data)
4462{
David Quigley14c43f72013-05-22 12:50:43 -04004463 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004464 kfree(data);
4465}
4466
Chuck Lever4f390c12006-08-22 20:06:22 -04004467static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004468 struct page *page, unsigned int len, struct iattr *sattr,
4469 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004470{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004471 struct nfs4_createdata *data;
4472 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004473
Chuck Lever94a6d752006-08-22 20:06:23 -04004474 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004475 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004476
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004477 status = -ENOMEM;
4478 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4479 if (data == NULL)
4480 goto out;
4481
4482 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4483 data->arg.u.symlink.pages = &page;
4484 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004485 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004486
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004487 status = nfs4_do_create(dir, dentry, data);
4488
4489 nfs4_free_createdata(data);
4490out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004491 return status;
4492}
4493
Chuck Lever4f390c12006-08-22 20:06:22 -04004494static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004495 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004496{
4497 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004498 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004500
4501 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4502
Linus Torvalds1da177e2005-04-16 15:20:36 -07004503 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004504 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4505 trace_nfs4_symlink(dir, &dentry->d_name, err);
4506 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 &exception);
4508 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004509
4510 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 return err;
4512}
4513
4514static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004515 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004517 struct nfs4_createdata *data;
4518 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004520 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4521 if (data == NULL)
4522 goto out;
4523
David Quigley1775fd32013-05-22 12:50:42 -04004524 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004525 status = nfs4_do_create(dir, dentry, data);
4526
4527 nfs4_free_createdata(data);
4528out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004529 return status;
4530}
4531
4532static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4533 struct iattr *sattr)
4534{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004535 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004536 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004537 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004538 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004539
David Quigleyaa9c2662013-05-22 12:50:44 -04004540 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4541
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004542 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4543 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004545 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4546 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4547 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004548 &exception);
4549 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004550 nfs4_label_release_security(label);
4551
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 return err;
4553}
4554
4555static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004556 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004557{
David Howells2b0143b2015-03-17 22:25:59 +00004558 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004559 struct nfs4_readdir_arg args = {
4560 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004561 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004562 .pgbase = 0,
4563 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004564 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004565 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004566 };
4567 struct nfs4_readdir_res res;
4568 struct rpc_message msg = {
4569 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4570 .rpc_argp = &args,
4571 .rpc_resp = &res,
4572 .rpc_cred = cred,
4573 };
4574 int status;
4575
Al Viro6de14722013-09-16 10:53:17 -04004576 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4577 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004578 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004579 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004580 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004581 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 -05004582 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004583 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004584 status += args.pgbase;
4585 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004586
4587 nfs_invalidate_atime(dir);
4588
Harvey Harrison3110ff82008-05-02 13:42:44 -07004589 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004590 return status;
4591}
4592
4593static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004594 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004595{
4596 struct nfs4_exception exception = { };
4597 int err;
4598 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004599 err = _nfs4_proc_readdir(dentry, cred, cookie,
4600 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004601 trace_nfs4_readdir(d_inode(dentry), err);
4602 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 &exception);
4604 } while (exception.retry);
4605 return err;
4606}
4607
4608static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004609 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004610{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004611 struct nfs4_createdata *data;
4612 int mode = sattr->ia_mode;
4613 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004614
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004615 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4616 if (data == NULL)
4617 goto out;
4618
Linus Torvalds1da177e2005-04-16 15:20:36 -07004619 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004620 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004621 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004622 data->arg.ftype = NF4BLK;
4623 data->arg.u.device.specdata1 = MAJOR(rdev);
4624 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 }
4626 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004627 data->arg.ftype = NF4CHR;
4628 data->arg.u.device.specdata1 = MAJOR(rdev);
4629 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004630 } else if (!S_ISSOCK(mode)) {
4631 status = -EINVAL;
4632 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
David Quigley1775fd32013-05-22 12:50:42 -04004634
David Quigleyaa9c2662013-05-22 12:50:44 -04004635 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004636 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004637out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004638 nfs4_free_createdata(data);
4639out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004640 return status;
4641}
4642
4643static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4644 struct iattr *sattr, dev_t rdev)
4645{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004646 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004647 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004648 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004649 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004650
David Quigleyaa9c2662013-05-22 12:50:44 -04004651 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4652
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004653 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4654 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004656 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4657 trace_nfs4_mknod(dir, &dentry->d_name, err);
4658 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 &exception);
4660 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004661
4662 nfs4_label_release_security(label);
4663
Linus Torvalds1da177e2005-04-16 15:20:36 -07004664 return err;
4665}
4666
4667static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4668 struct nfs_fsstat *fsstat)
4669{
4670 struct nfs4_statfs_arg args = {
4671 .fh = fhandle,
4672 .bitmask = server->attr_bitmask,
4673 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004674 struct nfs4_statfs_res res = {
4675 .fsstat = fsstat,
4676 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004677 struct rpc_message msg = {
4678 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4679 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004680 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004681 };
4682
Trond Myklebust0e574af2005-10-27 22:12:38 -04004683 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004684 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685}
4686
4687static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4688{
4689 struct nfs4_exception exception = { };
4690 int err;
4691 do {
4692 err = nfs4_handle_exception(server,
4693 _nfs4_proc_statfs(server, fhandle, fsstat),
4694 &exception);
4695 } while (exception.retry);
4696 return err;
4697}
4698
4699static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4700 struct nfs_fsinfo *fsinfo)
4701{
4702 struct nfs4_fsinfo_arg args = {
4703 .fh = fhandle,
4704 .bitmask = server->attr_bitmask,
4705 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004706 struct nfs4_fsinfo_res res = {
4707 .fsinfo = fsinfo,
4708 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004709 struct rpc_message msg = {
4710 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4711 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004712 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004713 };
4714
Bryan Schumaker7c513052011-03-24 17:12:24 +00004715 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004716}
4717
4718static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4719{
4720 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004721 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004722 int err;
4723
4724 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004725 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004726 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004727 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004728 nfs4_set_lease_period(server->nfs_client,
4729 fsinfo->lease_time * HZ,
4730 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004731 break;
4732 }
4733 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004734 } while (exception.retry);
4735 return err;
4736}
4737
4738static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4739{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004740 int error;
4741
Trond Myklebust0e574af2005-10-27 22:12:38 -04004742 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004743 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004744 if (error == 0) {
4745 /* block layout checks this! */
4746 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004747 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004748 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004749
4750 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004751}
4752
4753static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4754 struct nfs_pathconf *pathconf)
4755{
4756 struct nfs4_pathconf_arg args = {
4757 .fh = fhandle,
4758 .bitmask = server->attr_bitmask,
4759 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004760 struct nfs4_pathconf_res res = {
4761 .pathconf = pathconf,
4762 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004763 struct rpc_message msg = {
4764 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4765 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004766 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004767 };
4768
4769 /* None of the pathconf attributes are mandatory to implement */
4770 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4771 memset(pathconf, 0, sizeof(*pathconf));
4772 return 0;
4773 }
4774
Trond Myklebust0e574af2005-10-27 22:12:38 -04004775 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004776 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004777}
4778
4779static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4780 struct nfs_pathconf *pathconf)
4781{
4782 struct nfs4_exception exception = { };
4783 int err;
4784
4785 do {
4786 err = nfs4_handle_exception(server,
4787 _nfs4_proc_pathconf(server, fhandle, pathconf),
4788 &exception);
4789 } while (exception.retry);
4790 return err;
4791}
4792
Trond Myklebust5521abf2013-03-16 20:54:34 -04004793int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004794 const struct nfs_open_context *ctx,
4795 const struct nfs_lock_context *l_ctx,
4796 fmode_t fmode)
4797{
NeilBrown17393472016-10-13 15:26:47 +11004798 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004799}
4800EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4801
Trond Myklebust5521abf2013-03-16 20:54:34 -04004802static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4803 const struct nfs_open_context *ctx,
4804 const struct nfs_lock_context *l_ctx,
4805 fmode_t fmode)
4806{
4807 nfs4_stateid current_stateid;
4808
Trond Myklebuste1253be2014-03-05 08:44:23 -05004809 /* If the current stateid represents a lost lock, then exit */
4810 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4811 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004812 return nfs4_stateid_match(stateid, &current_stateid);
4813}
4814
4815static bool nfs4_error_stateid_expired(int err)
4816{
4817 switch (err) {
4818 case -NFS4ERR_DELEG_REVOKED:
4819 case -NFS4ERR_ADMIN_REVOKED:
4820 case -NFS4ERR_BAD_STATEID:
4821 case -NFS4ERR_STALE_STATEID:
4822 case -NFS4ERR_OLD_STATEID:
4823 case -NFS4ERR_OPENMODE:
4824 case -NFS4ERR_EXPIRED:
4825 return true;
4826 }
4827 return false;
4828}
4829
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004830static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004831{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004832 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004833
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004834 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004835 if (task->tk_status < 0) {
4836 struct nfs4_exception exception = {
4837 .inode = hdr->inode,
4838 .state = hdr->args.context->state,
4839 .stateid = &hdr->args.stateid,
4840 };
4841 task->tk_status = nfs4_async_handle_exception(task,
4842 server, task->tk_status, &exception);
4843 if (exception.retry) {
4844 rpc_restart_call_prepare(task);
4845 return -EAGAIN;
4846 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004847 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004848
Linus Torvalds1da177e2005-04-16 15:20:36 -07004849 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004850 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004851 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004852}
4853
Trond Myklebust5521abf2013-03-16 20:54:34 -04004854static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004855 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004856{
4857
4858 if (!nfs4_error_stateid_expired(task->tk_status) ||
4859 nfs4_stateid_is_current(&args->stateid,
4860 args->context,
4861 args->lock_context,
4862 FMODE_READ))
4863 return false;
4864 rpc_restart_call_prepare(task);
4865 return true;
4866}
4867
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004868static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004869{
4870
4871 dprintk("--> %s\n", __func__);
4872
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004873 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004874 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004875 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004876 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004877 if (task->tk_status > 0)
4878 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004879 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4880 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004881}
4882
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4884 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004885{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004886 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004887 if (!hdr->pgio_done_cb)
4888 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004889 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004890 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004891}
4892
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004893static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4894 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004895{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004896 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004897 &hdr->args.seq_args,
4898 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004899 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004900 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004901 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4902 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004903 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004904 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004905 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004906 return -EIO;
4907 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004908}
4909
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004910static int nfs4_write_done_cb(struct rpc_task *task,
4911 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004912{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004913 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004914
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004915 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004916 if (task->tk_status < 0) {
4917 struct nfs4_exception exception = {
4918 .inode = hdr->inode,
4919 .state = hdr->args.context->state,
4920 .stateid = &hdr->args.stateid,
4921 };
4922 task->tk_status = nfs4_async_handle_exception(task,
4923 NFS_SERVER(inode), task->tk_status,
4924 &exception);
4925 if (exception.retry) {
4926 rpc_restart_call_prepare(task);
4927 return -EAGAIN;
4928 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004929 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004930 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004931 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004932 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004933 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004934 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004935}
4936
Trond Myklebust5521abf2013-03-16 20:54:34 -04004937static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004938 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004939{
4940
4941 if (!nfs4_error_stateid_expired(task->tk_status) ||
4942 nfs4_stateid_is_current(&args->stateid,
4943 args->context,
4944 args->lock_context,
4945 FMODE_WRITE))
4946 return false;
4947 rpc_restart_call_prepare(task);
4948 return true;
4949}
4950
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004951static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004952{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004953 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004954 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004955 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004956 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004957 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4958 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004959}
4960
Trond Myklebust5a37f852012-04-28 14:55:16 -04004961static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004962bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004963{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004964 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004965 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004966 return false;
4967 /* Otherwise, request attributes if and only if we don't hold
4968 * a delegation
4969 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004970 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004971}
Fred Isamana69aef12011-03-03 15:13:47 +00004972
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004973static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004974 struct rpc_message *msg,
4975 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004976{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004977 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004978
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004979 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4980 hdr->args.bitmask = NULL;
4981 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004982 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004983 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004984
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004985 if (!hdr->pgio_done_cb)
4986 hdr->pgio_done_cb = nfs4_write_done_cb;
4987 hdr->res.server = server;
4988 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004989
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004990 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Anna Schumakerfba83f32018-05-04 16:22:50 -04004991 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1, 0);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004992 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004993}
4994
Fred Isaman0b7c0152012-04-20 14:47:39 -04004995static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4996{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004997 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004998 &data->args.seq_args,
4999 &data->res.seq_res,
5000 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001}
5002
Fred Isaman0b7c0152012-04-20 14:47:39 -04005003static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005004{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005005 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005006
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005007 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005008 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5009 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005010 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005011 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005013 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005014}
5015
Fred Isaman0b7c0152012-04-20 14:47:39 -04005016static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005017{
5018 if (!nfs4_sequence_done(task, &data->res.seq_res))
5019 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005020 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005021}
5022
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005023static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg,
5024 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005025{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005026 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005027
Fred Isaman0b7c0152012-04-20 14:47:39 -04005028 if (data->commit_done_cb == NULL)
5029 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005030 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005031 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Anna Schumakerfba83f32018-05-04 16:22:50 -04005032 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Anna Schumakere9ae1ee2018-05-04 16:22:49 -04005033 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_COMMIT, clnt, msg);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005034}
5035
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005036struct nfs4_renewdata {
5037 struct nfs_client *client;
5038 unsigned long timestamp;
5039};
5040
Linus Torvalds1da177e2005-04-16 15:20:36 -07005041/*
5042 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5043 * standalone procedure for queueing an asynchronous RENEW.
5044 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005045static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005046{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005047 struct nfs4_renewdata *data = calldata;
5048 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005049
Elena Reshetova212bf412017-10-20 12:53:38 +03005050 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005051 nfs4_schedule_state_renewal(clp);
5052 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005053 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005054}
5055
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005056static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005057{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005058 struct nfs4_renewdata *data = calldata;
5059 struct nfs_client *clp = data->client;
5060 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005061
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005062 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005063 switch (task->tk_status) {
5064 case 0:
5065 break;
5066 case -NFS4ERR_LEASE_MOVED:
5067 nfs4_schedule_lease_moved_recovery(clp);
5068 break;
5069 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005070 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005071 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5072 return;
5073 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005074 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005075 return;
5076 }
5077 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005078 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005079 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005080}
5081
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005082static const struct rpc_call_ops nfs4_renew_ops = {
5083 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005084 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005085};
5086
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005087static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005088{
5089 struct rpc_message msg = {
5090 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5091 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005092 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005093 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005094 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005095
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005096 if (renew_flags == 0)
5097 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005098 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005099 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005100 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005101 if (data == NULL) {
5102 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005103 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005104 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005105 data->client = clp;
5106 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005107 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005108 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005109}
5110
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005111static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005112{
5113 struct rpc_message msg = {
5114 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5115 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005116 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005117 };
5118 unsigned long now = jiffies;
5119 int status;
5120
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005121 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005122 if (status < 0)
5123 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005124 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005125 return 0;
5126}
5127
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005128static inline int nfs4_server_supports_acls(struct nfs_server *server)
5129{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005130 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005131}
5132
Trond Myklebust21f498c2012-08-24 10:59:25 -04005133/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5134 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005135 * the stack.
5136 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005137#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005138
Neil Hormane9e3d722011-03-04 19:26:03 -05005139static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005140 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005141{
5142 struct page *newpage, **spages;
5143 int rc = 0;
5144 size_t len;
5145 spages = pages;
5146
5147 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005148 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005149 newpage = alloc_page(GFP_KERNEL);
5150
5151 if (newpage == NULL)
5152 goto unwind;
5153 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005154 buf += len;
5155 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005156 *pages++ = newpage;
5157 rc++;
5158 } while (buflen != 0);
5159
5160 return rc;
5161
5162unwind:
5163 for(; rc > 0; rc--)
5164 __free_page(spages[rc-1]);
5165 return -ENOMEM;
5166}
5167
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005168struct nfs4_cached_acl {
5169 int cached;
5170 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005171 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005172};
5173
5174static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005175{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005176 struct nfs_inode *nfsi = NFS_I(inode);
5177
5178 spin_lock(&inode->i_lock);
5179 kfree(nfsi->nfs4_acl);
5180 nfsi->nfs4_acl = acl;
5181 spin_unlock(&inode->i_lock);
5182}
5183
5184static void nfs4_zap_acl_attr(struct inode *inode)
5185{
5186 nfs4_set_cached_acl(inode, NULL);
5187}
5188
5189static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5190{
5191 struct nfs_inode *nfsi = NFS_I(inode);
5192 struct nfs4_cached_acl *acl;
5193 int ret = -ENOENT;
5194
5195 spin_lock(&inode->i_lock);
5196 acl = nfsi->nfs4_acl;
5197 if (acl == NULL)
5198 goto out;
5199 if (buf == NULL) /* user is just asking for length */
5200 goto out_len;
5201 if (acl->cached == 0)
5202 goto out;
5203 ret = -ERANGE; /* see getxattr(2) man page */
5204 if (acl->len > buflen)
5205 goto out;
5206 memcpy(buf, acl->data, acl->len);
5207out_len:
5208 ret = acl->len;
5209out:
5210 spin_unlock(&inode->i_lock);
5211 return ret;
5212}
5213
Sachin Prabhu5794d212012-04-17 14:36:40 +01005214static 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 +00005215{
5216 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005217 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005218
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005219 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005220 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005221 if (acl == NULL)
5222 goto out;
5223 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005224 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005225 } else {
5226 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5227 if (acl == NULL)
5228 goto out;
5229 acl->cached = 0;
5230 }
5231 acl->len = acl_len;
5232out:
5233 nfs4_set_cached_acl(inode, acl);
5234}
5235
Andy Adamsonbf118a32011-12-07 11:55:27 -05005236/*
5237 * The getxattr API returns the required buffer length when called with a
5238 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5239 * the required buf. On a NULL buf, we send a page of data to the server
5240 * guessing that the ACL request can be serviced by a page. If so, we cache
5241 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5242 * the cache. If not so, we throw away the page, and cache the required
5243 * length. The next getxattr call will then produce another round trip to
5244 * the server, this time with the input buf of the required size.
5245 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005246static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005247{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005248 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005249 struct nfs_getaclargs args = {
5250 .fh = NFS_FH(inode),
5251 .acl_pages = pages,
5252 .acl_len = buflen,
5253 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005254 struct nfs_getaclres res = {
5255 .acl_len = buflen,
5256 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005257 struct rpc_message msg = {
5258 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5259 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005260 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005261 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005262 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005263 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005264
Trond Myklebust21f498c2012-08-24 10:59:25 -04005265 if (npages > ARRAY_SIZE(pages))
5266 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005267
Andy Adamsonbf118a32011-12-07 11:55:27 -05005268 for (i = 0; i < npages; i++) {
5269 pages[i] = alloc_page(GFP_KERNEL);
5270 if (!pages[i])
5271 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005272 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005273
5274 /* for decoding across pages */
5275 res.acl_scratch = alloc_page(GFP_KERNEL);
5276 if (!res.acl_scratch)
5277 goto out_free;
5278
Andy Adamsonbf118a32011-12-07 11:55:27 -05005279 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005280
Peng Taode040be2012-01-10 22:42:47 +08005281 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005282 __func__, buf, buflen, npages, args.acl_len);
5283 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5284 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005285 if (ret)
5286 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005287
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005288 /* Handle the case where the passed-in buffer is too short */
5289 if (res.acl_flags & NFS4_ACL_TRUNC) {
5290 /* Did the user only issue a request for the acl length? */
5291 if (buf == NULL)
5292 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005293 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005294 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005295 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005296 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005297 if (buf) {
5298 if (res.acl_len > buflen) {
5299 ret = -ERANGE;
5300 goto out_free;
5301 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005302 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005303 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005304out_ok:
5305 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005306out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005307 for (i = 0; i < npages; i++)
5308 if (pages[i])
5309 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005310 if (res.acl_scratch)
5311 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005312 return ret;
5313}
5314
Trond Myklebust16b42892006-08-24 12:27:15 -04005315static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5316{
5317 struct nfs4_exception exception = { };
5318 ssize_t ret;
5319 do {
5320 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005321 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005322 if (ret >= 0)
5323 break;
5324 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5325 } while (exception.retry);
5326 return ret;
5327}
5328
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005329static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5330{
5331 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005332 int ret;
5333
5334 if (!nfs4_server_supports_acls(server))
5335 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005336 ret = nfs_revalidate_inode(server, inode);
5337 if (ret < 0)
5338 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005339 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5340 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005341 ret = nfs4_read_cached_acl(inode, buf, buflen);
5342 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005343 /* -ENOENT is returned if there is no ACL or if there is an ACL
5344 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005345 return ret;
5346 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005347}
5348
Trond Myklebust16b42892006-08-24 12:27:15 -04005349static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005350{
5351 struct nfs_server *server = NFS_SERVER(inode);
5352 struct page *pages[NFS4ACL_MAXPAGES];
5353 struct nfs_setaclargs arg = {
5354 .fh = NFS_FH(inode),
5355 .acl_pages = pages,
5356 .acl_len = buflen,
5357 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005358 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005359 struct rpc_message msg = {
5360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5361 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005362 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005363 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005364 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005365 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005366
5367 if (!nfs4_server_supports_acls(server))
5368 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005369 if (npages > ARRAY_SIZE(pages))
5370 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005371 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005372 if (i < 0)
5373 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005374 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005375 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005376
5377 /*
5378 * Free each page after tx, so the only ref left is
5379 * held by the network stack
5380 */
5381 for (; i > 0; i--)
5382 put_page(pages[i-1]);
5383
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005384 /*
5385 * Acl update can result in inode attribute update.
5386 * so mark the attribute cache invalid.
5387 */
5388 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005389 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
Trond Myklebust472f7612018-04-08 18:14:43 -04005390 | NFS_INO_INVALID_CTIME
5391 | NFS_INO_REVAL_FORCED;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005392 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005393 nfs_access_zap_cache(inode);
5394 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005395 return ret;
5396}
5397
Trond Myklebust16b42892006-08-24 12:27:15 -04005398static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5399{
5400 struct nfs4_exception exception = { };
5401 int err;
5402 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005403 err = __nfs4_proc_set_acl(inode, buf, buflen);
5404 trace_nfs4_set_acl(inode, err);
5405 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005406 &exception);
5407 } while (exception.retry);
5408 return err;
5409}
5410
David Quigleyaa9c2662013-05-22 12:50:44 -04005411#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5412static int _nfs4_get_security_label(struct inode *inode, void *buf,
5413 size_t buflen)
5414{
5415 struct nfs_server *server = NFS_SERVER(inode);
5416 struct nfs_fattr fattr;
5417 struct nfs4_label label = {0, 0, buflen, buf};
5418
5419 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005420 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005421 .fh = NFS_FH(inode),
5422 .bitmask = bitmask,
5423 };
5424 struct nfs4_getattr_res res = {
5425 .fattr = &fattr,
5426 .label = &label,
5427 .server = server,
5428 };
5429 struct rpc_message msg = {
5430 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005431 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005432 .rpc_resp = &res,
5433 };
5434 int ret;
5435
5436 nfs_fattr_init(&fattr);
5437
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005438 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005439 if (ret)
5440 return ret;
5441 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5442 return -ENOENT;
5443 if (buflen < label.len)
5444 return -ERANGE;
5445 return 0;
5446}
5447
5448static int nfs4_get_security_label(struct inode *inode, void *buf,
5449 size_t buflen)
5450{
5451 struct nfs4_exception exception = { };
5452 int err;
5453
5454 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5455 return -EOPNOTSUPP;
5456
5457 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005458 err = _nfs4_get_security_label(inode, buf, buflen);
5459 trace_nfs4_get_security_label(inode, err);
5460 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005461 &exception);
5462 } while (exception.retry);
5463 return err;
5464}
5465
5466static int _nfs4_do_set_security_label(struct inode *inode,
5467 struct nfs4_label *ilabel,
5468 struct nfs_fattr *fattr,
5469 struct nfs4_label *olabel)
5470{
5471
5472 struct iattr sattr = {0};
5473 struct nfs_server *server = NFS_SERVER(inode);
5474 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005475 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005476 .fh = NFS_FH(inode),
5477 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005478 .server = server,
5479 .bitmask = bitmask,
5480 .label = ilabel,
5481 };
5482 struct nfs_setattrres res = {
5483 .fattr = fattr,
5484 .label = olabel,
5485 .server = server,
5486 };
5487 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005488 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5489 .rpc_argp = &arg,
5490 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005491 };
5492 int status;
5493
Jeff Layton12207f62013-11-01 10:49:32 -04005494 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005495
Jeff Layton12207f62013-11-01 10:49:32 -04005496 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005497 if (status)
5498 dprintk("%s failed: %d\n", __func__, status);
5499
5500 return status;
5501}
5502
5503static int nfs4_do_set_security_label(struct inode *inode,
5504 struct nfs4_label *ilabel,
5505 struct nfs_fattr *fattr,
5506 struct nfs4_label *olabel)
5507{
5508 struct nfs4_exception exception = { };
5509 int err;
5510
5511 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005512 err = _nfs4_do_set_security_label(inode, ilabel,
5513 fattr, olabel);
5514 trace_nfs4_set_security_label(inode, err);
5515 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005516 &exception);
5517 } while (exception.retry);
5518 return err;
5519}
5520
5521static int
Al Viro59301222016-05-27 10:19:30 -04005522nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005523{
5524 struct nfs4_label ilabel, *olabel = NULL;
5525 struct nfs_fattr fattr;
5526 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005527 int status;
5528
5529 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5530 return -EOPNOTSUPP;
5531
5532 nfs_fattr_init(&fattr);
5533
5534 ilabel.pi = 0;
5535 ilabel.lfs = 0;
5536 ilabel.label = (char *)buf;
5537 ilabel.len = buflen;
5538
5539 cred = rpc_lookup_cred();
5540 if (IS_ERR(cred))
5541 return PTR_ERR(cred);
5542
5543 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5544 if (IS_ERR(olabel)) {
5545 status = -PTR_ERR(olabel);
5546 goto out;
5547 }
5548
5549 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5550 if (status == 0)
5551 nfs_setsecurity(inode, &fattr, olabel);
5552
5553 nfs4_label_free(olabel);
5554out:
5555 put_rpccred(cred);
5556 return status;
5557}
5558#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5559
5560
Chuck Leverf0920752012-05-21 22:45:41 -04005561static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5562 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005563{
5564 __be32 verf[2];
5565
Chuck Lever2c820d92012-05-21 22:45:33 -04005566 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5567 /* An impossible timestamp guarantees this value
5568 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005569 verf[0] = cpu_to_be32(U32_MAX);
5570 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005571 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005572 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005573 u64 ns = ktime_to_ns(nn->boot_time);
5574
5575 verf[0] = cpu_to_be32(ns >> 32);
5576 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005577 }
Chuck Levercd937102012-03-02 17:14:31 -05005578 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5579}
5580
Jeff Laytona3192682015-06-09 19:43:59 -04005581static int
5582nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005583{
Jeff Laytona3192682015-06-09 19:43:59 -04005584 size_t len;
5585 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005586
Trond Myklebustceb3a162015-01-03 15:16:04 -05005587 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005588 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005589
Jeff Laytona3192682015-06-09 19:43:59 -04005590 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005591 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005592 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5593 1 +
5594 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5595 1;
5596 rcu_read_unlock();
5597
5598 if (len > NFS4_OPAQUE_LIMIT + 1)
5599 return -EINVAL;
5600
5601 /*
5602 * Since this string is allocated at mount time, and held until the
5603 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5604 * about a memory-reclaim deadlock.
5605 */
5606 str = kmalloc(len, GFP_KERNEL);
5607 if (!str)
5608 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005609
Chuck Levere984a552012-09-14 17:24:21 -04005610 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005611 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005612 clp->cl_ipaddr,
5613 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5614 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005615 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005616
Jeff Laytona3192682015-06-09 19:43:59 -04005617 clp->cl_owner_id = str;
5618 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005619}
5620
Jeff Layton873e3852015-06-09 19:44:00 -04005621static int
5622nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005623{
Jeff Layton873e3852015-06-09 19:44:00 -04005624 size_t len;
5625 char *str;
5626
5627 len = 10 + 10 + 1 + 10 + 1 +
5628 strlen(nfs4_client_id_uniquifier) + 1 +
5629 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5630
5631 if (len > NFS4_OPAQUE_LIMIT + 1)
5632 return -EINVAL;
5633
5634 /*
5635 * Since this string is allocated at mount time, and held until the
5636 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5637 * about a memory-reclaim deadlock.
5638 */
5639 str = kmalloc(len, GFP_KERNEL);
5640 if (!str)
5641 return -ENOMEM;
5642
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005643 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005644 clp->rpc_ops->version, clp->cl_minorversion,
5645 nfs4_client_id_uniquifier,
5646 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005647 clp->cl_owner_id = str;
5648 return 0;
5649}
5650
5651static int
5652nfs4_init_uniform_client_string(struct nfs_client *clp)
5653{
Jeff Layton873e3852015-06-09 19:44:00 -04005654 size_t len;
5655 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005656
5657 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005658 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005659
5660 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005661 return nfs4_init_uniquifier_client_string(clp);
5662
5663 len = 10 + 10 + 1 + 10 + 1 +
5664 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5665
5666 if (len > NFS4_OPAQUE_LIMIT + 1)
5667 return -EINVAL;
5668
5669 /*
5670 * Since this string is allocated at mount time, and held until the
5671 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5672 * about a memory-reclaim deadlock.
5673 */
5674 str = kmalloc(len, GFP_KERNEL);
5675 if (!str)
5676 return -ENOMEM;
5677
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005678 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005679 clp->rpc_ops->version, clp->cl_minorversion,
5680 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005681 clp->cl_owner_id = str;
5682 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005683}
5684
Chuck Lever706cb8d2014-03-12 12:51:47 -04005685/*
5686 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5687 * services. Advertise one based on the address family of the
5688 * clientaddr.
5689 */
5690static unsigned int
5691nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5692{
5693 if (strchr(clp->cl_ipaddr, ':') != NULL)
5694 return scnprintf(buf, len, "tcp6");
5695 else
5696 return scnprintf(buf, len, "tcp");
5697}
5698
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005699static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5700{
5701 struct nfs4_setclientid *sc = calldata;
5702
5703 if (task->tk_status == 0)
5704 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5705}
5706
5707static const struct rpc_call_ops nfs4_setclientid_ops = {
5708 .rpc_call_done = nfs4_setclientid_done,
5709};
5710
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005711/**
5712 * nfs4_proc_setclientid - Negotiate client ID
5713 * @clp: state data structure
5714 * @program: RPC program for NFSv4 callback service
5715 * @port: IP port number for NFS4 callback service
5716 * @cred: RPC credential to use for this call
5717 * @res: where to place the result
5718 *
5719 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5720 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005721int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5722 unsigned short port, struct rpc_cred *cred,
5723 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005724{
5725 nfs4_verifier sc_verifier;
5726 struct nfs4_setclientid setclientid = {
5727 .sc_verifier = &sc_verifier,
5728 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005729 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005730 };
5731 struct rpc_message msg = {
5732 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5733 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005734 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005735 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005736 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005737 struct rpc_task *task;
5738 struct rpc_task_setup task_setup_data = {
5739 .rpc_client = clp->cl_rpcclient,
5740 .rpc_message = &msg,
5741 .callback_ops = &nfs4_setclientid_ops,
5742 .callback_data = &setclientid,
5743 .flags = RPC_TASK_TIMEOUT,
5744 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005745 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005746
Chuck Leverde734832012-07-11 16:30:50 -04005747 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005748 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005749
5750 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5751 status = nfs4_init_uniform_client_string(clp);
5752 else
Jeff Laytona3192682015-06-09 19:43:59 -04005753 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005754
5755 if (status)
5756 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005757
Chuck Leverde734832012-07-11 16:30:50 -04005758 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005759 setclientid.sc_netid_len =
5760 nfs4_init_callback_netid(clp,
5761 setclientid.sc_netid,
5762 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005763 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005764 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005765 clp->cl_ipaddr, port >> 8, port & 255);
5766
Jeff Layton3a6bb732015-06-09 19:43:57 -04005767 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005768 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005769 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005770 task = rpc_run_task(&task_setup_data);
5771 if (IS_ERR(task)) {
5772 status = PTR_ERR(task);
5773 goto out;
5774 }
5775 status = task->tk_status;
5776 if (setclientid.sc_cred) {
5777 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5778 put_rpccred(setclientid.sc_cred);
5779 }
5780 rpc_put_task(task);
5781out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005782 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005783 dprintk("NFS reply setclientid: %d\n", status);
5784 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005785}
5786
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005787/**
5788 * nfs4_proc_setclientid_confirm - Confirm client ID
5789 * @clp: state data structure
5790 * @res: result of a previous SETCLIENTID
5791 * @cred: RPC credential to use for this call
5792 *
5793 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5794 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005795int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005796 struct nfs4_setclientid_res *arg,
5797 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005798{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005799 struct rpc_message msg = {
5800 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005801 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005802 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 int status;
5805
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005806 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5807 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5808 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005809 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005810 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005811 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005812 return status;
5813}
5814
Trond Myklebustfe650402006-01-03 09:55:18 +01005815struct nfs4_delegreturndata {
5816 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005817 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005818 struct nfs_fh fh;
5819 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005820 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005821 struct {
5822 struct nfs4_layoutreturn_args arg;
5823 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005824 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005825 u32 roc_barrier;
5826 bool roc;
5827 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005828 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005829 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005830 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005831};
5832
Trond Myklebustfe650402006-01-03 09:55:18 +01005833static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5834{
5835 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005836 struct nfs4_exception exception = {
5837 .inode = data->inode,
5838 .stateid = &data->stateid,
5839 };
Andy Adamson938e1012009-04-01 09:22:28 -04005840
Trond Myklebust14516c32010-07-31 14:29:06 -04005841 if (!nfs4_sequence_done(task, &data->res.seq_res))
5842 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005843
Trond Myklebustca8acf82013-08-13 10:36:56 -04005844 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005845
5846 /* Handle Layoutreturn errors */
5847 if (data->args.lr_args && task->tk_status != 0) {
5848 switch(data->res.lr_ret) {
5849 default:
5850 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5851 break;
5852 case 0:
5853 data->args.lr_args = NULL;
5854 data->res.lr_res = NULL;
5855 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005856 case -NFS4ERR_OLD_STATEID:
5857 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005858 data->inode))
5859 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005860 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005861 case -NFS4ERR_ADMIN_REVOKED:
5862 case -NFS4ERR_DELEG_REVOKED:
5863 case -NFS4ERR_EXPIRED:
5864 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005865 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5866 case -NFS4ERR_WRONG_CRED:
5867 data->args.lr_args = NULL;
5868 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005869 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005870 }
5871 }
5872
Ricardo Labiaga79708862009-12-07 09:23:21 -05005873 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005874 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005875 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005876 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005877 case -NFS4ERR_ADMIN_REVOKED:
5878 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005879 case -NFS4ERR_EXPIRED:
5880 nfs4_free_revoked_stateid(data->res.server,
5881 data->args.stateid,
5882 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005883 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005884 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005885 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005886 task->tk_status = 0;
5887 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005888 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005889 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5890 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005891 task->tk_status = 0;
5892 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005893 case -NFS4ERR_ACCESS:
5894 if (data->args.bitmask) {
5895 data->args.bitmask = NULL;
5896 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005897 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005898 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005899 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005900 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005901 task->tk_status = nfs4_async_handle_exception(task,
5902 data->res.server, task->tk_status,
5903 &exception);
5904 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005905 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005906 }
5907 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005908 return;
5909lr_restart:
5910 data->res.lr_ret = 0;
5911out_restart:
5912 task->tk_status = 0;
5913 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005914}
5915
5916static void nfs4_delegreturn_release(void *calldata)
5917{
Peng Tao039b7562014-07-03 13:05:02 +08005918 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005919 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005920
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005921 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005922 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005923 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5924 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005925 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005926 nfs_iput_and_deactive(inode);
5927 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005928 kfree(calldata);
5929}
5930
Andy Adamson938e1012009-04-01 09:22:28 -04005931static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5932{
5933 struct nfs4_delegreturndata *d_data;
5934
5935 d_data = (struct nfs4_delegreturndata *)data;
5936
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005937 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005938 return;
5939
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005940 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005941 &d_data->args.seq_args,
5942 &d_data->res.seq_res,
5943 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005944}
Andy Adamson938e1012009-04-01 09:22:28 -04005945
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005946static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005947 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005948 .rpc_call_done = nfs4_delegreturn_done,
5949 .rpc_release = nfs4_delegreturn_release,
5950};
5951
Trond Myklebuste6f81072008-01-24 18:14:34 -05005952static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005953{
5954 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005955 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005956 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005957 struct rpc_message msg = {
5958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5959 .rpc_cred = cred,
5960 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005961 struct rpc_task_setup task_setup_data = {
5962 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005963 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005964 .callback_ops = &nfs4_delegreturn_ops,
5965 .flags = RPC_TASK_ASYNC,
5966 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005967 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005968
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005969 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005970 if (data == NULL)
5971 return -ENOMEM;
Anna Schumakerfba83f32018-05-04 16:22:50 -04005972 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005973
5974 nfs4_state_protect(server->nfs_client,
5975 NFS_SP4_MACH_CRED_CLEANUP,
5976 &task_setup_data.rpc_client, &msg);
5977
Trond Myklebustfe650402006-01-03 09:55:18 +01005978 data->args.fhandle = &data->fh;
5979 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005980 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005981 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005982 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005983 data->res.fattr = &data->fattr;
5984 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005985 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005986 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005987 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005988 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005989 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005990 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005991 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005992 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005993 if (data->lr.roc) {
5994 data->args.lr_args = &data->lr.arg;
5995 data->res.lr_res = &data->lr.res;
5996 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005997 } else if (data->lr.roc) {
5998 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5999 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006000 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006001
Trond Myklebustc970aa82007-07-14 15:39:59 -04006002 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006003 msg.rpc_argp = &data->args;
6004 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006005 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006006 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006007 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006008 if (!issync)
6009 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006010 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006011 if (status != 0)
6012 goto out;
6013 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006014out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006015 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006016 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006017}
6018
Trond Myklebuste6f81072008-01-24 18:14:34 -05006019int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006020{
6021 struct nfs_server *server = NFS_SERVER(inode);
6022 struct nfs4_exception exception = { };
6023 int err;
6024 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006025 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006026 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006027 switch (err) {
6028 case -NFS4ERR_STALE_STATEID:
6029 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006030 case 0:
6031 return 0;
6032 }
6033 err = nfs4_handle_exception(server, err, &exception);
6034 } while (exception.retry);
6035 return err;
6036}
6037
Linus Torvalds1da177e2005-04-16 15:20:36 -07006038static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6039{
6040 struct inode *inode = state->inode;
6041 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006042 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006043 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006044 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006045 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006046 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006047 struct nfs_lockt_res res = {
6048 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 };
6050 struct rpc_message msg = {
6051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006052 .rpc_argp = &arg,
6053 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 .rpc_cred = state->owner->so_cred,
6055 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006056 struct nfs4_lock_state *lsp;
6057 int status;
6058
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006059 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006060 status = nfs4_set_lock_state(state, request);
6061 if (status != 0)
6062 goto out;
6063 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006064 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006065 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006066 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006067 switch (status) {
6068 case 0:
6069 request->fl_type = F_UNLCK;
6070 break;
6071 case -NFS4ERR_DENIED:
6072 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006073 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006074 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006075 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006076out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006077 return status;
6078}
6079
6080static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6081{
6082 struct nfs4_exception exception = { };
6083 int err;
6084
6085 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006086 err = _nfs4_proc_getlk(state, cmd, request);
6087 trace_nfs4_get_lock(request, state, cmd, err);
6088 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006089 &exception);
6090 } while (exception.retry);
6091 return err;
6092}
6093
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006094struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006095 struct nfs_locku_args arg;
6096 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006097 struct nfs4_lock_state *lsp;
6098 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006099 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006101 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006102 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006103};
6104
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006105static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6106 struct nfs_open_context *ctx,
6107 struct nfs4_lock_state *lsp,
6108 struct nfs_seqid *seqid)
6109{
6110 struct nfs4_unlockdata *p;
6111 struct inode *inode = lsp->ls_state->inode;
6112
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006113 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006114 if (p == NULL)
6115 return NULL;
6116 p->arg.fh = NFS_FH(inode);
6117 p->arg.fl = &p->fl;
6118 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006119 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006120 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006121 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006122 /* Ensure we don't close file until we're done freeing locks! */
6123 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006124 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006125 memcpy(&p->fl, fl, sizeof(p->fl));
6126 p->server = NFS_SERVER(inode);
6127 return p;
6128}
6129
Trond Myklebust06f814a2006-01-03 09:55:07 +01006130static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006131{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006132 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006133 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006134 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006135 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006136 put_nfs_open_context(calldata->ctx);
6137 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006138}
6139
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006140static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006141{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006142 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006143 struct nfs4_exception exception = {
6144 .inode = calldata->lsp->ls_state->inode,
6145 .stateid = &calldata->arg.stateid,
6146 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006147
Trond Myklebust14516c32010-07-31 14:29:06 -04006148 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6149 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006150 switch (task->tk_status) {
6151 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006152 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006153 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006154 if (nfs4_update_lock_stateid(calldata->lsp,
6155 &calldata->res.stateid))
6156 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006157 case -NFS4ERR_ADMIN_REVOKED:
6158 case -NFS4ERR_EXPIRED:
6159 nfs4_free_revoked_stateid(calldata->server,
6160 &calldata->arg.stateid,
6161 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006162 case -NFS4ERR_BAD_STATEID:
6163 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006164 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006165 if (!nfs4_stateid_match(&calldata->arg.stateid,
6166 &calldata->lsp->ls_stateid))
6167 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006168 break;
6169 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006170 task->tk_status = nfs4_async_handle_exception(task,
6171 calldata->server, task->tk_status,
6172 &exception);
6173 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006174 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006175 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006176 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006177}
6178
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006179static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006180{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006181 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006182
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006183 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6184 nfs_async_iocounter_wait(task, calldata->l_ctx))
6185 return;
6186
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006187 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006188 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006189 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006190 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006191 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006192 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006193 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006194 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006195 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006196 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006197 &calldata->res.seq_res,
6198 task) != 0)
6199 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006200 return;
6201out_no_action:
6202 task->tk_action = NULL;
6203out_wait:
6204 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006205}
6206
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006207static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006208 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006209 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006210 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006211};
6212
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006213static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6214 struct nfs_open_context *ctx,
6215 struct nfs4_lock_state *lsp,
6216 struct nfs_seqid *seqid)
6217{
6218 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006219 struct rpc_message msg = {
6220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6221 .rpc_cred = ctx->cred,
6222 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006223 struct rpc_task_setup task_setup_data = {
6224 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006225 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006226 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006227 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006228 .flags = RPC_TASK_ASYNC,
6229 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006230
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006231 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6232 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6233
Frank Filz137d6ac2007-07-09 15:32:29 -07006234 /* Ensure this is an unlock - when canceling a lock, the
6235 * canceled lock is passed in, and it won't be an unlock.
6236 */
6237 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006238 if (fl->fl_flags & FL_CLOSE)
6239 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006240
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006241 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6242 if (data == NULL) {
6243 nfs_free_seqid(seqid);
6244 return ERR_PTR(-ENOMEM);
6245 }
6246
Anna Schumakerfba83f32018-05-04 16:22:50 -04006247 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1, 0);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006248 msg.rpc_argp = &data->arg;
6249 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006250 task_setup_data.callback_data = data;
6251 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006252}
6253
Linus Torvalds1da177e2005-04-16 15:20:36 -07006254static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6255{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006256 struct inode *inode = state->inode;
6257 struct nfs4_state_owner *sp = state->owner;
6258 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006259 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006260 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006261 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006262 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006263 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006264 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006265
Trond Myklebust9b073572006-06-29 16:38:34 -04006266 status = nfs4_set_lock_state(state, request);
6267 /* Unlock _before_ we do the RPC call */
6268 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006269 /* Exclude nfs_delegation_claim_locks() */
6270 mutex_lock(&sp->so_delegreturn_mutex);
6271 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006272 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006273 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006274 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006275 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006276 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006277 }
6278 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006279 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006280 if (status != 0)
6281 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006282 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006283 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006284 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6285 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006286 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6287 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006288 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006289 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006290 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006291 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006292 status = PTR_ERR(task);
6293 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006294 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006295 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006296 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006297out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006298 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006299 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006300 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006301}
6302
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006303struct nfs4_lockdata {
6304 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006305 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006306 struct nfs4_lock_state *lsp;
6307 struct nfs_open_context *ctx;
6308 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006309 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006310 int rpc_status;
6311 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006312 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006313};
6314
6315static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006316 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6317 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006318{
6319 struct nfs4_lockdata *p;
6320 struct inode *inode = lsp->ls_state->inode;
6321 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006322 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006323
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006324 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006325 if (p == NULL)
6326 return NULL;
6327
6328 p->arg.fh = NFS_FH(inode);
6329 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006330 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006331 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006332 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006333 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6334 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006335 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006336 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006337 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006338 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006339 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006340 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006341 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006342 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006343 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006344 p->ctx = get_nfs_open_context(ctx);
6345 memcpy(&p->fl, fl, sizeof(p->fl));
6346 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006347out_free_seqid:
6348 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006349out_free:
6350 kfree(p);
6351 return NULL;
6352}
6353
6354static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6355{
6356 struct nfs4_lockdata *data = calldata;
6357 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006358
Harvey Harrison3110ff82008-05-02 13:42:44 -07006359 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006360 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006361 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006362 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006363 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006364 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006365 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006366 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006367 nfs4_stateid_copy(&data->arg.open_stateid,
6368 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006369 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006370 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006371 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006372 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006373 nfs4_stateid_copy(&data->arg.lock_stateid,
6374 &data->lsp->ls_stateid);
6375 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006376 if (!nfs4_valid_open_stateid(state)) {
6377 data->rpc_status = -EBADF;
6378 task->tk_action = NULL;
6379 goto out_release_open_seqid;
6380 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006381 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006382 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006383 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006384 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006385 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006386 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006387out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006388 nfs_release_seqid(data->arg.open_seqid);
6389out_release_lock_seqid:
6390 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006391out_wait:
6392 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006393 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006394}
6395
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006396static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6397{
6398 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006399 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006400
Harvey Harrison3110ff82008-05-02 13:42:44 -07006401 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006402
Trond Myklebust14516c32010-07-31 14:29:06 -04006403 if (!nfs4_sequence_done(task, &data->res.seq_res))
6404 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006405
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006406 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006407 switch (task->tk_status) {
6408 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006409 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006410 data->timestamp);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006411 if (data->arg.new_lock && !data->cancelled) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006412 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006413 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0)
Trond Myklebustc69899a2015-01-24 16:03:52 -05006414 break;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006415 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006416
Trond Myklebust39071e62015-01-24 15:07:56 -05006417 if (data->arg.new_lock_owner != 0) {
6418 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6419 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6420 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006421 goto out_done;
6422 } else if (nfs4_update_lock_stateid(lsp, &data->res.stateid))
6423 goto out_done;
6424
Trond Myklebust425c1d42015-01-24 14:57:53 -05006425 break;
6426 case -NFS4ERR_BAD_STATEID:
6427 case -NFS4ERR_OLD_STATEID:
6428 case -NFS4ERR_STALE_STATEID:
6429 case -NFS4ERR_EXPIRED:
6430 if (data->arg.new_lock_owner != 0) {
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006431 if (nfs4_stateid_match(&data->arg.open_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006432 &lsp->ls_state->open_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006433 goto out_done;
6434 } else if (nfs4_stateid_match(&data->arg.lock_stateid,
Trond Myklebust425c1d42015-01-24 14:57:53 -05006435 &lsp->ls_stateid))
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006436 goto out_done;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006437 }
Benjamin Coddingtona3cf9bc2018-05-03 07:12:57 -04006438 if (!data->cancelled)
6439 rpc_restart_call_prepare(task);
6440out_done:
Harvey Harrison3110ff82008-05-02 13:42:44 -07006441 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006442}
6443
6444static void nfs4_lock_release(void *calldata)
6445{
6446 struct nfs4_lockdata *data = calldata;
6447
Harvey Harrison3110ff82008-05-02 13:42:44 -07006448 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006449 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006450 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006451 struct rpc_task *task;
6452 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6453 data->arg.lock_seqid);
6454 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006455 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006456 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006457 } else
6458 nfs_free_seqid(data->arg.lock_seqid);
6459 nfs4_put_lock_state(data->lsp);
6460 put_nfs_open_context(data->ctx);
6461 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006462 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006463}
6464
6465static const struct rpc_call_ops nfs4_lock_ops = {
6466 .rpc_call_prepare = nfs4_lock_prepare,
6467 .rpc_call_done = nfs4_lock_done,
6468 .rpc_release = nfs4_lock_release,
6469};
6470
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006471static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6472{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006473 switch (error) {
6474 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006475 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006476 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006477 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006478 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006479 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006480 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006481 break;
6482 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006483 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006484 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006485 };
6486}
6487
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006488static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006489{
6490 struct nfs4_lockdata *data;
6491 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006492 struct rpc_message msg = {
6493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6494 .rpc_cred = state->owner->so_cred,
6495 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006496 struct rpc_task_setup task_setup_data = {
6497 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006498 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006499 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006500 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006501 .flags = RPC_TASK_ASYNC,
6502 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006503 int ret;
6504
Harvey Harrison3110ff82008-05-02 13:42:44 -07006505 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006506 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006507 fl->fl_u.nfs4_fl.owner,
6508 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006509 if (data == NULL)
6510 return -ENOMEM;
6511 if (IS_SETLKW(cmd))
6512 data->arg.block = 1;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006513 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1,
6514 recovery_type > NFS_LOCK_NEW);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006515 msg.rpc_argp = &data->arg;
6516 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006517 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006518 if (recovery_type > NFS_LOCK_NEW) {
6519 if (recovery_type == NFS_LOCK_RECLAIM)
6520 data->arg.reclaim = NFS_LOCK_RECLAIM;
Trond Myklebustc69899a2015-01-24 16:03:52 -05006521 } else
6522 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006523 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006524 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006525 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006526 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006527 if (ret == 0) {
6528 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006529 if (ret)
6530 nfs4_handle_setlk_error(data->server, data->lsp,
6531 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006532 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006533 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006534 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006535 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006536 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006538}
6539
6540static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6541{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006542 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006543 struct nfs4_exception exception = {
6544 .inode = state->inode,
6545 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006546 int err;
6547
6548 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006549 /* Cache the lock if possible... */
6550 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6551 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006552 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006553 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006554 break;
6555 nfs4_handle_exception(server, err, &exception);
6556 } while (exception.retry);
6557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558}
6559
6560static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6561{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006562 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006563 struct nfs4_exception exception = {
6564 .inode = state->inode,
6565 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006566 int err;
6567
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006568 err = nfs4_set_lock_state(state, request);
6569 if (err != 0)
6570 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006571 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006572 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6573 return 0;
6574 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006575 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006576 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6577 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006578 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006579 switch (err) {
6580 default:
6581 goto out;
6582 case -NFS4ERR_GRACE:
6583 case -NFS4ERR_DELAY:
6584 nfs4_handle_exception(server, err, &exception);
6585 err = 0;
6586 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006587 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006588out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006589 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006590}
6591
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006592#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006593static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6594{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006595 struct nfs4_lock_state *lsp;
6596 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006597
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006598 status = nfs4_set_lock_state(state, request);
6599 if (status != 0)
6600 return status;
6601 lsp = request->fl_u.nfs4_fl.owner;
6602 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6603 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6604 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006605 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006606}
6607#endif
6608
Linus Torvalds1da177e2005-04-16 15:20:36 -07006609static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6610{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006611 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006612 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006613 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006614 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006615
Trond Myklebust01c3b862006-06-29 16:38:39 -04006616 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006617 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006618 if (status < 0)
6619 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006620 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006621 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006622 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006623 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006624 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006625 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006626 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006627 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006628 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006629 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006630 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006631 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006632 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006633 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006634out:
6635 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006636 return status;
6637}
6638
6639static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6640{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006641 struct nfs4_exception exception = {
6642 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006643 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006644 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006645 int err;
6646
6647 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006648 err = _nfs4_proc_setlk(state, cmd, request);
6649 if (err == -NFS4ERR_DENIED)
6650 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006651 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006652 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006653 } while (exception.retry);
6654 return err;
6655}
6656
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006657#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6658#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6659
6660static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006661nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6662 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006663{
6664 int status = -ERESTARTSYS;
6665 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6666
6667 while(!signalled()) {
6668 status = nfs4_proc_setlk(state, cmd, request);
6669 if ((status != -EAGAIN) || IS_SETLK(cmd))
6670 break;
6671 freezable_schedule_timeout_interruptible(timeout);
6672 timeout *= 2;
6673 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6674 status = -ERESTARTSYS;
6675 }
6676 return status;
6677}
6678
Jeff Laytona1d617d82016-09-17 18:17:39 -04006679#ifdef CONFIG_NFS_V4_1
6680struct nfs4_lock_waiter {
6681 struct task_struct *task;
6682 struct inode *inode;
6683 struct nfs_lowner *owner;
6684 bool notified;
6685};
6686
6687static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006688nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006689{
6690 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006691 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006692
Jeff Layton57174592018-03-18 08:37:03 -04006693 /* NULL key means to wake up everyone */
6694 if (key) {
6695 struct cb_notify_lock_args *cbnl = key;
6696 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6697 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006698
Jeff Layton57174592018-03-18 08:37:03 -04006699 /* Only wake if the callback was for the same owner. */
6700 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6701 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006702
Jeff Layton57174592018-03-18 08:37:03 -04006703 /* Make sure it's for the right inode */
6704 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6705 return 0;
6706
6707 waiter->notified = true;
6708 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006709
6710 /* override "private" so we can use default_wake_function */
6711 wait->private = waiter->task;
6712 ret = autoremove_wake_function(wait, mode, flags, key);
6713 wait->private = waiter;
6714 return ret;
6715}
6716
6717static int
6718nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6719{
6720 int status = -ERESTARTSYS;
6721 unsigned long flags;
6722 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6723 struct nfs_server *server = NFS_SERVER(state->inode);
6724 struct nfs_client *clp = server->nfs_client;
6725 wait_queue_head_t *q = &clp->cl_lock_waitq;
6726 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6727 .id = lsp->ls_seqid.owner_id,
6728 .s_dev = server->s_dev };
6729 struct nfs4_lock_waiter waiter = { .task = current,
6730 .inode = state->inode,
6731 .owner = &owner,
6732 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006733 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006734
6735 /* Don't bother with waitqueue if we don't expect a callback */
6736 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6737 return nfs4_retry_setlk_simple(state, cmd, request);
6738
6739 init_wait(&wait);
6740 wait.private = &waiter;
6741 wait.func = nfs4_wake_lock_waiter;
6742 add_wait_queue(q, &wait);
6743
6744 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006745 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006746 status = nfs4_proc_setlk(state, cmd, request);
6747 if ((status != -EAGAIN) || IS_SETLK(cmd))
6748 break;
6749
6750 status = -ERESTARTSYS;
6751 spin_lock_irqsave(&q->lock, flags);
6752 if (waiter.notified) {
6753 spin_unlock_irqrestore(&q->lock, flags);
6754 continue;
6755 }
6756 set_current_state(TASK_INTERRUPTIBLE);
6757 spin_unlock_irqrestore(&q->lock, flags);
6758
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006759 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006760 }
6761
6762 finish_wait(q, &wait);
6763 return status;
6764}
6765#else /* !CONFIG_NFS_V4_1 */
6766static inline int
6767nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6768{
6769 return nfs4_retry_setlk_simple(state, cmd, request);
6770}
6771#endif
6772
Linus Torvalds1da177e2005-04-16 15:20:36 -07006773static int
6774nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6775{
6776 struct nfs_open_context *ctx;
6777 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006778 int status;
6779
6780 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006781 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006782 state = ctx->state;
6783
Trond Myklebustd9531262009-07-21 19:22:38 -04006784 if (IS_GETLK(cmd)) {
6785 if (state != NULL)
6786 return nfs4_proc_getlk(state, F_GETLK, request);
6787 return 0;
6788 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006789
6790 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6791 return -EINVAL;
6792
Trond Myklebustd9531262009-07-21 19:22:38 -04006793 if (request->fl_type == F_UNLCK) {
6794 if (state != NULL)
6795 return nfs4_proc_unlck(state, cmd, request);
6796 return 0;
6797 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006798
Trond Myklebustd9531262009-07-21 19:22:38 -04006799 if (state == NULL)
6800 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006801
6802 if ((request->fl_flags & FL_POSIX) &&
6803 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6804 return -ENOLCK;
6805
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006806 /*
6807 * Don't rely on the VFS having checked the file open mode,
6808 * since it won't do this for flock() locks.
6809 */
6810 switch (request->fl_type) {
6811 case F_RDLCK:
6812 if (!(filp->f_mode & FMODE_READ))
6813 return -EBADF;
6814 break;
6815 case F_WRLCK:
6816 if (!(filp->f_mode & FMODE_WRITE))
6817 return -EBADF;
6818 }
6819
Jeff Layton1ea67db2016-09-17 18:17:37 -04006820 status = nfs4_set_lock_state(state, request);
6821 if (status != 0)
6822 return status;
6823
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006824 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006825}
6826
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006827int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006828{
6829 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006830 int err;
6831
6832 err = nfs4_set_lock_state(state, fl);
6833 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006834 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006835 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006836 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006837}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006838
Trond Myklebustcf470c32012-03-07 13:49:12 -05006839struct nfs_release_lockowner_data {
6840 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006841 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006842 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006843 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006844 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006845};
6846
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006847static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6848{
6849 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006850 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006851 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6852 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006853 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006854 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006855}
6856
6857static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6858{
6859 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006860 struct nfs_server *server = data->server;
6861
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006862 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006863
6864 switch (task->tk_status) {
6865 case 0:
6866 renew_lease(server, data->timestamp);
6867 break;
6868 case -NFS4ERR_STALE_CLIENTID:
6869 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006870 nfs4_schedule_lease_recovery(server->nfs_client);
6871 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006872 case -NFS4ERR_LEASE_MOVED:
6873 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006874 if (nfs4_async_handle_error(task, server,
6875 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006876 rpc_restart_call_prepare(task);
6877 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006878}
6879
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006880static void nfs4_release_lockowner_release(void *calldata)
6881{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006882 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006883 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006884 kfree(calldata);
6885}
6886
Trond Myklebust17280172012-03-11 13:11:00 -04006887static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006888 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6889 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006890 .rpc_release = nfs4_release_lockowner_release,
6891};
6892
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006893static void
6894nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006895{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006896 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006897 struct rpc_message msg = {
6898 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6899 };
6900
6901 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006902 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006903
Trond Myklebustcf470c32012-03-07 13:49:12 -05006904 data = kmalloc(sizeof(*data), GFP_NOFS);
6905 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006906 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006907 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006908 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006909 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6910 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6911 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006912
Trond Myklebustcf470c32012-03-07 13:49:12 -05006913 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006914 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04006915 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006916 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006917}
6918
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006919#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6920
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006921static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006922 struct dentry *unused, struct inode *inode,
6923 const char *key, const void *buf,
6924 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006925{
Al Viro59301222016-05-27 10:19:30 -04006926 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006927}
6928
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006929static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006930 struct dentry *unused, struct inode *inode,
6931 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006932{
Al Virob2968212016-04-10 20:48:24 -04006933 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006934}
6935
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006936static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006937{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006938 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006939}
6940
David Quigleyc9bccef2013-05-22 12:50:45 -04006941#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006942
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006943static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006944 struct dentry *unused, struct inode *inode,
6945 const char *key, const void *buf,
6946 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006947{
6948 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006949 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006950
6951 return -EOPNOTSUPP;
6952}
6953
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006954static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006955 struct dentry *unused, struct inode *inode,
6956 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006957{
6958 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006959 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006960 return -EOPNOTSUPP;
6961}
6962
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006963static ssize_t
6964nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006965{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006966 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006967
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006968 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6969 len = security_inode_listsecurity(inode, list, list_len);
6970 if (list_len && len > list_len)
6971 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006972 }
6973 return len;
6974}
6975
6976static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6977 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006978 .get = nfs4_xattr_get_nfs4_label,
6979 .set = nfs4_xattr_set_nfs4_label,
6980};
David Quigleyc9bccef2013-05-22 12:50:45 -04006981
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006982#else
6983
6984static ssize_t
6985nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6986{
6987 return 0;
6988}
6989
6990#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006991
Andy Adamson533eb462011-06-13 18:25:56 -04006992/*
6993 * nfs_fhget will use either the mounted_on_fileid or the fileid
6994 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006995static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6996{
Andy Adamson533eb462011-06-13 18:25:56 -04006997 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6998 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6999 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007000 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007001 return;
7002
7003 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007004 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007005 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7006 fattr->nlink = 2;
7007}
7008
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007009static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7010 const struct qstr *name,
7011 struct nfs4_fs_locations *fs_locations,
7012 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007013{
7014 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007015 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007016 struct nfs4_fs_locations_arg args = {
7017 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007018 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007019 .page = page,
7020 .bitmask = bitmask,
7021 };
Benny Halevy22958462009-04-01 09:22:02 -04007022 struct nfs4_fs_locations_res res = {
7023 .fs_locations = fs_locations,
7024 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007025 struct rpc_message msg = {
7026 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7027 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007028 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007029 };
7030 int status;
7031
Harvey Harrison3110ff82008-05-02 13:42:44 -07007032 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007033
Chuck Leverc05cefc2017-11-05 15:45:22 -05007034 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7035 bitmask[1] = nfs4_fattr_bitmap[1];
7036
Andy Adamson533eb462011-06-13 18:25:56 -04007037 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7038 * is not supported */
7039 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007040 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007041 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007042 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007043
Trond Myklebustc228fd32007-01-13 02:28:11 -05007044 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007045 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007046 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007047 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007048 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007049 return status;
7050}
7051
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007052int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7053 const struct qstr *name,
7054 struct nfs4_fs_locations *fs_locations,
7055 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007056{
7057 struct nfs4_exception exception = { };
7058 int err;
7059 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007060 err = _nfs4_proc_fs_locations(client, dir, name,
7061 fs_locations, page);
7062 trace_nfs4_get_fs_locations(dir, name, err);
7063 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007064 &exception);
7065 } while (exception.retry);
7066 return err;
7067}
7068
Chuck Leverb03d7352013-10-17 14:12:50 -04007069/*
7070 * This operation also signals the server that this client is
7071 * performing migration recovery. The server can stop returning
7072 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7073 * appended to this compound to identify the client ID which is
7074 * performing recovery.
7075 */
7076static int _nfs40_proc_get_locations(struct inode *inode,
7077 struct nfs4_fs_locations *locations,
7078 struct page *page, struct rpc_cred *cred)
7079{
7080 struct nfs_server *server = NFS_SERVER(inode);
7081 struct rpc_clnt *clnt = server->client;
7082 u32 bitmask[2] = {
7083 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7084 };
7085 struct nfs4_fs_locations_arg args = {
7086 .clientid = server->nfs_client->cl_clientid,
7087 .fh = NFS_FH(inode),
7088 .page = page,
7089 .bitmask = bitmask,
7090 .migration = 1, /* skip LOOKUP */
7091 .renew = 1, /* append RENEW */
7092 };
7093 struct nfs4_fs_locations_res res = {
7094 .fs_locations = locations,
7095 .migration = 1,
7096 .renew = 1,
7097 };
7098 struct rpc_message msg = {
7099 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7100 .rpc_argp = &args,
7101 .rpc_resp = &res,
7102 .rpc_cred = cred,
7103 };
7104 unsigned long now = jiffies;
7105 int status;
7106
7107 nfs_fattr_init(&locations->fattr);
7108 locations->server = server;
7109 locations->nlocations = 0;
7110
Anna Schumakerfba83f32018-05-04 16:22:50 -04007111 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007112 status = nfs4_call_sync_sequence(clnt, server, &msg,
7113 &args.seq_args, &res.seq_res);
7114 if (status)
7115 return status;
7116
7117 renew_lease(server, now);
7118 return 0;
7119}
7120
7121#ifdef CONFIG_NFS_V4_1
7122
7123/*
7124 * This operation also signals the server that this client is
7125 * performing migration recovery. The server can stop asserting
7126 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7127 * performing this operation is identified in the SEQUENCE
7128 * operation in this compound.
7129 *
7130 * When the client supports GETATTR(fs_locations_info), it can
7131 * be plumbed in here.
7132 */
7133static int _nfs41_proc_get_locations(struct inode *inode,
7134 struct nfs4_fs_locations *locations,
7135 struct page *page, struct rpc_cred *cred)
7136{
7137 struct nfs_server *server = NFS_SERVER(inode);
7138 struct rpc_clnt *clnt = server->client;
7139 u32 bitmask[2] = {
7140 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7141 };
7142 struct nfs4_fs_locations_arg args = {
7143 .fh = NFS_FH(inode),
7144 .page = page,
7145 .bitmask = bitmask,
7146 .migration = 1, /* skip LOOKUP */
7147 };
7148 struct nfs4_fs_locations_res res = {
7149 .fs_locations = locations,
7150 .migration = 1,
7151 };
7152 struct rpc_message msg = {
7153 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7154 .rpc_argp = &args,
7155 .rpc_resp = &res,
7156 .rpc_cred = cred,
7157 };
7158 int status;
7159
7160 nfs_fattr_init(&locations->fattr);
7161 locations->server = server;
7162 locations->nlocations = 0;
7163
Anna Schumakerfba83f32018-05-04 16:22:50 -04007164 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Leverb03d7352013-10-17 14:12:50 -04007165 status = nfs4_call_sync_sequence(clnt, server, &msg,
7166 &args.seq_args, &res.seq_res);
7167 if (status == NFS4_OK &&
7168 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7169 status = -NFS4ERR_LEASE_MOVED;
7170 return status;
7171}
7172
7173#endif /* CONFIG_NFS_V4_1 */
7174
7175/**
7176 * nfs4_proc_get_locations - discover locations for a migrated FSID
7177 * @inode: inode on FSID that is migrating
7178 * @locations: result of query
7179 * @page: buffer
7180 * @cred: credential to use for this operation
7181 *
7182 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7183 * operation failed, or a negative errno if a local error occurred.
7184 *
7185 * On success, "locations" is filled in, but if the server has
7186 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7187 * asserted.
7188 *
7189 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7190 * from this client that require migration recovery.
7191 */
7192int nfs4_proc_get_locations(struct inode *inode,
7193 struct nfs4_fs_locations *locations,
7194 struct page *page, struct rpc_cred *cred)
7195{
7196 struct nfs_server *server = NFS_SERVER(inode);
7197 struct nfs_client *clp = server->nfs_client;
7198 const struct nfs4_mig_recovery_ops *ops =
7199 clp->cl_mvops->mig_recovery_ops;
7200 struct nfs4_exception exception = { };
7201 int status;
7202
7203 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7204 (unsigned long long)server->fsid.major,
7205 (unsigned long long)server->fsid.minor,
7206 clp->cl_hostname);
7207 nfs_display_fhandle(NFS_FH(inode), __func__);
7208
7209 do {
7210 status = ops->get_locations(inode, locations, page, cred);
7211 if (status != -NFS4ERR_DELAY)
7212 break;
7213 nfs4_handle_exception(server, status, &exception);
7214 } while (exception.retry);
7215 return status;
7216}
7217
Chuck Lever44c99932013-10-17 14:13:30 -04007218/*
7219 * This operation also signals the server that this client is
7220 * performing "lease moved" recovery. The server can stop
7221 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7222 * is appended to this compound to identify the client ID which is
7223 * performing recovery.
7224 */
7225static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7226{
7227 struct nfs_server *server = NFS_SERVER(inode);
7228 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7229 struct rpc_clnt *clnt = server->client;
7230 struct nfs4_fsid_present_arg args = {
7231 .fh = NFS_FH(inode),
7232 .clientid = clp->cl_clientid,
7233 .renew = 1, /* append RENEW */
7234 };
7235 struct nfs4_fsid_present_res res = {
7236 .renew = 1,
7237 };
7238 struct rpc_message msg = {
7239 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7240 .rpc_argp = &args,
7241 .rpc_resp = &res,
7242 .rpc_cred = cred,
7243 };
7244 unsigned long now = jiffies;
7245 int status;
7246
7247 res.fh = nfs_alloc_fhandle();
7248 if (res.fh == NULL)
7249 return -ENOMEM;
7250
Anna Schumakerfba83f32018-05-04 16:22:50 -04007251 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007252 status = nfs4_call_sync_sequence(clnt, server, &msg,
7253 &args.seq_args, &res.seq_res);
7254 nfs_free_fhandle(res.fh);
7255 if (status)
7256 return status;
7257
7258 do_renew_lease(clp, now);
7259 return 0;
7260}
7261
7262#ifdef CONFIG_NFS_V4_1
7263
7264/*
7265 * This operation also signals the server that this client is
7266 * performing "lease moved" recovery. The server can stop asserting
7267 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7268 * this operation is identified in the SEQUENCE operation in this
7269 * compound.
7270 */
7271static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7272{
7273 struct nfs_server *server = NFS_SERVER(inode);
7274 struct rpc_clnt *clnt = server->client;
7275 struct nfs4_fsid_present_arg args = {
7276 .fh = NFS_FH(inode),
7277 };
7278 struct nfs4_fsid_present_res res = {
7279 };
7280 struct rpc_message msg = {
7281 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7282 .rpc_argp = &args,
7283 .rpc_resp = &res,
7284 .rpc_cred = cred,
7285 };
7286 int status;
7287
7288 res.fh = nfs_alloc_fhandle();
7289 if (res.fh == NULL)
7290 return -ENOMEM;
7291
Anna Schumakerfba83f32018-05-04 16:22:50 -04007292 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Chuck Lever44c99932013-10-17 14:13:30 -04007293 status = nfs4_call_sync_sequence(clnt, server, &msg,
7294 &args.seq_args, &res.seq_res);
7295 nfs_free_fhandle(res.fh);
7296 if (status == NFS4_OK &&
7297 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7298 status = -NFS4ERR_LEASE_MOVED;
7299 return status;
7300}
7301
7302#endif /* CONFIG_NFS_V4_1 */
7303
7304/**
7305 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7306 * @inode: inode on FSID to check
7307 * @cred: credential to use for this operation
7308 *
7309 * Server indicates whether the FSID is present, moved, or not
7310 * recognized. This operation is necessary to clear a LEASE_MOVED
7311 * condition for this client ID.
7312 *
7313 * Returns NFS4_OK if the FSID is present on this server,
7314 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7315 * NFS4ERR code if some error occurred on the server, or a
7316 * negative errno if a local failure occurred.
7317 */
7318int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7319{
7320 struct nfs_server *server = NFS_SERVER(inode);
7321 struct nfs_client *clp = server->nfs_client;
7322 const struct nfs4_mig_recovery_ops *ops =
7323 clp->cl_mvops->mig_recovery_ops;
7324 struct nfs4_exception exception = { };
7325 int status;
7326
7327 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7328 (unsigned long long)server->fsid.major,
7329 (unsigned long long)server->fsid.minor,
7330 clp->cl_hostname);
7331 nfs_display_fhandle(NFS_FH(inode), __func__);
7332
7333 do {
7334 status = ops->fsid_present(inode, cred);
7335 if (status != -NFS4ERR_DELAY)
7336 break;
7337 nfs4_handle_exception(server, status, &exception);
7338 } while (exception.retry);
7339 return status;
7340}
7341
Andy Adamson5ec16a82013-08-08 10:57:55 -04007342/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007343 * If 'use_integrity' is true and the state managment nfs_client
7344 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7345 * and the machine credential as per RFC3530bis and RFC5661 Security
7346 * Considerations sections. Otherwise, just use the user cred with the
7347 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007348 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007349static 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 +00007350{
7351 int status;
7352 struct nfs4_secinfo_arg args = {
7353 .dir_fh = NFS_FH(dir),
7354 .name = name,
7355 };
7356 struct nfs4_secinfo_res res = {
7357 .flavors = flavors,
7358 };
7359 struct rpc_message msg = {
7360 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7361 .rpc_argp = &args,
7362 .rpc_resp = &res,
7363 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007364 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007365 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007366
7367 if (use_integrity) {
7368 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007369 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7370 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007371 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007372
7373 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007374
7375 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7376 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7377
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007378 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7379 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007380 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007381
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007382 if (cred)
7383 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007384
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007385 return status;
7386}
7387
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007388int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7389 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007390{
7391 struct nfs4_exception exception = { };
7392 int err;
7393 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007394 err = -NFS4ERR_WRONGSEC;
7395
7396 /* try to use integrity protection with machine cred */
7397 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7398 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7399
7400 /*
7401 * if unable to use integrity protection, or SECINFO with
7402 * integrity protection returns NFS4ERR_WRONGSEC (which is
7403 * disallowed by spec, but exists in deployed servers) use
7404 * the current filesystem's rpc_client and the user cred.
7405 */
7406 if (err == -NFS4ERR_WRONGSEC)
7407 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7408
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007409 trace_nfs4_secinfo(dir, name, err);
7410 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007411 &exception);
7412 } while (exception.retry);
7413 return err;
7414}
7415
Andy Adamson557134a2009-04-01 09:21:53 -04007416#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007417/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007418 * Check the exchange flags returned by the server for invalid flags, having
7419 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7420 * DS flags set.
7421 */
7422static int nfs4_check_cl_exchange_flags(u32 flags)
7423{
7424 if (flags & ~EXCHGID4_FLAG_MASK_R)
7425 goto out_inval;
7426 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7427 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7428 goto out_inval;
7429 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7430 goto out_inval;
7431 return NFS_OK;
7432out_inval:
7433 return -NFS4ERR_INVAL;
7434}
7435
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007436static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007437nfs41_same_server_scope(struct nfs41_server_scope *a,
7438 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007439{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007440 if (a->server_scope_sz != b->server_scope_sz)
7441 return false;
7442 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007443}
7444
Andy Adamson02a95de2016-02-05 16:08:37 -05007445static void
7446nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7447{
7448}
7449
7450static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7451 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7452};
7453
Andy Adamson357f54d2010-12-14 10:11:57 -05007454/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007455 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007456 *
7457 * The 4.1 client currently uses the same TCP connection for the
7458 * fore and backchannel.
7459 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007460static
7461int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7462 struct rpc_xprt *xprt,
7463 struct nfs_client *clp,
7464 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007465{
7466 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007467 struct nfs41_bind_conn_to_session_args args = {
7468 .client = clp,
7469 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7470 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007471 struct nfs41_bind_conn_to_session_res res;
7472 struct rpc_message msg = {
7473 .rpc_proc =
7474 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007475 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007476 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007477 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007478 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007479 struct rpc_task_setup task_setup_data = {
7480 .rpc_client = clnt,
7481 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007482 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007483 .rpc_message = &msg,
7484 .flags = RPC_TASK_TIMEOUT,
7485 };
7486 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007487
Trond Myklebust71a097c2015-02-18 09:27:18 -08007488 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7489 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7490 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007491
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007492 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7493 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7494 args.dir = NFS4_CDFC4_FORE;
7495
7496 task = rpc_run_task(&task_setup_data);
7497 if (!IS_ERR(task)) {
7498 status = task->tk_status;
7499 rpc_put_task(task);
7500 } else
7501 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007502 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007503 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007504 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007505 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7506 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007507 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007508 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007509 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007510 dprintk("NFS: %s: Unexpected direction from server\n",
7511 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007512 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007513 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007514 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007515 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7516 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007517 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007518 }
7519 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007520
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007521 return status;
7522}
7523
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007524struct rpc_bind_conn_calldata {
7525 struct nfs_client *clp;
7526 struct rpc_cred *cred;
7527};
7528
7529static int
7530nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7531 struct rpc_xprt *xprt,
7532 void *calldata)
7533{
7534 struct rpc_bind_conn_calldata *p = calldata;
7535
7536 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7537}
7538
7539int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7540{
7541 struct rpc_bind_conn_calldata data = {
7542 .clp = clp,
7543 .cred = cred,
7544 };
7545 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7546 nfs4_proc_bind_conn_to_session_callback, &data);
7547}
7548
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007549/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007550 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7551 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007552 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007553static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7554 .how = SP4_MACH_CRED,
7555 .enforce.u.words = {
7556 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7557 1 << (OP_EXCHANGE_ID - 32) |
7558 1 << (OP_CREATE_SESSION - 32) |
7559 1 << (OP_DESTROY_SESSION - 32) |
7560 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007561 },
7562 .allow.u.words = {
7563 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007564 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007565 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007566 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007567 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007568 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007569 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007570 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007571 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007572 1 << (OP_FREE_STATEID - 32) |
7573 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007574 }
7575};
7576
7577/*
7578 * Select the state protection mode for client `clp' given the server results
7579 * from exchange_id in `sp'.
7580 *
7581 * Returns 0 on success, negative errno otherwise.
7582 */
7583static int nfs4_sp4_select_mode(struct nfs_client *clp,
7584 struct nfs41_state_protection *sp)
7585{
7586 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7587 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7588 1 << (OP_EXCHANGE_ID - 32) |
7589 1 << (OP_CREATE_SESSION - 32) |
7590 1 << (OP_DESTROY_SESSION - 32) |
7591 1 << (OP_DESTROY_CLIENTID - 32)
7592 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007593 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007594 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007595 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007596
7597 if (sp->how == SP4_MACH_CRED) {
7598 /* Print state protect result */
7599 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7600 for (i = 0; i <= LAST_NFS4_OP; i++) {
7601 if (test_bit(i, sp->enforce.u.longs))
7602 dfprintk(MOUNT, " enforce op %d\n", i);
7603 if (test_bit(i, sp->allow.u.longs))
7604 dfprintk(MOUNT, " allow op %d\n", i);
7605 }
7606
7607 /* make sure nothing is on enforce list that isn't supported */
7608 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7609 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7610 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007611 ret = -EINVAL;
7612 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007613 }
7614 }
7615
7616 /*
7617 * Minimal mode - state operations are allowed to use machine
7618 * credential. Note this already happens by default, so the
7619 * client doesn't have to do anything more than the negotiation.
7620 *
7621 * NOTE: we don't care if EXCHANGE_ID is in the list -
7622 * we're already using the machine cred for exchange_id
7623 * and will never use a different cred.
7624 */
7625 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7626 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7627 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7628 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7629 dfprintk(MOUNT, "sp4_mach_cred:\n");
7630 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007631 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007632 } else {
7633 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007634 ret = -EINVAL;
7635 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007636 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007637
7638 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007639 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7640 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007641 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7642 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007643 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007644 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007645
Andrew Elble99ade3c2015-12-02 09:39:51 -05007646 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7647 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007648 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007649 }
7650
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007651 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7652 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7653 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007654 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007655 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007656
7657 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7658 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7659 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007660 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007661 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007662
7663 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7664 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007665 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007666 }
7667
7668 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7669 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007670 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007671 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007672 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007673out:
7674 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007675 return 0;
7676}
7677
Andy Adamson8d89bd72016-09-09 09:22:18 -04007678struct nfs41_exchange_id_data {
7679 struct nfs41_exchange_id_res res;
7680 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007681};
7682
Andy Adamson8d89bd72016-09-09 09:22:18 -04007683static void nfs4_exchange_id_release(void *data)
7684{
7685 struct nfs41_exchange_id_data *cdata =
7686 (struct nfs41_exchange_id_data *)data;
7687
Olga Kornievskaia63513232017-03-13 10:36:19 -04007688 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007689 kfree(cdata->res.impl_id);
7690 kfree(cdata->res.server_scope);
7691 kfree(cdata->res.server_owner);
7692 kfree(cdata);
7693}
7694
7695static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007696 .rpc_release = nfs4_exchange_id_release,
7697};
7698
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007699/*
7700 * _nfs4_proc_exchange_id()
7701 *
7702 * Wrapper for EXCHANGE_ID operation.
7703 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007704static struct rpc_task *
7705nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007706 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007707{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007708 struct rpc_message msg = {
7709 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007710 .rpc_cred = cred,
7711 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007712 struct rpc_task_setup task_setup_data = {
7713 .rpc_client = clp->cl_rpcclient,
7714 .callback_ops = &nfs4_exchange_id_call_ops,
7715 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007716 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007717 };
7718 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007719 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007720
Elena Reshetova212bf412017-10-20 12:53:38 +03007721 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007722 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007723
Trond Myklebust9c760d12017-07-31 18:38:50 -04007724 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007725 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007726 if (!calldata)
7727 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007728
Trond Myklebustfd405592017-08-01 16:02:47 -04007729 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007730
7731 status = nfs4_init_uniform_client_string(clp);
7732 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007733 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007734
Andy Adamson8d89bd72016-09-09 09:22:18 -04007735 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7736 GFP_NOFS);
7737 status = -ENOMEM;
7738 if (unlikely(calldata->res.server_owner == NULL))
7739 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007740
Andy Adamson8d89bd72016-09-09 09:22:18 -04007741 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007742 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007743 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007744 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007745
Andy Adamson8d89bd72016-09-09 09:22:18 -04007746 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7747 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007748 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007749
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007750 switch (sp4_how) {
7751 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007752 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007753 break;
7754
7755 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007756 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007757 break;
7758
7759 default:
7760 /* unsupported! */
7761 WARN_ON_ONCE(1);
7762 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007763 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007764 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007765 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007766 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007767 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007768 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7769 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007770 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007771 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007772 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7773 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007774#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007775 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007776#endif
7777 msg.rpc_argp = &calldata->args;
7778 msg.rpc_resp = &calldata->res;
7779 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007780
Trond Myklebust9c760d12017-07-31 18:38:50 -04007781 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007782
7783out_impl_id:
7784 kfree(calldata->res.impl_id);
7785out_server_scope:
7786 kfree(calldata->res.server_scope);
7787out_server_owner:
7788 kfree(calldata->res.server_owner);
7789out_calldata:
7790 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007791out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007792 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007793 return ERR_PTR(status);
7794}
7795
7796/*
7797 * _nfs4_proc_exchange_id()
7798 *
7799 * Wrapper for EXCHANGE_ID operation.
7800 */
7801static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7802 u32 sp4_how)
7803{
7804 struct rpc_task *task;
7805 struct nfs41_exchange_id_args *argp;
7806 struct nfs41_exchange_id_res *resp;
7807 int status;
7808
7809 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7810 if (IS_ERR(task))
7811 return PTR_ERR(task);
7812
7813 argp = task->tk_msg.rpc_argp;
7814 resp = task->tk_msg.rpc_resp;
7815 status = task->tk_status;
7816 if (status != 0)
7817 goto out;
7818
7819 status = nfs4_check_cl_exchange_flags(resp->flags);
7820 if (status != 0)
7821 goto out;
7822
7823 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7824 if (status != 0)
7825 goto out;
7826
7827 clp->cl_clientid = resp->clientid;
7828 clp->cl_exchange_flags = resp->flags;
7829 clp->cl_seqid = resp->seqid;
7830 /* Client ID is not confirmed */
7831 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7832 clear_bit(NFS4_SESSION_ESTABLISHED,
7833 &clp->cl_session->session_state);
7834
7835 if (clp->cl_serverscope != NULL &&
7836 !nfs41_same_server_scope(clp->cl_serverscope,
7837 resp->server_scope)) {
7838 dprintk("%s: server_scope mismatch detected\n",
7839 __func__);
7840 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7841 }
7842
7843 swap(clp->cl_serverowner, resp->server_owner);
7844 swap(clp->cl_serverscope, resp->server_scope);
7845 swap(clp->cl_implid, resp->impl_id);
7846
7847 /* Save the EXCHANGE_ID verifier session trunk tests */
7848 memcpy(clp->cl_confirm.data, argp->verifier.data,
7849 sizeof(clp->cl_confirm.data));
7850out:
7851 trace_nfs4_exchange_id(clp, status);
7852 rpc_put_task(task);
7853 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007854}
7855
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007856/*
7857 * nfs4_proc_exchange_id()
7858 *
7859 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7860 *
7861 * Since the clientid has expired, all compounds using sessions
7862 * associated with the stale clientid will be returning
7863 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7864 * be in some phase of session reset.
7865 *
7866 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7867 */
7868int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7869{
7870 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7871 int status;
7872
7873 /* try SP4_MACH_CRED if krb5i/p */
7874 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7875 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007876 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007877 if (!status)
7878 return 0;
7879 }
7880
7881 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007882 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007883}
7884
Andy Adamson04fa2c62016-09-09 09:22:29 -04007885/**
7886 * nfs4_test_session_trunk
7887 *
7888 * This is an add_xprt_test() test function called from
7889 * rpc_clnt_setup_test_and_add_xprt.
7890 *
7891 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7892 * and is dereferrenced in nfs4_exchange_id_release
7893 *
7894 * Upon success, add the new transport to the rpc_clnt
7895 *
7896 * @clnt: struct rpc_clnt to get new transport
7897 * @xprt: the rpc_xprt to test
7898 * @data: call data for _nfs4_proc_exchange_id.
7899 */
7900int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7901 void *data)
7902{
7903 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007904 struct rpc_task *task;
7905 int status;
7906
Andy Adamson04fa2c62016-09-09 09:22:29 -04007907 u32 sp4_how;
7908
7909 dprintk("--> %s try %s\n", __func__,
7910 xprt->address_strings[RPC_DISPLAY_ADDR]);
7911
7912 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7913
7914 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007915 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7916 if (IS_ERR(task))
7917 return PTR_ERR(task);
7918
7919 status = task->tk_status;
7920 if (status == 0)
7921 status = nfs4_detect_session_trunking(adata->clp,
7922 task->tk_msg.rpc_resp, xprt);
7923
7924 rpc_put_task(task);
7925 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007926}
7927EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7928
Trond Myklebust66245532012-05-25 17:18:09 -04007929static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7930 struct rpc_cred *cred)
7931{
7932 struct rpc_message msg = {
7933 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7934 .rpc_argp = clp,
7935 .rpc_cred = cred,
7936 };
7937 int status;
7938
7939 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007940 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007941 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007942 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007943 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7944 return status;
7945}
7946
7947static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7948 struct rpc_cred *cred)
7949{
7950 unsigned int loop;
7951 int ret;
7952
7953 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7954 ret = _nfs4_proc_destroy_clientid(clp, cred);
7955 switch (ret) {
7956 case -NFS4ERR_DELAY:
7957 case -NFS4ERR_CLIENTID_BUSY:
7958 ssleep(1);
7959 break;
7960 default:
7961 return ret;
7962 }
7963 }
7964 return 0;
7965}
7966
7967int nfs4_destroy_clientid(struct nfs_client *clp)
7968{
7969 struct rpc_cred *cred;
7970 int ret = 0;
7971
7972 if (clp->cl_mvops->minor_version < 1)
7973 goto out;
7974 if (clp->cl_exchange_flags == 0)
7975 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007976 if (clp->cl_preserve_clid)
7977 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007978 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007979 ret = nfs4_proc_destroy_clientid(clp, cred);
7980 if (cred)
7981 put_rpccred(cred);
7982 switch (ret) {
7983 case 0:
7984 case -NFS4ERR_STALE_CLIENTID:
7985 clp->cl_exchange_flags = 0;
7986 }
7987out:
7988 return ret;
7989}
7990
Andy Adamson2050f0c2009-04-01 09:22:30 -04007991struct nfs4_get_lease_time_data {
7992 struct nfs4_get_lease_time_args *args;
7993 struct nfs4_get_lease_time_res *res;
7994 struct nfs_client *clp;
7995};
7996
7997static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7998 void *calldata)
7999{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008000 struct nfs4_get_lease_time_data *data =
8001 (struct nfs4_get_lease_time_data *)calldata;
8002
8003 dprintk("--> %s\n", __func__);
8004 /* just setup sequence, do not trigger session recovery
8005 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008006 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008007 &data->args->la_seq_args,
8008 &data->res->lr_seq_res,
8009 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008010 dprintk("<-- %s\n", __func__);
8011}
8012
8013/*
8014 * Called from nfs4_state_manager thread for session setup, so don't recover
8015 * from sequence operation or clientid errors.
8016 */
8017static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8018{
8019 struct nfs4_get_lease_time_data *data =
8020 (struct nfs4_get_lease_time_data *)calldata;
8021
8022 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008023 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8024 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008025 switch (task->tk_status) {
8026 case -NFS4ERR_DELAY:
8027 case -NFS4ERR_GRACE:
8028 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8029 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8030 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008031 /* fall through */
8032 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008033 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008034 return;
8035 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008036 dprintk("<-- %s\n", __func__);
8037}
8038
Trond Myklebust17280172012-03-11 13:11:00 -04008039static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008040 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8041 .rpc_call_done = nfs4_get_lease_time_done,
8042};
8043
8044int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8045{
8046 struct rpc_task *task;
8047 struct nfs4_get_lease_time_args args;
8048 struct nfs4_get_lease_time_res res = {
8049 .lr_fsinfo = fsinfo,
8050 };
8051 struct nfs4_get_lease_time_data data = {
8052 .args = &args,
8053 .res = &res,
8054 .clp = clp,
8055 };
8056 struct rpc_message msg = {
8057 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8058 .rpc_argp = &args,
8059 .rpc_resp = &res,
8060 };
8061 struct rpc_task_setup task_setup = {
8062 .rpc_client = clp->cl_rpcclient,
8063 .rpc_message = &msg,
8064 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008065 .callback_data = &data,
8066 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008067 };
8068 int status;
8069
Anna Schumakerfba83f32018-05-04 16:22:50 -04008070 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0, 1);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008071 task = rpc_run_task(&task_setup);
8072
8073 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008074 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008075
Anna Schumakerf6148712017-04-07 14:15:23 -04008076 status = task->tk_status;
8077 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008078 return status;
8079}
8080
Andy Adamsonfc931582009-04-01 09:22:31 -04008081/*
8082 * Initialize the values to be used by the client in CREATE_SESSION
8083 * If nfs4_init_session set the fore channel request and response sizes,
8084 * use them.
8085 *
8086 * Set the back channel max_resp_sz_cached to zero to force the client to
8087 * always set csa_cachethis to FALSE because the current implementation
8088 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8089 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008090static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8091 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008092{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008093 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008094 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008095
Andy Adamson18aad3d2013-06-26 12:21:49 -04008096 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8097 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8098
Andy Adamsonfc931582009-04-01 09:22:31 -04008099 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008100 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8101 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008102 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008103 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008104
8105 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008106 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008107 __func__,
8108 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008109 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008110
8111 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008112 args->bc_attrs.max_rqst_sz = max_bc_payload;
8113 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008114 args->bc_attrs.max_resp_sz_cached = 0;
8115 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008116 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008117
8118 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8119 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8120 __func__,
8121 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8122 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8123 args->bc_attrs.max_reqs);
8124}
8125
Trond Myklebust79969dd2015-02-18 11:30:18 -08008126static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8127 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008128{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008129 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008130 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008131
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008132 if (rcvd->max_resp_sz > sent->max_resp_sz)
8133 return -EINVAL;
8134 /*
8135 * Our requested max_ops is the minimum we need; we're not
8136 * prepared to break up compounds into smaller pieces than that.
8137 * So, no point even trying to continue if the server won't
8138 * cooperate:
8139 */
8140 if (rcvd->max_ops < sent->max_ops)
8141 return -EINVAL;
8142 if (rcvd->max_reqs == 0)
8143 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008144 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8145 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008146 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008147}
8148
Trond Myklebust79969dd2015-02-18 11:30:18 -08008149static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8150 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008151{
8152 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008153 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008154
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008155 if (!(res->flags & SESSION4_BACK_CHAN))
8156 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008157 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8158 return -EINVAL;
8159 if (rcvd->max_resp_sz < sent->max_resp_sz)
8160 return -EINVAL;
8161 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8162 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008163 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008164 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008165 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008166 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008167out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008168 return 0;
8169}
Andy Adamson8d353012009-04-01 09:22:32 -04008170
Andy Adamson8d353012009-04-01 09:22:32 -04008171static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008172 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008173{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008174 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008175
Trond Myklebust79969dd2015-02-18 11:30:18 -08008176 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008177 if (ret)
8178 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008179 return nfs4_verify_back_channel_attrs(args, res);
8180}
8181
8182static void nfs4_update_session(struct nfs4_session *session,
8183 struct nfs41_create_session_res *res)
8184{
8185 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008186 /* Mark client id and session as being confirmed */
8187 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8188 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008189 session->flags = res->flags;
8190 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008191 if (res->flags & SESSION4_BACK_CHAN)
8192 memcpy(&session->bc_attrs, &res->bc_attrs,
8193 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008194}
8195
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008196static int _nfs4_proc_create_session(struct nfs_client *clp,
8197 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008198{
8199 struct nfs4_session *session = clp->cl_session;
8200 struct nfs41_create_session_args args = {
8201 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008202 .clientid = clp->cl_clientid,
8203 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008204 .cb_program = NFS4_CALLBACK,
8205 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008206 struct nfs41_create_session_res res;
8207
Andy Adamsonfc931582009-04-01 09:22:31 -04008208 struct rpc_message msg = {
8209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8210 .rpc_argp = &args,
8211 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008212 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008213 };
8214 int status;
8215
Chuck Lever6b26cc82016-05-02 14:40:40 -04008216 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008217 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008218
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008219 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008220 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008221
Trond Myklebustb519d402016-09-11 14:50:01 -04008222 switch (status) {
8223 case -NFS4ERR_STALE_CLIENTID:
8224 case -NFS4ERR_DELAY:
8225 case -ETIMEDOUT:
8226 case -EACCES:
8227 case -EAGAIN:
8228 goto out;
8229 };
8230
8231 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008232 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008233 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008234 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008235 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008236 if (status)
8237 goto out;
8238 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008239 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008240out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008241 return status;
8242}
8243
8244/*
8245 * Issues a CREATE_SESSION operation to the server.
8246 * It is the responsibility of the caller to verify the session is
8247 * expired before calling this routine.
8248 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008249int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008250{
8251 int status;
8252 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008253 struct nfs4_session *session = clp->cl_session;
8254
8255 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8256
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008257 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008258 if (status)
8259 goto out;
8260
Andy Adamsonaacd5532011-11-09 13:58:21 -05008261 /* Init or reset the session slot tables */
8262 status = nfs4_setup_session_slot_tables(session);
8263 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008264 if (status)
8265 goto out;
8266
8267 ptr = (unsigned *)&session->sess_id.data[0];
8268 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8269 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008270out:
8271 dprintk("<-- %s\n", __func__);
8272 return status;
8273}
8274
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008275/*
8276 * Issue the over-the-wire RPC DESTROY_SESSION.
8277 * The caller must serialize access to this routine.
8278 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008279int nfs4_proc_destroy_session(struct nfs4_session *session,
8280 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008281{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008282 struct rpc_message msg = {
8283 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8284 .rpc_argp = session,
8285 .rpc_cred = cred,
8286 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008287 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008288
8289 dprintk("--> nfs4_proc_destroy_session\n");
8290
8291 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008292 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8293 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008294
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008295 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008296 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008297
8298 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008299 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008300 "Session has been destroyed regardless...\n", status);
8301
8302 dprintk("<-- nfs4_proc_destroy_session\n");
8303 return status;
8304}
8305
Trond Myklebust7b38c362012-05-23 13:23:31 -04008306/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008307 * Renew the cl_session lease.
8308 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008309struct nfs4_sequence_data {
8310 struct nfs_client *clp;
8311 struct nfs4_sequence_args args;
8312 struct nfs4_sequence_res res;
8313};
8314
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008315static void nfs41_sequence_release(void *data)
8316{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008317 struct nfs4_sequence_data *calldata = data;
8318 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008319
Elena Reshetova212bf412017-10-20 12:53:38 +03008320 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008321 nfs4_schedule_state_renewal(clp);
8322 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008323 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008324}
8325
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008326static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8327{
8328 switch(task->tk_status) {
8329 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008330 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8331 return -EAGAIN;
8332 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008333 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008334 }
8335 return 0;
8336}
8337
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008338static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008339{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008340 struct nfs4_sequence_data *calldata = data;
8341 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008342
Trond Myklebust14516c32010-07-31 14:29:06 -04008343 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8344 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008345
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008346 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008347 if (task->tk_status < 0) {
8348 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008349 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008350 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008351
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008352 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8353 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008354 return;
8355 }
8356 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008357 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008358out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008359 dprintk("<-- %s\n", __func__);
8360}
8361
8362static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8363{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008364 struct nfs4_sequence_data *calldata = data;
8365 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008366 struct nfs4_sequence_args *args;
8367 struct nfs4_sequence_res *res;
8368
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008369 args = task->tk_msg.rpc_argp;
8370 res = task->tk_msg.rpc_resp;
8371
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008372 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008373}
8374
8375static const struct rpc_call_ops nfs41_sequence_ops = {
8376 .rpc_call_done = nfs41_sequence_call_done,
8377 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008378 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008379};
8380
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008381static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8382 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008383 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008384 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008385{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008386 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008387 struct rpc_message msg = {
8388 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8389 .rpc_cred = cred,
8390 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008391 struct rpc_task_setup task_setup_data = {
8392 .rpc_client = clp->cl_rpcclient,
8393 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008394 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008395 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008396 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008397 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008398
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008399 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008400 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008401 goto out_err;
8402
8403 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008404 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008405 if (calldata == NULL)
8406 goto out_put_clp;
Anna Schumakerfba83f32018-05-04 16:22:50 -04008407 nfs4_init_sequence(&calldata->args, &calldata->res, 0, is_privileged);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008408 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008409 msg.rpc_argp = &calldata->args;
8410 msg.rpc_resp = &calldata->res;
8411 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008412 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008413
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008414 ret = rpc_run_task(&task_setup_data);
8415 if (IS_ERR(ret))
8416 goto out_err;
8417 return ret;
8418out_put_clp:
8419 nfs_put_client(clp);
8420out_err:
8421 nfs41_release_slot(slot);
8422 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008423}
8424
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008425static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008426{
8427 struct rpc_task *task;
8428 int ret = 0;
8429
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008430 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008431 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008432 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008433 if (IS_ERR(task))
8434 ret = PTR_ERR(task);
8435 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008436 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008437 dprintk("<-- %s status=%d\n", __func__, ret);
8438 return ret;
8439}
8440
8441static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8442{
8443 struct rpc_task *task;
8444 int ret;
8445
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008446 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008447 if (IS_ERR(task)) {
8448 ret = PTR_ERR(task);
8449 goto out;
8450 }
8451 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008452 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008453 ret = task->tk_status;
8454 rpc_put_task(task);
8455out:
8456 dprintk("<-- %s status=%d\n", __func__, ret);
8457 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008458}
8459
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008460struct nfs4_reclaim_complete_data {
8461 struct nfs_client *clp;
8462 struct nfs41_reclaim_complete_args arg;
8463 struct nfs41_reclaim_complete_res res;
8464};
8465
8466static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8467{
8468 struct nfs4_reclaim_complete_data *calldata = data;
8469
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008470 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008471 &calldata->arg.seq_args,
8472 &calldata->res.seq_res,
8473 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008474}
8475
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008476static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8477{
8478 switch(task->tk_status) {
8479 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008480 wake_up_all(&clp->cl_lock_waitq);
8481 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008482 case -NFS4ERR_COMPLETE_ALREADY:
8483 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8484 break;
8485 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008486 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008487 /* fall through */
8488 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008489 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008490 case -NFS4ERR_BADSESSION:
8491 case -NFS4ERR_DEADSESSION:
8492 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8493 nfs4_schedule_session_recovery(clp->cl_session,
8494 task->tk_status);
8495 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008496 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008497 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008498 }
8499 return 0;
8500}
8501
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008502static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8503{
8504 struct nfs4_reclaim_complete_data *calldata = data;
8505 struct nfs_client *clp = calldata->clp;
8506 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8507
8508 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008509 if (!nfs41_sequence_done(task, res))
8510 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008511
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008512 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008513 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8514 rpc_restart_call_prepare(task);
8515 return;
8516 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008517 dprintk("<-- %s\n", __func__);
8518}
8519
8520static void nfs4_free_reclaim_complete_data(void *data)
8521{
8522 struct nfs4_reclaim_complete_data *calldata = data;
8523
8524 kfree(calldata);
8525}
8526
8527static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8528 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8529 .rpc_call_done = nfs4_reclaim_complete_done,
8530 .rpc_release = nfs4_free_reclaim_complete_data,
8531};
8532
8533/*
8534 * Issue a global reclaim complete.
8535 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008536static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8537 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008538{
8539 struct nfs4_reclaim_complete_data *calldata;
8540 struct rpc_task *task;
8541 struct rpc_message msg = {
8542 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008543 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008544 };
8545 struct rpc_task_setup task_setup_data = {
8546 .rpc_client = clp->cl_rpcclient,
8547 .rpc_message = &msg,
8548 .callback_ops = &nfs4_reclaim_complete_call_ops,
8549 .flags = RPC_TASK_ASYNC,
8550 };
8551 int status = -ENOMEM;
8552
8553 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008554 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008555 if (calldata == NULL)
8556 goto out;
8557 calldata->clp = clp;
8558 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008559
Anna Schumakerfba83f32018-05-04 16:22:50 -04008560 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0, 1);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008561 msg.rpc_argp = &calldata->arg;
8562 msg.rpc_resp = &calldata->res;
8563 task_setup_data.callback_data = calldata;
8564 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008565 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008566 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008567 goto out;
8568 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008569 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008570 if (status == 0)
8571 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008572 rpc_put_task(task);
8573out:
8574 dprintk("<-- %s status=%d\n", __func__, status);
8575 return status;
8576}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008577
8578static void
8579nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8580{
8581 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008582 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008583
8584 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008585 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008586 &lgp->res.seq_res, task);
8587 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008588}
8589
8590static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8591{
8592 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008593
8594 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008595 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008596 dprintk("<-- %s\n", __func__);
8597}
8598
8599static int
8600nfs4_layoutget_handle_exception(struct rpc_task *task,
8601 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8602{
Trond Myklebustee314c22012-10-01 17:25:48 -07008603 struct inode *inode = lgp->args.inode;
8604 struct nfs_server *server = NFS_SERVER(inode);
8605 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008606 int nfs4err = task->tk_status;
8607 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008608 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008609
Boaz Harroshed7e5422014-01-22 20:34:54 +02008610 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008611
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008612 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008613 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008614 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008615
8616 /*
8617 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8618 * on the file. set tk_status to -ENODATA to tell upper layer to
8619 * retry go inband.
8620 */
8621 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008622 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008623 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008624 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008625 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8626 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8627 */
8628 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008629 status = -EOVERFLOW;
8630 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008631 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008632 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008633 * (or clients) writing to the same RAID stripe except when
8634 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008635 *
8636 * Treat it like we would RECALLCONFLICT -- we retry for a little
8637 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008638 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008639 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008640 if (lgp->args.minlength == 0) {
8641 status = -EOVERFLOW;
8642 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008643 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008644 status = -EBUSY;
8645 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008646 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008647 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008648 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008649 case -NFS4ERR_DELEG_REVOKED:
8650 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008651 case -NFS4ERR_EXPIRED:
8652 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008653 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008654 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008655 lo = NFS_I(inode)->layout;
8656 /* If the open stateid was bad, then recover it. */
8657 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008658 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008659 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008660 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008661 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008662 break;
8663 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008664
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008665 /*
8666 * Mark the bad layout state as invalid, then retry
8667 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008668 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008669 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008670 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008671 pnfs_free_lseg_list(&head);
8672 status = -EAGAIN;
8673 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008674 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008675
Trond Myklebust8ac09252017-01-23 22:44:12 -05008676 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008677 err = nfs4_handle_exception(server, nfs4err, exception);
8678 if (!status) {
8679 if (exception->retry)
8680 status = -EAGAIN;
8681 else
8682 status = err;
8683 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008684out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008685 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008686 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008687}
8688
Fred Isamandacb4522016-09-19 17:47:09 -04008689size_t max_response_pages(struct nfs_server *server)
Idan Kedar85541162012-08-02 11:47:10 +03008690{
8691 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8692 return nfs_page_array_len(0, max_resp_sz);
8693}
8694
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008695static void nfs4_layoutget_release(void *calldata)
8696{
8697 struct nfs4_layoutget *lgp = calldata;
8698
8699 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008700 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebust29a8bfe2018-05-30 17:16:20 -04008701 pnfs_layoutget_free(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008702 dprintk("<-- %s\n", __func__);
8703}
8704
8705static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8706 .rpc_call_prepare = nfs4_layoutget_prepare,
8707 .rpc_call_done = nfs4_layoutget_done,
8708 .rpc_release = nfs4_layoutget_release,
8709};
8710
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008711struct pnfs_layout_segment *
Fred Isamandacb4522016-09-19 17:47:09 -04008712nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008713{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008714 struct inode *inode = lgp->args.inode;
8715 struct nfs_server *server = NFS_SERVER(inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008716 struct rpc_task *task;
8717 struct rpc_message msg = {
8718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8719 .rpc_argp = &lgp->args,
8720 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008721 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008722 };
8723 struct rpc_task_setup task_setup_data = {
8724 .rpc_client = server->client,
8725 .rpc_message = &msg,
8726 .callback_ops = &nfs4_layoutget_call_ops,
8727 .callback_data = lgp,
8728 .flags = RPC_TASK_ASYNC,
8729 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008730 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008731 struct nfs4_exception exception = {
8732 .inode = inode,
8733 .timeout = *timeout,
8734 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008735 int status = 0;
8736
8737 dprintk("--> %s\n", __func__);
8738
Peng Tao4bd5a982014-11-17 11:05:17 +08008739 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8740 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8741
Anna Schumakerfba83f32018-05-04 16:22:50 -04008742 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008743
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008744 task = rpc_run_task(&task_setup_data);
8745 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008746 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008747 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008748 if (status == 0) {
8749 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8750 *timeout = exception.timeout;
8751 }
8752
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008753 trace_nfs4_layoutget(lgp->args.ctx,
8754 &lgp->args.range,
8755 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008756 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008757 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008758
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008759 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8760 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008761 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008762 rpc_put_task(task);
8763 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008764 if (status)
8765 return ERR_PTR(status);
8766 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008767}
8768
Benny Halevycbe82602011-05-22 19:52:37 +03008769static void
8770nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8771{
8772 struct nfs4_layoutreturn *lrp = calldata;
8773
8774 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008775 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008776 &lrp->args.seq_args,
8777 &lrp->res.seq_res,
8778 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008779}
8780
8781static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8782{
8783 struct nfs4_layoutreturn *lrp = calldata;
8784 struct nfs_server *server;
8785
8786 dprintk("--> %s\n", __func__);
8787
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008788 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008789 return;
8790
8791 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008792 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008793 case -NFS4ERR_OLD_STATEID:
8794 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8795 lrp->args.inode))
8796 goto out_restart;
8797 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008798 default:
8799 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008800 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008801 case 0:
8802 break;
8803 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008804 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008805 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008806 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008807 }
Benny Halevycbe82602011-05-22 19:52:37 +03008808 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008809 return;
8810out_restart:
8811 task->tk_status = 0;
8812 nfs4_sequence_free_slot(&lrp->res.seq_res);
8813 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008814}
8815
8816static void nfs4_layoutreturn_release(void *calldata)
8817{
8818 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008819 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008820
8821 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008822 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008823 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008824 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008825 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8826 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008827 pnfs_put_layout_hdr(lrp->args.layout);
8828 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008829 kfree(calldata);
8830 dprintk("<-- %s\n", __func__);
8831}
8832
8833static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8834 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8835 .rpc_call_done = nfs4_layoutreturn_done,
8836 .rpc_release = nfs4_layoutreturn_release,
8837};
8838
Peng Tao6c166052014-11-17 09:30:40 +08008839int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008840{
8841 struct rpc_task *task;
8842 struct rpc_message msg = {
8843 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8844 .rpc_argp = &lrp->args,
8845 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008846 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008847 };
8848 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008849 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008850 .rpc_message = &msg,
8851 .callback_ops = &nfs4_layoutreturn_call_ops,
8852 .callback_data = lrp,
8853 };
Peng Tao6c166052014-11-17 09:30:40 +08008854 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008855
Andrew Elble99ade3c2015-12-02 09:39:51 -05008856 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8857 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8858 &task_setup_data.rpc_client, &msg);
8859
Benny Halevycbe82602011-05-22 19:52:37 +03008860 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008861 if (!sync) {
8862 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8863 if (!lrp->inode) {
8864 nfs4_layoutreturn_release(lrp);
8865 return -EAGAIN;
8866 }
8867 task_setup_data.flags |= RPC_TASK_ASYNC;
8868 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04008869 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1, 0);
Benny Halevycbe82602011-05-22 19:52:37 +03008870 task = rpc_run_task(&task_setup_data);
8871 if (IS_ERR(task))
8872 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008873 if (sync)
8874 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008875 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008876 dprintk("<-- %s status=%d\n", __func__, status);
8877 rpc_put_task(task);
8878 return status;
8879}
8880
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008881static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008882_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8883 struct pnfs_device *pdev,
8884 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008885{
8886 struct nfs4_getdeviceinfo_args args = {
8887 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008888 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8889 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008890 };
8891 struct nfs4_getdeviceinfo_res res = {
8892 .pdev = pdev,
8893 };
8894 struct rpc_message msg = {
8895 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8896 .rpc_argp = &args,
8897 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008898 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008899 };
8900 int status;
8901
8902 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008903 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008904 if (res.notification & ~args.notify_types)
8905 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008906 if (res.notification != args.notify_types)
8907 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008908
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008909 dprintk("<-- %s status=%d\n", __func__, status);
8910
8911 return status;
8912}
8913
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008914int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8915 struct pnfs_device *pdev,
8916 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008917{
8918 struct nfs4_exception exception = { };
8919 int err;
8920
8921 do {
8922 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008923 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008924 &exception);
8925 } while (exception.retry);
8926 return err;
8927}
8928EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8929
Andy Adamson863a3c62011-03-23 13:27:54 +00008930static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8931{
8932 struct nfs4_layoutcommit_data *data = calldata;
8933 struct nfs_server *server = NFS_SERVER(data->args.inode);
8934
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008935 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008936 &data->args.seq_args,
8937 &data->res.seq_res,
8938 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008939}
8940
8941static void
8942nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8943{
8944 struct nfs4_layoutcommit_data *data = calldata;
8945 struct nfs_server *server = NFS_SERVER(data->args.inode);
8946
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008947 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008948 return;
8949
8950 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008951 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8952 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8953 case -NFS4ERR_BADLAYOUT: /* no layout */
8954 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008955 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008956 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008957 break;
8958 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008959 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008960 rpc_restart_call_prepare(task);
8961 return;
8962 }
8963 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008964}
8965
8966static void nfs4_layoutcommit_release(void *calldata)
8967{
8968 struct nfs4_layoutcommit_data *data = calldata;
8969
Andy Adamsondb29c082011-07-30 20:52:38 -04008970 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05008971 nfs_post_op_update_inode_force_wcc(data->args.inode,
8972 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00008973 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05008974 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00008975 kfree(data);
8976}
8977
8978static const struct rpc_call_ops nfs4_layoutcommit_ops = {
8979 .rpc_call_prepare = nfs4_layoutcommit_prepare,
8980 .rpc_call_done = nfs4_layoutcommit_done,
8981 .rpc_release = nfs4_layoutcommit_release,
8982};
8983
8984int
Andy Adamsonef311532011-03-12 02:58:10 -05008985nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00008986{
8987 struct rpc_message msg = {
8988 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
8989 .rpc_argp = &data->args,
8990 .rpc_resp = &data->res,
8991 .rpc_cred = data->cred,
8992 };
8993 struct rpc_task_setup task_setup_data = {
8994 .task = &data->task,
8995 .rpc_client = NFS_CLIENT(data->args.inode),
8996 .rpc_message = &msg,
8997 .callback_ops = &nfs4_layoutcommit_ops,
8998 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00008999 };
9000 struct rpc_task *task;
9001 int status = 0;
9002
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009003 dprintk("NFS: initiating layoutcommit call. sync %d "
9004 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009005 data->args.lastbytewritten,
9006 data->args.inode->i_ino);
9007
Trond Myklebust472e2592015-02-05 16:50:30 -05009008 if (!sync) {
9009 data->inode = nfs_igrab_and_active(data->args.inode);
9010 if (data->inode == NULL) {
9011 nfs4_layoutcommit_release(data);
9012 return -EAGAIN;
9013 }
9014 task_setup_data.flags = RPC_TASK_ASYNC;
9015 }
Anna Schumakerfba83f32018-05-04 16:22:50 -04009016 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, 0);
Andy Adamson863a3c62011-03-23 13:27:54 +00009017 task = rpc_run_task(&task_setup_data);
9018 if (IS_ERR(task))
9019 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009020 if (sync)
9021 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009022 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009023 dprintk("%s: status %d\n", __func__, status);
9024 rpc_put_task(task);
9025 return status;
9026}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009027
Andy Adamson97431202013-08-08 10:57:56 -04009028/**
9029 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9030 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9031 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009032static int
9033_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009034 struct nfs_fsinfo *info,
9035 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009036{
9037 struct nfs41_secinfo_no_name_args args = {
9038 .style = SECINFO_STYLE_CURRENT_FH,
9039 };
9040 struct nfs4_secinfo_res res = {
9041 .flavors = flavors,
9042 };
9043 struct rpc_message msg = {
9044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9045 .rpc_argp = &args,
9046 .rpc_resp = &res,
9047 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009048 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009049 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009050 int status;
9051
9052 if (use_integrity) {
9053 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009054 cred = nfs4_get_clid_cred(server->nfs_client);
9055 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009056 }
9057
9058 dprintk("--> %s\n", __func__);
9059 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9060 &res.seq_res, 0);
9061 dprintk("<-- %s status=%d\n", __func__, status);
9062
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009063 if (cred)
9064 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009065
9066 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009067}
9068
9069static int
9070nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9071 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9072{
9073 struct nfs4_exception exception = { };
9074 int err;
9075 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009076 /* first try using integrity protection */
9077 err = -NFS4ERR_WRONGSEC;
9078
9079 /* try to use integrity protection with machine cred */
9080 if (_nfs4_is_integrity_protected(server->nfs_client))
9081 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9082 flavors, true);
9083
9084 /*
9085 * if unable to use integrity protection, or SECINFO with
9086 * integrity protection returns NFS4ERR_WRONGSEC (which is
9087 * disallowed by spec, but exists in deployed servers) use
9088 * the current filesystem's rpc_client and the user cred.
9089 */
9090 if (err == -NFS4ERR_WRONGSEC)
9091 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9092 flavors, false);
9093
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009094 switch (err) {
9095 case 0:
9096 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009097 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009098 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009099 default:
9100 err = nfs4_handle_exception(server, err, &exception);
9101 }
9102 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009103out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009104 return err;
9105}
9106
9107static int
9108nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9109 struct nfs_fsinfo *info)
9110{
9111 int err;
9112 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009113 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009114 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009115 struct nfs4_secinfo4 *secinfo;
9116 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009117
9118 page = alloc_page(GFP_KERNEL);
9119 if (!page) {
9120 err = -ENOMEM;
9121 goto out;
9122 }
9123
9124 flavors = page_address(page);
9125 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9126
9127 /*
9128 * Fall back on "guess and check" method if
9129 * the server doesn't support SECINFO_NO_NAME
9130 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009131 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009132 err = nfs4_find_root_sec(server, fhandle, info);
9133 goto out_freepage;
9134 }
9135 if (err)
9136 goto out_freepage;
9137
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009138 for (i = 0; i < flavors->num_flavors; i++) {
9139 secinfo = &flavors->flavors[i];
9140
9141 switch (secinfo->flavor) {
9142 case RPC_AUTH_NULL:
9143 case RPC_AUTH_UNIX:
9144 case RPC_AUTH_GSS:
9145 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9146 &secinfo->flavor_info);
9147 break;
9148 default:
9149 flavor = RPC_AUTH_MAXFLAVOR;
9150 break;
9151 }
9152
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009153 if (!nfs_auth_info_match(&server->auth_info, flavor))
9154 flavor = RPC_AUTH_MAXFLAVOR;
9155
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009156 if (flavor != RPC_AUTH_MAXFLAVOR) {
9157 err = nfs4_lookup_root_sec(server, fhandle,
9158 info, flavor);
9159 if (!err)
9160 break;
9161 }
9162 }
9163
9164 if (flavor == RPC_AUTH_MAXFLAVOR)
9165 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009166
9167out_freepage:
9168 put_page(page);
9169 if (err == -EACCES)
9170 return -EPERM;
9171out:
9172 return err;
9173}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009174
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009175static int _nfs41_test_stateid(struct nfs_server *server,
9176 nfs4_stateid *stateid,
9177 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009178{
9179 int status;
9180 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009181 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009182 };
9183 struct nfs41_test_stateid_res res;
9184 struct rpc_message msg = {
9185 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9186 .rpc_argp = &args,
9187 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009188 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009189 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009190 struct rpc_clnt *rpc_client = server->client;
9191
9192 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9193 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009194
Chuck Lever38527b12012-07-11 16:30:23 -04009195 dprintk("NFS call test_stateid %p\n", stateid);
Anna Schumakerfba83f32018-05-04 16:22:50 -04009196 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0, 1);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009197 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009198 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009199 if (status != NFS_OK) {
9200 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009201 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009202 }
9203 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009204 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009205}
9206
Trond Myklebust43912bb2016-09-22 13:38:56 -04009207static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9208 int err, struct nfs4_exception *exception)
9209{
9210 exception->retry = 0;
9211 switch(err) {
9212 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009213 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009214 nfs4_handle_exception(server, err, exception);
9215 break;
9216 case -NFS4ERR_BADSESSION:
9217 case -NFS4ERR_BADSLOT:
9218 case -NFS4ERR_BAD_HIGH_SLOT:
9219 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9220 case -NFS4ERR_DEADSESSION:
9221 nfs4_do_handle_exception(server, err, exception);
9222 }
9223}
9224
Chuck Lever38527b12012-07-11 16:30:23 -04009225/**
9226 * nfs41_test_stateid - perform a TEST_STATEID operation
9227 *
9228 * @server: server / transport on which to perform the operation
9229 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009230 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009231 *
9232 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9233 * Otherwise a negative NFS4ERR value is returned if the operation
9234 * failed or the state ID is not currently valid.
9235 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009236static int nfs41_test_stateid(struct nfs_server *server,
9237 nfs4_stateid *stateid,
9238 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009239{
9240 struct nfs4_exception exception = { };
9241 int err;
9242 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009243 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009244 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009245 } while (exception.retry);
9246 return err;
9247}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009248
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009249struct nfs_free_stateid_data {
9250 struct nfs_server *server;
9251 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009252 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009253};
9254
9255static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9256{
9257 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009258 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009259 &data->args.seq_args,
9260 &data->res.seq_res,
9261 task);
9262}
9263
9264static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9265{
9266 struct nfs_free_stateid_data *data = calldata;
9267
9268 nfs41_sequence_done(task, &data->res.seq_res);
9269
9270 switch (task->tk_status) {
9271 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009272 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009273 rpc_restart_call_prepare(task);
9274 }
9275}
9276
9277static void nfs41_free_stateid_release(void *calldata)
9278{
9279 kfree(calldata);
9280}
9281
Trond Myklebust17f26b12013-08-21 15:48:42 -04009282static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009283 .rpc_call_prepare = nfs41_free_stateid_prepare,
9284 .rpc_call_done = nfs41_free_stateid_done,
9285 .rpc_release = nfs41_free_stateid_release,
9286};
9287
Anna Schumaker2f261022018-05-15 13:03:39 -04009288/**
9289 * nfs41_free_stateid - perform a FREE_STATEID operation
9290 *
9291 * @server: server / transport on which to perform the operation
9292 * @stateid: state ID to release
9293 * @cred: credential
9294 * @is_recovery: set to true if this call needs to be privileged
9295 *
9296 * Note: this function is always asynchronous.
9297 */
9298static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009299 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009300 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009301 bool privileged)
9302{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009303 struct rpc_message msg = {
9304 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009305 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009306 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009307 struct rpc_task_setup task_setup = {
9308 .rpc_client = server->client,
9309 .rpc_message = &msg,
9310 .callback_ops = &nfs41_free_stateid_ops,
9311 .flags = RPC_TASK_ASYNC,
9312 };
9313 struct nfs_free_stateid_data *data;
Anna Schumaker2f261022018-05-15 13:03:39 -04009314 struct rpc_task *task;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009315
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009316 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9317 &task_setup.rpc_client, &msg);
9318
Chuck Lever38527b12012-07-11 16:30:23 -04009319 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009320 data = kmalloc(sizeof(*data), GFP_NOFS);
9321 if (!data)
Anna Schumaker2f261022018-05-15 13:03:39 -04009322 return -ENOMEM;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009323 data->server = server;
9324 nfs4_stateid_copy(&data->args.stateid, stateid);
9325
9326 task_setup.callback_data = data;
9327
9328 msg.rpc_argp = &data->args;
9329 msg.rpc_resp = &data->res;
Anna Schumakerfba83f32018-05-04 16:22:50 -04009330 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1, privileged);
Anna Schumaker2f261022018-05-15 13:03:39 -04009331 task = rpc_run_task(&task_setup);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009332 if (IS_ERR(task))
9333 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009334 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009335 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009336}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009337
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009338static void
9339nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009340{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009341 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009342
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009343 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009344 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009345}
9346
Trond Myklebust36281ca2012-03-04 18:13:56 -05009347static bool nfs41_match_stateid(const nfs4_stateid *s1,
9348 const nfs4_stateid *s2)
9349{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009350 if (s1->type != s2->type)
9351 return false;
9352
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009353 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009354 return false;
9355
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009356 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009357 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009358
Anna Schumaker045c5512017-01-11 16:59:48 -05009359 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009360}
9361
Andy Adamson557134a2009-04-01 09:21:53 -04009362#endif /* CONFIG_NFS_V4_1 */
9363
Trond Myklebust36281ca2012-03-04 18:13:56 -05009364static bool nfs4_match_stateid(const nfs4_stateid *s1,
9365 const nfs4_stateid *s2)
9366{
Trond Myklebustf597c532012-03-04 18:13:56 -05009367 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009368}
9369
9370
Trond Myklebust17280172012-03-11 13:11:00 -04009371static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009372 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009373 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009374 .recover_open = nfs4_open_reclaim,
9375 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009376 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009377 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009378};
9379
Andy Adamson591d71c2009-04-01 09:22:47 -04009380#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009381static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009382 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9383 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9384 .recover_open = nfs4_open_reclaim,
9385 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009386 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009387 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009388 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009389};
9390#endif /* CONFIG_NFS_V4_1 */
9391
Trond Myklebust17280172012-03-11 13:11:00 -04009392static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009393 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009394 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009395 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009396 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009397 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009398};
9399
Andy Adamson591d71c2009-04-01 09:22:47 -04009400#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009401static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009402 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9403 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009404 .recover_open = nfs41_open_expired,
9405 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009406 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009407};
9408#endif /* CONFIG_NFS_V4_1 */
9409
Trond Myklebust17280172012-03-11 13:11:00 -04009410static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009411 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009412 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009413 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009414};
9415
9416#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009417static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009418 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009419 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009420 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009421};
9422#endif
9423
Chuck Leverec011fe2013-10-17 14:12:39 -04009424static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009425 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009426 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009427};
9428
9429#if defined(CONFIG_NFS_V4_1)
9430static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009431 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009432 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009433};
9434#endif /* CONFIG_NFS_V4_1 */
9435
Trond Myklebust97dc1352010-06-16 09:52:26 -04009436static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9437 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009438 .init_caps = NFS_CAP_READDIRPLUS
9439 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009440 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009441 .init_client = nfs40_init_client,
9442 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009443 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009444 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009445 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009446 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009447 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009448 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009449 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9450 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9451 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009452 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009453};
9454
9455#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009456static struct nfs_seqid *
9457nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9458{
9459 return NULL;
9460}
9461
Trond Myklebust97dc1352010-06-16 09:52:26 -04009462static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9463 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009464 .init_caps = NFS_CAP_READDIRPLUS
9465 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009466 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009467 | NFS_CAP_STATEID_NFSV41
Fred Isaman6e012602016-10-04 15:26:41 -04009468 | NFS_CAP_ATOMIC_OPEN_V1
9469 | NFS_CAP_LGOPEN,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009470 .init_client = nfs41_init_client,
9471 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009472 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009473 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009474 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009475 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009476 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009477 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009478 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009479 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9480 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9481 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009482 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009483};
9484#endif
9485
Steve Dickson42c2c422013-05-22 12:50:38 -04009486#if defined(CONFIG_NFS_V4_2)
9487static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9488 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009489 .init_caps = NFS_CAP_READDIRPLUS
9490 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009491 | NFS_CAP_POSIX_LOCK
9492 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009493 | NFS_CAP_ATOMIC_OPEN_V1
Fred Isaman6e012602016-10-04 15:26:41 -04009494 | NFS_CAP_LGOPEN
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009495 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009496 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009497 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009498 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009499 | NFS_CAP_LAYOUTSTATS
9500 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009501 .init_client = nfs41_init_client,
9502 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009503 .match_stateid = nfs41_match_stateid,
9504 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009505 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009506 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009507 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009508 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009509 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009510 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9511 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9512 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009513 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009514};
9515#endif
9516
Trond Myklebust97dc1352010-06-16 09:52:26 -04009517const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9518 [0] = &nfs_v4_0_minor_ops,
9519#if defined(CONFIG_NFS_V4_1)
9520 [1] = &nfs_v4_1_minor_ops,
9521#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009522#if defined(CONFIG_NFS_V4_2)
9523 [2] = &nfs_v4_2_minor_ops,
9524#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009525};
9526
Trond Myklebust13997822016-07-24 17:10:52 -04009527static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009528{
9529 ssize_t error, error2;
9530
9531 error = generic_listxattr(dentry, list, size);
9532 if (error < 0)
9533 return error;
9534 if (list) {
9535 list += error;
9536 size -= error;
9537 }
9538
9539 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9540 if (error2 < 0)
9541 return error2;
9542 return error + error2;
9543}
9544
Trond Myklebust17f26b12013-08-21 15:48:42 -04009545static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009546 .create = nfs_create,
9547 .lookup = nfs_lookup,
9548 .atomic_open = nfs_atomic_open,
9549 .link = nfs_link,
9550 .unlink = nfs_unlink,
9551 .symlink = nfs_symlink,
9552 .mkdir = nfs_mkdir,
9553 .rmdir = nfs_rmdir,
9554 .mknod = nfs_mknod,
9555 .rename = nfs_rename,
9556 .permission = nfs_permission,
9557 .getattr = nfs_getattr,
9558 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009559 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009560};
9561
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009562static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009563 .permission = nfs_permission,
9564 .getattr = nfs_getattr,
9565 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009566 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009567};
9568
David Howells509de812006-08-22 20:06:11 -04009569const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009570 .version = 4, /* protocol version */
9571 .dentry_ops = &nfs4_dentry_operations,
9572 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009573 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009574 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009575 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009576 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009577 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009578 .getattr = nfs4_proc_getattr,
9579 .setattr = nfs4_proc_setattr,
9580 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009581 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009582 .access = nfs4_proc_access,
9583 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009584 .create = nfs4_proc_create,
9585 .remove = nfs4_proc_remove,
9586 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009587 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009588 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009589 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009590 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009591 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009592 .link = nfs4_proc_link,
9593 .symlink = nfs4_proc_symlink,
9594 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009595 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009596 .readdir = nfs4_proc_readdir,
9597 .mknod = nfs4_proc_mknod,
9598 .statfs = nfs4_proc_statfs,
9599 .fsinfo = nfs4_proc_fsinfo,
9600 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009601 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009602 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009603 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009604 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009605 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009606 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009607 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009608 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009609 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009610 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009612 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009613 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009614 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009615 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009616 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009617 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009618 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009619 .create_server = nfs4_create_server,
9620 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009621};
9622
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009623static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009624 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009625 .list = nfs4_xattr_list_nfs4_acl,
9626 .get = nfs4_xattr_get_nfs4_acl,
9627 .set = nfs4_xattr_set_nfs4_acl,
9628};
9629
9630const struct xattr_handler *nfs4_xattr_handlers[] = {
9631 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009632#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9633 &nfs4_xattr_nfs4_label_handler,
9634#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009635 NULL
9636};
9637
Linus Torvalds1da177e2005-04-16 15:20:36 -07009638/*
9639 * Local variables:
9640 * c-basic-offset: 8
9641 * End:
9642 */