blob: 09be575e9194b2f7e7670deae4fe3e7953e98831 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400100static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
101 struct rpc_cred *cred,
102 struct nfs4_slot *slot,
103 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400104static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
105 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400106static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
107 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400108#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400109
110#ifdef CONFIG_NFS_V4_SECURITY_LABEL
111static inline struct nfs4_label *
112nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
114{
115 int err;
116
117 if (label == NULL)
118 return NULL;
119
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
121 return NULL;
122
David Quigleyaa9c2662013-05-22 12:50:44 -0400123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
125 if (err == 0)
126 return label;
127
128 return NULL;
129}
130static inline void
131nfs4_label_release_security(struct nfs4_label *label)
132{
133 if (label)
134 security_release_secctx(label->label, label->len);
135}
136static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{
138 if (label)
139 return server->attr_bitmask;
140
141 return server->attr_bitmask_nl;
142}
143#else
144static inline struct nfs4_label *
145nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
147{ return NULL; }
148static inline void
149nfs4_label_release_security(struct nfs4_label *label)
150{ return; }
151static inline u32 *
152nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153{ return server->attr_bitmask; }
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500157static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebust52567b02009-10-23 14:46:42 -0400159 if (err >= -1000)
160 return err;
161 switch (err) {
162 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000166 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400167 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000168 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
171 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400172 case -NFS4ERR_SHARE_DENIED:
173 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400176 case -NFS4ERR_FILE_OPEN:
177 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700180 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * This is our standard bitmap for GETATTR requests.
188 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400189const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Trond Myklebust1549210f2012-06-05 09:16:47 -0400210static const u32 nfs4_pnfs_open_bitmap[3] = {
211 FATTR4_WORD0_TYPE
212 | FATTR4_WORD0_CHANGE
213 | FATTR4_WORD0_SIZE
214 | FATTR4_WORD0_FSID
215 | FATTR4_WORD0_FILEID,
216 FATTR4_WORD1_MODE
217 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
228#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400229};
230
Andy Adamsone23008e2012-10-02 21:07:32 -0400231static const u32 nfs4_open_noattr_bitmap[3] = {
232 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400233 | FATTR4_WORD0_FILEID,
234};
235
David Quigleya09df2c2013-05-22 12:50:41 -0400236const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_MAXLINK
247 | FATTR4_WORD0_MAXNAME,
248 0
249};
250
Fred Isamandae100c2011-07-30 20:52:37 -0400251const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700255 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800258 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
David Quigleya09df2c2013-05-22 12:50:41 -0400261const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500262 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400280 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700281 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000284 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
286 return;
287 }
288
289 readdir->cookie = 0;
290 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
291 if (cookie == 2)
292 return;
293
294 /*
295 * NFSv4 servers do not return entries for '.' and '..'
296 * Therefore, we fake these entries here. We let '.'
297 * have cookie 0 and '..' have cookie 1. Note that
298 * when talking to the server, we always send cookie 0
299 * instead of 1 or 2.
300 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800301 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (cookie == 0) {
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_one; /* cookie, second word */
307 *p++ = xdr_one; /* entry len */
308 memcpy(p, ".\0\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400311 *p++ = htonl(attrs); /* bitmap */
312 *p++ = htonl(12); /* attribute buffer length */
313 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000314 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 *p++ = xdr_one; /* next */
318 *p++ = xdr_zero; /* cookie, first word */
319 *p++ = xdr_two; /* cookie, second word */
320 *p++ = xdr_two; /* entry len */
321 memcpy(p, "..\0\0", 4); /* entry */
322 p++;
323 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400324 *p++ = htonl(attrs); /* bitmap */
325 *p++ = htonl(12); /* attribute buffer length */
326 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000327 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 readdir->pgbase = (char *)p - (char *)start;
330 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800331 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Trond Myklebust26d36302016-09-22 13:39:05 -0400334static void nfs4_test_and_free_stateid(struct nfs_server *server,
335 nfs4_stateid *stateid,
336 struct rpc_cred *cred)
337{
338 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
339
340 ops->test_and_free_expired(server, stateid, cred);
341}
342
343static void __nfs4_free_revoked_stateid(struct nfs_server *server,
344 nfs4_stateid *stateid,
345 struct rpc_cred *cred)
346{
347 stateid->type = NFS4_REVOKED_STATEID_TYPE;
348 nfs4_test_and_free_stateid(server, stateid, cred);
349}
350
351static void nfs4_free_revoked_stateid(struct nfs_server *server,
352 const nfs4_stateid *stateid,
353 struct rpc_cred *cred)
354{
355 nfs4_stateid tmp;
356
357 nfs4_stateid_copy(&tmp, stateid);
358 __nfs4_free_revoked_stateid(server, &tmp, cred);
359}
360
NeilBrown8478eaa2014-09-18 16:09:27 +1000361static long nfs4_update_delay(long *timeout)
362{
363 long ret;
364 if (!timeout)
365 return NFS4_POLL_RETRY_MAX;
366 if (*timeout <= 0)
367 *timeout = NFS4_POLL_RETRY_MIN;
368 if (*timeout > NFS4_POLL_RETRY_MAX)
369 *timeout = NFS4_POLL_RETRY_MAX;
370 ret = *timeout;
371 *timeout <<= 1;
372 return ret;
373}
374
Trond Myklebust65de8722008-12-23 15:21:44 -0500375static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
376{
377 int res = 0;
378
379 might_sleep();
380
NeilBrown8478eaa2014-09-18 16:09:27 +1000381 freezable_schedule_timeout_killable_unsafe(
382 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 if (fatal_signal_pending(current))
384 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500385 return res;
386}
387
388/* This is the error handling routine for processes that are allowed
389 * to sleep.
390 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400391static int nfs4_do_handle_exception(struct nfs_server *server,
392 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500393{
394 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500395 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400396 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500398 int ret = errorcode;
399
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400400 exception->delay = 0;
401 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400403
404 if (stateid == NULL && state != NULL)
405 stateid = &state->stateid;
406
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 switch(errorcode) {
408 case 0:
409 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_DELEG_REVOKED:
411 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400412 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400413 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400414 if (inode != NULL && stateid != NULL) {
415 nfs_inode_find_state_and_recover(inode,
416 stateid);
417 goto wait_on_recovery;
418 }
419 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400420 if (inode) {
421 int err;
422
423 err = nfs_async_inode_return_delegation(inode,
424 stateid);
425 if (err == 0)
426 goto wait_on_recovery;
427 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
428 exception->retry = 1;
429 break;
430 }
431 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 if (state == NULL)
433 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400434 ret = nfs4_schedule_stateid_recovery(server, state);
435 if (ret < 0)
436 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500437 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500438 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500439 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500440 nfs4_schedule_lease_recovery(clp);
441 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400442 case -NFS4ERR_MOVED:
443 ret = nfs4_schedule_migration_recovery(server);
444 if (ret < 0)
445 break;
446 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400447 case -NFS4ERR_LEASE_MOVED:
448 nfs4_schedule_lease_moved_recovery(clp);
449 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500450#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400451 case -NFS4ERR_BADSESSION:
452 case -NFS4ERR_BADSLOT:
453 case -NFS4ERR_BAD_HIGH_SLOT:
454 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
455 case -NFS4ERR_DEADSESSION:
456 case -NFS4ERR_SEQ_FALSE_RETRY:
457 case -NFS4ERR_SEQ_MISORDERED:
458 dprintk("%s ERROR: %d Reset session\n", __func__,
459 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400460 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400461 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500462#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500463 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500464 if (exception->timeout > HZ) {
465 /* We have retried a decent amount, time to
466 * fail
467 */
468 ret = -EBUSY;
469 break;
470 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400472 nfs_inc_server_stats(server, NFSIOS_DELAY);
473 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400474 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400475 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400476 exception->delay = 1;
477 return 0;
478
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400479 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500480 case -NFS4ERR_OLD_STATEID:
481 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800482 break;
483 case -NFS4ERR_BADOWNER:
484 /* The following works around a Linux server bug! */
485 case -NFS4ERR_BADNAME:
486 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
487 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
488 exception->retry = 1;
489 printk(KERN_WARNING "NFS: v4 server %s "
490 "does not accept raw "
491 "uid/gids. "
492 "Reenabling the idmapper.\n",
493 server->nfs_client->cl_hostname);
494 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500495 }
496 /* We failed to handle the error */
497 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500498wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400499 exception->recovering = 1;
500 return 0;
501}
502
503/* This is the error handling routine for processes that are allowed
504 * to sleep.
505 */
506int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
507{
508 struct nfs_client *clp = server->nfs_client;
509 int ret;
510
511 ret = nfs4_do_handle_exception(server, errorcode, exception);
512 if (exception->delay) {
513 ret = nfs4_delay(server->client, &exception->timeout);
514 goto out_retry;
515 }
516 if (exception->recovering) {
517 ret = nfs4_wait_clnt_recover(clp);
518 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
519 return -EIO;
520 goto out_retry;
521 }
522 return ret;
523out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500524 if (ret == 0)
525 exception->retry = 1;
526 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500527}
528
Trond Myklebust037fc982015-09-20 15:51:00 -0400529static int
530nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
531 int errorcode, struct nfs4_exception *exception)
532{
533 struct nfs_client *clp = server->nfs_client;
534 int ret;
535
536 ret = nfs4_do_handle_exception(server, errorcode, exception);
537 if (exception->delay) {
538 rpc_delay(task, nfs4_update_delay(&exception->timeout));
539 goto out_retry;
540 }
541 if (exception->recovering) {
542 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
543 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
544 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
545 goto out_retry;
546 }
547 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
548 ret = -EIO;
549 return ret;
550out_retry:
551 if (ret == 0)
552 exception->retry = 1;
553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500585 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400586}
Trond Myklebust65de8722008-12-23 15:21:44 -0500587
Trond Myklebust452e9352010-07-31 14:29:06 -0400588static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
594}
595
Trond Myklebust452e9352010-07-31 14:29:06 -0400596static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597{
Trond Myklebustbe824162015-07-05 14:50:46 -0400598 struct nfs_client *clp = server->nfs_client;
599
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400602}
603
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400604struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
608};
609
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800610void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400612{
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
616
617 res->sr_slot = NULL;
618}
619
620static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621{
622 args->sa_privileged = 1;
623}
624
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400625static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400626{
627 struct nfs4_slot *slot = res->sr_slot;
628 struct nfs4_slot_table *tbl;
629
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 tbl = slot->table;
631 spin_lock(&tbl->slot_tbl_lock);
632 if (!nfs41_wake_and_assign_slot(tbl, slot))
633 nfs4_free_slot(tbl, slot);
634 spin_unlock(&tbl->slot_tbl_lock);
635
636 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400637}
638
639static int nfs40_sequence_done(struct rpc_task *task,
640 struct nfs4_sequence_res *res)
641{
642 if (res->sr_slot != NULL)
643 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400644 return 1;
645}
646
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400647#if defined(CONFIG_NFS_V4_1)
648
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400649static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400650{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500651 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400652 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500653 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400654
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400655 if (!slot)
656 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500657 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500658 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500659
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400660 /* Bump the slot sequence number */
661 if (slot->seq_done)
662 slot->seq_nr++;
663 slot->seq_done = 0;
664
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500665 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500666 /* Be nice to the server: try to ensure that the last transmitted
667 * value for highest_user_slotid <= target_highest_slotid
668 */
669 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
670 send_new_highest_used_slotid = true;
671
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500672 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500673 send_new_highest_used_slotid = false;
674 goto out_unlock;
675 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500676 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677
678 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
679 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500680out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500681 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500682 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400683 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400684 if (waitqueue_active(&tbl->slot_waitq))
685 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400686}
687
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400688static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
689{
690 nfs41_release_slot(res->sr_slot);
691 res->sr_slot = NULL;
692}
693
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400694static int nfs41_sequence_process(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500698 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400699 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500700 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500701 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 if (slot == NULL)
704 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400705 /* don't increment the sequence number if the task wasn't sent */
706 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400707 goto out;
708
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500709 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500710
Trond Myklebustac20d162012-12-15 15:36:07 -0500711 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400712 if (res->sr_status != -NFS4ERR_DELAY)
713 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500714 interrupted = true;
715 }
716
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400717 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500718 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400719 switch (res->sr_status) {
720 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400721 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400722 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500724 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500725 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400726 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
727 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500728 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400729 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500730 case 1:
731 /*
732 * sr_status remains 1 if an RPC level error occurred.
733 * The server may or may not have processed the sequence
734 * operation..
735 * Mark the slot as having hosted an interrupted RPC call.
736 */
737 slot->interrupted = 1;
738 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400739 case -NFS4ERR_DELAY:
740 /* The server detected a resend of the RPC call and
741 * returned NFS4ERR_DELAY as per Section 2.10.6.2
742 * of RFC5661.
743 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500744 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400745 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500746 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500747 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400748 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500749 case -NFS4ERR_BADSLOT:
750 /*
751 * The slot id we used was probably retired. Try again
752 * using a different slot id.
753 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400754 if (slot->seq_nr < slot->table->target_highest_slotid)
755 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500756 goto retry_nowait;
757 case -NFS4ERR_SEQ_MISORDERED:
758 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500759 * Was the last operation on this sequence interrupted?
760 * If so, retry after bumping the sequence number.
761 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400762 if (interrupted)
763 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500764 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500765 * Could this slot have been previously retired?
766 * If so, then the server may be expecting seq_nr = 1!
767 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500768 if (slot->seq_nr != 1) {
769 slot->seq_nr = 1;
770 goto retry_nowait;
771 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400772 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500773 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400774 if (interrupted)
775 goto retry_new_seq;
776 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 default:
778 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400779 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400780 }
781out:
782 /* The session may be reset by one of the error handlers. */
783 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500784out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500785 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400786session_recover:
787 nfs4_schedule_session_recovery(session, res->sr_status);
788 goto retry_nowait;
789retry_new_seq:
790 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500791retry_nowait:
792 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 task->tk_status = 0;
795 ret = 0;
796 }
797 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400799 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 goto out;
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
802 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400804
805int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (!nfs41_sequence_process(task, res))
808 return 0;
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
811 return 1;
812
813}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500814EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400815
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400816static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
817{
818 if (res->sr_slot == NULL)
819 return 1;
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
823}
824
825static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
826{
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
830 else
831 nfs40_sequence_free_slot(res);
832 }
833}
834
Peng Tao2c4b1312014-06-11 05:24:16 +0800835int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400836{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500837 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842}
Peng Tao2c4b1312014-06-11 05:24:16 +0800843EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844
Andy Adamsonce5039c2009-04-01 09:22:13 -0400845static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
846{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400847 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400848
Trond Myklebust035168ab2010-06-16 09:52:26 -0400849 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
850
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500851 nfs4_setup_sequence(data->seq_server->nfs_client,
852 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400853}
854
Andy Adamson69ab40c2009-04-01 09:22:19 -0400855static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
856{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400857 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400858
Trond Myklebust14516c32010-07-31 14:29:06 -0400859 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400860}
861
Trond Myklebust17280172012-03-11 13:11:00 -0400862static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400863 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400864 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400865};
866
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400867static void
868nfs4_sequence_process_interrupted(struct nfs_client *client,
869 struct nfs4_slot *slot, struct rpc_cred *cred)
870{
871 struct rpc_task *task;
872
873 task = _nfs41_proc_sequence(client, cred, slot, true);
874 if (!IS_ERR(task))
875 rpc_put_task_async(task);
876}
877
Chuck Lever3bd23842013-08-09 12:49:19 -0400878#else /* !CONFIG_NFS_V4_1 */
879
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400880static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
881{
882 return nfs40_sequence_done(task, res);
883}
884
885static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
886{
887 if (res->sr_slot != NULL)
888 nfs40_sequence_free_slot(res);
889}
890
Peng Tao2c4b1312014-06-11 05:24:16 +0800891int nfs4_sequence_done(struct rpc_task *task,
892 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400893{
Chuck Lever3bd23842013-08-09 12:49:19 -0400894 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400895}
Peng Tao2c4b1312014-06-11 05:24:16 +0800896EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400897
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400898static void
899nfs4_sequence_process_interrupted(struct nfs_client *client,
900 struct nfs4_slot *slot, struct rpc_cred *cred)
901{
902 WARN_ON_ONCE(1);
903 slot->interrupted = 0;
904}
905
Chuck Lever3bd23842013-08-09 12:49:19 -0400906#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400907
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400908static
909void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
910 struct nfs4_sequence_res *res,
911 struct nfs4_slot *slot)
912{
913 if (!slot)
914 return;
915 slot->privileged = args->sa_privileged ? 1 : 0;
916 args->sa_slot = slot;
917
918 res->sr_slot = slot;
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
922
923}
924
925int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500926 struct nfs4_sequence_args *args,
927 struct nfs4_sequence_res *res,
928 struct rpc_task *task)
929{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500930 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500931 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500932 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500933
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500934 /* slot already allocated? */
935 if (res->sr_slot != NULL)
936 goto out_start;
937
Anna Schumaker76ee0352017-01-10 16:49:31 -0500938 if (session) {
939 tbl = &session->fc_slot_table;
940 task->tk_timeout = 0;
941 }
942
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400943 for (;;) {
944 spin_lock(&tbl->slot_tbl_lock);
945 /* The state manager will wait until the slot table is empty */
946 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
947 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500948
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400949 slot = nfs4_alloc_slot(tbl);
950 if (IS_ERR(slot)) {
951 /* Try again in 1/4 second */
952 if (slot == ERR_PTR(-ENOMEM))
953 task->tk_timeout = HZ >> 2;
954 goto out_sleep;
955 }
956 spin_unlock(&tbl->slot_tbl_lock);
957
958 if (likely(!slot->interrupted))
959 break;
960 nfs4_sequence_process_interrupted(client,
961 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500962 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500963
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400964 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500965
Anna Schumakerad05cc02017-01-11 13:37:06 -0500966 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500967out_start:
968 rpc_call_start(task);
969 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500970
971out_sleep:
972 if (args->sa_privileged)
973 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
974 NULL, RPC_PRIORITY_PRIVILEGED);
975 else
976 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
977 spin_unlock(&tbl->slot_tbl_lock);
978 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500979}
980EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
981
Chuck Lever9915ea72013-08-09 12:48:27 -0400982static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
983{
984 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500985 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400986 data->seq_args, data->seq_res, task);
987}
988
989static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
990{
991 struct nfs4_call_sync_data *data = calldata;
992 nfs4_sequence_done(task, data->seq_res);
993}
994
995static const struct rpc_call_ops nfs40_call_sync_ops = {
996 .rpc_call_prepare = nfs40_call_sync_prepare,
997 .rpc_call_done = nfs40_call_sync_done,
998};
999
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001000static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001001 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 struct nfs4_sequence_args *args,
1004 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001005{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 int ret;
1007 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001008 struct nfs_client *clp = server->nfs_client;
1009 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 .seq_server = server,
1011 .seq_args = args,
1012 .seq_res = res,
1013 };
1014 struct rpc_task_setup task_setup = {
1015 .rpc_client = clnt,
1016 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001017 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 .callback_data = &data
1019 };
1020
1021 task = rpc_run_task(&task_setup);
1022 if (IS_ERR(task))
1023 ret = PTR_ERR(task);
1024 else {
1025 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001026 rpc_put_task(task);
1027 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001028 return ret;
1029}
1030
Bryan Schumaker7c513052011-03-24 17:12:24 +00001031int nfs4_call_sync(struct rpc_clnt *clnt,
1032 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001033 struct rpc_message *msg,
1034 struct nfs4_sequence_args *args,
1035 struct nfs4_sequence_res *res,
1036 int cache_reply)
1037{
Chuck Levera9c92d62013-08-09 12:48:18 -04001038 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001039 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001042static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1043 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct nfs_inode *nfsi = NFS_I(dir);
1046
1047 spin_lock(&dir->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04001048 nfsi->cache_validity |= NFS_INO_INVALID_CTIME
1049 | NFS_INO_INVALID_MTIME
1050 | NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001051 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001052 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1053 nfsi->attrtimeo_timestamp = jiffies;
1054 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001055 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001056 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001057 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1058 NFS_INO_INVALID_ACL;
1059 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001060 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001061 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001062 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001063 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001064 spin_unlock(&dir->i_lock);
1065}
1066
1067struct nfs4_opendata {
1068 struct kref kref;
1069 struct nfs_openargs o_arg;
1070 struct nfs_openres o_res;
1071 struct nfs_open_confirmargs c_arg;
1072 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001073 struct nfs4_string owner_name;
1074 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001075 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001077 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001079 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001080 struct nfs4_state_owner *owner;
1081 struct nfs4_state *state;
1082 struct iattr attrs;
1083 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001084 bool rpc_done;
1085 bool file_created;
1086 bool is_recover;
1087 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001088 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001089};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001090
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001091struct nfs4_open_createattrs {
1092 struct nfs4_label *label;
1093 struct iattr *sattr;
1094 const __u32 verf[2];
1095};
1096
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001097static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1098 int err, struct nfs4_exception *exception)
1099{
1100 if (err != -EINVAL)
1101 return false;
1102 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1103 return false;
1104 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1105 exception->retry = 1;
1106 return true;
1107}
1108
Trond Myklebust6ae37332015-01-30 14:21:14 -05001109static u32
1110nfs4_map_atomic_open_share(struct nfs_server *server,
1111 fmode_t fmode, int openflags)
1112{
1113 u32 res = 0;
1114
1115 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1116 case FMODE_READ:
1117 res = NFS4_SHARE_ACCESS_READ;
1118 break;
1119 case FMODE_WRITE:
1120 res = NFS4_SHARE_ACCESS_WRITE;
1121 break;
1122 case FMODE_READ|FMODE_WRITE:
1123 res = NFS4_SHARE_ACCESS_BOTH;
1124 }
1125 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1126 goto out;
1127 /* Want no delegation if we're using O_DIRECT */
1128 if (openflags & O_DIRECT)
1129 res |= NFS4_SHARE_WANT_NO_DELEG;
1130out:
1131 return res;
1132}
1133
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001134static enum open_claim_type4
1135nfs4_map_atomic_open_claim(struct nfs_server *server,
1136 enum open_claim_type4 claim)
1137{
1138 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1139 return claim;
1140 switch (claim) {
1141 default:
1142 return claim;
1143 case NFS4_OPEN_CLAIM_FH:
1144 return NFS4_OPEN_CLAIM_NULL;
1145 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1146 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1147 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1148 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1149 }
1150}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151
1152static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001153{
1154 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001155 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001156 p->o_res.seqid = p->o_arg.seqid;
1157 p->c_res.seqid = p->c_arg.seqid;
1158 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001159 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001160 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001161 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001162}
1163
Al Viro82a2c1b2011-06-22 18:30:55 -04001164static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001165 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001166 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001167 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001168 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001169{
Al Viro82a2c1b2011-06-22 18:30:55 -04001170 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001171 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001172 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001173 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001174 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001175 struct nfs4_opendata *p;
1176
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001177 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001178 if (p == NULL)
1179 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001180
1181 p->f_label = nfs4_label_alloc(server, gfp_mask);
1182 if (IS_ERR(p->f_label))
1183 goto err_free_p;
1184
Kinglong Meea49c2692015-07-27 15:31:38 +08001185 p->a_label = nfs4_label_alloc(server, gfp_mask);
1186 if (IS_ERR(p->a_label))
1187 goto err_free_f;
1188
Trond Myklebust63f5f792015-01-23 19:19:25 -05001189 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1190 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001191 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001192 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001193 nfs_sb_active(dentry->d_sb);
1194 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001195 p->dir = parent;
1196 p->owner = sp;
1197 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001198 p->o_arg.open_flags = flags;
1199 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001200 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001201 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001202 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1203 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001204 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1205 * will return permission denied for all bits until close */
1206 if (!(flags & O_EXCL)) {
1207 /* ask server to check for all possible rights as results
1208 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001209 switch (p->o_arg.claim) {
1210 default:
1211 break;
1212 case NFS4_OPEN_CLAIM_NULL:
1213 case NFS4_OPEN_CLAIM_FH:
1214 p->o_arg.access = NFS4_ACCESS_READ |
1215 NFS4_ACCESS_MODIFY |
1216 NFS4_ACCESS_EXTEND |
1217 NFS4_ACCESS_EXECUTE;
1218 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001219 }
David Howells7539bba2006-08-22 20:06:09 -04001220 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001221 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1222 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001223 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001224 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001225 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001226 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001227 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001228 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001229 case NFS4_OPEN_CLAIM_NULL:
1230 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1231 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1232 p->o_arg.fh = NFS_FH(dir);
1233 break;
1234 case NFS4_OPEN_CLAIM_PREVIOUS:
1235 case NFS4_OPEN_CLAIM_FH:
1236 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1237 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001238 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001239 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001240 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001241 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001242 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001243
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001244 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001245 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001246 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001247 p->c_arg.fh = &p->o_res.fh;
1248 p->c_arg.stateid = &p->o_res.stateid;
1249 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001250 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001251 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001252 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001253
1254err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001255 nfs4_label_free(p->a_label);
1256err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001257 nfs4_label_free(p->f_label);
1258err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001259 kfree(p);
1260err:
1261 dput(parent);
1262 return NULL;
1263}
1264
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001265static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001266{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001267 struct nfs4_opendata *p = container_of(kref,
1268 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001269 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001270
1271 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001272 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001273 if (p->state != NULL)
1274 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001275 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001276
Kinglong Meea49c2692015-07-27 15:31:38 +08001277 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001278 nfs4_label_free(p->f_label);
1279
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001280 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001281 dput(p->dentry);
1282 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001283 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001284 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001285 kfree(p);
1286}
1287
1288static void nfs4_opendata_put(struct nfs4_opendata *p)
1289{
1290 if (p != NULL)
1291 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001292}
1293
Trond Myklebust24311f82015-09-20 10:50:17 -04001294static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1295 fmode_t fmode)
1296{
1297 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1298 case FMODE_READ|FMODE_WRITE:
1299 return state->n_rdwr != 0;
1300 case FMODE_WRITE:
1301 return state->n_wronly != 0;
1302 case FMODE_READ:
1303 return state->n_rdonly != 0;
1304 }
1305 WARN_ON_ONCE(1);
1306 return false;
1307}
1308
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001309static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001310{
1311 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312
Trond Myklebust536e43d2012-01-17 22:04:26 -05001313 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001314 goto out;
1315 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001316 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001317 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1318 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001319 break;
1320 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001321 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1322 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001323 break;
1324 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001325 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1326 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001327 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001328out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001329 return ret;
1330}
1331
Trond Myklebust2a606182015-08-19 22:30:00 -05001332static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1333 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001334{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001335 if (delegation == NULL)
1336 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001337 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001338 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001339 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1340 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001341 switch (claim) {
1342 case NFS4_OPEN_CLAIM_NULL:
1343 case NFS4_OPEN_CLAIM_FH:
1344 break;
1345 case NFS4_OPEN_CLAIM_PREVIOUS:
1346 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1347 break;
1348 default:
1349 return 0;
1350 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001351 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001352 return 1;
1353}
1354
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001356{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001357 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001358 case FMODE_WRITE:
1359 state->n_wronly++;
1360 break;
1361 case FMODE_READ:
1362 state->n_rdonly++;
1363 break;
1364 case FMODE_READ|FMODE_WRITE:
1365 state->n_rdwr++;
1366 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001367 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001368}
1369
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001370#ifdef CONFIG_NFS_V4_1
1371static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1372{
1373 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1374 return true;
1375 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1376 return true;
1377 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1378 return true;
1379 return false;
1380}
1381#endif /* CONFIG_NFS_V4_1 */
1382
Trond Myklebustc9399f22017-11-06 15:28:01 -05001383static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1384{
1385 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1386 wake_up_all(&state->waitq);
1387}
1388
1389static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1390 const nfs4_stateid *stateid)
1391{
1392 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1393 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1394
1395 if (stateid_seqid == state_seqid + 1U ||
1396 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1397 nfs_state_log_update_open_stateid(state);
1398 else
1399 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1400}
1401
Trond Myklebust4f14c192014-02-12 19:15:06 -05001402static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001403{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001404 struct nfs_client *clp = state->owner->so_server->nfs_client;
1405 bool need_recover = false;
1406
1407 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1410 need_recover = true;
1411 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1412 need_recover = true;
1413 if (need_recover)
1414 nfs4_state_mark_reclaim_nograce(clp, state);
1415}
1416
Trond Myklebustc9399f22017-11-06 15:28:01 -05001417/*
1418 * Check for whether or not the caller may update the open stateid
1419 * to the value passed in by stateid.
1420 *
1421 * Note: This function relies heavily on the server implementing
1422 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1423 * correctly.
1424 * i.e. The stateid seqids have to be initialised to 1, and
1425 * are then incremented on every state transition.
1426 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001427static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001428 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001429{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001430 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1431 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1432 if (stateid->seqid == cpu_to_be32(1))
1433 nfs_state_log_update_open_stateid(state);
1434 else
1435 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001436 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001437 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001438
1439 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1440 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001441 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001442 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001443 return false;
1444}
1445
Trond Myklebustf95549c2015-01-23 18:06:09 -05001446static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1447{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001448 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1449 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001450 if (state->n_wronly)
1451 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1452 if (state->n_rdonly)
1453 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1454 if (state->n_rdwr)
1455 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001456 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001457}
1458
Trond Myklebust226056c2014-02-11 10:41:07 -05001459static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1460 nfs4_stateid *stateid, fmode_t fmode)
1461{
1462 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1463 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1464 case FMODE_WRITE:
1465 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1466 break;
1467 case FMODE_READ:
1468 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1469 break;
1470 case 0:
1471 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1472 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1473 clear_bit(NFS_OPEN_STATE, &state->flags);
1474 }
1475 if (stateid == NULL)
1476 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001477 /* Handle OPEN+OPEN_DOWNGRADE races */
1478 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1479 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001480 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001481 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001482 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001483 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001484 nfs4_stateid_copy(&state->stateid, stateid);
1485 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001486 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001487out:
1488 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001489}
1490
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001491static void nfs_clear_open_stateid(struct nfs4_state *state,
1492 nfs4_stateid *arg_stateid,
1493 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001494{
1495 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001496 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1497 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1498 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001499 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001500 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1501 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001502}
1503
Trond Myklebust1393d962016-09-22 13:39:13 -04001504static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001505 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001506{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001507 DEFINE_WAIT(wait);
1508 int status = 0;
1509 for (;;) {
1510
1511 if (!nfs_need_update_open_stateid(state, stateid))
1512 return;
1513 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001514 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001515 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001516 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001517 /* Rely on seqids for serialisation with NFSv4.0 */
1518 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1519 break;
1520
1521 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1522 /*
1523 * Ensure we process the state changes in the same order
1524 * in which the server processed them by delaying the
1525 * update of the stateid until we are in sequence.
1526 */
1527 write_sequnlock(&state->seqlock);
1528 spin_unlock(&state->owner->so_lock);
1529 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001530 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001531 if (!signal_pending(current)) {
1532 if (schedule_timeout(5*HZ) == 0)
1533 status = -EAGAIN;
1534 else
1535 status = 0;
1536 } else
1537 status = -EINTR;
1538 finish_wait(&state->waitq, &wait);
1539 rcu_read_lock();
1540 spin_lock(&state->owner->so_lock);
1541 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001542 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001543
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001544 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1545 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001546 nfs4_stateid_copy(freeme, &state->open_stateid);
1547 nfs_test_and_clear_all_open_stateid(state);
1548 }
1549
Trond Myklebuste999e802014-02-10 18:20:47 -05001550 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1551 nfs4_stateid_copy(&state->stateid, stateid);
1552 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001553 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001554 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001555}
1556
Trond Myklebustc9399f22017-11-06 15:28:01 -05001557static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001558 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001559 fmode_t fmode,
1560 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001561{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001562 /*
1563 * Protect the call to nfs4_state_set_mode_locked and
1564 * serialise the stateid update
1565 */
1566 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001567 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1568 switch (fmode) {
1569 case FMODE_READ:
1570 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1571 break;
1572 case FMODE_WRITE:
1573 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1574 break;
1575 case FMODE_READ|FMODE_WRITE:
1576 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001577 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001579 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001580}
1581
1582static void nfs_state_set_delegation(struct nfs4_state *state,
1583 const nfs4_stateid *deleg_stateid,
1584 fmode_t fmode)
1585{
1586 /*
1587 * Protect the call to nfs4_state_set_mode_locked and
1588 * serialise the stateid update
1589 */
1590 write_seqlock(&state->seqlock);
1591 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1592 set_bit(NFS_DELEGATED_STATE, &state->flags);
1593 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594}
1595
Trond Myklebust1393d962016-09-22 13:39:13 -04001596static int update_open_stateid(struct nfs4_state *state,
1597 const nfs4_stateid *open_stateid,
1598 const nfs4_stateid *delegation,
1599 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001600{
Trond Myklebust1393d962016-09-22 13:39:13 -04001601 struct nfs_server *server = NFS_SERVER(state->inode);
1602 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001603 struct nfs_inode *nfsi = NFS_I(state->inode);
1604 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001605 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001606 int ret = 0;
1607
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001608 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001609
1610 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001611 spin_lock(&state->owner->so_lock);
1612 if (open_stateid != NULL) {
1613 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1614 ret = 1;
1615 }
1616
Trond Myklebust34310432008-12-23 15:21:38 -05001617 deleg_cur = rcu_dereference(nfsi->delegation);
1618 if (deleg_cur == NULL)
1619 goto no_delegation;
1620
1621 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001622 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001623 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001624 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001625 goto no_delegation_unlock;
1626
1627 if (delegation == NULL)
1628 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001629 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001630 goto no_delegation_unlock;
1631
Trond Myklebustb7391f42008-12-23 15:21:52 -05001632 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001633 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001634 ret = 1;
1635no_delegation_unlock:
1636 spin_unlock(&deleg_cur->lock);
1637no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001638 if (ret)
1639 update_open_stateflags(state, fmode);
1640 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001641 rcu_read_unlock();
1642
Trond Myklebust4f14c192014-02-12 19:15:06 -05001643 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001644 nfs4_schedule_state_manager(clp);
1645 if (freeme.type != 0)
1646 nfs4_test_and_free_stateid(server, &freeme,
1647 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001648
1649 return ret;
1650}
1651
Trond Myklebust39071e62015-01-24 15:07:56 -05001652static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1653 const nfs4_stateid *stateid)
1654{
1655 struct nfs4_state *state = lsp->ls_state;
1656 bool ret = false;
1657
1658 spin_lock(&state->state_lock);
1659 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1660 goto out_noupdate;
1661 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1662 goto out_noupdate;
1663 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1664 ret = true;
1665out_noupdate:
1666 spin_unlock(&state->state_lock);
1667 return ret;
1668}
Trond Myklebust34310432008-12-23 15:21:38 -05001669
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001670static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001671{
1672 struct nfs_delegation *delegation;
1673
Trond Myklebustf5086242018-03-20 16:43:13 -04001674 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001675 rcu_read_lock();
1676 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001677 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001678 rcu_read_unlock();
1679 return;
1680 }
1681 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001682 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001683}
1684
Trond Myklebust6ee41262007-07-08 14:11:36 -04001685static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001686{
1687 struct nfs4_state *state = opendata->state;
1688 struct nfs_inode *nfsi = NFS_I(state->inode);
1689 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001690 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001691 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001692 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001693 nfs4_stateid stateid;
1694 int ret = -EAGAIN;
1695
Trond Myklebustaac00a82007-07-05 19:02:21 -04001696 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001698 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001700 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001701 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001702 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001703 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001704 rcu_read_lock();
1705 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001706 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001707 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001708 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001709 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001711 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001712 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001713 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001714 if (!opendata->is_recover) {
1715 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1716 if (ret != 0)
1717 goto out;
1718 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001719 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001720
1721 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001722 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001723 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001724 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001725out:
1726 return ERR_PTR(ret);
1727out_return_state:
1728 atomic_inc(&state->count);
1729 return state;
1730}
1731
Andy Adamsone23008e2012-10-02 21:07:32 -04001732static void
1733nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1734{
1735 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1736 struct nfs_delegation *delegation;
1737 int delegation_flags = 0;
1738
1739 rcu_read_lock();
1740 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1741 if (delegation)
1742 delegation_flags = delegation->flags;
1743 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001744 switch (data->o_arg.claim) {
1745 default:
1746 break;
1747 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1748 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001749 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1750 "returning a delegation for "
1751 "OPEN(CLAIM_DELEGATE_CUR)\n",
1752 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001753 return;
1754 }
1755 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001756 nfs_inode_set_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001757 data->owner->so_cred,
1758 data->o_res.delegation_type,
1759 &data->o_res.delegation,
1760 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001761 else
1762 nfs_inode_reclaim_delegation(state->inode,
Trond Myklebust35156bf2018-03-20 17:03:13 -04001763 data->owner->so_cred,
1764 data->o_res.delegation_type,
1765 &data->o_res.delegation,
1766 data->o_res.pagemod_limit);
Andy Adamsone23008e2012-10-02 21:07:32 -04001767}
1768
1769/*
1770 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1771 * and update the nfs4_state.
1772 */
1773static struct nfs4_state *
1774_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1775{
1776 struct inode *inode = data->state->inode;
1777 struct nfs4_state *state = data->state;
1778 int ret;
1779
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001780 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001781 if (data->rpc_status)
1782 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001783 /* cached opens have already been processed */
1784 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001785 }
1786
Andy Adamsone23008e2012-10-02 21:07:32 -04001787 ret = nfs_refresh_inode(inode, &data->f_attr);
1788 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001789 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001790
1791 if (data->o_res.delegation_type != 0)
1792 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001793update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001794 update_open_stateid(state, &data->o_res.stateid, NULL,
1795 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001796 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001797
1798 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001799}
1800
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001801static struct inode *
1802nfs4_opendata_get_inode(struct nfs4_opendata *data)
1803{
1804 struct inode *inode;
1805
1806 switch (data->o_arg.claim) {
1807 case NFS4_OPEN_CLAIM_NULL:
1808 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1809 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1810 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1811 return ERR_PTR(-EAGAIN);
1812 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1813 &data->f_attr, data->f_label);
1814 break;
1815 default:
1816 inode = d_inode(data->dentry);
1817 ihold(inode);
1818 nfs_refresh_inode(inode, &data->f_attr);
1819 }
1820 return inode;
1821}
1822
Andy Adamsone23008e2012-10-02 21:07:32 -04001823static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001824nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1825{
1826 struct nfs4_state *state;
1827 struct inode *inode;
1828
1829 inode = nfs4_opendata_get_inode(data);
1830 if (IS_ERR(inode))
1831 return ERR_CAST(inode);
1832 if (data->state != NULL && data->state->inode == inode) {
1833 state = data->state;
1834 atomic_inc(&state->count);
1835 } else
1836 state = nfs4_get_open_state(inode, data->owner);
1837 iput(inode);
1838 if (state == NULL)
1839 state = ERR_PTR(-ENOMEM);
1840 return state;
1841}
1842
Andy Adamsone23008e2012-10-02 21:07:32 -04001843static struct nfs4_state *
1844_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001845{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001846 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001847
Trond Myklebustaac00a82007-07-05 19:02:21 -04001848 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001849 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001850 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001851 goto out;
1852 }
1853
Trond Myklebust75e8c482017-08-08 10:38:07 -04001854 state = nfs4_opendata_find_nfs4_state(data);
1855 if (IS_ERR(state))
1856 goto out;
1857
Andy Adamsone23008e2012-10-02 21:07:32 -04001858 if (data->o_res.delegation_type != 0)
1859 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001860 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001861 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001862out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001863 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001864 return state;
1865}
1866
Andy Adamsone23008e2012-10-02 21:07:32 -04001867static struct nfs4_state *
1868nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1869{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001870 struct nfs4_state *ret;
1871
Andy Adamsone23008e2012-10-02 21:07:32 -04001872 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001873 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1874 else
1875 ret = _nfs4_opendata_to_nfs4_state(data);
1876 nfs4_sequence_free_slot(&data->o_res.seq_res);
1877 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001878}
1879
Trond Myklebust864472e2006-01-03 09:55:15 +01001880static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1881{
1882 struct nfs_inode *nfsi = NFS_I(state->inode);
1883 struct nfs_open_context *ctx;
1884
1885 spin_lock(&state->inode->i_lock);
1886 list_for_each_entry(ctx, &nfsi->open_files, list) {
1887 if (ctx->state != state)
1888 continue;
1889 get_nfs_open_context(ctx);
1890 spin_unlock(&state->inode->i_lock);
1891 return ctx;
1892 }
1893 spin_unlock(&state->inode->i_lock);
1894 return ERR_PTR(-ENOENT);
1895}
1896
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001897static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1898 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001899{
1900 struct nfs4_opendata *opendata;
1901
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001902 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001903 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001904 if (opendata == NULL)
1905 return ERR_PTR(-ENOMEM);
1906 opendata->state = state;
1907 atomic_inc(&state->count);
1908 return opendata;
1909}
1910
Trond Myklebust24311f82015-09-20 10:50:17 -04001911static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1912 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001913{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001914 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001915 int ret;
1916
Trond Myklebust24311f82015-09-20 10:50:17 -04001917 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001918 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001919 opendata->o_arg.open_flags = 0;
1920 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001921 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1922 NFS_SB(opendata->dentry->d_sb),
1923 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001924 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1925 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1926 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001927 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001928 if (ret != 0)
1929 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001930 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001931 if (IS_ERR(newstate))
1932 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001933 if (newstate != opendata->state)
1934 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001935 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001936 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001937}
1938
1939static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1940{
Trond Myklebust864472e2006-01-03 09:55:15 +01001941 int ret;
1942
Trond Myklebust4f14c192014-02-12 19:15:06 -05001943 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1944 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1945 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1946 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001947 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001948 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001949 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001950 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001951 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1952 if (ret != 0)
1953 return ret;
1954 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1955 if (ret != 0)
1956 return ret;
1957 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1958 if (ret != 0)
1959 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001960 /*
1961 * We may have performed cached opens for all three recoveries.
1962 * Check if we need to update the current stateid.
1963 */
1964 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001965 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001966 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001967 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001968 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001969 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001970 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001971 return 0;
1972}
1973
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974/*
1975 * OPEN_RECLAIM:
1976 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001978static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001980 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001981 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001982 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001983 int status;
1984
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001985 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1986 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001987 if (IS_ERR(opendata))
1988 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001989 rcu_read_lock();
1990 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001991 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001992 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001993 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001994 opendata->o_arg.u.delegation_type = delegation_type;
1995 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001996 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997 return status;
1998}
1999
Trond Myklebust539cd032007-06-05 11:46:42 -04002000static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001{
2002 struct nfs_server *server = NFS_SERVER(state->inode);
2003 struct nfs4_exception exception = { };
2004 int err;
2005 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002006 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002007 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002008 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2009 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002010 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002011 break;
2012 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002013 } while (exception.retry);
2014 return err;
2015}
2016
Trond Myklebust864472e2006-01-03 09:55:15 +01002017static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2018{
2019 struct nfs_open_context *ctx;
2020 int ret;
2021
2022 ctx = nfs4_state_find_open_context(state);
2023 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002024 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002025 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002026 put_nfs_open_context(ctx);
2027 return ret;
2028}
2029
NeilBrowndce26302017-12-13 09:57:09 +11002030static 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 -07002031{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002032 switch (err) {
2033 default:
2034 printk(KERN_ERR "NFS: %s: unhandled error "
2035 "%d.\n", __func__, err);
2036 case 0:
2037 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002038 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002039 case -ESTALE:
2040 break;
2041 case -NFS4ERR_BADSESSION:
2042 case -NFS4ERR_BADSLOT:
2043 case -NFS4ERR_BAD_HIGH_SLOT:
2044 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2045 case -NFS4ERR_DEADSESSION:
2046 set_bit(NFS_DELEGATED_STATE, &state->flags);
2047 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2048 return -EAGAIN;
2049 case -NFS4ERR_STALE_CLIENTID:
2050 case -NFS4ERR_STALE_STATEID:
2051 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002052 /* Don't recall a delegation if it was lost */
2053 nfs4_schedule_lease_recovery(server->nfs_client);
2054 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002055 case -NFS4ERR_MOVED:
2056 nfs4_schedule_migration_recovery(server);
2057 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002058 case -NFS4ERR_LEASE_MOVED:
2059 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2060 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002061 case -NFS4ERR_DELEG_REVOKED:
2062 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002063 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002064 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002065 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002066 nfs_inode_find_state_and_recover(state->inode,
2067 stateid);
2068 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002069 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002070 case -NFS4ERR_DELAY:
2071 case -NFS4ERR_GRACE:
2072 set_bit(NFS_DELEGATED_STATE, &state->flags);
2073 ssleep(1);
2074 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002075 case -ENOMEM:
2076 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002077 if (fl) {
2078 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2079 if (lsp)
2080 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2081 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002082 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084 return err;
2085}
2086
Trond Myklebust24311f82015-09-20 10:50:17 -04002087int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2088 struct nfs4_state *state, const nfs4_stateid *stateid,
2089 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002090{
2091 struct nfs_server *server = NFS_SERVER(state->inode);
2092 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002093 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002094
2095 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2096 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2097 if (IS_ERR(opendata))
2098 return PTR_ERR(opendata);
2099 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002100 write_seqlock(&state->seqlock);
2101 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2102 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002103 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2104 switch (type & (FMODE_READ|FMODE_WRITE)) {
2105 case FMODE_READ|FMODE_WRITE:
2106 case FMODE_WRITE:
2107 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2108 if (err)
2109 break;
2110 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2111 if (err)
2112 break;
2113 case FMODE_READ:
2114 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2115 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002116 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002117 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002118}
2119
Chuck Leverbe05c862013-08-09 12:49:47 -04002120static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2121{
2122 struct nfs4_opendata *data = calldata;
2123
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002124 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2125 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002126}
2127
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002128static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2129{
2130 struct nfs4_opendata *data = calldata;
2131
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002132 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002133
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002134 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002135 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002136 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002137 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002138 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002139 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002140 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002141}
2142
2143static void nfs4_open_confirm_release(void *calldata)
2144{
2145 struct nfs4_opendata *data = calldata;
2146 struct nfs4_state *state = NULL;
2147
2148 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002149 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002150 goto out_free;
2151 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002152 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002153 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002154 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002155 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002156 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002157out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002158 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002159}
2160
2161static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002162 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002163 .rpc_call_done = nfs4_open_confirm_done,
2164 .rpc_release = nfs4_open_confirm_release,
2165};
2166
2167/*
2168 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2169 */
2170static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2171{
David Howells2b0143b2015-03-17 22:25:59 +00002172 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002173 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002174 struct rpc_message msg = {
2175 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2176 .rpc_argp = &data->c_arg,
2177 .rpc_resp = &data->c_res,
2178 .rpc_cred = data->owner->so_cred,
2179 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002180 struct rpc_task_setup task_setup_data = {
2181 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002182 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002183 .callback_ops = &nfs4_open_confirm_ops,
2184 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002185 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002186 .flags = RPC_TASK_ASYNC,
2187 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002188 int status;
2189
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002190 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002191 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002192 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002193 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002194 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002195 if (data->is_recover)
2196 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002197 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002198 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002199 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002200 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002201 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002202 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002203 smp_wmb();
2204 } else
2205 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002206 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 return status;
2208}
2209
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002210static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002211{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002212 struct nfs4_opendata *data = calldata;
2213 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002214 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002215 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002216
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002217 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002218 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002219 /*
2220 * Check if we still need to send an OPEN call, or if we can use
2221 * a delegation instead.
2222 */
2223 if (data->state != NULL) {
2224 struct nfs_delegation *delegation;
2225
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002226 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002227 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002228 rcu_read_lock();
2229 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002230 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002231 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002232 rcu_read_unlock();
2233 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002234 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002235 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002236 switch (claim) {
2237 default:
2238 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002239 case NFS4_OPEN_CLAIM_PREVIOUS:
2240 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2241 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002242 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002243 case NFS4_OPEN_CLAIM_FH:
2244 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002245 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002246 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002247 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002248 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002249 &data->o_res.seq_res,
2250 task) != 0)
2251 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002252
2253 /* Set the create mode (note dependency on the session type) */
2254 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2255 if (data->o_arg.open_flags & O_EXCL) {
2256 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2257 if (nfs4_has_persistent_session(clp))
2258 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2259 else if (clp->cl_mvops->minor_version > 0)
2260 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2261 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002262 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002263unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002264 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002265 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002266out_no_action:
2267 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002268out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002269 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002270}
2271
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002272static void nfs4_open_done(struct rpc_task *task, void *calldata)
2273{
2274 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002275
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002276 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002277
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002278 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002279 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002280
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002282 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2283 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002284 case S_IFREG:
2285 break;
2286 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002287 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002288 break;
2289 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002290 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002291 break;
2292 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002293 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002294 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002295 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002296 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002297 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2298 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002299 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002300 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002301}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002302
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002303static void nfs4_open_release(void *calldata)
2304{
2305 struct nfs4_opendata *data = calldata;
2306 struct nfs4_state *state = NULL;
2307
2308 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002309 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002310 goto out_free;
2311 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002312 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002313 goto out_free;
2314 /* In case we need an open_confirm, no cleanup! */
2315 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2316 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002317 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002318 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002319 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002320out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002321 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002322}
2323
2324static const struct rpc_call_ops nfs4_open_ops = {
2325 .rpc_call_prepare = nfs4_open_prepare,
2326 .rpc_call_done = nfs4_open_done,
2327 .rpc_release = nfs4_open_release,
2328};
2329
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002330static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002331{
David Howells2b0143b2015-03-17 22:25:59 +00002332 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002333 struct nfs_server *server = NFS_SERVER(dir);
2334 struct nfs_openargs *o_arg = &data->o_arg;
2335 struct nfs_openres *o_res = &data->o_res;
2336 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002337 struct rpc_message msg = {
2338 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2339 .rpc_argp = o_arg,
2340 .rpc_resp = o_res,
2341 .rpc_cred = data->owner->so_cred,
2342 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 struct rpc_task_setup task_setup_data = {
2344 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002345 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002346 .callback_ops = &nfs4_open_ops,
2347 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002348 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002349 .flags = RPC_TASK_ASYNC,
2350 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002351 int status;
2352
Chuck Levera9c92d62013-08-09 12:48:18 -04002353 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002354 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002355 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002356 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002357 data->cancelled = false;
2358 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002359 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002360 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002361 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002362 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002363 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002364 if (IS_ERR(task))
2365 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002366 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002367 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002368 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002369 smp_wmb();
2370 } else
2371 status = data->rpc_status;
2372 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002373
2374 return status;
2375}
2376
2377static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2378{
David Howells2b0143b2015-03-17 22:25:59 +00002379 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002380 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002381 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002382
2383 status = nfs4_run_open_task(data, 1);
2384 if (status != 0 || !data->rpc_done)
2385 return status;
2386
Trond Myklebust6926afd12012-01-07 13:22:46 -05002387 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2388
Anna Schumakerd7e98252017-01-11 16:13:29 -05002389 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002390 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002391
2392 return status;
2393}
2394
Trond Myklebustf3792d62014-07-10 08:54:32 -04002395/*
2396 * Additional permission checks in order to distinguish between an
2397 * open for read, and an open for execute. This works around the
2398 * fact that NFSv4 OPEN treats read and execute permissions as being
2399 * the same.
2400 * Note that in the non-execute case, we want to turn off permission
2401 * checking if we just created a new file (POSIX open() semantics).
2402 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002403static int nfs4_opendata_access(struct rpc_cred *cred,
2404 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002405 struct nfs4_state *state, fmode_t fmode,
2406 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002407{
2408 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002409 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002410
2411 /* access call failed or for some reason the server doesn't
2412 * support any access modes -- defer access call until later */
2413 if (opendata->o_res.access_supported == 0)
2414 return 0;
2415
2416 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002417 /*
2418 * Use openflags to check for exec, because fmode won't
2419 * always have FMODE_EXEC set when file open for exec.
2420 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002421 if (openflags & __FMODE_EXEC) {
2422 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002423 if (S_ISDIR(state->inode->i_mode))
2424 mask = NFS4_ACCESS_LOOKUP;
2425 else
2426 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002427 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002428 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002429
2430 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002431 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2432 nfs_access_add_cache(state->inode, &cache);
2433
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002434 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2435 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002436 return 0;
2437
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002438 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002439}
2440
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002441/*
2442 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2443 */
2444static int _nfs4_proc_open(struct nfs4_opendata *data)
2445{
David Howells2b0143b2015-03-17 22:25:59 +00002446 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002447 struct nfs_server *server = NFS_SERVER(dir);
2448 struct nfs_openargs *o_arg = &data->o_arg;
2449 struct nfs_openres *o_res = &data->o_res;
2450 int status;
2451
2452 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002453 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002454 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002455 if (status != 0) {
2456 if (status == -NFS4ERR_BADNAME &&
2457 !(o_arg->open_flags & O_CREAT))
2458 return -ENOENT;
2459 return status;
2460 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002461
Trond Myklebust6926afd12012-01-07 13:22:46 -05002462 nfs_fattr_map_and_free_names(server, &data->f_attr);
2463
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002464 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002465 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002466 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002467 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002468 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002469 if (data->file_created ||
2470 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002471 update_changeattr(dir, &o_res->cinfo,
2472 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002473 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002474 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2475 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002476 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002477 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002479 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002481 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2482 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002483 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002484 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002485 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002486}
2487
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488/*
2489 * OPEN_EXPIRED:
2490 * reclaim state on the server after a network partition.
2491 * Assumes caller holds the appropriate lock
2492 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002493static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002494{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002495 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002496 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002497
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002498 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002499 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002500 if (IS_ERR(opendata))
2501 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002502 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002503 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002504 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002505 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002506 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507}
2508
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002509static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002510{
Trond Myklebust539cd032007-06-05 11:46:42 -04002511 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002512 struct nfs4_exception exception = { };
2513 int err;
2514
2515 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002516 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002517 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002518 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2519 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002520 switch (err) {
2521 default:
2522 goto out;
2523 case -NFS4ERR_GRACE:
2524 case -NFS4ERR_DELAY:
2525 nfs4_handle_exception(server, err, &exception);
2526 err = 0;
2527 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002528 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002529out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002530 return err;
2531}
2532
Linus Torvalds1da177e2005-04-16 15:20:36 -07002533static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2534{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002536 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002537
Trond Myklebust864472e2006-01-03 09:55:15 +01002538 ctx = nfs4_state_find_open_context(state);
2539 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002540 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002541 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002542 put_nfs_open_context(ctx);
2543 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544}
2545
Trond Myklebust41020b62016-09-22 13:38:58 -04002546static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2547 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002548{
Trond Myklebust41020b62016-09-22 13:38:58 -04002549 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002550 write_seqlock(&state->seqlock);
2551 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2552 write_sequnlock(&state->seqlock);
2553 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2554}
2555
2556static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2557{
2558 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002559 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002560}
2561
2562static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2563{
2564 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2565 nfs40_clear_delegation_stateid(state);
2566 return nfs4_open_expired(sp, state);
2567}
2568
Trond Myklebust45870d62016-09-22 13:38:59 -04002569static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2570 nfs4_stateid *stateid,
2571 struct rpc_cred *cred)
2572{
2573 return -NFS4ERR_BAD_STATEID;
2574}
2575
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002576#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002577static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2578 nfs4_stateid *stateid,
2579 struct rpc_cred *cred)
2580{
2581 int status;
2582
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002583 switch (stateid->type) {
2584 default:
2585 break;
2586 case NFS4_INVALID_STATEID_TYPE:
2587 case NFS4_SPECIAL_STATEID_TYPE:
2588 return -NFS4ERR_BAD_STATEID;
2589 case NFS4_REVOKED_STATEID_TYPE:
2590 goto out_free;
2591 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002592
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002593 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002594 switch (status) {
2595 case -NFS4ERR_EXPIRED:
2596 case -NFS4ERR_ADMIN_REVOKED:
2597 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002598 break;
2599 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002600 return status;
2601 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002602out_free:
2603 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002604 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002605 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002606}
2607
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002608static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002609{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002610 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002611 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002612 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002613 struct rpc_cred *cred;
2614 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002615
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002616 /* Get the delegation credential for use by test/free_stateid */
2617 rcu_read_lock();
2618 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002619 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002620 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002621 return;
2622 }
2623
2624 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002625 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2626 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2627 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002628 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002629 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002630 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002631 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002632
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002633 cred = get_rpccred(delegation->cred);
2634 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002635 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002636 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002637 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002638 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002639
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002640 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002641}
2642
2643/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002644 * nfs41_check_expired_locks - possibly free a lock stateid
2645 *
2646 * @state: NFSv4 state for an inode
2647 *
2648 * Returns NFS_OK if recovery for this stateid is now finished.
2649 * Otherwise a negative NFS4ERR value is returned.
2650 */
2651static int nfs41_check_expired_locks(struct nfs4_state *state)
2652{
2653 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002654 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002655 struct nfs_server *server = NFS_SERVER(state->inode);
2656
2657 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2658 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002659
2660 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002661 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2662 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2663 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2664
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002665 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002666 spin_unlock(&state->state_lock);
2667
2668 nfs4_put_lock_state(prev);
2669 prev = lsp;
2670
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002671 status = nfs41_test_and_free_expired_stateid(server,
2672 &lsp->ls_stateid,
2673 cred);
2674 trace_nfs4_test_lock_stateid(state, lsp, status);
2675 if (status == -NFS4ERR_EXPIRED ||
2676 status == -NFS4ERR_BAD_STATEID) {
2677 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002678 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002679 if (!recover_lost_locks)
2680 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2681 } else if (status != NFS_OK) {
2682 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002683 nfs4_put_lock_state(prev);
2684 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002685 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002686 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002687 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002688 }
2689 spin_unlock(&state->state_lock);
2690 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002691out:
2692 return ret;
2693}
2694
2695/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002696 * nfs41_check_open_stateid - possibly free an open stateid
2697 *
2698 * @state: NFSv4 state for an inode
2699 *
2700 * Returns NFS_OK if recovery for this stateid is now finished.
2701 * Otherwise a negative NFS4ERR value is returned.
2702 */
2703static int nfs41_check_open_stateid(struct nfs4_state *state)
2704{
2705 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002706 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002707 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002708 int status;
2709
Trond Myklebustb134fc42016-09-22 13:39:16 -04002710 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002711 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2712 if (nfs4_have_delegation(state->inode, state->state))
2713 return NFS_OK;
2714 return -NFS4ERR_OPENMODE;
2715 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002716 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002717 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002718 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002719 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002720 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002721 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2722 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2723 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002724 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002725 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002726 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002727 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002728 if (nfs_open_stateid_recover_openmode(state))
2729 return -NFS4ERR_OPENMODE;
2730 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002731}
2732
2733static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2734{
Chuck Levereb64cf92012-07-11 16:30:05 -04002735 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002736
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002737 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002738 status = nfs41_check_expired_locks(state);
2739 if (status != NFS_OK)
2740 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002741 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002742 if (status != NFS_OK)
2743 status = nfs4_open_expired(sp, state);
2744 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002745}
2746#endif
2747
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002749 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2750 * fields corresponding to attributes that were used to store the verifier.
2751 * Make sure we clobber those fields in the later setattr call
2752 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002753static unsigned nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002754 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002755{
Trond Myklebust609339c2018-03-28 16:18:17 -04002756 const __u32 *bitmask = opendata->o_arg.server->exclcreat_bitmask;
2757 __u32 attrset[3];
2758 unsigned ret;
2759 unsigned i;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002760
Trond Myklebust609339c2018-03-28 16:18:17 -04002761 for (i = 0; i < ARRAY_SIZE(attrset); i++) {
2762 attrset[i] = opendata->o_res.attrset[i];
2763 if (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE4_1)
2764 attrset[i] &= ~bitmask[i];
2765 }
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002766
Trond Myklebust609339c2018-03-28 16:18:17 -04002767 ret = (opendata->o_arg.createmode == NFS4_CREATE_EXCLUSIVE) ?
2768 sattr->ia_valid : 0;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002769
Trond Myklebust609339c2018-03-28 16:18:17 -04002770 if ((attrset[1] & (FATTR4_WORD1_TIME_ACCESS|FATTR4_WORD1_TIME_ACCESS_SET))) {
2771 if (sattr->ia_valid & ATTR_ATIME_SET)
2772 ret |= ATTR_ATIME_SET;
2773 else
2774 ret |= ATTR_ATIME;
2775 }
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002776
Trond Myklebust609339c2018-03-28 16:18:17 -04002777 if ((attrset[1] & (FATTR4_WORD1_TIME_MODIFY|FATTR4_WORD1_TIME_MODIFY_SET))) {
2778 if (sattr->ia_valid & ATTR_MTIME_SET)
2779 ret |= ATTR_MTIME_SET;
2780 else
2781 ret |= ATTR_MTIME;
2782 }
2783
2784 if (!(attrset[2] & FATTR4_WORD2_SECURITY_LABEL))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002785 *label = NULL;
Trond Myklebust609339c2018-03-28 16:18:17 -04002786 return ret;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002787}
2788
Trond Myklebustc21443c2013-02-07 14:26:21 -05002789static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2790 fmode_t fmode,
2791 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002792 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002793{
2794 struct nfs4_state_owner *sp = opendata->owner;
2795 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002796 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002797 struct nfs4_state *state;
2798 unsigned int seq;
2799 int ret;
2800
2801 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2802
2803 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002804 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002805 goto out;
2806
2807 state = nfs4_opendata_to_nfs4_state(opendata);
2808 ret = PTR_ERR(state);
2809 if (IS_ERR(state))
2810 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002811 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002812 if (server->caps & NFS_CAP_POSIX_LOCK)
2813 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002814 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2815 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002816
Trond Myklebust275bb302013-05-29 13:11:28 -04002817 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002818 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002819 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002820 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002821 alias = d_exact_alias(dentry, state->inode);
2822 if (!alias)
2823 alias = d_splice_alias(igrab(state->inode), dentry);
2824 /* d_splice_alias() can't fail here - it's a non-directory */
2825 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002826 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002827 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002828 }
2829 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002830 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002831 }
2832
Trond Myklebustc21443c2013-02-07 14:26:21 -05002833 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2834 if (ret != 0)
2835 goto out;
2836
David Howells2b0143b2015-03-17 22:25:59 +00002837 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002838 nfs_inode_attach_open_context(ctx);
2839 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2840 nfs4_schedule_stateid_recovery(server, state);
2841 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002842out:
2843 return ret;
2844}
2845
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002846/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002847 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002848 */
Andy Adamson82be4172012-05-23 05:02:35 -04002849static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002850 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002851 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002852 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002853 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854{
2855 struct nfs4_state_owner *sp;
2856 struct nfs4_state *state = NULL;
2857 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002858 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002859 struct dentry *dentry = ctx->dentry;
2860 struct rpc_cred *cred = ctx->cred;
2861 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2862 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002863 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002864 struct iattr *sattr = c->sattr;
2865 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002866 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002867 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002870 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002871 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2872 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002873 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2874 goto out_err;
2875 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002876 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002877 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002878 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002879 if (d_really_is_positive(dentry))
2880 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002881 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002882 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002883 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002884 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2885 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002886 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002887 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002888
David Quigley14c43f72013-05-22 12:50:43 -04002889 if (label) {
2890 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2891 if (IS_ERR(olabel)) {
2892 status = PTR_ERR(olabel);
2893 goto err_opendata_put;
2894 }
2895 }
2896
Trond Myklebuste911b812014-03-26 13:24:37 -07002897 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2898 if (!opendata->f_attr.mdsthreshold) {
2899 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2900 if (!opendata->f_attr.mdsthreshold)
2901 goto err_free_label;
2902 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002903 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002904 }
David Howells2b0143b2015-03-17 22:25:59 +00002905 if (d_really_is_positive(dentry))
2906 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002907
Trond Myklebust3efb9722013-05-29 13:17:04 -04002908 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002909 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002910 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002911 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002912
NeilBrownefcbc042015-07-30 13:00:56 +10002913 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002914 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Trond Myklebust609339c2018-03-28 16:18:17 -04002915 unsigned attrs = nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002916 /*
2917 * send create attributes which was not set by open
2918 * with an extra setattr.
2919 */
Trond Myklebust609339c2018-03-28 16:18:17 -04002920 if (attrs || label) {
2921 unsigned ia_old = sattr->ia_valid;
2922
2923 sattr->ia_valid = attrs;
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002924 nfs_fattr_init(opendata->o_res.f_attr);
2925 status = nfs4_do_setattr(state->inode, cred,
2926 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002927 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002928 if (status == 0) {
2929 nfs_setattr_update_inode(state->inode, sattr,
2930 opendata->o_res.f_attr);
2931 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2932 }
Trond Myklebust609339c2018-03-28 16:18:17 -04002933 sattr->ia_valid = ia_old;
David Quigley1775fd32013-05-22 12:50:42 -04002934 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002935 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002936 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002937 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002938
Trond Myklebuste911b812014-03-26 13:24:37 -07002939 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002940 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002941 opendata->f_attr.mdsthreshold = NULL;
2942 }
Andy Adamson82be4172012-05-23 05:02:35 -04002943
David Quigley14c43f72013-05-22 12:50:43 -04002944 nfs4_label_free(olabel);
2945
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002946 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002947 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002948 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002949err_free_label:
2950 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002951err_opendata_put:
2952 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002953err_put_state_owner:
2954 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002956 return status;
2957}
2958
2959
Andy Adamson82be4172012-05-23 05:02:35 -04002960static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002961 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002962 int flags,
2963 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002964 struct nfs4_label *label,
2965 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002966{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002967 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002968 struct nfs4_exception exception = { };
2969 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002970 struct nfs4_open_createattrs c = {
2971 .label = label,
2972 .sattr = sattr,
2973 .verf = {
2974 [0] = (__u32)jiffies,
2975 [1] = (__u32)current->pid,
2976 },
2977 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 int status;
2979
2980 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002981 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002982 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002983 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002984 if (status == 0)
2985 break;
2986 /* NOTE: BAD_SEQID means the server and client disagree about the
2987 * book-keeping w.r.t. state-changing operations
2988 * (OPEN/CLOSE/LOCK/LOCKU...)
2989 * It is actually a sign of a bug on the client or on the server.
2990 *
2991 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002992 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002993 * have unhashed the old state_owner for us, and that we can
2994 * therefore safely retry using a new one. We should still warn
2995 * the user though...
2996 */
2997 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002998 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002999 " returned a bad sequence-id error!\n",
3000 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003001 exception.retry = 1;
3002 continue;
3003 }
Trond Myklebust550f5742005-10-18 14:20:21 -07003004 /*
3005 * BAD_STATEID on OPEN means that the server cancelled our
3006 * state before it received the OPEN_CONFIRM.
3007 * Recover by retrying the request as per the discussion
3008 * on Page 181 of RFC3530.
3009 */
3010 if (status == -NFS4ERR_BAD_STATEID) {
3011 exception.retry = 1;
3012 continue;
3013 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04003014 if (status == -EAGAIN) {
3015 /* We must have found a delegation */
3016 exception.retry = 1;
3017 continue;
3018 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04003019 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
3020 continue;
3021 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003022 status, &exception));
3023 } while (exception.retry);
3024 return res;
3025}
3026
Trond Myklebust8487c472016-06-26 08:44:35 -04003027static int _nfs4_do_setattr(struct inode *inode,
3028 struct nfs_setattrargs *arg,
3029 struct nfs_setattrres *res,
3030 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003031 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003032{
3033 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003034 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003035 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3036 .rpc_argp = arg,
3037 .rpc_resp = res,
3038 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003039 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003040 struct rpc_cred *delegation_cred = NULL;
3041 unsigned long timestamp = jiffies;
3042 fmode_t fmode;
3043 bool truncate;
3044 int status;
3045
3046 nfs_fattr_init(res->fattr);
3047
3048 /* Servers should only apply open mode checks for file size changes */
3049 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3050 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3051
3052 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3053 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003054 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003055 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003056 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003057 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003058 l_ctx = nfs_get_lock_context(ctx);
3059 if (IS_ERR(l_ctx))
3060 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003061 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3062 &arg->stateid, &delegation_cred);
3063 nfs_put_lock_context(l_ctx);
3064 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003065 return -EBADF;
3066 } else
3067 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3068 if (delegation_cred)
3069 msg.rpc_cred = delegation_cred;
3070
3071 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3072
3073 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003074 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003075 renew_lease(server, timestamp);
3076 trace_nfs4_setattr(inode, &arg->stateid, status);
3077 return status;
3078}
3079
3080static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3081 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003082 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003083 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003084{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003085 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003086 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003087 struct nfs_setattrargs arg = {
3088 .fh = NFS_FH(inode),
3089 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003090 .server = server,
3091 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003092 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003093 };
3094 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003095 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003096 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003097 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003098 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003099 struct nfs4_exception exception = {
3100 .state = state,
3101 .inode = inode,
3102 .stateid = &arg.stateid,
3103 };
3104 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003105
David Quigleyaa9c2662013-05-22 12:50:44 -04003106 arg.bitmask = nfs4_bitmask(server, ilabel);
3107 if (ilabel)
3108 arg.bitmask = nfs4_bitmask(server, olabel);
3109
Linus Torvalds1da177e2005-04-16 15:20:36 -07003110 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003111 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003112 switch (err) {
3113 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003114 if (!(sattr->ia_valid & ATTR_SIZE)) {
3115 pr_warn_once("NFSv4: server %s is incorrectly "
3116 "applying open mode checks to "
3117 "a SETATTR that is not "
3118 "changing file size.\n",
3119 server->nfs_client->cl_hostname);
3120 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003121 if (state && !(state->state & FMODE_WRITE)) {
3122 err = -EBADF;
3123 if (sattr->ia_valid & ATTR_OPEN)
3124 err = -EACCES;
3125 goto out;
3126 }
3127 }
3128 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003129 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003130out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003131 return err;
3132}
3133
Peng Tao500d7012015-09-22 11:35:22 +08003134static bool
3135nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3136{
3137 if (inode == NULL || !nfs_have_layout(inode))
3138 return false;
3139
3140 return pnfs_wait_on_layoutreturn(inode, task);
3141}
3142
Linus Torvalds1da177e2005-04-16 15:20:36 -07003143struct nfs4_closedata {
3144 struct inode *inode;
3145 struct nfs4_state *state;
3146 struct nfs_closeargs arg;
3147 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003148 struct {
3149 struct nfs4_layoutreturn_args arg;
3150 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003151 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003152 u32 roc_barrier;
3153 bool roc;
3154 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003155 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003156 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003157};
3158
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003159static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003160{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003161 struct nfs4_closedata *calldata = data;
3162 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003163 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003164
Trond Myklebustcf805162016-11-15 14:56:07 -05003165 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003166 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3167 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003168 nfs4_put_open_state(calldata->state);
3169 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003170 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003171 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003172 kfree(calldata);
3173}
3174
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003175static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003176{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003177 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003178 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003179 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003180 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003181 struct nfs4_exception exception = {
3182 .state = state,
3183 .inode = calldata->inode,
3184 .stateid = &calldata->arg.stateid,
3185 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003186
Chuck Levera3ca5652012-03-01 17:00:40 -05003187 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003188 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3189 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003190 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003191
3192 /* Handle Layoutreturn errors */
3193 if (calldata->arg.lr_args && task->tk_status != 0) {
3194 switch (calldata->res.lr_ret) {
3195 default:
3196 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3197 break;
3198 case 0:
3199 calldata->arg.lr_args = NULL;
3200 calldata->res.lr_res = NULL;
3201 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003202 case -NFS4ERR_OLD_STATEID:
3203 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003204 calldata->inode))
3205 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003206 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003207 case -NFS4ERR_ADMIN_REVOKED:
3208 case -NFS4ERR_DELEG_REVOKED:
3209 case -NFS4ERR_EXPIRED:
3210 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003211 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3212 case -NFS4ERR_WRONG_CRED:
3213 calldata->arg.lr_args = NULL;
3214 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003215 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003216 }
3217 }
3218
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003219 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003220 * the state_owner. we keep this around to process errors
3221 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003222 switch (task->tk_status) {
3223 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003224 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003225 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003226 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003227 case -NFS4ERR_ACCESS:
3228 if (calldata->arg.bitmask != NULL) {
3229 calldata->arg.bitmask = NULL;
3230 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003231 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003232
3233 }
3234 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003235 case -NFS4ERR_OLD_STATEID:
3236 /* Did we race with OPEN? */
3237 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003238 state))
3239 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003240 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003241 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003242 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003243 case -NFS4ERR_EXPIRED:
3244 nfs4_free_revoked_stateid(server,
3245 &calldata->arg.stateid,
3246 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003247 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003248 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003249 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003251 task->tk_status = nfs4_async_handle_exception(task,
3252 server, task->tk_status, &exception);
3253 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003254 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003255 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003256 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3257 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003258out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003259 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003260 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003261 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003262 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003263 return;
3264lr_restart:
3265 calldata->res.lr_ret = 0;
3266out_restart:
3267 task->tk_status = 0;
3268 rpc_restart_call_prepare(task);
3269 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003270}
3271
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003272static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003273{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003274 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003275 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003276 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003277 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003278 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003279
Chuck Levera3ca5652012-03-01 17:00:40 -05003280 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003281 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003282 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003283
Trond Myklebust88069f72009-12-08 08:33:16 -05003284 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003285 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003286 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3287 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3288 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003289 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003290 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003291 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003292 if (state->n_rdonly == 0)
3293 call_close |= is_rdonly;
3294 else if (is_rdonly)
3295 calldata->arg.fmode |= FMODE_READ;
3296 if (state->n_wronly == 0)
3297 call_close |= is_wronly;
3298 else if (is_wronly)
3299 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003300 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3301 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003302 } else if (is_rdwr)
3303 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3304
Trond Myklebust5cc78612016-11-14 11:19:56 -05003305 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003306 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003307 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003308 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003309
3310 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003311 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003312 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003313 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003314
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003315 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003316 nfs_release_seqid(calldata->arg.seqid);
3317 goto out_wait;
3318 }
3319
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003320 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003321 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003322
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003323 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003324 /* Close-to-open cache consistency revalidation */
3325 if (!nfs4_have_delegation(inode, FMODE_READ))
3326 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3327 else
3328 calldata->arg.bitmask = NULL;
3329 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003330
Trond Myklebust6ae37332015-01-30 14:21:14 -05003331 calldata->arg.share_access =
3332 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3333 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003334
Trond Myklebustd8d84982016-12-19 12:14:44 -05003335 if (calldata->res.fattr == NULL)
3336 calldata->arg.bitmask = NULL;
3337 else if (calldata->arg.bitmask == NULL)
3338 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003339 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003340 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003341 &calldata->arg.seq_args,
3342 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003343 task) != 0)
3344 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003345 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003346 return;
3347out_no_action:
3348 task->tk_action = NULL;
3349out_wait:
3350 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351}
3352
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003353static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003354 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003355 .rpc_call_done = nfs4_close_done,
3356 .rpc_release = nfs4_free_closedata,
3357};
3358
Linus Torvalds1da177e2005-04-16 15:20:36 -07003359/*
3360 * It is possible for data to be read/written from a mem-mapped file
3361 * after the sys_close call (which hits the vfs layer as a flush).
3362 * This means that we can't safely call nfsv4 close on a file until
3363 * the inode is cleared. This in turn means that we are not good
3364 * NFSv4 citizens - we do not indicate to the server to update the file's
3365 * share state even when we are done with one of the three share
3366 * stateid's in the inode.
3367 *
3368 * NOTE: Caller must be holding the sp->so_owner semaphore!
3369 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003370int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003372 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003373 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003374 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003375 struct nfs4_state_owner *sp = state->owner;
3376 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003377 struct rpc_message msg = {
3378 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3379 .rpc_cred = state->owner->so_cred,
3380 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003381 struct rpc_task_setup task_setup_data = {
3382 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003383 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003384 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003385 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003386 .flags = RPC_TASK_ASYNC,
3387 };
Trond Myklebust95121352005-10-18 14:20:12 -07003388 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003389
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003390 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3391 &task_setup_data.rpc_client, &msg);
3392
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003393 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003394 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003395 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003396 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003397 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003398 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003399 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003400 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3401 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003402 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003403 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3404 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003405 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003406 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003407 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003408 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003409 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003410 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003411 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003412 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003413 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003414 calldata->lr.roc = pnfs_roc(state->inode,
3415 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3416 if (calldata->lr.roc) {
3417 calldata->arg.lr_args = &calldata->lr.arg;
3418 calldata->res.lr_res = &calldata->lr.res;
3419 }
Al Viro643168c2011-06-22 18:20:23 -04003420 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003421
Trond Myklebust1174dd12010-12-21 10:52:24 -05003422 msg.rpc_argp = &calldata->arg;
3423 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003424 task_setup_data.callback_data = calldata;
3425 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003426 if (IS_ERR(task))
3427 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003428 status = 0;
3429 if (wait)
3430 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003431 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003432 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003433out_free_calldata:
3434 kfree(calldata);
3435out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003436 nfs4_put_open_state(state);
3437 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003438 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003439}
3440
Trond Myklebust2b484292010-09-17 10:56:51 -04003441static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003442nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3443 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003444{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003445 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003446 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3447
3448 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003449
Trond Myklebust565277f2007-10-15 18:17:53 -04003450 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003451 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003452
3453 nfs4_label_release_security(label);
3454
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003455 if (IS_ERR(state))
3456 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003457 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458}
3459
Trond Myklebust1185a552009-12-03 15:54:02 -05003460static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003461{
3462 if (ctx->state == NULL)
3463 return;
3464 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003465 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003466 else
Al Viro643168c2011-06-22 18:20:23 -04003467 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003468}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003469
Trond Myklebustb944dba2013-11-04 15:20:20 -05003470#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3471#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003472#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003473
Linus Torvalds1da177e2005-04-16 15:20:36 -07003474static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3475{
Kinglong Mee8c612822015-08-26 21:12:58 +08003476 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003477 struct nfs4_server_caps_arg args = {
3478 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003479 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003480 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003481 struct nfs4_server_caps_res res = {};
3482 struct rpc_message msg = {
3483 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003484 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003485 .rpc_resp = &res,
3486 };
3487 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003488 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489
Kinglong Mee8c612822015-08-26 21:12:58 +08003490 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3491 FATTR4_WORD0_FH_EXPIRE_TYPE |
3492 FATTR4_WORD0_LINK_SUPPORT |
3493 FATTR4_WORD0_SYMLINK_SUPPORT |
3494 FATTR4_WORD0_ACLSUPPORT;
3495 if (minorversion)
3496 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3497
Bryan Schumaker7c513052011-03-24 17:12:24 +00003498 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003499 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003500 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003501 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003502 case 0:
3503 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3504 res.attr_bitmask[2] = 0;
3505 break;
3506 case 1:
3507 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3508 break;
3509 case 2:
3510 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3511 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003512 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003513 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3514 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3515 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3516 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003517 NFS_CAP_CTIME|NFS_CAP_MTIME|
3518 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003519 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3520 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003521 server->caps |= NFS_CAP_ACLS;
3522 if (res.has_links != 0)
3523 server->caps |= NFS_CAP_HARDLINKS;
3524 if (res.has_symlinks != 0)
3525 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003526 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3527 server->caps |= NFS_CAP_FILEID;
3528 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3529 server->caps |= NFS_CAP_MODE;
3530 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3531 server->caps |= NFS_CAP_NLINK;
3532 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3533 server->caps |= NFS_CAP_OWNER;
3534 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3535 server->caps |= NFS_CAP_OWNER_GROUP;
3536 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3537 server->caps |= NFS_CAP_ATIME;
3538 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3539 server->caps |= NFS_CAP_CTIME;
3540 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3541 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003542#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3543 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3544 server->caps |= NFS_CAP_SECURITY_LABEL;
3545#endif
3546 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3547 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003548 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003549
Trond Myklebusta65318b2009-03-11 14:10:28 -04003550 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3551 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3552 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003553 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003554
3555 /* Avoid a regression due to buggy server */
3556 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3557 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003558 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3559 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003560
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003562 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003564
Linus Torvalds1da177e2005-04-16 15:20:36 -07003565 return status;
3566}
3567
Trond Myklebust55a97592006-06-09 09:34:19 -04003568int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003569{
3570 struct nfs4_exception exception = { };
3571 int err;
3572 do {
3573 err = nfs4_handle_exception(server,
3574 _nfs4_server_capabilities(server, fhandle),
3575 &exception);
3576 } while (exception.retry);
3577 return err;
3578}
3579
3580static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3581 struct nfs_fsinfo *info)
3582{
David Quigleyaa9c2662013-05-22 12:50:44 -04003583 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003585 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003586 };
3587 struct nfs4_lookup_res res = {
3588 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003589 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003590 .fh = fhandle,
3591 };
3592 struct rpc_message msg = {
3593 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3594 .rpc_argp = &args,
3595 .rpc_resp = &res,
3596 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003597
David Quigleyaa9c2662013-05-22 12:50:44 -04003598 bitmask[0] = nfs4_fattr_bitmap[0];
3599 bitmask[1] = nfs4_fattr_bitmap[1];
3600 /*
3601 * Process the label in the upcoming getfattr
3602 */
3603 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3604
Trond Myklebust0e574af2005-10-27 22:12:38 -04003605 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003606 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003607}
3608
3609static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3610 struct nfs_fsinfo *info)
3611{
3612 struct nfs4_exception exception = { };
3613 int err;
3614 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003615 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003616 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003617 switch (err) {
3618 case 0:
3619 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003620 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003621 default:
3622 err = nfs4_handle_exception(server, err, &exception);
3623 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003624 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003625out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003626 return err;
3627}
3628
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003629static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3630 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3631{
Trond Myklebustc2190662013-08-26 19:23:04 -04003632 struct rpc_auth_create_args auth_args = {
3633 .pseudoflavor = flavor,
3634 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003635 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003636
Trond Myklebustc2190662013-08-26 19:23:04 -04003637 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003638 if (IS_ERR(auth))
3639 return -EACCES;
3640 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003641}
3642
Chuck Lever9a744ba2013-03-16 15:56:02 -04003643/*
3644 * Retry pseudoroot lookup with various security flavors. We do this when:
3645 *
3646 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3647 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3648 *
3649 * Returns zero on success, or a negative NFS4ERR value, or a
3650 * negative errno value.
3651 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003652static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003653 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003654{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003655 /* Per 3530bis 15.33.5 */
3656 static const rpc_authflavor_t flav_array[] = {
3657 RPC_AUTH_GSS_KRB5P,
3658 RPC_AUTH_GSS_KRB5I,
3659 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003660 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003661 RPC_AUTH_NULL,
3662 };
3663 int status = -EPERM;
3664 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003665
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003666 if (server->auth_info.flavor_len > 0) {
3667 /* try each flavor specified by user */
3668 for (i = 0; i < server->auth_info.flavor_len; i++) {
3669 status = nfs4_lookup_root_sec(server, fhandle, info,
3670 server->auth_info.flavors[i]);
3671 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3672 continue;
3673 break;
3674 }
3675 } else {
3676 /* no flavors specified by user, try default list */
3677 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3678 status = nfs4_lookup_root_sec(server, fhandle, info,
3679 flav_array[i]);
3680 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3681 continue;
3682 break;
3683 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003684 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003685
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003686 /*
3687 * -EACCESS could mean that the user doesn't have correct permissions
3688 * to access the mount. It could also mean that we tried to mount
3689 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3690 * existing mount programs don't handle -EACCES very well so it should
3691 * be mapped to -EPERM instead.
3692 */
3693 if (status == -EACCES)
3694 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003695 return status;
3696}
3697
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003698/**
3699 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3700 * @server: initialized nfs_server handle
3701 * @fhandle: we fill in the pseudo-fs root file handle
3702 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003703 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003704 *
3705 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003706 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003707int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003708 struct nfs_fsinfo *info,
3709 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003710{
Andre Przywarac7757072015-04-23 17:17:40 +01003711 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003712
Andre Przywarac7757072015-04-23 17:17:40 +01003713 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003714 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003715
3716 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003717 status = server->nfs_client->cl_mvops->find_root_sec(server,
3718 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003719
Linus Torvalds1da177e2005-04-16 15:20:36 -07003720 if (status == 0)
3721 status = nfs4_server_capabilities(server, fhandle);
3722 if (status == 0)
3723 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003724
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003725 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003726}
3727
Bryan Schumakerbae36242012-05-10 15:07:31 -04003728static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3729 struct nfs_fsinfo *info)
3730{
3731 int error;
3732 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003733 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003734
3735 error = nfs4_server_capabilities(server, mntfh);
3736 if (error < 0) {
3737 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3738 return error;
3739 }
3740
David Quigley14c43f72013-05-22 12:50:43 -04003741 label = nfs4_label_alloc(server, GFP_KERNEL);
3742 if (IS_ERR(label))
3743 return PTR_ERR(label);
3744
David Quigley1775fd32013-05-22 12:50:42 -04003745 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003746 if (error < 0) {
3747 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003748 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003749 }
3750
3751 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3752 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3753 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3754
David Quigley14c43f72013-05-22 12:50:43 -04003755err_free_label:
3756 nfs4_label_free(label);
3757
Bryan Schumakerbae36242012-05-10 15:07:31 -04003758 return error;
3759}
3760
Manoj Naik6b97fd32006-06-09 09:34:29 -04003761/*
3762 * Get locations and (maybe) other attributes of a referral.
3763 * Note that we'll actually follow the referral later when
3764 * we detect fsid mismatch in inode revalidation
3765 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003766static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3767 const struct qstr *name, struct nfs_fattr *fattr,
3768 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003769{
3770 int status = -ENOMEM;
3771 struct page *page = NULL;
3772 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003773
3774 page = alloc_page(GFP_KERNEL);
3775 if (page == NULL)
3776 goto out;
3777 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3778 if (locations == NULL)
3779 goto out;
3780
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003781 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003782 if (status != 0)
3783 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003784
3785 /*
3786 * If the fsid didn't change, this is a migration event, not a
3787 * referral. Cause us to drop into the exception handler, which
3788 * will kick off migration recovery.
3789 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003790 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003791 dprintk("%s: server did not return a different fsid for"
3792 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003793 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003794 goto out;
3795 }
Andy Adamson533eb462011-06-13 18:25:56 -04003796 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3797 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003798
Andy Adamson533eb462011-06-13 18:25:56 -04003799 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003800 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003801 memset(fhandle, 0, sizeof(struct nfs_fh));
3802out:
3803 if (page)
3804 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003805 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003806 return status;
3807}
3808
David Quigley1775fd32013-05-22 12:50:42 -04003809static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3810 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003811{
3812 struct nfs4_getattr_arg args = {
3813 .fh = fhandle,
3814 .bitmask = server->attr_bitmask,
3815 };
3816 struct nfs4_getattr_res res = {
3817 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003818 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 .server = server,
3820 };
3821 struct rpc_message msg = {
3822 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3823 .rpc_argp = &args,
3824 .rpc_resp = &res,
3825 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003826
3827 args.bitmask = nfs4_bitmask(server, label);
3828
Trond Myklebust0e574af2005-10-27 22:12:38 -04003829 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003830 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003831}
3832
David Quigley1775fd32013-05-22 12:50:42 -04003833static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3834 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003835{
3836 struct nfs4_exception exception = { };
3837 int err;
3838 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003839 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3840 trace_nfs4_getattr(server, fhandle, fattr, err);
3841 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003842 &exception);
3843 } while (exception.retry);
3844 return err;
3845}
3846
3847/*
3848 * The file is not closed if it is opened due to the a request to change
3849 * the size of the file. The open call will not be needed once the
3850 * VFS layer lookup-intents are implemented.
3851 *
3852 * Close is called when the inode is destroyed.
3853 * If we haven't opened the file for O_WRONLY, we
3854 * need to in the size_change case to obtain a stateid.
3855 *
3856 * Got race?
3857 * Because OPEN is always done by name in nfsv4, it is
3858 * possible that we opened a different file by the same
3859 * name. We can recognize this race condition, but we
3860 * can't do anything about it besides returning an error.
3861 *
3862 * This will be fixed with VFS changes (lookup-intent).
3863 */
3864static int
3865nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3866 struct iattr *sattr)
3867{
David Howells2b0143b2015-03-17 22:25:59 +00003868 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003869 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003870 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003871 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003872 int status;
3873
Peng Tao88ac8152014-09-12 11:04:10 +08003874 if (pnfs_ld_layoutret_on_setattr(inode) &&
3875 sattr->ia_valid & ATTR_SIZE &&
3876 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003877 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003878
Trond Myklebust0e574af2005-10-27 22:12:38 -04003879 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003880
Andy Adamson26699402012-05-30 16:12:24 -04003881 /* Deal with open(O_TRUNC) */
3882 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003883 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003884
3885 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003886 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003887 return 0;
3888
Trond Myklebustd5308382005-11-04 15:33:38 -05003889 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003890 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003891
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003892 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003893 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003894 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003895 }
3896
David Quigley14c43f72013-05-22 12:50:43 -04003897 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3898 if (IS_ERR(label))
3899 return PTR_ERR(label);
3900
Trond Myklebust199366f2018-03-20 16:43:18 -04003901 /* Return any delegations if we're going to change ACLs */
3902 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
Trond Myklebustc01d3642018-03-20 16:43:20 -04003903 nfs4_inode_make_writeable(inode);
Trond Myklebust199366f2018-03-20 16:43:18 -04003904
NeilBrown29b59f92016-10-13 15:26:47 +11003905 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003906 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003907 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003908 nfs_setsecurity(inode, fattr, label);
3909 }
David Quigley14c43f72013-05-22 12:50:43 -04003910 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003911 return status;
3912}
3913
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003914static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3915 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003916 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003917{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003918 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003919 int status;
3920 struct nfs4_lookup_arg args = {
3921 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003922 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003923 .name = name,
3924 };
3925 struct nfs4_lookup_res res = {
3926 .server = server,
3927 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003928 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003929 .fh = fhandle,
3930 };
3931 struct rpc_message msg = {
3932 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3933 .rpc_argp = &args,
3934 .rpc_resp = &res,
3935 };
3936
David Quigleyaa9c2662013-05-22 12:50:44 -04003937 args.bitmask = nfs4_bitmask(server, label);
3938
David Howells2b3de442006-08-22 20:06:09 -04003939 nfs_fattr_init(fattr);
3940
Linus Torvalds1da177e2005-04-16 15:20:36 -07003941 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003942 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003943 dprintk("NFS reply lookup: %d\n", status);
3944 return status;
3945}
3946
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003947static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003948{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003949 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003950 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003951 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3952 fattr->nlink = 2;
3953}
3954
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003955static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003956 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003957 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003958{
3959 struct nfs4_exception exception = { };
3960 struct rpc_clnt *client = *clnt;
3961 int err;
3962 do {
David Quigley1775fd32013-05-22 12:50:42 -04003963 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003964 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003965 switch (err) {
3966 case -NFS4ERR_BADNAME:
3967 err = -ENOENT;
3968 goto out;
3969 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003970 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003971 if (err == -NFS4ERR_MOVED)
3972 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003973 goto out;
3974 case -NFS4ERR_WRONGSEC:
3975 err = -EPERM;
3976 if (client != *clnt)
3977 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003978 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003979 if (IS_ERR(client))
3980 return PTR_ERR(client);
3981
3982 exception.retry = 1;
3983 break;
3984 default:
3985 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3986 }
3987 } while (exception.retry);
3988
3989out:
3990 if (err == 0)
3991 *clnt = client;
3992 else if (client != *clnt)
3993 rpc_shutdown_client(client);
3994
3995 return err;
3996}
3997
Al Virobeffb8f2016-07-20 16:34:42 -04003998static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003999 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4000 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004001{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004002 int status;
4003 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07004004
David Quigley1775fd32013-05-22 12:50:42 -04004005 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04004006 if (client != NFS_CLIENT(dir)) {
4007 rpc_shutdown_client(client);
4008 nfs_fixup_secinfo_attributes(fattr);
4009 }
4010 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004011}
4012
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004013struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04004014nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004015 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
4016{
Trond Myklebustb72888c2013-08-07 20:38:07 -04004017 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004018 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004019
David Quigley1775fd32013-05-22 12:50:42 -04004020 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004021 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004022 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004023 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004024}
4025
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004026static int _nfs4_proc_lookupp(struct inode *inode,
4027 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4028 struct nfs4_label *label)
4029{
4030 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4031 struct nfs_server *server = NFS_SERVER(inode);
4032 int status;
4033 struct nfs4_lookupp_arg args = {
4034 .bitmask = server->attr_bitmask,
4035 .fh = NFS_FH(inode),
4036 };
4037 struct nfs4_lookupp_res res = {
4038 .server = server,
4039 .fattr = fattr,
4040 .label = label,
4041 .fh = fhandle,
4042 };
4043 struct rpc_message msg = {
4044 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4045 .rpc_argp = &args,
4046 .rpc_resp = &res,
4047 };
4048
4049 args.bitmask = nfs4_bitmask(server, label);
4050
4051 nfs_fattr_init(fattr);
4052
4053 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4054 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4055 &res.seq_res, 0);
4056 dprintk("NFS reply lookupp: %d\n", status);
4057 return status;
4058}
4059
4060static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4061 struct nfs_fattr *fattr, struct nfs4_label *label)
4062{
4063 struct nfs4_exception exception = { };
4064 int err;
4065 do {
4066 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4067 trace_nfs4_lookupp(inode, err);
4068 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4069 &exception);
4070 } while (exception.retry);
4071 return err;
4072}
4073
Linus Torvalds1da177e2005-04-16 15:20:36 -07004074static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4075{
Trond Myklebust76b32992007-08-10 17:45:11 -04004076 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004077 struct nfs4_accessargs args = {
4078 .fh = NFS_FH(inode),
Anna Schumaker1750d922017-07-26 12:00:21 -04004079 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004081 struct nfs4_accessres res = {
4082 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004083 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004084 struct rpc_message msg = {
4085 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4086 .rpc_argp = &args,
4087 .rpc_resp = &res,
4088 .rpc_cred = entry->cred,
4089 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004090 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004092 if (!nfs_have_delegated_attributes(inode)) {
4093 res.fattr = nfs_alloc_fattr();
4094 if (res.fattr == NULL)
4095 return -ENOMEM;
4096 args.bitmask = server->cache_consistency_bitmask;
4097 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004098
Bryan Schumaker7c513052011-03-24 17:12:24 +00004099 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004100 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004101 nfs_access_set_mask(entry, res.access);
Trond Myklebust8bcbe7d2018-03-20 17:03:11 -04004102 if (res.fattr)
4103 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004104 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004105 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004106 return status;
4107}
4108
4109static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4110{
4111 struct nfs4_exception exception = { };
4112 int err;
4113 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004114 err = _nfs4_proc_access(inode, entry);
4115 trace_nfs4_access(inode, err);
4116 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004117 &exception);
4118 } while (exception.retry);
4119 return err;
4120}
4121
4122/*
4123 * TODO: For the time being, we don't try to get any attributes
4124 * along with any of the zero-copy operations READ, READDIR,
4125 * READLINK, WRITE.
4126 *
4127 * In the case of the first three, we want to put the GETATTR
4128 * after the read-type operation -- this is because it is hard
4129 * to predict the length of a GETATTR response in v4, and thus
4130 * align the READ data correctly. This means that the GETATTR
4131 * may end up partially falling into the page cache, and we should
4132 * shift it into the 'tail' of the xdr_buf before processing.
4133 * To do this efficiently, we need to know the total length
4134 * of data received, which doesn't seem to be available outside
4135 * of the RPC layer.
4136 *
4137 * In the case of WRITE, we also want to put the GETATTR after
4138 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004139 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004140 *
4141 * Both of these changes to the XDR layer would in fact be quite
4142 * minor, but I decided to leave them for a subsequent patch.
4143 */
4144static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4145 unsigned int pgbase, unsigned int pglen)
4146{
4147 struct nfs4_readlink args = {
4148 .fh = NFS_FH(inode),
4149 .pgbase = pgbase,
4150 .pglen = pglen,
4151 .pages = &page,
4152 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004153 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154 struct rpc_message msg = {
4155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4156 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004157 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004158 };
4159
Bryan Schumaker7c513052011-03-24 17:12:24 +00004160 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 -07004161}
4162
4163static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4164 unsigned int pgbase, unsigned int pglen)
4165{
4166 struct nfs4_exception exception = { };
4167 int err;
4168 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004169 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4170 trace_nfs4_readlink(inode, err);
4171 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004172 &exception);
4173 } while (exception.retry);
4174 return err;
4175}
4176
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004178 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004179 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180static int
4181nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004182 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004183{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004184 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004185 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004186 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004188 int status = 0;
4189
NeilBrown532d4de2016-10-13 15:26:47 +11004190 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004191 if (IS_ERR(ctx))
4192 return PTR_ERR(ctx);
4193
David Quigleyaa9c2662013-05-22 12:50:44 -04004194 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4195
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004196 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4197 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004198 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004199 if (IS_ERR(state)) {
4200 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004201 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004202 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004203out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004204 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004205 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004206 return status;
4207}
4208
Al Virobeffb8f2016-07-20 16:34:42 -04004209static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004210{
Trond Myklebust16e42952005-10-27 22:12:44 -04004211 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004212 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004213 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004214 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004215 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004216 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004217 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004218 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004219 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004220 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4221 .rpc_argp = &args,
4222 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004223 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004224 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004225 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004226
Bryan Schumaker7c513052011-03-24 17:12:24 +00004227 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004228 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004229 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004230 return status;
4231}
4232
Trond Myklebust912678d2018-03-20 16:43:15 -04004233static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4234{
4235 struct nfs4_exception exception = { };
4236 struct inode *inode = d_inode(dentry);
4237 int err;
4238
Trond Myklebustc01d3642018-03-20 16:43:20 -04004239 if (inode) {
4240 if (inode->i_nlink == 1)
4241 nfs4_inode_return_delegation(inode);
4242 else
4243 nfs4_inode_make_writeable(inode);
4244 }
Trond Myklebust912678d2018-03-20 16:43:15 -04004245 do {
4246 err = _nfs4_proc_remove(dir, &dentry->d_name);
4247 trace_nfs4_remove(dir, &dentry->d_name, err);
4248 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4249 &exception);
4250 } while (exception.retry);
4251 return err;
4252}
4253
4254static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004255{
4256 struct nfs4_exception exception = { };
4257 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004258
Linus Torvalds1da177e2005-04-16 15:20:36 -07004259 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004260 err = _nfs4_proc_remove(dir, name);
4261 trace_nfs4_remove(dir, name, err);
4262 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263 &exception);
4264 } while (exception.retry);
4265 return err;
4266}
4267
Trond Myklebust977fcc22018-03-20 16:43:17 -04004268static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004269{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004270 struct nfs_removeargs *args = msg->rpc_argp;
4271 struct nfs_removeres *res = msg->rpc_resp;
Trond Myklebust977fcc22018-03-20 16:43:17 -04004272 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004273
Trond Myklebust977fcc22018-03-20 16:43:17 -04004274 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004276 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004277
4278 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004279
4280 if (inode)
4281 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004282}
4283
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004284static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4285{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004286 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004287 &data->args.seq_args,
4288 &data->res.seq_res,
4289 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004290}
4291
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004292static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004293{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004294 struct nfs_unlinkdata *data = task->tk_calldata;
4295 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004296
Trond Myklebust14516c32010-07-31 14:29:06 -04004297 if (!nfs4_sequence_done(task, &res->seq_res))
4298 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004299 if (nfs4_async_handle_error(task, res->server, NULL,
4300 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004301 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004302 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004303 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004304 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004305}
4306
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004307static void nfs4_proc_rename_setup(struct rpc_message *msg,
4308 struct dentry *old_dentry,
4309 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004310{
Jeff Laytond3d41522010-09-17 17:31:57 -04004311 struct nfs_renameargs *arg = msg->rpc_argp;
4312 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004313 struct inode *old_inode = d_inode(old_dentry);
4314 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004315
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004316 if (old_inode)
Trond Myklebustc01d3642018-03-20 16:43:20 -04004317 nfs4_inode_make_writeable(old_inode);
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004318 if (new_inode)
4319 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004320 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004321 res->server = NFS_SB(old_dentry->d_sb);
Chuck Levera9c92d62013-08-09 12:48:18 -04004322 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004323}
4324
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004325static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4326{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004327 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004328 &data->args.seq_args,
4329 &data->res.seq_res,
4330 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004331}
4332
4333static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4334 struct inode *new_dir)
4335{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004336 struct nfs_renamedata *data = task->tk_calldata;
4337 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004338
4339 if (!nfs4_sequence_done(task, &res->seq_res))
4340 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004341 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004342 return 0;
4343
Trond Myklebustc733c492017-01-11 12:32:26 -05004344 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004345 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004346 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004347 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004348 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004349 return 1;
4350}
4351
Al Virobeffb8f2016-07-20 16:34:42 -04004352static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004353{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004354 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004355 struct nfs4_link_arg arg = {
4356 .fh = NFS_FH(inode),
4357 .dir_fh = NFS_FH(dir),
4358 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004359 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004360 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004361 struct nfs4_link_res res = {
4362 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004363 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004364 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004365 struct rpc_message msg = {
4366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4367 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004368 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004369 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004370 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004371
Trond Myklebust136f2622010-04-16 16:22:49 -04004372 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004373 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004374 goto out;
4375
David Quigley14c43f72013-05-22 12:50:43 -04004376 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4377 if (IS_ERR(res.label)) {
4378 status = PTR_ERR(res.label);
4379 goto out;
4380 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004381 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004382
Trond Myklebustc01d3642018-03-20 16:43:20 -04004383 nfs4_inode_make_writeable(inode);
Trond Myklebust9f768272018-03-20 16:43:14 -04004384
Bryan Schumaker7c513052011-03-24 17:12:24 +00004385 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004386 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004387 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004388 status = nfs_post_op_update_inode(inode, res.fattr);
4389 if (!status)
4390 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004391 }
David Quigley14c43f72013-05-22 12:50:43 -04004392
4393
4394 nfs4_label_free(res.label);
4395
Trond Myklebust136f2622010-04-16 16:22:49 -04004396out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004397 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004398 return status;
4399}
4400
Al Virobeffb8f2016-07-20 16:34:42 -04004401static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004402{
4403 struct nfs4_exception exception = { };
4404 int err;
4405 do {
4406 err = nfs4_handle_exception(NFS_SERVER(inode),
4407 _nfs4_proc_link(inode, dir, name),
4408 &exception);
4409 } while (exception.retry);
4410 return err;
4411}
4412
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004413struct nfs4_createdata {
4414 struct rpc_message msg;
4415 struct nfs4_create_arg arg;
4416 struct nfs4_create_res res;
4417 struct nfs_fh fh;
4418 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004419 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004420};
4421
4422static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004423 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004424{
4425 struct nfs4_createdata *data;
4426
4427 data = kzalloc(sizeof(*data), GFP_KERNEL);
4428 if (data != NULL) {
4429 struct nfs_server *server = NFS_SERVER(dir);
4430
David Quigley14c43f72013-05-22 12:50:43 -04004431 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4432 if (IS_ERR(data->label))
4433 goto out_free;
4434
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004435 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4436 data->msg.rpc_argp = &data->arg;
4437 data->msg.rpc_resp = &data->res;
4438 data->arg.dir_fh = NFS_FH(dir);
4439 data->arg.server = server;
4440 data->arg.name = name;
4441 data->arg.attrs = sattr;
4442 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004443 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004444 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004445 data->res.server = server;
4446 data->res.fh = &data->fh;
4447 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004448 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004450 }
4451 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004452out_free:
4453 kfree(data);
4454 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004455}
4456
4457static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4458{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004459 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004460 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004461 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004462 update_changeattr(dir, &data->res.dir_cinfo,
4463 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004464 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004465 }
4466 return status;
4467}
4468
4469static void nfs4_free_createdata(struct nfs4_createdata *data)
4470{
David Quigley14c43f72013-05-22 12:50:43 -04004471 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004472 kfree(data);
4473}
4474
Chuck Lever4f390c12006-08-22 20:06:22 -04004475static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004476 struct page *page, unsigned int len, struct iattr *sattr,
4477 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004478{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004479 struct nfs4_createdata *data;
4480 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481
Chuck Lever94a6d752006-08-22 20:06:23 -04004482 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004483 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004484
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004485 status = -ENOMEM;
4486 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4487 if (data == NULL)
4488 goto out;
4489
4490 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4491 data->arg.u.symlink.pages = &page;
4492 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004493 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004495 status = nfs4_do_create(dir, dentry, data);
4496
4497 nfs4_free_createdata(data);
4498out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004499 return status;
4500}
4501
Chuck Lever4f390c12006-08-22 20:06:22 -04004502static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004503 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004504{
4505 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004506 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004508
4509 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4510
Linus Torvalds1da177e2005-04-16 15:20:36 -07004511 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004512 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4513 trace_nfs4_symlink(dir, &dentry->d_name, err);
4514 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004515 &exception);
4516 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004517
4518 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004519 return err;
4520}
4521
4522static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004523 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004524{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004525 struct nfs4_createdata *data;
4526 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004527
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004528 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4529 if (data == NULL)
4530 goto out;
4531
David Quigley1775fd32013-05-22 12:50:42 -04004532 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004533 status = nfs4_do_create(dir, dentry, data);
4534
4535 nfs4_free_createdata(data);
4536out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 return status;
4538}
4539
4540static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4541 struct iattr *sattr)
4542{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004543 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004545 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004546 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004547
David Quigleyaa9c2662013-05-22 12:50:44 -04004548 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4549
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004550 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4551 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004552 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004553 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4554 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4555 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004556 &exception);
4557 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004558 nfs4_label_release_security(label);
4559
Linus Torvalds1da177e2005-04-16 15:20:36 -07004560 return err;
4561}
4562
4563static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004564 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004565{
David Howells2b0143b2015-03-17 22:25:59 +00004566 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004567 struct nfs4_readdir_arg args = {
4568 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004569 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004570 .pgbase = 0,
4571 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004572 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004573 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004574 };
4575 struct nfs4_readdir_res res;
4576 struct rpc_message msg = {
4577 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4578 .rpc_argp = &args,
4579 .rpc_resp = &res,
4580 .rpc_cred = cred,
4581 };
4582 int status;
4583
Al Viro6de14722013-09-16 10:53:17 -04004584 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4585 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004586 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004587 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004589 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 -05004590 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004591 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004592 status += args.pgbase;
4593 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004594
4595 nfs_invalidate_atime(dir);
4596
Harvey Harrison3110ff82008-05-02 13:42:44 -07004597 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004598 return status;
4599}
4600
4601static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004602 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603{
4604 struct nfs4_exception exception = { };
4605 int err;
4606 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004607 err = _nfs4_proc_readdir(dentry, cred, cookie,
4608 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004609 trace_nfs4_readdir(d_inode(dentry), err);
4610 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 &exception);
4612 } while (exception.retry);
4613 return err;
4614}
4615
4616static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004617 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004619 struct nfs4_createdata *data;
4620 int mode = sattr->ia_mode;
4621 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004622
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004623 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4624 if (data == NULL)
4625 goto out;
4626
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004628 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004629 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004630 data->arg.ftype = NF4BLK;
4631 data->arg.u.device.specdata1 = MAJOR(rdev);
4632 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 }
4634 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004635 data->arg.ftype = NF4CHR;
4636 data->arg.u.device.specdata1 = MAJOR(rdev);
4637 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004638 } else if (!S_ISSOCK(mode)) {
4639 status = -EINVAL;
4640 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004641 }
David Quigley1775fd32013-05-22 12:50:42 -04004642
David Quigleyaa9c2662013-05-22 12:50:44 -04004643 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004644 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004645out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004646 nfs4_free_createdata(data);
4647out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004648 return status;
4649}
4650
4651static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4652 struct iattr *sattr, dev_t rdev)
4653{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004654 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004656 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004657 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004658
David Quigleyaa9c2662013-05-22 12:50:44 -04004659 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4660
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004661 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4662 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004664 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4665 trace_nfs4_mknod(dir, &dentry->d_name, err);
4666 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004667 &exception);
4668 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004669
4670 nfs4_label_release_security(label);
4671
Linus Torvalds1da177e2005-04-16 15:20:36 -07004672 return err;
4673}
4674
4675static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4676 struct nfs_fsstat *fsstat)
4677{
4678 struct nfs4_statfs_arg args = {
4679 .fh = fhandle,
4680 .bitmask = server->attr_bitmask,
4681 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004682 struct nfs4_statfs_res res = {
4683 .fsstat = fsstat,
4684 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004685 struct rpc_message msg = {
4686 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4687 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004688 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004689 };
4690
Trond Myklebust0e574af2005-10-27 22:12:38 -04004691 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004692 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004693}
4694
4695static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4696{
4697 struct nfs4_exception exception = { };
4698 int err;
4699 do {
4700 err = nfs4_handle_exception(server,
4701 _nfs4_proc_statfs(server, fhandle, fsstat),
4702 &exception);
4703 } while (exception.retry);
4704 return err;
4705}
4706
4707static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4708 struct nfs_fsinfo *fsinfo)
4709{
4710 struct nfs4_fsinfo_arg args = {
4711 .fh = fhandle,
4712 .bitmask = server->attr_bitmask,
4713 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004714 struct nfs4_fsinfo_res res = {
4715 .fsinfo = fsinfo,
4716 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004717 struct rpc_message msg = {
4718 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4719 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004720 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004721 };
4722
Bryan Schumaker7c513052011-03-24 17:12:24 +00004723 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004724}
4725
4726static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4727{
4728 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004729 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004730 int err;
4731
4732 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004733 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004734 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004735 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004736 nfs4_set_lease_period(server->nfs_client,
4737 fsinfo->lease_time * HZ,
4738 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004739 break;
4740 }
4741 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004742 } while (exception.retry);
4743 return err;
4744}
4745
4746static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4747{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004748 int error;
4749
Trond Myklebust0e574af2005-10-27 22:12:38 -04004750 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004751 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004752 if (error == 0) {
4753 /* block layout checks this! */
4754 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004755 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004756 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004757
4758 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004759}
4760
4761static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4762 struct nfs_pathconf *pathconf)
4763{
4764 struct nfs4_pathconf_arg args = {
4765 .fh = fhandle,
4766 .bitmask = server->attr_bitmask,
4767 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004768 struct nfs4_pathconf_res res = {
4769 .pathconf = pathconf,
4770 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004771 struct rpc_message msg = {
4772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4773 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004774 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004775 };
4776
4777 /* None of the pathconf attributes are mandatory to implement */
4778 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4779 memset(pathconf, 0, sizeof(*pathconf));
4780 return 0;
4781 }
4782
Trond Myklebust0e574af2005-10-27 22:12:38 -04004783 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004784 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004785}
4786
4787static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4788 struct nfs_pathconf *pathconf)
4789{
4790 struct nfs4_exception exception = { };
4791 int err;
4792
4793 do {
4794 err = nfs4_handle_exception(server,
4795 _nfs4_proc_pathconf(server, fhandle, pathconf),
4796 &exception);
4797 } while (exception.retry);
4798 return err;
4799}
4800
Trond Myklebust5521abf2013-03-16 20:54:34 -04004801int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004802 const struct nfs_open_context *ctx,
4803 const struct nfs_lock_context *l_ctx,
4804 fmode_t fmode)
4805{
NeilBrown17393472016-10-13 15:26:47 +11004806 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004807}
4808EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4809
Trond Myklebust5521abf2013-03-16 20:54:34 -04004810static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4811 const struct nfs_open_context *ctx,
4812 const struct nfs_lock_context *l_ctx,
4813 fmode_t fmode)
4814{
4815 nfs4_stateid current_stateid;
4816
Trond Myklebuste1253be2014-03-05 08:44:23 -05004817 /* If the current stateid represents a lost lock, then exit */
4818 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4819 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004820 return nfs4_stateid_match(stateid, &current_stateid);
4821}
4822
4823static bool nfs4_error_stateid_expired(int err)
4824{
4825 switch (err) {
4826 case -NFS4ERR_DELEG_REVOKED:
4827 case -NFS4ERR_ADMIN_REVOKED:
4828 case -NFS4ERR_BAD_STATEID:
4829 case -NFS4ERR_STALE_STATEID:
4830 case -NFS4ERR_OLD_STATEID:
4831 case -NFS4ERR_OPENMODE:
4832 case -NFS4ERR_EXPIRED:
4833 return true;
4834 }
4835 return false;
4836}
4837
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004838static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004839{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004840 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004841
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004842 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004843 if (task->tk_status < 0) {
4844 struct nfs4_exception exception = {
4845 .inode = hdr->inode,
4846 .state = hdr->args.context->state,
4847 .stateid = &hdr->args.stateid,
4848 };
4849 task->tk_status = nfs4_async_handle_exception(task,
4850 server, task->tk_status, &exception);
4851 if (exception.retry) {
4852 rpc_restart_call_prepare(task);
4853 return -EAGAIN;
4854 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004855 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004856
Linus Torvalds1da177e2005-04-16 15:20:36 -07004857 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004858 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004859 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004860}
4861
Trond Myklebust5521abf2013-03-16 20:54:34 -04004862static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004863 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004864{
4865
4866 if (!nfs4_error_stateid_expired(task->tk_status) ||
4867 nfs4_stateid_is_current(&args->stateid,
4868 args->context,
4869 args->lock_context,
4870 FMODE_READ))
4871 return false;
4872 rpc_restart_call_prepare(task);
4873 return true;
4874}
4875
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004876static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004877{
4878
4879 dprintk("--> %s\n", __func__);
4880
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004881 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004882 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004884 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004885 if (task->tk_status > 0)
4886 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004887 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4888 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004889}
4890
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004891static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4892 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004893{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004894 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004895 if (!hdr->pgio_done_cb)
4896 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004897 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004898 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004899}
4900
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004901static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4902 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004903{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004904 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004905 &hdr->args.seq_args,
4906 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004907 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004908 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004909 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4910 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004911 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004912 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004913 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004914 return -EIO;
4915 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004916}
4917
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004918static int nfs4_write_done_cb(struct rpc_task *task,
4919 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004920{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004921 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004922
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004923 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004924 if (task->tk_status < 0) {
4925 struct nfs4_exception exception = {
4926 .inode = hdr->inode,
4927 .state = hdr->args.context->state,
4928 .stateid = &hdr->args.stateid,
4929 };
4930 task->tk_status = nfs4_async_handle_exception(task,
4931 NFS_SERVER(inode), task->tk_status,
4932 &exception);
4933 if (exception.retry) {
4934 rpc_restart_call_prepare(task);
4935 return -EAGAIN;
4936 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004937 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004938 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004939 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004940 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004941 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004942 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004943}
4944
Trond Myklebust5521abf2013-03-16 20:54:34 -04004945static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004946 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004947{
4948
4949 if (!nfs4_error_stateid_expired(task->tk_status) ||
4950 nfs4_stateid_is_current(&args->stateid,
4951 args->context,
4952 args->lock_context,
4953 FMODE_WRITE))
4954 return false;
4955 rpc_restart_call_prepare(task);
4956 return true;
4957}
4958
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004959static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004960{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004961 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004962 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004963 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004964 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004965 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4966 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004967}
4968
Trond Myklebust5a37f852012-04-28 14:55:16 -04004969static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004970bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004971{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004972 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004973 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004974 return false;
4975 /* Otherwise, request attributes if and only if we don't hold
4976 * a delegation
4977 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004978 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004979}
Fred Isamana69aef12011-03-03 15:13:47 +00004980
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004981static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
Anna Schumakerfb91fb02018-05-04 16:22:48 -04004982 struct rpc_message *msg,
4983 struct rpc_clnt **clnt)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004984{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004985 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004986
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004987 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4988 hdr->args.bitmask = NULL;
4989 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004990 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004991 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004992
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004993 if (!hdr->pgio_done_cb)
4994 hdr->pgio_done_cb = nfs4_write_done_cb;
4995 hdr->res.server = server;
4996 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004997
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004998 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004999 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Anna Schumakerfb91fb02018-05-04 16:22:48 -04005000 nfs4_state_protect_write(server->nfs_client, clnt, msg, hdr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005001}
5002
Fred Isaman0b7c0152012-04-20 14:47:39 -04005003static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
5004{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005005 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005006 &data->args.seq_args,
5007 &data->res.seq_res,
5008 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005009}
5010
Fred Isaman0b7c0152012-04-20 14:47:39 -04005011static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005012{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005013 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04005014
Trond Myklebustcc668ab2013-08-14 15:31:28 -04005015 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10005016 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
5017 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07005018 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05005019 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005020 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05005021 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005022}
5023
Fred Isaman0b7c0152012-04-20 14:47:39 -04005024static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00005025{
5026 if (!nfs4_sequence_done(task, &data->res.seq_res))
5027 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04005028 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00005029}
5030
Fred Isaman0b7c0152012-04-20 14:47:39 -04005031static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005032{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005033 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005034
Fred Isaman0b7c0152012-04-20 14:47:39 -04005035 if (data->commit_done_cb == NULL)
5036 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005037 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005038 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04005039 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005040}
5041
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005042struct nfs4_renewdata {
5043 struct nfs_client *client;
5044 unsigned long timestamp;
5045};
5046
Linus Torvalds1da177e2005-04-16 15:20:36 -07005047/*
5048 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5049 * standalone procedure for queueing an asynchronous RENEW.
5050 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005051static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005052{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005053 struct nfs4_renewdata *data = calldata;
5054 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005055
Elena Reshetova212bf412017-10-20 12:53:38 +03005056 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005057 nfs4_schedule_state_renewal(clp);
5058 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005059 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005060}
5061
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005062static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005063{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005064 struct nfs4_renewdata *data = calldata;
5065 struct nfs_client *clp = data->client;
5066 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005068 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005069 switch (task->tk_status) {
5070 case 0:
5071 break;
5072 case -NFS4ERR_LEASE_MOVED:
5073 nfs4_schedule_lease_moved_recovery(clp);
5074 break;
5075 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005076 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005077 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5078 return;
5079 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005080 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005081 return;
5082 }
5083 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005084 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005085 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086}
5087
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005088static const struct rpc_call_ops nfs4_renew_ops = {
5089 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005090 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005091};
5092
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005093static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005094{
5095 struct rpc_message msg = {
5096 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5097 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005098 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005100 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005101
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005102 if (renew_flags == 0)
5103 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005104 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005105 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005106 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005107 if (data == NULL) {
5108 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005109 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005110 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005111 data->client = clp;
5112 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005113 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005114 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005115}
5116
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005117static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005118{
5119 struct rpc_message msg = {
5120 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5121 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005122 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005123 };
5124 unsigned long now = jiffies;
5125 int status;
5126
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005127 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005128 if (status < 0)
5129 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005130 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005131 return 0;
5132}
5133
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005134static inline int nfs4_server_supports_acls(struct nfs_server *server)
5135{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005136 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005137}
5138
Trond Myklebust21f498c2012-08-24 10:59:25 -04005139/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5140 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005141 * the stack.
5142 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005143#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005144
Neil Hormane9e3d722011-03-04 19:26:03 -05005145static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005146 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005147{
5148 struct page *newpage, **spages;
5149 int rc = 0;
5150 size_t len;
5151 spages = pages;
5152
5153 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005154 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005155 newpage = alloc_page(GFP_KERNEL);
5156
5157 if (newpage == NULL)
5158 goto unwind;
5159 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005160 buf += len;
5161 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005162 *pages++ = newpage;
5163 rc++;
5164 } while (buflen != 0);
5165
5166 return rc;
5167
5168unwind:
5169 for(; rc > 0; rc--)
5170 __free_page(spages[rc-1]);
5171 return -ENOMEM;
5172}
5173
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005174struct nfs4_cached_acl {
5175 int cached;
5176 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005177 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005178};
5179
5180static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005181{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005182 struct nfs_inode *nfsi = NFS_I(inode);
5183
5184 spin_lock(&inode->i_lock);
5185 kfree(nfsi->nfs4_acl);
5186 nfsi->nfs4_acl = acl;
5187 spin_unlock(&inode->i_lock);
5188}
5189
5190static void nfs4_zap_acl_attr(struct inode *inode)
5191{
5192 nfs4_set_cached_acl(inode, NULL);
5193}
5194
5195static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5196{
5197 struct nfs_inode *nfsi = NFS_I(inode);
5198 struct nfs4_cached_acl *acl;
5199 int ret = -ENOENT;
5200
5201 spin_lock(&inode->i_lock);
5202 acl = nfsi->nfs4_acl;
5203 if (acl == NULL)
5204 goto out;
5205 if (buf == NULL) /* user is just asking for length */
5206 goto out_len;
5207 if (acl->cached == 0)
5208 goto out;
5209 ret = -ERANGE; /* see getxattr(2) man page */
5210 if (acl->len > buflen)
5211 goto out;
5212 memcpy(buf, acl->data, acl->len);
5213out_len:
5214 ret = acl->len;
5215out:
5216 spin_unlock(&inode->i_lock);
5217 return ret;
5218}
5219
Sachin Prabhu5794d212012-04-17 14:36:40 +01005220static 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 +00005221{
5222 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005223 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005224
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005225 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005226 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005227 if (acl == NULL)
5228 goto out;
5229 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005230 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005231 } else {
5232 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5233 if (acl == NULL)
5234 goto out;
5235 acl->cached = 0;
5236 }
5237 acl->len = acl_len;
5238out:
5239 nfs4_set_cached_acl(inode, acl);
5240}
5241
Andy Adamsonbf118a32011-12-07 11:55:27 -05005242/*
5243 * The getxattr API returns the required buffer length when called with a
5244 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5245 * the required buf. On a NULL buf, we send a page of data to the server
5246 * guessing that the ACL request can be serviced by a page. If so, we cache
5247 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5248 * the cache. If not so, we throw away the page, and cache the required
5249 * length. The next getxattr call will then produce another round trip to
5250 * the server, this time with the input buf of the required size.
5251 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005252static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005253{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005254 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005255 struct nfs_getaclargs args = {
5256 .fh = NFS_FH(inode),
5257 .acl_pages = pages,
5258 .acl_len = buflen,
5259 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005260 struct nfs_getaclres res = {
5261 .acl_len = buflen,
5262 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005263 struct rpc_message msg = {
5264 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5265 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005266 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005267 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005268 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005269 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005270
Trond Myklebust21f498c2012-08-24 10:59:25 -04005271 if (npages > ARRAY_SIZE(pages))
5272 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005273
Andy Adamsonbf118a32011-12-07 11:55:27 -05005274 for (i = 0; i < npages; i++) {
5275 pages[i] = alloc_page(GFP_KERNEL);
5276 if (!pages[i])
5277 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005278 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005279
5280 /* for decoding across pages */
5281 res.acl_scratch = alloc_page(GFP_KERNEL);
5282 if (!res.acl_scratch)
5283 goto out_free;
5284
Andy Adamsonbf118a32011-12-07 11:55:27 -05005285 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005286
Peng Taode040be2012-01-10 22:42:47 +08005287 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005288 __func__, buf, buflen, npages, args.acl_len);
5289 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5290 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005291 if (ret)
5292 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005293
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005294 /* Handle the case where the passed-in buffer is too short */
5295 if (res.acl_flags & NFS4_ACL_TRUNC) {
5296 /* Did the user only issue a request for the acl length? */
5297 if (buf == NULL)
5298 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005299 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005300 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005301 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005302 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005303 if (buf) {
5304 if (res.acl_len > buflen) {
5305 ret = -ERANGE;
5306 goto out_free;
5307 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005308 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005309 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005310out_ok:
5311 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005312out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005313 for (i = 0; i < npages; i++)
5314 if (pages[i])
5315 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005316 if (res.acl_scratch)
5317 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005318 return ret;
5319}
5320
Trond Myklebust16b42892006-08-24 12:27:15 -04005321static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5322{
5323 struct nfs4_exception exception = { };
5324 ssize_t ret;
5325 do {
5326 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005327 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005328 if (ret >= 0)
5329 break;
5330 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5331 } while (exception.retry);
5332 return ret;
5333}
5334
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005335static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5336{
5337 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005338 int ret;
5339
5340 if (!nfs4_server_supports_acls(server))
5341 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005342 ret = nfs_revalidate_inode(server, inode);
5343 if (ret < 0)
5344 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005345 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5346 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005347 ret = nfs4_read_cached_acl(inode, buf, buflen);
5348 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005349 /* -ENOENT is returned if there is no ACL or if there is an ACL
5350 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005351 return ret;
5352 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005353}
5354
Trond Myklebust16b42892006-08-24 12:27:15 -04005355static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005356{
5357 struct nfs_server *server = NFS_SERVER(inode);
5358 struct page *pages[NFS4ACL_MAXPAGES];
5359 struct nfs_setaclargs arg = {
5360 .fh = NFS_FH(inode),
5361 .acl_pages = pages,
5362 .acl_len = buflen,
5363 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005364 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005365 struct rpc_message msg = {
5366 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5367 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005368 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005369 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005370 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005371 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005372
5373 if (!nfs4_server_supports_acls(server))
5374 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005375 if (npages > ARRAY_SIZE(pages))
5376 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005377 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005378 if (i < 0)
5379 return i;
Trond Myklebustc01d3642018-03-20 16:43:20 -04005380 nfs4_inode_make_writeable(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005381 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005382
5383 /*
5384 * Free each page after tx, so the only ref left is
5385 * held by the network stack
5386 */
5387 for (; i > 0; i--)
5388 put_page(pages[i-1]);
5389
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005390 /*
5391 * Acl update can result in inode attribute update.
5392 * so mark the attribute cache invalid.
5393 */
5394 spin_lock(&inode->i_lock);
Trond Myklebust16e14372018-03-20 16:53:31 -04005395 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_CHANGE
5396 | NFS_INO_INVALID_CTIME;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005397 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005398 nfs_access_zap_cache(inode);
5399 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005400 return ret;
5401}
5402
Trond Myklebust16b42892006-08-24 12:27:15 -04005403static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5404{
5405 struct nfs4_exception exception = { };
5406 int err;
5407 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005408 err = __nfs4_proc_set_acl(inode, buf, buflen);
5409 trace_nfs4_set_acl(inode, err);
5410 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005411 &exception);
5412 } while (exception.retry);
5413 return err;
5414}
5415
David Quigleyaa9c2662013-05-22 12:50:44 -04005416#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5417static int _nfs4_get_security_label(struct inode *inode, void *buf,
5418 size_t buflen)
5419{
5420 struct nfs_server *server = NFS_SERVER(inode);
5421 struct nfs_fattr fattr;
5422 struct nfs4_label label = {0, 0, buflen, buf};
5423
5424 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005425 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005426 .fh = NFS_FH(inode),
5427 .bitmask = bitmask,
5428 };
5429 struct nfs4_getattr_res res = {
5430 .fattr = &fattr,
5431 .label = &label,
5432 .server = server,
5433 };
5434 struct rpc_message msg = {
5435 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005436 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005437 .rpc_resp = &res,
5438 };
5439 int ret;
5440
5441 nfs_fattr_init(&fattr);
5442
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005443 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005444 if (ret)
5445 return ret;
5446 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5447 return -ENOENT;
5448 if (buflen < label.len)
5449 return -ERANGE;
5450 return 0;
5451}
5452
5453static int nfs4_get_security_label(struct inode *inode, void *buf,
5454 size_t buflen)
5455{
5456 struct nfs4_exception exception = { };
5457 int err;
5458
5459 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5460 return -EOPNOTSUPP;
5461
5462 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005463 err = _nfs4_get_security_label(inode, buf, buflen);
5464 trace_nfs4_get_security_label(inode, err);
5465 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005466 &exception);
5467 } while (exception.retry);
5468 return err;
5469}
5470
5471static int _nfs4_do_set_security_label(struct inode *inode,
5472 struct nfs4_label *ilabel,
5473 struct nfs_fattr *fattr,
5474 struct nfs4_label *olabel)
5475{
5476
5477 struct iattr sattr = {0};
5478 struct nfs_server *server = NFS_SERVER(inode);
5479 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005480 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005481 .fh = NFS_FH(inode),
5482 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005483 .server = server,
5484 .bitmask = bitmask,
5485 .label = ilabel,
5486 };
5487 struct nfs_setattrres res = {
5488 .fattr = fattr,
5489 .label = olabel,
5490 .server = server,
5491 };
5492 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005493 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5494 .rpc_argp = &arg,
5495 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005496 };
5497 int status;
5498
Jeff Layton12207f62013-11-01 10:49:32 -04005499 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005500
Jeff Layton12207f62013-11-01 10:49:32 -04005501 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005502 if (status)
5503 dprintk("%s failed: %d\n", __func__, status);
5504
5505 return status;
5506}
5507
5508static int nfs4_do_set_security_label(struct inode *inode,
5509 struct nfs4_label *ilabel,
5510 struct nfs_fattr *fattr,
5511 struct nfs4_label *olabel)
5512{
5513 struct nfs4_exception exception = { };
5514 int err;
5515
5516 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005517 err = _nfs4_do_set_security_label(inode, ilabel,
5518 fattr, olabel);
5519 trace_nfs4_set_security_label(inode, err);
5520 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005521 &exception);
5522 } while (exception.retry);
5523 return err;
5524}
5525
5526static int
Al Viro59301222016-05-27 10:19:30 -04005527nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005528{
5529 struct nfs4_label ilabel, *olabel = NULL;
5530 struct nfs_fattr fattr;
5531 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005532 int status;
5533
5534 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5535 return -EOPNOTSUPP;
5536
5537 nfs_fattr_init(&fattr);
5538
5539 ilabel.pi = 0;
5540 ilabel.lfs = 0;
5541 ilabel.label = (char *)buf;
5542 ilabel.len = buflen;
5543
5544 cred = rpc_lookup_cred();
5545 if (IS_ERR(cred))
5546 return PTR_ERR(cred);
5547
5548 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5549 if (IS_ERR(olabel)) {
5550 status = -PTR_ERR(olabel);
5551 goto out;
5552 }
5553
5554 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5555 if (status == 0)
5556 nfs_setsecurity(inode, &fattr, olabel);
5557
5558 nfs4_label_free(olabel);
5559out:
5560 put_rpccred(cred);
5561 return status;
5562}
5563#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5564
5565
Chuck Leverf0920752012-05-21 22:45:41 -04005566static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5567 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005568{
5569 __be32 verf[2];
5570
Chuck Lever2c820d92012-05-21 22:45:33 -04005571 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5572 /* An impossible timestamp guarantees this value
5573 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005574 verf[0] = cpu_to_be32(U32_MAX);
5575 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005576 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005577 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005578 u64 ns = ktime_to_ns(nn->boot_time);
5579
5580 verf[0] = cpu_to_be32(ns >> 32);
5581 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005582 }
Chuck Levercd937102012-03-02 17:14:31 -05005583 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5584}
5585
Jeff Laytona3192682015-06-09 19:43:59 -04005586static int
5587nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005588{
Jeff Laytona3192682015-06-09 19:43:59 -04005589 size_t len;
5590 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005591
Trond Myklebustceb3a162015-01-03 15:16:04 -05005592 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005593 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005594
Jeff Laytona3192682015-06-09 19:43:59 -04005595 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005596 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005597 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5598 1 +
5599 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5600 1;
5601 rcu_read_unlock();
5602
5603 if (len > NFS4_OPAQUE_LIMIT + 1)
5604 return -EINVAL;
5605
5606 /*
5607 * Since this string is allocated at mount time, and held until the
5608 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5609 * about a memory-reclaim deadlock.
5610 */
5611 str = kmalloc(len, GFP_KERNEL);
5612 if (!str)
5613 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005614
Chuck Levere984a552012-09-14 17:24:21 -04005615 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005616 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005617 clp->cl_ipaddr,
5618 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5619 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005620 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005621
Jeff Laytona3192682015-06-09 19:43:59 -04005622 clp->cl_owner_id = str;
5623 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005624}
5625
Jeff Layton873e3852015-06-09 19:44:00 -04005626static int
5627nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005628{
Jeff Layton873e3852015-06-09 19:44:00 -04005629 size_t len;
5630 char *str;
5631
5632 len = 10 + 10 + 1 + 10 + 1 +
5633 strlen(nfs4_client_id_uniquifier) + 1 +
5634 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5635
5636 if (len > NFS4_OPAQUE_LIMIT + 1)
5637 return -EINVAL;
5638
5639 /*
5640 * Since this string is allocated at mount time, and held until the
5641 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5642 * about a memory-reclaim deadlock.
5643 */
5644 str = kmalloc(len, GFP_KERNEL);
5645 if (!str)
5646 return -ENOMEM;
5647
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005648 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005649 clp->rpc_ops->version, clp->cl_minorversion,
5650 nfs4_client_id_uniquifier,
5651 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005652 clp->cl_owner_id = str;
5653 return 0;
5654}
5655
5656static int
5657nfs4_init_uniform_client_string(struct nfs_client *clp)
5658{
Jeff Layton873e3852015-06-09 19:44:00 -04005659 size_t len;
5660 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005661
5662 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005663 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005664
5665 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005666 return nfs4_init_uniquifier_client_string(clp);
5667
5668 len = 10 + 10 + 1 + 10 + 1 +
5669 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5670
5671 if (len > NFS4_OPAQUE_LIMIT + 1)
5672 return -EINVAL;
5673
5674 /*
5675 * Since this string is allocated at mount time, and held until the
5676 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5677 * about a memory-reclaim deadlock.
5678 */
5679 str = kmalloc(len, GFP_KERNEL);
5680 if (!str)
5681 return -ENOMEM;
5682
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005683 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005684 clp->rpc_ops->version, clp->cl_minorversion,
5685 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005686 clp->cl_owner_id = str;
5687 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005688}
5689
Chuck Lever706cb8d2014-03-12 12:51:47 -04005690/*
5691 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5692 * services. Advertise one based on the address family of the
5693 * clientaddr.
5694 */
5695static unsigned int
5696nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5697{
5698 if (strchr(clp->cl_ipaddr, ':') != NULL)
5699 return scnprintf(buf, len, "tcp6");
5700 else
5701 return scnprintf(buf, len, "tcp");
5702}
5703
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005704static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5705{
5706 struct nfs4_setclientid *sc = calldata;
5707
5708 if (task->tk_status == 0)
5709 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5710}
5711
5712static const struct rpc_call_ops nfs4_setclientid_ops = {
5713 .rpc_call_done = nfs4_setclientid_done,
5714};
5715
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005716/**
5717 * nfs4_proc_setclientid - Negotiate client ID
5718 * @clp: state data structure
5719 * @program: RPC program for NFSv4 callback service
5720 * @port: IP port number for NFS4 callback service
5721 * @cred: RPC credential to use for this call
5722 * @res: where to place the result
5723 *
5724 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5725 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005726int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5727 unsigned short port, struct rpc_cred *cred,
5728 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005729{
5730 nfs4_verifier sc_verifier;
5731 struct nfs4_setclientid setclientid = {
5732 .sc_verifier = &sc_verifier,
5733 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005734 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005735 };
5736 struct rpc_message msg = {
5737 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5738 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005739 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005740 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005741 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005742 struct rpc_task *task;
5743 struct rpc_task_setup task_setup_data = {
5744 .rpc_client = clp->cl_rpcclient,
5745 .rpc_message = &msg,
5746 .callback_ops = &nfs4_setclientid_ops,
5747 .callback_data = &setclientid,
5748 .flags = RPC_TASK_TIMEOUT,
5749 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005750 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005751
Chuck Leverde734832012-07-11 16:30:50 -04005752 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005753 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005754
5755 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5756 status = nfs4_init_uniform_client_string(clp);
5757 else
Jeff Laytona3192682015-06-09 19:43:59 -04005758 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005759
5760 if (status)
5761 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005762
Chuck Leverde734832012-07-11 16:30:50 -04005763 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005764 setclientid.sc_netid_len =
5765 nfs4_init_callback_netid(clp,
5766 setclientid.sc_netid,
5767 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005768 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005769 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770 clp->cl_ipaddr, port >> 8, port & 255);
5771
Jeff Layton3a6bb732015-06-09 19:43:57 -04005772 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005773 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005774 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005775 task = rpc_run_task(&task_setup_data);
5776 if (IS_ERR(task)) {
5777 status = PTR_ERR(task);
5778 goto out;
5779 }
5780 status = task->tk_status;
5781 if (setclientid.sc_cred) {
5782 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5783 put_rpccred(setclientid.sc_cred);
5784 }
5785 rpc_put_task(task);
5786out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005787 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005788 dprintk("NFS reply setclientid: %d\n", status);
5789 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005790}
5791
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005792/**
5793 * nfs4_proc_setclientid_confirm - Confirm client ID
5794 * @clp: state data structure
5795 * @res: result of a previous SETCLIENTID
5796 * @cred: RPC credential to use for this call
5797 *
5798 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5799 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005800int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005801 struct nfs4_setclientid_res *arg,
5802 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005803{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005804 struct rpc_message msg = {
5805 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005806 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005807 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005808 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005809 int status;
5810
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005811 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5812 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5813 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005814 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005815 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005816 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005817 return status;
5818}
5819
Trond Myklebustfe650402006-01-03 09:55:18 +01005820struct nfs4_delegreturndata {
5821 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005822 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005823 struct nfs_fh fh;
5824 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005825 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005826 struct {
5827 struct nfs4_layoutreturn_args arg;
5828 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005829 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005830 u32 roc_barrier;
5831 bool roc;
5832 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005833 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005834 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005835 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005836};
5837
Trond Myklebustfe650402006-01-03 09:55:18 +01005838static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5839{
5840 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005841 struct nfs4_exception exception = {
5842 .inode = data->inode,
5843 .stateid = &data->stateid,
5844 };
Andy Adamson938e1012009-04-01 09:22:28 -04005845
Trond Myklebust14516c32010-07-31 14:29:06 -04005846 if (!nfs4_sequence_done(task, &data->res.seq_res))
5847 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005848
Trond Myklebustca8acf82013-08-13 10:36:56 -04005849 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005850
5851 /* Handle Layoutreturn errors */
5852 if (data->args.lr_args && task->tk_status != 0) {
5853 switch(data->res.lr_ret) {
5854 default:
5855 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5856 break;
5857 case 0:
5858 data->args.lr_args = NULL;
5859 data->res.lr_res = NULL;
5860 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005861 case -NFS4ERR_OLD_STATEID:
5862 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005863 data->inode))
5864 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005865 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005866 case -NFS4ERR_ADMIN_REVOKED:
5867 case -NFS4ERR_DELEG_REVOKED:
5868 case -NFS4ERR_EXPIRED:
5869 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005870 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5871 case -NFS4ERR_WRONG_CRED:
5872 data->args.lr_args = NULL;
5873 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005874 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005875 }
5876 }
5877
Ricardo Labiaga79708862009-12-07 09:23:21 -05005878 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005879 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005880 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005881 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005882 case -NFS4ERR_ADMIN_REVOKED:
5883 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005884 case -NFS4ERR_EXPIRED:
5885 nfs4_free_revoked_stateid(data->res.server,
5886 data->args.stateid,
5887 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005888 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005889 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005890 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005891 task->tk_status = 0;
5892 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005893 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005894 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5895 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005896 task->tk_status = 0;
5897 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005898 case -NFS4ERR_ACCESS:
5899 if (data->args.bitmask) {
5900 data->args.bitmask = NULL;
5901 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005902 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005903 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005904 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005905 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005906 task->tk_status = nfs4_async_handle_exception(task,
5907 data->res.server, task->tk_status,
5908 &exception);
5909 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005910 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005911 }
5912 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005913 return;
5914lr_restart:
5915 data->res.lr_ret = 0;
5916out_restart:
5917 task->tk_status = 0;
5918 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005919}
5920
5921static void nfs4_delegreturn_release(void *calldata)
5922{
Peng Tao039b7562014-07-03 13:05:02 +08005923 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005924 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005925
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005926 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005927 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005928 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5929 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005930 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005931 nfs_iput_and_deactive(inode);
5932 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005933 kfree(calldata);
5934}
5935
Andy Adamson938e1012009-04-01 09:22:28 -04005936static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5937{
5938 struct nfs4_delegreturndata *d_data;
5939
5940 d_data = (struct nfs4_delegreturndata *)data;
5941
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005942 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005943 return;
5944
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005945 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005946 &d_data->args.seq_args,
5947 &d_data->res.seq_res,
5948 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005949}
Andy Adamson938e1012009-04-01 09:22:28 -04005950
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005951static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005952 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005953 .rpc_call_done = nfs4_delegreturn_done,
5954 .rpc_release = nfs4_delegreturn_release,
5955};
5956
Trond Myklebuste6f81072008-01-24 18:14:34 -05005957static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005958{
5959 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005960 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005961 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005962 struct rpc_message msg = {
5963 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5964 .rpc_cred = cred,
5965 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005966 struct rpc_task_setup task_setup_data = {
5967 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005968 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005969 .callback_ops = &nfs4_delegreturn_ops,
5970 .flags = RPC_TASK_ASYNC,
5971 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005972 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005973
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005974 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005975 if (data == NULL)
5976 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005977 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005978
5979 nfs4_state_protect(server->nfs_client,
5980 NFS_SP4_MACH_CRED_CLEANUP,
5981 &task_setup_data.rpc_client, &msg);
5982
Trond Myklebustfe650402006-01-03 09:55:18 +01005983 data->args.fhandle = &data->fh;
5984 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005985 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005986 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005987 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005988 data->res.fattr = &data->fattr;
5989 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005990 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005991 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005992 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005993 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005994 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005995 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005996 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005997 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005998 if (data->lr.roc) {
5999 data->args.lr_args = &data->lr.arg;
6000 data->res.lr_res = &data->lr.res;
6001 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05006002 } else if (data->lr.roc) {
6003 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
6004 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05006005 }
Trond Myklebustfe650402006-01-03 09:55:18 +01006006
Trond Myklebustc970aa82007-07-14 15:39:59 -04006007 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05006008 msg.rpc_argp = &data->args;
6009 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006010 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006011 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01006012 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006013 if (!issync)
6014 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006015 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05006016 if (status != 0)
6017 goto out;
6018 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05006019out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006020 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01006021 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006022}
6023
Trond Myklebuste6f81072008-01-24 18:14:34 -05006024int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07006025{
6026 struct nfs_server *server = NFS_SERVER(inode);
6027 struct nfs4_exception exception = { };
6028 int err;
6029 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05006030 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006031 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006032 switch (err) {
6033 case -NFS4ERR_STALE_STATEID:
6034 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006035 case 0:
6036 return 0;
6037 }
6038 err = nfs4_handle_exception(server, err, &exception);
6039 } while (exception.retry);
6040 return err;
6041}
6042
Linus Torvalds1da177e2005-04-16 15:20:36 -07006043static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6044{
6045 struct inode *inode = state->inode;
6046 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006047 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006048 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006050 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006051 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006052 struct nfs_lockt_res res = {
6053 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006054 };
6055 struct rpc_message msg = {
6056 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006057 .rpc_argp = &arg,
6058 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006059 .rpc_cred = state->owner->so_cred,
6060 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 struct nfs4_lock_state *lsp;
6062 int status;
6063
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006064 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006065 status = nfs4_set_lock_state(state, request);
6066 if (status != 0)
6067 goto out;
6068 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006069 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006070 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006071 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006072 switch (status) {
6073 case 0:
6074 request->fl_type = F_UNLCK;
6075 break;
6076 case -NFS4ERR_DENIED:
6077 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006078 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006079 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006080 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006081out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006082 return status;
6083}
6084
6085static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6086{
6087 struct nfs4_exception exception = { };
6088 int err;
6089
6090 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006091 err = _nfs4_proc_getlk(state, cmd, request);
6092 trace_nfs4_get_lock(request, state, cmd, err);
6093 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006094 &exception);
6095 } while (exception.retry);
6096 return err;
6097}
6098
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006099struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006100 struct nfs_locku_args arg;
6101 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006102 struct nfs4_lock_state *lsp;
6103 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006104 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006105 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006106 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006107 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006108};
6109
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006110static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6111 struct nfs_open_context *ctx,
6112 struct nfs4_lock_state *lsp,
6113 struct nfs_seqid *seqid)
6114{
6115 struct nfs4_unlockdata *p;
6116 struct inode *inode = lsp->ls_state->inode;
6117
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006118 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006119 if (p == NULL)
6120 return NULL;
6121 p->arg.fh = NFS_FH(inode);
6122 p->arg.fl = &p->fl;
6123 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006124 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006125 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006126 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006127 /* Ensure we don't close file until we're done freeing locks! */
6128 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006129 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006130 memcpy(&p->fl, fl, sizeof(p->fl));
6131 p->server = NFS_SERVER(inode);
6132 return p;
6133}
6134
Trond Myklebust06f814a2006-01-03 09:55:07 +01006135static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006136{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006137 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006138 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006139 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006140 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006141 put_nfs_open_context(calldata->ctx);
6142 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006143}
6144
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006145static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006146{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006147 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006148 struct nfs4_exception exception = {
6149 .inode = calldata->lsp->ls_state->inode,
6150 .stateid = &calldata->arg.stateid,
6151 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006152
Trond Myklebust14516c32010-07-31 14:29:06 -04006153 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6154 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006155 switch (task->tk_status) {
6156 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006157 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006158 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006159 if (nfs4_update_lock_stateid(calldata->lsp,
6160 &calldata->res.stateid))
6161 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006162 case -NFS4ERR_ADMIN_REVOKED:
6163 case -NFS4ERR_EXPIRED:
6164 nfs4_free_revoked_stateid(calldata->server,
6165 &calldata->arg.stateid,
6166 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006167 case -NFS4ERR_BAD_STATEID:
6168 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006169 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006170 if (!nfs4_stateid_match(&calldata->arg.stateid,
6171 &calldata->lsp->ls_stateid))
6172 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006173 break;
6174 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006175 task->tk_status = nfs4_async_handle_exception(task,
6176 calldata->server, task->tk_status,
6177 &exception);
6178 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006179 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006180 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006181 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006182}
6183
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006184static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006185{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006186 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006187
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006188 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6189 nfs_async_iocounter_wait(task, calldata->l_ctx))
6190 return;
6191
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006192 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006193 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006194 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006195 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006196 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006197 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006198 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006199 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006200 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006201 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006202 &calldata->res.seq_res,
6203 task) != 0)
6204 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006205 return;
6206out_no_action:
6207 task->tk_action = NULL;
6208out_wait:
6209 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006210}
6211
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006212static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006213 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006214 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006215 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006216};
6217
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006218static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6219 struct nfs_open_context *ctx,
6220 struct nfs4_lock_state *lsp,
6221 struct nfs_seqid *seqid)
6222{
6223 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006224 struct rpc_message msg = {
6225 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6226 .rpc_cred = ctx->cred,
6227 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006228 struct rpc_task_setup task_setup_data = {
6229 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006230 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006231 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006232 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006233 .flags = RPC_TASK_ASYNC,
6234 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006235
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006236 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6237 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6238
Frank Filz137d6ac2007-07-09 15:32:29 -07006239 /* Ensure this is an unlock - when canceling a lock, the
6240 * canceled lock is passed in, and it won't be an unlock.
6241 */
6242 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006243 if (fl->fl_flags & FL_CLOSE)
6244 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006245
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006246 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6247 if (data == NULL) {
6248 nfs_free_seqid(seqid);
6249 return ERR_PTR(-ENOMEM);
6250 }
6251
Chuck Levera9c92d62013-08-09 12:48:18 -04006252 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006253 msg.rpc_argp = &data->arg;
6254 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006255 task_setup_data.callback_data = data;
6256 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006257}
6258
Linus Torvalds1da177e2005-04-16 15:20:36 -07006259static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6260{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006261 struct inode *inode = state->inode;
6262 struct nfs4_state_owner *sp = state->owner;
6263 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006264 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006265 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006266 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006267 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006268 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006269 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006270
Trond Myklebust9b073572006-06-29 16:38:34 -04006271 status = nfs4_set_lock_state(state, request);
6272 /* Unlock _before_ we do the RPC call */
6273 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006274 /* Exclude nfs_delegation_claim_locks() */
6275 mutex_lock(&sp->so_delegreturn_mutex);
6276 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006277 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006278 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006279 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006280 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006281 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006282 }
6283 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006284 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006285 if (status != 0)
6286 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006287 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006288 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006289 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6290 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006291 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6292 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006293 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006294 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006295 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006296 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006297 status = PTR_ERR(task);
6298 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006299 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006300 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006301 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006302out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006303 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006304 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006305 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006306}
6307
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006308struct nfs4_lockdata {
6309 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006310 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006311 struct nfs4_lock_state *lsp;
6312 struct nfs_open_context *ctx;
6313 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006314 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006315 int rpc_status;
6316 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006317 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006318};
6319
6320static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006321 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6322 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006323{
6324 struct nfs4_lockdata *p;
6325 struct inode *inode = lsp->ls_state->inode;
6326 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006327 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006328
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006329 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006330 if (p == NULL)
6331 return NULL;
6332
6333 p->arg.fh = NFS_FH(inode);
6334 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006335 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006336 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006337 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006338 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6339 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006340 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006341 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006342 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006343 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006344 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006345 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006346 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006347 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006348 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006349 p->ctx = get_nfs_open_context(ctx);
6350 memcpy(&p->fl, fl, sizeof(p->fl));
6351 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006352out_free_seqid:
6353 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006354out_free:
6355 kfree(p);
6356 return NULL;
6357}
6358
6359static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6360{
6361 struct nfs4_lockdata *data = calldata;
6362 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006363
Harvey Harrison3110ff82008-05-02 13:42:44 -07006364 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006365 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006366 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006367 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006368 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006369 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006370 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006371 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006372 nfs4_stateid_copy(&data->arg.open_stateid,
6373 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006374 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006375 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006376 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006377 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006378 nfs4_stateid_copy(&data->arg.lock_stateid,
6379 &data->lsp->ls_stateid);
6380 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006381 if (!nfs4_valid_open_stateid(state)) {
6382 data->rpc_status = -EBADF;
6383 task->tk_action = NULL;
6384 goto out_release_open_seqid;
6385 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006386 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006387 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006388 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006389 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006390 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006391 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006392out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006393 nfs_release_seqid(data->arg.open_seqid);
6394out_release_lock_seqid:
6395 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006396out_wait:
6397 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006398 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006399}
6400
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006401static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6402{
6403 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006404 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006405
Harvey Harrison3110ff82008-05-02 13:42:44 -07006406 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006407
Trond Myklebust14516c32010-07-31 14:29:06 -04006408 if (!nfs4_sequence_done(task, &data->res.seq_res))
6409 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006410
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006411 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006412 switch (task->tk_status) {
6413 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006414 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006415 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006416 if (data->arg.new_lock) {
6417 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006418 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006419 rpc_restart_call_prepare(task);
6420 break;
6421 }
6422 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006423 if (data->arg.new_lock_owner != 0) {
6424 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6425 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6426 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6427 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6428 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006429 break;
6430 case -NFS4ERR_BAD_STATEID:
6431 case -NFS4ERR_OLD_STATEID:
6432 case -NFS4ERR_STALE_STATEID:
6433 case -NFS4ERR_EXPIRED:
6434 if (data->arg.new_lock_owner != 0) {
6435 if (!nfs4_stateid_match(&data->arg.open_stateid,
6436 &lsp->ls_state->open_stateid))
6437 rpc_restart_call_prepare(task);
6438 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6439 &lsp->ls_stateid))
6440 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006441 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006442 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006443}
6444
6445static void nfs4_lock_release(void *calldata)
6446{
6447 struct nfs4_lockdata *data = calldata;
6448
Harvey Harrison3110ff82008-05-02 13:42:44 -07006449 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006450 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006451 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006452 struct rpc_task *task;
6453 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6454 data->arg.lock_seqid);
6455 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006456 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006457 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006458 } else
6459 nfs_free_seqid(data->arg.lock_seqid);
6460 nfs4_put_lock_state(data->lsp);
6461 put_nfs_open_context(data->ctx);
6462 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006463 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006464}
6465
6466static const struct rpc_call_ops nfs4_lock_ops = {
6467 .rpc_call_prepare = nfs4_lock_prepare,
6468 .rpc_call_done = nfs4_lock_done,
6469 .rpc_release = nfs4_lock_release,
6470};
6471
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006472static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6473{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006474 switch (error) {
6475 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006476 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006477 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006478 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006479 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006480 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006481 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006482 break;
6483 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006484 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006485 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006486 };
6487}
6488
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006489static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006490{
6491 struct nfs4_lockdata *data;
6492 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006493 struct rpc_message msg = {
6494 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6495 .rpc_cred = state->owner->so_cred,
6496 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006497 struct rpc_task_setup task_setup_data = {
6498 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006499 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006500 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006501 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006502 .flags = RPC_TASK_ASYNC,
6503 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006504 int ret;
6505
Harvey Harrison3110ff82008-05-02 13:42:44 -07006506 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006507 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006508 fl->fl_u.nfs4_fl.owner,
6509 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006510 if (data == NULL)
6511 return -ENOMEM;
6512 if (IS_SETLKW(cmd))
6513 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006514 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
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;
6521 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006522 } else
6523 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006524 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006525 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006526 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006527 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006528 if (ret == 0) {
6529 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006530 if (ret)
6531 nfs4_handle_setlk_error(data->server, data->lsp,
6532 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006533 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006534 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006535 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006536 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006537 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006538 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006539}
6540
6541static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6542{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006543 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006544 struct nfs4_exception exception = {
6545 .inode = state->inode,
6546 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006547 int err;
6548
6549 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006550 /* Cache the lock if possible... */
6551 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6552 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006553 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006554 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006555 break;
6556 nfs4_handle_exception(server, err, &exception);
6557 } while (exception.retry);
6558 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006559}
6560
6561static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6562{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006563 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006564 struct nfs4_exception exception = {
6565 .inode = state->inode,
6566 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006567 int err;
6568
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006569 err = nfs4_set_lock_state(state, request);
6570 if (err != 0)
6571 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006572 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006573 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6574 return 0;
6575 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006576 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006577 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6578 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006579 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006580 switch (err) {
6581 default:
6582 goto out;
6583 case -NFS4ERR_GRACE:
6584 case -NFS4ERR_DELAY:
6585 nfs4_handle_exception(server, err, &exception);
6586 err = 0;
6587 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006588 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006589out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006590 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006591}
6592
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006593#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006594static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6595{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006596 struct nfs4_lock_state *lsp;
6597 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006598
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006599 status = nfs4_set_lock_state(state, request);
6600 if (status != 0)
6601 return status;
6602 lsp = request->fl_u.nfs4_fl.owner;
6603 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6604 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6605 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006606 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006607}
6608#endif
6609
Linus Torvalds1da177e2005-04-16 15:20:36 -07006610static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6611{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006612 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006613 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006614 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006615 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006616
Trond Myklebust01c3b862006-06-29 16:38:39 -04006617 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006618 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006619 if (status < 0)
6620 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006621 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006622 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006623 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006624 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006625 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006626 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006627 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006628 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006629 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006630 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006631 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006632 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006633 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006634 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006635out:
6636 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006637 return status;
6638}
6639
6640static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6641{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006642 struct nfs4_exception exception = {
6643 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006644 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006645 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006646 int err;
6647
6648 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006649 err = _nfs4_proc_setlk(state, cmd, request);
6650 if (err == -NFS4ERR_DENIED)
6651 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006652 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006653 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006654 } while (exception.retry);
6655 return err;
6656}
6657
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006658#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6659#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6660
6661static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006662nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6663 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006664{
6665 int status = -ERESTARTSYS;
6666 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6667
6668 while(!signalled()) {
6669 status = nfs4_proc_setlk(state, cmd, request);
6670 if ((status != -EAGAIN) || IS_SETLK(cmd))
6671 break;
6672 freezable_schedule_timeout_interruptible(timeout);
6673 timeout *= 2;
6674 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6675 status = -ERESTARTSYS;
6676 }
6677 return status;
6678}
6679
Jeff Laytona1d617d82016-09-17 18:17:39 -04006680#ifdef CONFIG_NFS_V4_1
6681struct nfs4_lock_waiter {
6682 struct task_struct *task;
6683 struct inode *inode;
6684 struct nfs_lowner *owner;
6685 bool notified;
6686};
6687
6688static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006689nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006690{
6691 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006692 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006693
Jeff Layton57174592018-03-18 08:37:03 -04006694 /* NULL key means to wake up everyone */
6695 if (key) {
6696 struct cb_notify_lock_args *cbnl = key;
6697 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6698 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006699
Jeff Layton57174592018-03-18 08:37:03 -04006700 /* Only wake if the callback was for the same owner. */
6701 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6702 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006703
Jeff Layton57174592018-03-18 08:37:03 -04006704 /* Make sure it's for the right inode */
6705 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6706 return 0;
6707
6708 waiter->notified = true;
6709 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006710
6711 /* override "private" so we can use default_wake_function */
6712 wait->private = waiter->task;
6713 ret = autoremove_wake_function(wait, mode, flags, key);
6714 wait->private = waiter;
6715 return ret;
6716}
6717
6718static int
6719nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6720{
6721 int status = -ERESTARTSYS;
6722 unsigned long flags;
6723 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6724 struct nfs_server *server = NFS_SERVER(state->inode);
6725 struct nfs_client *clp = server->nfs_client;
6726 wait_queue_head_t *q = &clp->cl_lock_waitq;
6727 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6728 .id = lsp->ls_seqid.owner_id,
6729 .s_dev = server->s_dev };
6730 struct nfs4_lock_waiter waiter = { .task = current,
6731 .inode = state->inode,
6732 .owner = &owner,
6733 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006734 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006735
6736 /* Don't bother with waitqueue if we don't expect a callback */
6737 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6738 return nfs4_retry_setlk_simple(state, cmd, request);
6739
6740 init_wait(&wait);
6741 wait.private = &waiter;
6742 wait.func = nfs4_wake_lock_waiter;
6743 add_wait_queue(q, &wait);
6744
6745 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006746 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006747 status = nfs4_proc_setlk(state, cmd, request);
6748 if ((status != -EAGAIN) || IS_SETLK(cmd))
6749 break;
6750
6751 status = -ERESTARTSYS;
6752 spin_lock_irqsave(&q->lock, flags);
6753 if (waiter.notified) {
6754 spin_unlock_irqrestore(&q->lock, flags);
6755 continue;
6756 }
6757 set_current_state(TASK_INTERRUPTIBLE);
6758 spin_unlock_irqrestore(&q->lock, flags);
6759
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006760 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006761 }
6762
6763 finish_wait(q, &wait);
6764 return status;
6765}
6766#else /* !CONFIG_NFS_V4_1 */
6767static inline int
6768nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6769{
6770 return nfs4_retry_setlk_simple(state, cmd, request);
6771}
6772#endif
6773
Linus Torvalds1da177e2005-04-16 15:20:36 -07006774static int
6775nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6776{
6777 struct nfs_open_context *ctx;
6778 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006779 int status;
6780
6781 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006782 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006783 state = ctx->state;
6784
Trond Myklebustd9531262009-07-21 19:22:38 -04006785 if (IS_GETLK(cmd)) {
6786 if (state != NULL)
6787 return nfs4_proc_getlk(state, F_GETLK, request);
6788 return 0;
6789 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006790
6791 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6792 return -EINVAL;
6793
Trond Myklebustd9531262009-07-21 19:22:38 -04006794 if (request->fl_type == F_UNLCK) {
6795 if (state != NULL)
6796 return nfs4_proc_unlck(state, cmd, request);
6797 return 0;
6798 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006799
Trond Myklebustd9531262009-07-21 19:22:38 -04006800 if (state == NULL)
6801 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006802
6803 if ((request->fl_flags & FL_POSIX) &&
6804 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6805 return -ENOLCK;
6806
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006807 /*
6808 * Don't rely on the VFS having checked the file open mode,
6809 * since it won't do this for flock() locks.
6810 */
6811 switch (request->fl_type) {
6812 case F_RDLCK:
6813 if (!(filp->f_mode & FMODE_READ))
6814 return -EBADF;
6815 break;
6816 case F_WRLCK:
6817 if (!(filp->f_mode & FMODE_WRITE))
6818 return -EBADF;
6819 }
6820
Jeff Layton1ea67db2016-09-17 18:17:37 -04006821 status = nfs4_set_lock_state(state, request);
6822 if (status != 0)
6823 return status;
6824
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006825 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006826}
6827
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006828int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006829{
6830 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006831 int err;
6832
6833 err = nfs4_set_lock_state(state, fl);
6834 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006835 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006836 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006837 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006838}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006839
Trond Myklebustcf470c32012-03-07 13:49:12 -05006840struct nfs_release_lockowner_data {
6841 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006842 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006843 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006844 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006845 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006846};
6847
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006848static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6849{
6850 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006851 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006852 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6853 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006854 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006855 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006856}
6857
6858static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6859{
6860 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006861 struct nfs_server *server = data->server;
6862
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006863 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006864
6865 switch (task->tk_status) {
6866 case 0:
6867 renew_lease(server, data->timestamp);
6868 break;
6869 case -NFS4ERR_STALE_CLIENTID:
6870 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006871 nfs4_schedule_lease_recovery(server->nfs_client);
6872 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006873 case -NFS4ERR_LEASE_MOVED:
6874 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006875 if (nfs4_async_handle_error(task, server,
6876 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006877 rpc_restart_call_prepare(task);
6878 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006879}
6880
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006881static void nfs4_release_lockowner_release(void *calldata)
6882{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006883 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006884 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006885 kfree(calldata);
6886}
6887
Trond Myklebust17280172012-03-11 13:11:00 -04006888static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006889 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6890 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006891 .rpc_release = nfs4_release_lockowner_release,
6892};
6893
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006894static void
6895nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006896{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006897 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006898 struct rpc_message msg = {
6899 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6900 };
6901
6902 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006903 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006904
Trond Myklebustcf470c32012-03-07 13:49:12 -05006905 data = kmalloc(sizeof(*data), GFP_NOFS);
6906 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006907 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006908 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006909 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006910 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6911 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6912 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006913
Trond Myklebustcf470c32012-03-07 13:49:12 -05006914 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006915 msg.rpc_resp = &data->res;
6916 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006917 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006918}
6919
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006920#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6921
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006922static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006923 struct dentry *unused, struct inode *inode,
6924 const char *key, const void *buf,
6925 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006926{
Al Viro59301222016-05-27 10:19:30 -04006927 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006928}
6929
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006930static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006931 struct dentry *unused, struct inode *inode,
6932 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006933{
Al Virob2968212016-04-10 20:48:24 -04006934 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006935}
6936
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006937static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006938{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006939 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006940}
6941
David Quigleyc9bccef2013-05-22 12:50:45 -04006942#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006943
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006944static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006945 struct dentry *unused, struct inode *inode,
6946 const char *key, const void *buf,
6947 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006948{
6949 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006950 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006951
6952 return -EOPNOTSUPP;
6953}
6954
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006955static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006956 struct dentry *unused, struct inode *inode,
6957 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006958{
6959 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006960 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006961 return -EOPNOTSUPP;
6962}
6963
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006964static ssize_t
6965nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006966{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006967 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006968
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006969 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6970 len = security_inode_listsecurity(inode, list, list_len);
6971 if (list_len && len > list_len)
6972 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006973 }
6974 return len;
6975}
6976
6977static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6978 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006979 .get = nfs4_xattr_get_nfs4_label,
6980 .set = nfs4_xattr_set_nfs4_label,
6981};
David Quigleyc9bccef2013-05-22 12:50:45 -04006982
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006983#else
6984
6985static ssize_t
6986nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6987{
6988 return 0;
6989}
6990
6991#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006992
Andy Adamson533eb462011-06-13 18:25:56 -04006993/*
6994 * nfs_fhget will use either the mounted_on_fileid or the fileid
6995 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006996static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6997{
Andy Adamson533eb462011-06-13 18:25:56 -04006998 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6999 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
7000 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05007001 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007002 return;
7003
7004 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05007005 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04007006 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
7007 fattr->nlink = 2;
7008}
7009
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007010static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7011 const struct qstr *name,
7012 struct nfs4_fs_locations *fs_locations,
7013 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04007014{
7015 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05007016 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04007017 struct nfs4_fs_locations_arg args = {
7018 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05007019 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007020 .page = page,
7021 .bitmask = bitmask,
7022 };
Benny Halevy22958462009-04-01 09:22:02 -04007023 struct nfs4_fs_locations_res res = {
7024 .fs_locations = fs_locations,
7025 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04007026 struct rpc_message msg = {
7027 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7028 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04007029 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04007030 };
7031 int status;
7032
Harvey Harrison3110ff82008-05-02 13:42:44 -07007033 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007034
Chuck Leverc05cefc2017-11-05 15:45:22 -05007035 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7036 bitmask[1] = nfs4_fattr_bitmap[1];
7037
Andy Adamson533eb462011-06-13 18:25:56 -04007038 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7039 * is not supported */
7040 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007041 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007042 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007043 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007044
Trond Myklebustc228fd32007-01-13 02:28:11 -05007045 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007046 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007047 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007048 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007049 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007050 return status;
7051}
7052
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007053int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7054 const struct qstr *name,
7055 struct nfs4_fs_locations *fs_locations,
7056 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007057{
7058 struct nfs4_exception exception = { };
7059 int err;
7060 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007061 err = _nfs4_proc_fs_locations(client, dir, name,
7062 fs_locations, page);
7063 trace_nfs4_get_fs_locations(dir, name, err);
7064 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007065 &exception);
7066 } while (exception.retry);
7067 return err;
7068}
7069
Chuck Leverb03d7352013-10-17 14:12:50 -04007070/*
7071 * This operation also signals the server that this client is
7072 * performing migration recovery. The server can stop returning
7073 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7074 * appended to this compound to identify the client ID which is
7075 * performing recovery.
7076 */
7077static int _nfs40_proc_get_locations(struct inode *inode,
7078 struct nfs4_fs_locations *locations,
7079 struct page *page, struct rpc_cred *cred)
7080{
7081 struct nfs_server *server = NFS_SERVER(inode);
7082 struct rpc_clnt *clnt = server->client;
7083 u32 bitmask[2] = {
7084 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7085 };
7086 struct nfs4_fs_locations_arg args = {
7087 .clientid = server->nfs_client->cl_clientid,
7088 .fh = NFS_FH(inode),
7089 .page = page,
7090 .bitmask = bitmask,
7091 .migration = 1, /* skip LOOKUP */
7092 .renew = 1, /* append RENEW */
7093 };
7094 struct nfs4_fs_locations_res res = {
7095 .fs_locations = locations,
7096 .migration = 1,
7097 .renew = 1,
7098 };
7099 struct rpc_message msg = {
7100 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7101 .rpc_argp = &args,
7102 .rpc_resp = &res,
7103 .rpc_cred = cred,
7104 };
7105 unsigned long now = jiffies;
7106 int status;
7107
7108 nfs_fattr_init(&locations->fattr);
7109 locations->server = server;
7110 locations->nlocations = 0;
7111
7112 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7113 nfs4_set_sequence_privileged(&args.seq_args);
7114 status = nfs4_call_sync_sequence(clnt, server, &msg,
7115 &args.seq_args, &res.seq_res);
7116 if (status)
7117 return status;
7118
7119 renew_lease(server, now);
7120 return 0;
7121}
7122
7123#ifdef CONFIG_NFS_V4_1
7124
7125/*
7126 * This operation also signals the server that this client is
7127 * performing migration recovery. The server can stop asserting
7128 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7129 * performing this operation is identified in the SEQUENCE
7130 * operation in this compound.
7131 *
7132 * When the client supports GETATTR(fs_locations_info), it can
7133 * be plumbed in here.
7134 */
7135static int _nfs41_proc_get_locations(struct inode *inode,
7136 struct nfs4_fs_locations *locations,
7137 struct page *page, struct rpc_cred *cred)
7138{
7139 struct nfs_server *server = NFS_SERVER(inode);
7140 struct rpc_clnt *clnt = server->client;
7141 u32 bitmask[2] = {
7142 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7143 };
7144 struct nfs4_fs_locations_arg args = {
7145 .fh = NFS_FH(inode),
7146 .page = page,
7147 .bitmask = bitmask,
7148 .migration = 1, /* skip LOOKUP */
7149 };
7150 struct nfs4_fs_locations_res res = {
7151 .fs_locations = locations,
7152 .migration = 1,
7153 };
7154 struct rpc_message msg = {
7155 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7156 .rpc_argp = &args,
7157 .rpc_resp = &res,
7158 .rpc_cred = cred,
7159 };
7160 int status;
7161
7162 nfs_fattr_init(&locations->fattr);
7163 locations->server = server;
7164 locations->nlocations = 0;
7165
7166 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7167 nfs4_set_sequence_privileged(&args.seq_args);
7168 status = nfs4_call_sync_sequence(clnt, server, &msg,
7169 &args.seq_args, &res.seq_res);
7170 if (status == NFS4_OK &&
7171 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7172 status = -NFS4ERR_LEASE_MOVED;
7173 return status;
7174}
7175
7176#endif /* CONFIG_NFS_V4_1 */
7177
7178/**
7179 * nfs4_proc_get_locations - discover locations for a migrated FSID
7180 * @inode: inode on FSID that is migrating
7181 * @locations: result of query
7182 * @page: buffer
7183 * @cred: credential to use for this operation
7184 *
7185 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7186 * operation failed, or a negative errno if a local error occurred.
7187 *
7188 * On success, "locations" is filled in, but if the server has
7189 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7190 * asserted.
7191 *
7192 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7193 * from this client that require migration recovery.
7194 */
7195int nfs4_proc_get_locations(struct inode *inode,
7196 struct nfs4_fs_locations *locations,
7197 struct page *page, struct rpc_cred *cred)
7198{
7199 struct nfs_server *server = NFS_SERVER(inode);
7200 struct nfs_client *clp = server->nfs_client;
7201 const struct nfs4_mig_recovery_ops *ops =
7202 clp->cl_mvops->mig_recovery_ops;
7203 struct nfs4_exception exception = { };
7204 int status;
7205
7206 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7207 (unsigned long long)server->fsid.major,
7208 (unsigned long long)server->fsid.minor,
7209 clp->cl_hostname);
7210 nfs_display_fhandle(NFS_FH(inode), __func__);
7211
7212 do {
7213 status = ops->get_locations(inode, locations, page, cred);
7214 if (status != -NFS4ERR_DELAY)
7215 break;
7216 nfs4_handle_exception(server, status, &exception);
7217 } while (exception.retry);
7218 return status;
7219}
7220
Chuck Lever44c99932013-10-17 14:13:30 -04007221/*
7222 * This operation also signals the server that this client is
7223 * performing "lease moved" recovery. The server can stop
7224 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7225 * is appended to this compound to identify the client ID which is
7226 * performing recovery.
7227 */
7228static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7229{
7230 struct nfs_server *server = NFS_SERVER(inode);
7231 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7232 struct rpc_clnt *clnt = server->client;
7233 struct nfs4_fsid_present_arg args = {
7234 .fh = NFS_FH(inode),
7235 .clientid = clp->cl_clientid,
7236 .renew = 1, /* append RENEW */
7237 };
7238 struct nfs4_fsid_present_res res = {
7239 .renew = 1,
7240 };
7241 struct rpc_message msg = {
7242 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7243 .rpc_argp = &args,
7244 .rpc_resp = &res,
7245 .rpc_cred = cred,
7246 };
7247 unsigned long now = jiffies;
7248 int status;
7249
7250 res.fh = nfs_alloc_fhandle();
7251 if (res.fh == NULL)
7252 return -ENOMEM;
7253
7254 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7255 nfs4_set_sequence_privileged(&args.seq_args);
7256 status = nfs4_call_sync_sequence(clnt, server, &msg,
7257 &args.seq_args, &res.seq_res);
7258 nfs_free_fhandle(res.fh);
7259 if (status)
7260 return status;
7261
7262 do_renew_lease(clp, now);
7263 return 0;
7264}
7265
7266#ifdef CONFIG_NFS_V4_1
7267
7268/*
7269 * This operation also signals the server that this client is
7270 * performing "lease moved" recovery. The server can stop asserting
7271 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7272 * this operation is identified in the SEQUENCE operation in this
7273 * compound.
7274 */
7275static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7276{
7277 struct nfs_server *server = NFS_SERVER(inode);
7278 struct rpc_clnt *clnt = server->client;
7279 struct nfs4_fsid_present_arg args = {
7280 .fh = NFS_FH(inode),
7281 };
7282 struct nfs4_fsid_present_res res = {
7283 };
7284 struct rpc_message msg = {
7285 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7286 .rpc_argp = &args,
7287 .rpc_resp = &res,
7288 .rpc_cred = cred,
7289 };
7290 int status;
7291
7292 res.fh = nfs_alloc_fhandle();
7293 if (res.fh == NULL)
7294 return -ENOMEM;
7295
7296 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7297 nfs4_set_sequence_privileged(&args.seq_args);
7298 status = nfs4_call_sync_sequence(clnt, server, &msg,
7299 &args.seq_args, &res.seq_res);
7300 nfs_free_fhandle(res.fh);
7301 if (status == NFS4_OK &&
7302 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7303 status = -NFS4ERR_LEASE_MOVED;
7304 return status;
7305}
7306
7307#endif /* CONFIG_NFS_V4_1 */
7308
7309/**
7310 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7311 * @inode: inode on FSID to check
7312 * @cred: credential to use for this operation
7313 *
7314 * Server indicates whether the FSID is present, moved, or not
7315 * recognized. This operation is necessary to clear a LEASE_MOVED
7316 * condition for this client ID.
7317 *
7318 * Returns NFS4_OK if the FSID is present on this server,
7319 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7320 * NFS4ERR code if some error occurred on the server, or a
7321 * negative errno if a local failure occurred.
7322 */
7323int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7324{
7325 struct nfs_server *server = NFS_SERVER(inode);
7326 struct nfs_client *clp = server->nfs_client;
7327 const struct nfs4_mig_recovery_ops *ops =
7328 clp->cl_mvops->mig_recovery_ops;
7329 struct nfs4_exception exception = { };
7330 int status;
7331
7332 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7333 (unsigned long long)server->fsid.major,
7334 (unsigned long long)server->fsid.minor,
7335 clp->cl_hostname);
7336 nfs_display_fhandle(NFS_FH(inode), __func__);
7337
7338 do {
7339 status = ops->fsid_present(inode, cred);
7340 if (status != -NFS4ERR_DELAY)
7341 break;
7342 nfs4_handle_exception(server, status, &exception);
7343 } while (exception.retry);
7344 return status;
7345}
7346
Andy Adamson5ec16a82013-08-08 10:57:55 -04007347/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007348 * If 'use_integrity' is true and the state managment nfs_client
7349 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7350 * and the machine credential as per RFC3530bis and RFC5661 Security
7351 * Considerations sections. Otherwise, just use the user cred with the
7352 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007353 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007354static 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 +00007355{
7356 int status;
7357 struct nfs4_secinfo_arg args = {
7358 .dir_fh = NFS_FH(dir),
7359 .name = name,
7360 };
7361 struct nfs4_secinfo_res res = {
7362 .flavors = flavors,
7363 };
7364 struct rpc_message msg = {
7365 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7366 .rpc_argp = &args,
7367 .rpc_resp = &res,
7368 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007369 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007370 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007371
7372 if (use_integrity) {
7373 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007374 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7375 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007376 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007377
7378 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007379
7380 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7381 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7382
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007383 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7384 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007385 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007386
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007387 if (cred)
7388 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007389
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007390 return status;
7391}
7392
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007393int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7394 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007395{
7396 struct nfs4_exception exception = { };
7397 int err;
7398 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007399 err = -NFS4ERR_WRONGSEC;
7400
7401 /* try to use integrity protection with machine cred */
7402 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7403 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7404
7405 /*
7406 * if unable to use integrity protection, or SECINFO with
7407 * integrity protection returns NFS4ERR_WRONGSEC (which is
7408 * disallowed by spec, but exists in deployed servers) use
7409 * the current filesystem's rpc_client and the user cred.
7410 */
7411 if (err == -NFS4ERR_WRONGSEC)
7412 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7413
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007414 trace_nfs4_secinfo(dir, name, err);
7415 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007416 &exception);
7417 } while (exception.retry);
7418 return err;
7419}
7420
Andy Adamson557134a2009-04-01 09:21:53 -04007421#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007422/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007423 * Check the exchange flags returned by the server for invalid flags, having
7424 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7425 * DS flags set.
7426 */
7427static int nfs4_check_cl_exchange_flags(u32 flags)
7428{
7429 if (flags & ~EXCHGID4_FLAG_MASK_R)
7430 goto out_inval;
7431 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7432 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7433 goto out_inval;
7434 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7435 goto out_inval;
7436 return NFS_OK;
7437out_inval:
7438 return -NFS4ERR_INVAL;
7439}
7440
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007441static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007442nfs41_same_server_scope(struct nfs41_server_scope *a,
7443 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007444{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007445 if (a->server_scope_sz != b->server_scope_sz)
7446 return false;
7447 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007448}
7449
Andy Adamson02a95de2016-02-05 16:08:37 -05007450static void
7451nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7452{
7453}
7454
7455static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7456 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7457};
7458
Andy Adamson357f54d2010-12-14 10:11:57 -05007459/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007460 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007461 *
7462 * The 4.1 client currently uses the same TCP connection for the
7463 * fore and backchannel.
7464 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007465static
7466int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7467 struct rpc_xprt *xprt,
7468 struct nfs_client *clp,
7469 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007470{
7471 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007472 struct nfs41_bind_conn_to_session_args args = {
7473 .client = clp,
7474 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7475 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007476 struct nfs41_bind_conn_to_session_res res;
7477 struct rpc_message msg = {
7478 .rpc_proc =
7479 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007480 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007481 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007482 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007483 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007484 struct rpc_task_setup task_setup_data = {
7485 .rpc_client = clnt,
7486 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007487 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007488 .rpc_message = &msg,
7489 .flags = RPC_TASK_TIMEOUT,
7490 };
7491 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007492
Trond Myklebust71a097c2015-02-18 09:27:18 -08007493 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7494 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7495 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007496
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007497 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7498 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7499 args.dir = NFS4_CDFC4_FORE;
7500
7501 task = rpc_run_task(&task_setup_data);
7502 if (!IS_ERR(task)) {
7503 status = task->tk_status;
7504 rpc_put_task(task);
7505 } else
7506 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007507 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007508 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007509 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007510 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7511 dprintk("NFS: %s: Session ID mismatch\n", __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.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007515 dprintk("NFS: %s: Unexpected direction from server\n",
7516 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007517 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007518 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007519 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007520 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7521 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007522 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007523 }
7524 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007525
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007526 return status;
7527}
7528
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007529struct rpc_bind_conn_calldata {
7530 struct nfs_client *clp;
7531 struct rpc_cred *cred;
7532};
7533
7534static int
7535nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7536 struct rpc_xprt *xprt,
7537 void *calldata)
7538{
7539 struct rpc_bind_conn_calldata *p = calldata;
7540
7541 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7542}
7543
7544int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7545{
7546 struct rpc_bind_conn_calldata data = {
7547 .clp = clp,
7548 .cred = cred,
7549 };
7550 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7551 nfs4_proc_bind_conn_to_session_callback, &data);
7552}
7553
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007554/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007555 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7556 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007557 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007558static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7559 .how = SP4_MACH_CRED,
7560 .enforce.u.words = {
7561 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7562 1 << (OP_EXCHANGE_ID - 32) |
7563 1 << (OP_CREATE_SESSION - 32) |
7564 1 << (OP_DESTROY_SESSION - 32) |
7565 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007566 },
7567 .allow.u.words = {
7568 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007569 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007570 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007571 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007572 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007573 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007574 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007575 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007576 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007577 1 << (OP_FREE_STATEID - 32) |
7578 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007579 }
7580};
7581
7582/*
7583 * Select the state protection mode for client `clp' given the server results
7584 * from exchange_id in `sp'.
7585 *
7586 * Returns 0 on success, negative errno otherwise.
7587 */
7588static int nfs4_sp4_select_mode(struct nfs_client *clp,
7589 struct nfs41_state_protection *sp)
7590{
7591 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7592 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7593 1 << (OP_EXCHANGE_ID - 32) |
7594 1 << (OP_CREATE_SESSION - 32) |
7595 1 << (OP_DESTROY_SESSION - 32) |
7596 1 << (OP_DESTROY_CLIENTID - 32)
7597 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007598 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007599 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007600 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007601
7602 if (sp->how == SP4_MACH_CRED) {
7603 /* Print state protect result */
7604 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7605 for (i = 0; i <= LAST_NFS4_OP; i++) {
7606 if (test_bit(i, sp->enforce.u.longs))
7607 dfprintk(MOUNT, " enforce op %d\n", i);
7608 if (test_bit(i, sp->allow.u.longs))
7609 dfprintk(MOUNT, " allow op %d\n", i);
7610 }
7611
7612 /* make sure nothing is on enforce list that isn't supported */
7613 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7614 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7615 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007616 ret = -EINVAL;
7617 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007618 }
7619 }
7620
7621 /*
7622 * Minimal mode - state operations are allowed to use machine
7623 * credential. Note this already happens by default, so the
7624 * client doesn't have to do anything more than the negotiation.
7625 *
7626 * NOTE: we don't care if EXCHANGE_ID is in the list -
7627 * we're already using the machine cred for exchange_id
7628 * and will never use a different cred.
7629 */
7630 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7631 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7632 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7633 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7634 dfprintk(MOUNT, "sp4_mach_cred:\n");
7635 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007636 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007637 } else {
7638 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007639 ret = -EINVAL;
7640 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007641 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007642
7643 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007644 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7645 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007646 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7647 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007648 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007649 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007650
Andrew Elble99ade3c2015-12-02 09:39:51 -05007651 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7652 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007653 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007654 }
7655
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007656 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7657 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7658 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007659 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007660 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007661
7662 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7663 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7664 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007665 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007666 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007667
7668 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7669 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007670 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007671 }
7672
7673 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7674 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007675 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007676 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007677 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007678out:
7679 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007680 return 0;
7681}
7682
Andy Adamson8d89bd72016-09-09 09:22:18 -04007683struct nfs41_exchange_id_data {
7684 struct nfs41_exchange_id_res res;
7685 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007686};
7687
Andy Adamson8d89bd72016-09-09 09:22:18 -04007688static void nfs4_exchange_id_release(void *data)
7689{
7690 struct nfs41_exchange_id_data *cdata =
7691 (struct nfs41_exchange_id_data *)data;
7692
Olga Kornievskaia63513232017-03-13 10:36:19 -04007693 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007694 kfree(cdata->res.impl_id);
7695 kfree(cdata->res.server_scope);
7696 kfree(cdata->res.server_owner);
7697 kfree(cdata);
7698}
7699
7700static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007701 .rpc_release = nfs4_exchange_id_release,
7702};
7703
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007704/*
7705 * _nfs4_proc_exchange_id()
7706 *
7707 * Wrapper for EXCHANGE_ID operation.
7708 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007709static struct rpc_task *
7710nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007711 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007712{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007713 struct rpc_message msg = {
7714 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007715 .rpc_cred = cred,
7716 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007717 struct rpc_task_setup task_setup_data = {
7718 .rpc_client = clp->cl_rpcclient,
7719 .callback_ops = &nfs4_exchange_id_call_ops,
7720 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007721 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007722 };
7723 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007724 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007725
Elena Reshetova212bf412017-10-20 12:53:38 +03007726 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007727 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007728
Trond Myklebust9c760d12017-07-31 18:38:50 -04007729 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007730 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007731 if (!calldata)
7732 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007733
Trond Myklebustfd405592017-08-01 16:02:47 -04007734 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007735
7736 status = nfs4_init_uniform_client_string(clp);
7737 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007738 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007739
Andy Adamson8d89bd72016-09-09 09:22:18 -04007740 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7741 GFP_NOFS);
7742 status = -ENOMEM;
7743 if (unlikely(calldata->res.server_owner == NULL))
7744 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007745
Andy Adamson8d89bd72016-09-09 09:22:18 -04007746 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007747 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007748 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007749 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007750
Andy Adamson8d89bd72016-09-09 09:22:18 -04007751 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7752 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007753 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007754
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007755 switch (sp4_how) {
7756 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007757 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007758 break;
7759
7760 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007761 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007762 break;
7763
7764 default:
7765 /* unsupported! */
7766 WARN_ON_ONCE(1);
7767 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007768 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007769 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007770 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007771 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007772 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007773 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7774 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007775 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007776 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007777 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7778 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007779#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007780 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007781#endif
7782 msg.rpc_argp = &calldata->args;
7783 msg.rpc_resp = &calldata->res;
7784 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007785
Trond Myklebust9c760d12017-07-31 18:38:50 -04007786 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007787
7788out_impl_id:
7789 kfree(calldata->res.impl_id);
7790out_server_scope:
7791 kfree(calldata->res.server_scope);
7792out_server_owner:
7793 kfree(calldata->res.server_owner);
7794out_calldata:
7795 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007796out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007797 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007798 return ERR_PTR(status);
7799}
7800
7801/*
7802 * _nfs4_proc_exchange_id()
7803 *
7804 * Wrapper for EXCHANGE_ID operation.
7805 */
7806static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7807 u32 sp4_how)
7808{
7809 struct rpc_task *task;
7810 struct nfs41_exchange_id_args *argp;
7811 struct nfs41_exchange_id_res *resp;
7812 int status;
7813
7814 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7815 if (IS_ERR(task))
7816 return PTR_ERR(task);
7817
7818 argp = task->tk_msg.rpc_argp;
7819 resp = task->tk_msg.rpc_resp;
7820 status = task->tk_status;
7821 if (status != 0)
7822 goto out;
7823
7824 status = nfs4_check_cl_exchange_flags(resp->flags);
7825 if (status != 0)
7826 goto out;
7827
7828 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7829 if (status != 0)
7830 goto out;
7831
7832 clp->cl_clientid = resp->clientid;
7833 clp->cl_exchange_flags = resp->flags;
7834 clp->cl_seqid = resp->seqid;
7835 /* Client ID is not confirmed */
7836 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7837 clear_bit(NFS4_SESSION_ESTABLISHED,
7838 &clp->cl_session->session_state);
7839
7840 if (clp->cl_serverscope != NULL &&
7841 !nfs41_same_server_scope(clp->cl_serverscope,
7842 resp->server_scope)) {
7843 dprintk("%s: server_scope mismatch detected\n",
7844 __func__);
7845 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7846 }
7847
7848 swap(clp->cl_serverowner, resp->server_owner);
7849 swap(clp->cl_serverscope, resp->server_scope);
7850 swap(clp->cl_implid, resp->impl_id);
7851
7852 /* Save the EXCHANGE_ID verifier session trunk tests */
7853 memcpy(clp->cl_confirm.data, argp->verifier.data,
7854 sizeof(clp->cl_confirm.data));
7855out:
7856 trace_nfs4_exchange_id(clp, status);
7857 rpc_put_task(task);
7858 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007859}
7860
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007861/*
7862 * nfs4_proc_exchange_id()
7863 *
7864 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7865 *
7866 * Since the clientid has expired, all compounds using sessions
7867 * associated with the stale clientid will be returning
7868 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7869 * be in some phase of session reset.
7870 *
7871 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7872 */
7873int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7874{
7875 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7876 int status;
7877
7878 /* try SP4_MACH_CRED if krb5i/p */
7879 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7880 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007881 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007882 if (!status)
7883 return 0;
7884 }
7885
7886 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007887 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007888}
7889
Andy Adamson04fa2c62016-09-09 09:22:29 -04007890/**
7891 * nfs4_test_session_trunk
7892 *
7893 * This is an add_xprt_test() test function called from
7894 * rpc_clnt_setup_test_and_add_xprt.
7895 *
7896 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7897 * and is dereferrenced in nfs4_exchange_id_release
7898 *
7899 * Upon success, add the new transport to the rpc_clnt
7900 *
7901 * @clnt: struct rpc_clnt to get new transport
7902 * @xprt: the rpc_xprt to test
7903 * @data: call data for _nfs4_proc_exchange_id.
7904 */
7905int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7906 void *data)
7907{
7908 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007909 struct rpc_task *task;
7910 int status;
7911
Andy Adamson04fa2c62016-09-09 09:22:29 -04007912 u32 sp4_how;
7913
7914 dprintk("--> %s try %s\n", __func__,
7915 xprt->address_strings[RPC_DISPLAY_ADDR]);
7916
7917 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7918
7919 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007920 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7921 if (IS_ERR(task))
7922 return PTR_ERR(task);
7923
7924 status = task->tk_status;
7925 if (status == 0)
7926 status = nfs4_detect_session_trunking(adata->clp,
7927 task->tk_msg.rpc_resp, xprt);
7928
7929 rpc_put_task(task);
7930 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007931}
7932EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7933
Trond Myklebust66245532012-05-25 17:18:09 -04007934static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7935 struct rpc_cred *cred)
7936{
7937 struct rpc_message msg = {
7938 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7939 .rpc_argp = clp,
7940 .rpc_cred = cred,
7941 };
7942 int status;
7943
7944 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007945 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007946 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007947 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007948 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7949 return status;
7950}
7951
7952static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7953 struct rpc_cred *cred)
7954{
7955 unsigned int loop;
7956 int ret;
7957
7958 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7959 ret = _nfs4_proc_destroy_clientid(clp, cred);
7960 switch (ret) {
7961 case -NFS4ERR_DELAY:
7962 case -NFS4ERR_CLIENTID_BUSY:
7963 ssleep(1);
7964 break;
7965 default:
7966 return ret;
7967 }
7968 }
7969 return 0;
7970}
7971
7972int nfs4_destroy_clientid(struct nfs_client *clp)
7973{
7974 struct rpc_cred *cred;
7975 int ret = 0;
7976
7977 if (clp->cl_mvops->minor_version < 1)
7978 goto out;
7979 if (clp->cl_exchange_flags == 0)
7980 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007981 if (clp->cl_preserve_clid)
7982 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007983 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007984 ret = nfs4_proc_destroy_clientid(clp, cred);
7985 if (cred)
7986 put_rpccred(cred);
7987 switch (ret) {
7988 case 0:
7989 case -NFS4ERR_STALE_CLIENTID:
7990 clp->cl_exchange_flags = 0;
7991 }
7992out:
7993 return ret;
7994}
7995
Andy Adamson2050f0c2009-04-01 09:22:30 -04007996struct nfs4_get_lease_time_data {
7997 struct nfs4_get_lease_time_args *args;
7998 struct nfs4_get_lease_time_res *res;
7999 struct nfs_client *clp;
8000};
8001
8002static void nfs4_get_lease_time_prepare(struct rpc_task *task,
8003 void *calldata)
8004{
Andy Adamson2050f0c2009-04-01 09:22:30 -04008005 struct nfs4_get_lease_time_data *data =
8006 (struct nfs4_get_lease_time_data *)calldata;
8007
8008 dprintk("--> %s\n", __func__);
8009 /* just setup sequence, do not trigger session recovery
8010 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008011 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008012 &data->args->la_seq_args,
8013 &data->res->lr_seq_res,
8014 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008015 dprintk("<-- %s\n", __func__);
8016}
8017
8018/*
8019 * Called from nfs4_state_manager thread for session setup, so don't recover
8020 * from sequence operation or clientid errors.
8021 */
8022static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
8023{
8024 struct nfs4_get_lease_time_data *data =
8025 (struct nfs4_get_lease_time_data *)calldata;
8026
8027 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008028 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
8029 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04008030 switch (task->tk_status) {
8031 case -NFS4ERR_DELAY:
8032 case -NFS4ERR_GRACE:
8033 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8034 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8035 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008036 /* fall through */
8037 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008038 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008039 return;
8040 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008041 dprintk("<-- %s\n", __func__);
8042}
8043
Trond Myklebust17280172012-03-11 13:11:00 -04008044static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008045 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8046 .rpc_call_done = nfs4_get_lease_time_done,
8047};
8048
8049int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8050{
8051 struct rpc_task *task;
8052 struct nfs4_get_lease_time_args args;
8053 struct nfs4_get_lease_time_res res = {
8054 .lr_fsinfo = fsinfo,
8055 };
8056 struct nfs4_get_lease_time_data data = {
8057 .args = &args,
8058 .res = &res,
8059 .clp = clp,
8060 };
8061 struct rpc_message msg = {
8062 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8063 .rpc_argp = &args,
8064 .rpc_resp = &res,
8065 };
8066 struct rpc_task_setup task_setup = {
8067 .rpc_client = clp->cl_rpcclient,
8068 .rpc_message = &msg,
8069 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008070 .callback_data = &data,
8071 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008072 };
8073 int status;
8074
Chuck Levera9c92d62013-08-09 12:48:18 -04008075 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008076 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008077 task = rpc_run_task(&task_setup);
8078
8079 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008080 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008081
Anna Schumakerf6148712017-04-07 14:15:23 -04008082 status = task->tk_status;
8083 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008084 return status;
8085}
8086
Andy Adamsonfc931582009-04-01 09:22:31 -04008087/*
8088 * Initialize the values to be used by the client in CREATE_SESSION
8089 * If nfs4_init_session set the fore channel request and response sizes,
8090 * use them.
8091 *
8092 * Set the back channel max_resp_sz_cached to zero to force the client to
8093 * always set csa_cachethis to FALSE because the current implementation
8094 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8095 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008096static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8097 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008098{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008099 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008100 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008101
Andy Adamson18aad3d2013-06-26 12:21:49 -04008102 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8103 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8104
Andy Adamsonfc931582009-04-01 09:22:31 -04008105 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008106 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8107 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008108 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008109 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008110
8111 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008112 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008113 __func__,
8114 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008115 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008116
8117 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008118 args->bc_attrs.max_rqst_sz = max_bc_payload;
8119 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008120 args->bc_attrs.max_resp_sz_cached = 0;
8121 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008122 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008123
8124 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8125 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8126 __func__,
8127 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8128 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8129 args->bc_attrs.max_reqs);
8130}
8131
Trond Myklebust79969dd2015-02-18 11:30:18 -08008132static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8133 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008134{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008135 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008136 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008137
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008138 if (rcvd->max_resp_sz > sent->max_resp_sz)
8139 return -EINVAL;
8140 /*
8141 * Our requested max_ops is the minimum we need; we're not
8142 * prepared to break up compounds into smaller pieces than that.
8143 * So, no point even trying to continue if the server won't
8144 * cooperate:
8145 */
8146 if (rcvd->max_ops < sent->max_ops)
8147 return -EINVAL;
8148 if (rcvd->max_reqs == 0)
8149 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008150 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8151 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008152 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008153}
8154
Trond Myklebust79969dd2015-02-18 11:30:18 -08008155static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8156 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008157{
8158 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008159 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008160
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008161 if (!(res->flags & SESSION4_BACK_CHAN))
8162 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008163 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8164 return -EINVAL;
8165 if (rcvd->max_resp_sz < sent->max_resp_sz)
8166 return -EINVAL;
8167 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8168 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008169 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008170 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008171 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008172 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008173out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008174 return 0;
8175}
Andy Adamson8d353012009-04-01 09:22:32 -04008176
Andy Adamson8d353012009-04-01 09:22:32 -04008177static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008178 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008179{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008180 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008181
Trond Myklebust79969dd2015-02-18 11:30:18 -08008182 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008183 if (ret)
8184 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008185 return nfs4_verify_back_channel_attrs(args, res);
8186}
8187
8188static void nfs4_update_session(struct nfs4_session *session,
8189 struct nfs41_create_session_res *res)
8190{
8191 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008192 /* Mark client id and session as being confirmed */
8193 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8194 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008195 session->flags = res->flags;
8196 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008197 if (res->flags & SESSION4_BACK_CHAN)
8198 memcpy(&session->bc_attrs, &res->bc_attrs,
8199 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008200}
8201
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008202static int _nfs4_proc_create_session(struct nfs_client *clp,
8203 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008204{
8205 struct nfs4_session *session = clp->cl_session;
8206 struct nfs41_create_session_args args = {
8207 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008208 .clientid = clp->cl_clientid,
8209 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008210 .cb_program = NFS4_CALLBACK,
8211 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008212 struct nfs41_create_session_res res;
8213
Andy Adamsonfc931582009-04-01 09:22:31 -04008214 struct rpc_message msg = {
8215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8216 .rpc_argp = &args,
8217 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008218 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008219 };
8220 int status;
8221
Chuck Lever6b26cc82016-05-02 14:40:40 -04008222 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008223 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008224
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008225 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008226 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008227
Trond Myklebustb519d402016-09-11 14:50:01 -04008228 switch (status) {
8229 case -NFS4ERR_STALE_CLIENTID:
8230 case -NFS4ERR_DELAY:
8231 case -ETIMEDOUT:
8232 case -EACCES:
8233 case -EAGAIN:
8234 goto out;
8235 };
8236
8237 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008238 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008239 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008240 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008241 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008242 if (status)
8243 goto out;
8244 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008245 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008246out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008247 return status;
8248}
8249
8250/*
8251 * Issues a CREATE_SESSION operation to the server.
8252 * It is the responsibility of the caller to verify the session is
8253 * expired before calling this routine.
8254 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008255int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008256{
8257 int status;
8258 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008259 struct nfs4_session *session = clp->cl_session;
8260
8261 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8262
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008263 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008264 if (status)
8265 goto out;
8266
Andy Adamsonaacd5532011-11-09 13:58:21 -05008267 /* Init or reset the session slot tables */
8268 status = nfs4_setup_session_slot_tables(session);
8269 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008270 if (status)
8271 goto out;
8272
8273 ptr = (unsigned *)&session->sess_id.data[0];
8274 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8275 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008276out:
8277 dprintk("<-- %s\n", __func__);
8278 return status;
8279}
8280
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008281/*
8282 * Issue the over-the-wire RPC DESTROY_SESSION.
8283 * The caller must serialize access to this routine.
8284 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008285int nfs4_proc_destroy_session(struct nfs4_session *session,
8286 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008287{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008288 struct rpc_message msg = {
8289 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8290 .rpc_argp = session,
8291 .rpc_cred = cred,
8292 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008293 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008294
8295 dprintk("--> nfs4_proc_destroy_session\n");
8296
8297 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008298 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8299 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008300
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008301 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008302 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008303
8304 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008305 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008306 "Session has been destroyed regardless...\n", status);
8307
8308 dprintk("<-- nfs4_proc_destroy_session\n");
8309 return status;
8310}
8311
Trond Myklebust7b38c362012-05-23 13:23:31 -04008312/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008313 * Renew the cl_session lease.
8314 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008315struct nfs4_sequence_data {
8316 struct nfs_client *clp;
8317 struct nfs4_sequence_args args;
8318 struct nfs4_sequence_res res;
8319};
8320
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008321static void nfs41_sequence_release(void *data)
8322{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008323 struct nfs4_sequence_data *calldata = data;
8324 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008325
Elena Reshetova212bf412017-10-20 12:53:38 +03008326 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008327 nfs4_schedule_state_renewal(clp);
8328 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008329 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008330}
8331
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008332static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8333{
8334 switch(task->tk_status) {
8335 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008336 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8337 return -EAGAIN;
8338 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008339 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008340 }
8341 return 0;
8342}
8343
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008344static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008345{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008346 struct nfs4_sequence_data *calldata = data;
8347 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008348
Trond Myklebust14516c32010-07-31 14:29:06 -04008349 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8350 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008351
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008352 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008353 if (task->tk_status < 0) {
8354 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008355 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008356 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008357
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008358 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8359 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008360 return;
8361 }
8362 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008363 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008364out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008365 dprintk("<-- %s\n", __func__);
8366}
8367
8368static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8369{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008370 struct nfs4_sequence_data *calldata = data;
8371 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008372 struct nfs4_sequence_args *args;
8373 struct nfs4_sequence_res *res;
8374
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008375 args = task->tk_msg.rpc_argp;
8376 res = task->tk_msg.rpc_resp;
8377
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008378 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008379}
8380
8381static const struct rpc_call_ops nfs41_sequence_ops = {
8382 .rpc_call_done = nfs41_sequence_call_done,
8383 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008384 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008385};
8386
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008387static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8388 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008389 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008390 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008391{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008392 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008393 struct rpc_message msg = {
8394 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8395 .rpc_cred = cred,
8396 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008397 struct rpc_task_setup task_setup_data = {
8398 .rpc_client = clp->cl_rpcclient,
8399 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008400 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008401 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008402 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008403 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008404
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008405 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008406 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008407 goto out_err;
8408
8409 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008410 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008411 if (calldata == NULL)
8412 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008413 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008414 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008415 if (is_privileged)
8416 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008417 msg.rpc_argp = &calldata->args;
8418 msg.rpc_resp = &calldata->res;
8419 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008420 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008421
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008422 ret = rpc_run_task(&task_setup_data);
8423 if (IS_ERR(ret))
8424 goto out_err;
8425 return ret;
8426out_put_clp:
8427 nfs_put_client(clp);
8428out_err:
8429 nfs41_release_slot(slot);
8430 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008431}
8432
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008433static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008434{
8435 struct rpc_task *task;
8436 int ret = 0;
8437
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008438 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008439 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008440 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008441 if (IS_ERR(task))
8442 ret = PTR_ERR(task);
8443 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008444 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008445 dprintk("<-- %s status=%d\n", __func__, ret);
8446 return ret;
8447}
8448
8449static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8450{
8451 struct rpc_task *task;
8452 int ret;
8453
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008454 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008455 if (IS_ERR(task)) {
8456 ret = PTR_ERR(task);
8457 goto out;
8458 }
8459 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008460 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008461 ret = task->tk_status;
8462 rpc_put_task(task);
8463out:
8464 dprintk("<-- %s status=%d\n", __func__, ret);
8465 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008466}
8467
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008468struct nfs4_reclaim_complete_data {
8469 struct nfs_client *clp;
8470 struct nfs41_reclaim_complete_args arg;
8471 struct nfs41_reclaim_complete_res res;
8472};
8473
8474static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8475{
8476 struct nfs4_reclaim_complete_data *calldata = data;
8477
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008478 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008479 &calldata->arg.seq_args,
8480 &calldata->res.seq_res,
8481 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008482}
8483
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008484static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8485{
8486 switch(task->tk_status) {
8487 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008488 wake_up_all(&clp->cl_lock_waitq);
8489 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008490 case -NFS4ERR_COMPLETE_ALREADY:
8491 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8492 break;
8493 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008494 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008495 /* fall through */
8496 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008497 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008498 case -NFS4ERR_BADSESSION:
8499 case -NFS4ERR_DEADSESSION:
8500 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8501 nfs4_schedule_session_recovery(clp->cl_session,
8502 task->tk_status);
8503 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008504 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008505 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008506 }
8507 return 0;
8508}
8509
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008510static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8511{
8512 struct nfs4_reclaim_complete_data *calldata = data;
8513 struct nfs_client *clp = calldata->clp;
8514 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8515
8516 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008517 if (!nfs41_sequence_done(task, res))
8518 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008519
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008520 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008521 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8522 rpc_restart_call_prepare(task);
8523 return;
8524 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008525 dprintk("<-- %s\n", __func__);
8526}
8527
8528static void nfs4_free_reclaim_complete_data(void *data)
8529{
8530 struct nfs4_reclaim_complete_data *calldata = data;
8531
8532 kfree(calldata);
8533}
8534
8535static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8536 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8537 .rpc_call_done = nfs4_reclaim_complete_done,
8538 .rpc_release = nfs4_free_reclaim_complete_data,
8539};
8540
8541/*
8542 * Issue a global reclaim complete.
8543 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008544static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8545 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008546{
8547 struct nfs4_reclaim_complete_data *calldata;
8548 struct rpc_task *task;
8549 struct rpc_message msg = {
8550 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008551 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008552 };
8553 struct rpc_task_setup task_setup_data = {
8554 .rpc_client = clp->cl_rpcclient,
8555 .rpc_message = &msg,
8556 .callback_ops = &nfs4_reclaim_complete_call_ops,
8557 .flags = RPC_TASK_ASYNC,
8558 };
8559 int status = -ENOMEM;
8560
8561 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008562 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008563 if (calldata == NULL)
8564 goto out;
8565 calldata->clp = clp;
8566 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008567
Chuck Levera9c92d62013-08-09 12:48:18 -04008568 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008569 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008570 msg.rpc_argp = &calldata->arg;
8571 msg.rpc_resp = &calldata->res;
8572 task_setup_data.callback_data = calldata;
8573 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008574 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008575 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008576 goto out;
8577 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008578 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008579 if (status == 0)
8580 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008581 rpc_put_task(task);
8582out:
8583 dprintk("<-- %s status=%d\n", __func__, status);
8584 return status;
8585}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008586
8587static void
8588nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8589{
8590 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008591 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008592
8593 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008594 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008595 &lgp->res.seq_res, task);
8596 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008597}
8598
8599static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8600{
8601 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008602
8603 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008604 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008605 dprintk("<-- %s\n", __func__);
8606}
8607
8608static int
8609nfs4_layoutget_handle_exception(struct rpc_task *task,
8610 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8611{
Trond Myklebustee314c22012-10-01 17:25:48 -07008612 struct inode *inode = lgp->args.inode;
8613 struct nfs_server *server = NFS_SERVER(inode);
8614 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008615 int nfs4err = task->tk_status;
8616 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008617 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008618
Boaz Harroshed7e5422014-01-22 20:34:54 +02008619 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008620
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008621 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008622 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008623 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008624
8625 /*
8626 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8627 * on the file. set tk_status to -ENODATA to tell upper layer to
8628 * retry go inband.
8629 */
8630 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008631 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008632 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008633 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008634 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8635 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8636 */
8637 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008638 status = -EOVERFLOW;
8639 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008640 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008641 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008642 * (or clients) writing to the same RAID stripe except when
8643 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008644 *
8645 * Treat it like we would RECALLCONFLICT -- we retry for a little
8646 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008647 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008648 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008649 if (lgp->args.minlength == 0) {
8650 status = -EOVERFLOW;
8651 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008652 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008653 status = -EBUSY;
8654 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008655 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008656 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008657 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008658 case -NFS4ERR_DELEG_REVOKED:
8659 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008660 case -NFS4ERR_EXPIRED:
8661 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008662 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008663 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008664 lo = NFS_I(inode)->layout;
8665 /* If the open stateid was bad, then recover it. */
8666 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008667 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008668 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008669 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008670 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008671 break;
8672 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008673
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008674 /*
8675 * Mark the bad layout state as invalid, then retry
8676 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008677 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008678 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008679 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008680 pnfs_free_lseg_list(&head);
8681 status = -EAGAIN;
8682 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008683 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008684
Trond Myklebust8ac09252017-01-23 22:44:12 -05008685 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008686 err = nfs4_handle_exception(server, nfs4err, exception);
8687 if (!status) {
8688 if (exception->retry)
8689 status = -EAGAIN;
8690 else
8691 status = err;
8692 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008693out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008694 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008695 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008696}
8697
Idan Kedar85541162012-08-02 11:47:10 +03008698static size_t max_response_pages(struct nfs_server *server)
8699{
8700 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8701 return nfs_page_array_len(0, max_resp_sz);
8702}
8703
8704static void nfs4_free_pages(struct page **pages, size_t size)
8705{
8706 int i;
8707
8708 if (!pages)
8709 return;
8710
8711 for (i = 0; i < size; i++) {
8712 if (!pages[i])
8713 break;
8714 __free_page(pages[i]);
8715 }
8716 kfree(pages);
8717}
8718
8719static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8720{
8721 struct page **pages;
8722 int i;
8723
8724 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8725 if (!pages) {
8726 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8727 return NULL;
8728 }
8729
8730 for (i = 0; i < size; i++) {
8731 pages[i] = alloc_page(gfp_flags);
8732 if (!pages[i]) {
8733 dprintk("%s: failed to allocate page\n", __func__);
8734 nfs4_free_pages(pages, size);
8735 return NULL;
8736 }
8737 }
8738
8739 return pages;
8740}
8741
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008742static void nfs4_layoutget_release(void *calldata)
8743{
8744 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008745 struct inode *inode = lgp->args.inode;
8746 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008747 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008748
8749 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008750 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008751 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008752 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008753 put_nfs_open_context(lgp->args.ctx);
8754 kfree(calldata);
8755 dprintk("<-- %s\n", __func__);
8756}
8757
8758static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8759 .rpc_call_prepare = nfs4_layoutget_prepare,
8760 .rpc_call_done = nfs4_layoutget_done,
8761 .rpc_release = nfs4_layoutget_release,
8762};
8763
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008764struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008765nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008766{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008767 struct inode *inode = lgp->args.inode;
8768 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008769 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008770 struct rpc_task *task;
8771 struct rpc_message msg = {
8772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8773 .rpc_argp = &lgp->args,
8774 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008775 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008776 };
8777 struct rpc_task_setup task_setup_data = {
8778 .rpc_client = server->client,
8779 .rpc_message = &msg,
8780 .callback_ops = &nfs4_layoutget_call_ops,
8781 .callback_data = lgp,
8782 .flags = RPC_TASK_ASYNC,
8783 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008784 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008785 struct nfs4_exception exception = {
8786 .inode = inode,
8787 .timeout = *timeout,
8788 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008789 int status = 0;
8790
8791 dprintk("--> %s\n", __func__);
8792
Peng Tao4bd5a982014-11-17 11:05:17 +08008793 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8794 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8795
Idan Kedar85541162012-08-02 11:47:10 +03008796 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8797 if (!lgp->args.layout.pages) {
8798 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008799 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008800 }
8801 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8802
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008803 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008804 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008805 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008806
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008807 task = rpc_run_task(&task_setup_data);
8808 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008809 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008810 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008811 if (status == 0) {
8812 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8813 *timeout = exception.timeout;
8814 }
8815
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008816 trace_nfs4_layoutget(lgp->args.ctx,
8817 &lgp->args.range,
8818 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008819 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008820 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008821
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008822 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8823 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008824 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008825 rpc_put_task(task);
8826 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008827 if (status)
8828 return ERR_PTR(status);
8829 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008830}
8831
Benny Halevycbe82602011-05-22 19:52:37 +03008832static void
8833nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8834{
8835 struct nfs4_layoutreturn *lrp = calldata;
8836
8837 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008838 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008839 &lrp->args.seq_args,
8840 &lrp->res.seq_res,
8841 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008842}
8843
8844static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8845{
8846 struct nfs4_layoutreturn *lrp = calldata;
8847 struct nfs_server *server;
8848
8849 dprintk("--> %s\n", __func__);
8850
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008851 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008852 return;
8853
8854 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008855 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008856 case -NFS4ERR_OLD_STATEID:
8857 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8858 lrp->args.inode))
8859 goto out_restart;
8860 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008861 default:
8862 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008863 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008864 case 0:
8865 break;
8866 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008867 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008868 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008869 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008870 }
Benny Halevycbe82602011-05-22 19:52:37 +03008871 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008872 return;
8873out_restart:
8874 task->tk_status = 0;
8875 nfs4_sequence_free_slot(&lrp->res.seq_res);
8876 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008877}
8878
8879static void nfs4_layoutreturn_release(void *calldata)
8880{
8881 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008882 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008883
8884 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008885 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008886 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008887 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008888 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8889 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008890 pnfs_put_layout_hdr(lrp->args.layout);
8891 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008892 kfree(calldata);
8893 dprintk("<-- %s\n", __func__);
8894}
8895
8896static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8897 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8898 .rpc_call_done = nfs4_layoutreturn_done,
8899 .rpc_release = nfs4_layoutreturn_release,
8900};
8901
Peng Tao6c166052014-11-17 09:30:40 +08008902int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008903{
8904 struct rpc_task *task;
8905 struct rpc_message msg = {
8906 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8907 .rpc_argp = &lrp->args,
8908 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008909 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008910 };
8911 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008912 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008913 .rpc_message = &msg,
8914 .callback_ops = &nfs4_layoutreturn_call_ops,
8915 .callback_data = lrp,
8916 };
Peng Tao6c166052014-11-17 09:30:40 +08008917 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008918
Andrew Elble99ade3c2015-12-02 09:39:51 -05008919 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8920 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8921 &task_setup_data.rpc_client, &msg);
8922
Benny Halevycbe82602011-05-22 19:52:37 +03008923 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008924 if (!sync) {
8925 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8926 if (!lrp->inode) {
8927 nfs4_layoutreturn_release(lrp);
8928 return -EAGAIN;
8929 }
8930 task_setup_data.flags |= RPC_TASK_ASYNC;
8931 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008932 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008933 task = rpc_run_task(&task_setup_data);
8934 if (IS_ERR(task))
8935 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008936 if (sync)
8937 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008938 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008939 dprintk("<-- %s status=%d\n", __func__, status);
8940 rpc_put_task(task);
8941 return status;
8942}
8943
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008944static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008945_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8946 struct pnfs_device *pdev,
8947 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008948{
8949 struct nfs4_getdeviceinfo_args args = {
8950 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008951 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8952 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008953 };
8954 struct nfs4_getdeviceinfo_res res = {
8955 .pdev = pdev,
8956 };
8957 struct rpc_message msg = {
8958 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8959 .rpc_argp = &args,
8960 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008961 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008962 };
8963 int status;
8964
8965 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008966 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008967 if (res.notification & ~args.notify_types)
8968 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008969 if (res.notification != args.notify_types)
8970 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008971
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008972 dprintk("<-- %s status=%d\n", __func__, status);
8973
8974 return status;
8975}
8976
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008977int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8978 struct pnfs_device *pdev,
8979 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008980{
8981 struct nfs4_exception exception = { };
8982 int err;
8983
8984 do {
8985 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008986 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008987 &exception);
8988 } while (exception.retry);
8989 return err;
8990}
8991EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8992
Andy Adamson863a3c62011-03-23 13:27:54 +00008993static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8994{
8995 struct nfs4_layoutcommit_data *data = calldata;
8996 struct nfs_server *server = NFS_SERVER(data->args.inode);
8997
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008998 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008999 &data->args.seq_args,
9000 &data->res.seq_res,
9001 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00009002}
9003
9004static void
9005nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
9006{
9007 struct nfs4_layoutcommit_data *data = calldata;
9008 struct nfs_server *server = NFS_SERVER(data->args.inode);
9009
Trond Myklebust6ba7db32012-10-22 20:07:20 -04009010 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00009011 return;
9012
9013 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009014 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
9015 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
9016 case -NFS4ERR_BADLAYOUT: /* no layout */
9017 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00009018 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009019 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009020 break;
9021 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10009022 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04009023 rpc_restart_call_prepare(task);
9024 return;
9025 }
9026 }
Andy Adamson863a3c62011-03-23 13:27:54 +00009027}
9028
9029static void nfs4_layoutcommit_release(void *calldata)
9030{
9031 struct nfs4_layoutcommit_data *data = calldata;
9032
Andy Adamsondb29c082011-07-30 20:52:38 -04009033 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009034 nfs_post_op_update_inode_force_wcc(data->args.inode,
9035 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009036 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009037 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009038 kfree(data);
9039}
9040
9041static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9042 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9043 .rpc_call_done = nfs4_layoutcommit_done,
9044 .rpc_release = nfs4_layoutcommit_release,
9045};
9046
9047int
Andy Adamsonef311532011-03-12 02:58:10 -05009048nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009049{
9050 struct rpc_message msg = {
9051 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9052 .rpc_argp = &data->args,
9053 .rpc_resp = &data->res,
9054 .rpc_cred = data->cred,
9055 };
9056 struct rpc_task_setup task_setup_data = {
9057 .task = &data->task,
9058 .rpc_client = NFS_CLIENT(data->args.inode),
9059 .rpc_message = &msg,
9060 .callback_ops = &nfs4_layoutcommit_ops,
9061 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009062 };
9063 struct rpc_task *task;
9064 int status = 0;
9065
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009066 dprintk("NFS: initiating layoutcommit call. sync %d "
9067 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009068 data->args.lastbytewritten,
9069 data->args.inode->i_ino);
9070
Trond Myklebust472e2592015-02-05 16:50:30 -05009071 if (!sync) {
9072 data->inode = nfs_igrab_and_active(data->args.inode);
9073 if (data->inode == NULL) {
9074 nfs4_layoutcommit_release(data);
9075 return -EAGAIN;
9076 }
9077 task_setup_data.flags = RPC_TASK_ASYNC;
9078 }
Chuck Levera9c92d62013-08-09 12:48:18 -04009079 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00009080 task = rpc_run_task(&task_setup_data);
9081 if (IS_ERR(task))
9082 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009083 if (sync)
9084 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009085 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009086 dprintk("%s: status %d\n", __func__, status);
9087 rpc_put_task(task);
9088 return status;
9089}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009090
Andy Adamson97431202013-08-08 10:57:56 -04009091/**
9092 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9093 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9094 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009095static int
9096_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009097 struct nfs_fsinfo *info,
9098 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009099{
9100 struct nfs41_secinfo_no_name_args args = {
9101 .style = SECINFO_STYLE_CURRENT_FH,
9102 };
9103 struct nfs4_secinfo_res res = {
9104 .flavors = flavors,
9105 };
9106 struct rpc_message msg = {
9107 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9108 .rpc_argp = &args,
9109 .rpc_resp = &res,
9110 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009111 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009112 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009113 int status;
9114
9115 if (use_integrity) {
9116 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009117 cred = nfs4_get_clid_cred(server->nfs_client);
9118 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009119 }
9120
9121 dprintk("--> %s\n", __func__);
9122 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9123 &res.seq_res, 0);
9124 dprintk("<-- %s status=%d\n", __func__, status);
9125
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009126 if (cred)
9127 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009128
9129 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009130}
9131
9132static int
9133nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9134 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9135{
9136 struct nfs4_exception exception = { };
9137 int err;
9138 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009139 /* first try using integrity protection */
9140 err = -NFS4ERR_WRONGSEC;
9141
9142 /* try to use integrity protection with machine cred */
9143 if (_nfs4_is_integrity_protected(server->nfs_client))
9144 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9145 flavors, true);
9146
9147 /*
9148 * if unable to use integrity protection, or SECINFO with
9149 * integrity protection returns NFS4ERR_WRONGSEC (which is
9150 * disallowed by spec, but exists in deployed servers) use
9151 * the current filesystem's rpc_client and the user cred.
9152 */
9153 if (err == -NFS4ERR_WRONGSEC)
9154 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9155 flavors, false);
9156
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009157 switch (err) {
9158 case 0:
9159 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009160 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009161 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009162 default:
9163 err = nfs4_handle_exception(server, err, &exception);
9164 }
9165 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009166out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009167 return err;
9168}
9169
9170static int
9171nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9172 struct nfs_fsinfo *info)
9173{
9174 int err;
9175 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009176 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009177 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009178 struct nfs4_secinfo4 *secinfo;
9179 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009180
9181 page = alloc_page(GFP_KERNEL);
9182 if (!page) {
9183 err = -ENOMEM;
9184 goto out;
9185 }
9186
9187 flavors = page_address(page);
9188 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9189
9190 /*
9191 * Fall back on "guess and check" method if
9192 * the server doesn't support SECINFO_NO_NAME
9193 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009194 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009195 err = nfs4_find_root_sec(server, fhandle, info);
9196 goto out_freepage;
9197 }
9198 if (err)
9199 goto out_freepage;
9200
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009201 for (i = 0; i < flavors->num_flavors; i++) {
9202 secinfo = &flavors->flavors[i];
9203
9204 switch (secinfo->flavor) {
9205 case RPC_AUTH_NULL:
9206 case RPC_AUTH_UNIX:
9207 case RPC_AUTH_GSS:
9208 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9209 &secinfo->flavor_info);
9210 break;
9211 default:
9212 flavor = RPC_AUTH_MAXFLAVOR;
9213 break;
9214 }
9215
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009216 if (!nfs_auth_info_match(&server->auth_info, flavor))
9217 flavor = RPC_AUTH_MAXFLAVOR;
9218
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009219 if (flavor != RPC_AUTH_MAXFLAVOR) {
9220 err = nfs4_lookup_root_sec(server, fhandle,
9221 info, flavor);
9222 if (!err)
9223 break;
9224 }
9225 }
9226
9227 if (flavor == RPC_AUTH_MAXFLAVOR)
9228 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009229
9230out_freepage:
9231 put_page(page);
9232 if (err == -EACCES)
9233 return -EPERM;
9234out:
9235 return err;
9236}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009237
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009238static int _nfs41_test_stateid(struct nfs_server *server,
9239 nfs4_stateid *stateid,
9240 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009241{
9242 int status;
9243 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009244 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009245 };
9246 struct nfs41_test_stateid_res res;
9247 struct rpc_message msg = {
9248 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9249 .rpc_argp = &args,
9250 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009251 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009252 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009253 struct rpc_clnt *rpc_client = server->client;
9254
9255 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9256 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009257
Chuck Lever38527b12012-07-11 16:30:23 -04009258 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009259 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009260 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009261 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009262 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009263 if (status != NFS_OK) {
9264 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009265 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009266 }
9267 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009268 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009269}
9270
Trond Myklebust43912bb2016-09-22 13:38:56 -04009271static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9272 int err, struct nfs4_exception *exception)
9273{
9274 exception->retry = 0;
9275 switch(err) {
9276 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009277 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009278 nfs4_handle_exception(server, err, exception);
9279 break;
9280 case -NFS4ERR_BADSESSION:
9281 case -NFS4ERR_BADSLOT:
9282 case -NFS4ERR_BAD_HIGH_SLOT:
9283 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9284 case -NFS4ERR_DEADSESSION:
9285 nfs4_do_handle_exception(server, err, exception);
9286 }
9287}
9288
Chuck Lever38527b12012-07-11 16:30:23 -04009289/**
9290 * nfs41_test_stateid - perform a TEST_STATEID operation
9291 *
9292 * @server: server / transport on which to perform the operation
9293 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009294 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009295 *
9296 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9297 * Otherwise a negative NFS4ERR value is returned if the operation
9298 * failed or the state ID is not currently valid.
9299 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009300static int nfs41_test_stateid(struct nfs_server *server,
9301 nfs4_stateid *stateid,
9302 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009303{
9304 struct nfs4_exception exception = { };
9305 int err;
9306 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009307 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009308 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009309 } while (exception.retry);
9310 return err;
9311}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009312
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009313struct nfs_free_stateid_data {
9314 struct nfs_server *server;
9315 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009316 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009317};
9318
9319static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9320{
9321 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009322 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009323 &data->args.seq_args,
9324 &data->res.seq_res,
9325 task);
9326}
9327
9328static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9329{
9330 struct nfs_free_stateid_data *data = calldata;
9331
9332 nfs41_sequence_done(task, &data->res.seq_res);
9333
9334 switch (task->tk_status) {
9335 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009336 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009337 rpc_restart_call_prepare(task);
9338 }
9339}
9340
9341static void nfs41_free_stateid_release(void *calldata)
9342{
9343 kfree(calldata);
9344}
9345
Trond Myklebust17f26b12013-08-21 15:48:42 -04009346static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009347 .rpc_call_prepare = nfs41_free_stateid_prepare,
9348 .rpc_call_done = nfs41_free_stateid_done,
9349 .rpc_release = nfs41_free_stateid_release,
9350};
9351
9352static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009353 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009354 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009355 bool privileged)
9356{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009357 struct rpc_message msg = {
9358 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009359 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009360 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009361 struct rpc_task_setup task_setup = {
9362 .rpc_client = server->client,
9363 .rpc_message = &msg,
9364 .callback_ops = &nfs41_free_stateid_ops,
9365 .flags = RPC_TASK_ASYNC,
9366 };
9367 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009368
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009369 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9370 &task_setup.rpc_client, &msg);
9371
Chuck Lever38527b12012-07-11 16:30:23 -04009372 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009373 data = kmalloc(sizeof(*data), GFP_NOFS);
9374 if (!data)
9375 return ERR_PTR(-ENOMEM);
9376 data->server = server;
9377 nfs4_stateid_copy(&data->args.stateid, stateid);
9378
9379 task_setup.callback_data = data;
9380
9381 msg.rpc_argp = &data->args;
9382 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009383 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009384 if (privileged)
9385 nfs4_set_sequence_privileged(&data->args.seq_args);
9386
9387 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009388}
9389
Chuck Lever38527b12012-07-11 16:30:23 -04009390/**
9391 * nfs41_free_stateid - perform a FREE_STATEID operation
9392 *
9393 * @server: server / transport on which to perform the operation
9394 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009395 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009396 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009397 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009398 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009399 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009400static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009401 const nfs4_stateid *stateid,
9402 struct rpc_cred *cred,
9403 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009404{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009405 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009406
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009407 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009408 if (IS_ERR(task))
9409 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009410 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009411 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009412}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009413
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009414static void
9415nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009416{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009417 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009418
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009419 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009420 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009421}
9422
Trond Myklebust36281ca2012-03-04 18:13:56 -05009423static bool nfs41_match_stateid(const nfs4_stateid *s1,
9424 const nfs4_stateid *s2)
9425{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009426 if (s1->type != s2->type)
9427 return false;
9428
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009429 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009430 return false;
9431
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009432 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009433 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009434
Anna Schumaker045c5512017-01-11 16:59:48 -05009435 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009436}
9437
Andy Adamson557134a2009-04-01 09:21:53 -04009438#endif /* CONFIG_NFS_V4_1 */
9439
Trond Myklebust36281ca2012-03-04 18:13:56 -05009440static bool nfs4_match_stateid(const nfs4_stateid *s1,
9441 const nfs4_stateid *s2)
9442{
Trond Myklebustf597c532012-03-04 18:13:56 -05009443 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009444}
9445
9446
Trond Myklebust17280172012-03-11 13:11:00 -04009447static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009448 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009449 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009450 .recover_open = nfs4_open_reclaim,
9451 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009452 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009453 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009454};
9455
Andy Adamson591d71c2009-04-01 09:22:47 -04009456#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009457static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009458 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9459 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9460 .recover_open = nfs4_open_reclaim,
9461 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009462 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009463 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009464 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009465};
9466#endif /* CONFIG_NFS_V4_1 */
9467
Trond Myklebust17280172012-03-11 13:11:00 -04009468static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009469 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009470 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009471 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009472 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009473 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009474};
9475
Andy Adamson591d71c2009-04-01 09:22:47 -04009476#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009477static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009478 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9479 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009480 .recover_open = nfs41_open_expired,
9481 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009482 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009483};
9484#endif /* CONFIG_NFS_V4_1 */
9485
Trond Myklebust17280172012-03-11 13:11:00 -04009486static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009487 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009488 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009489 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009490};
9491
9492#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009493static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009494 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009495 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009496 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009497};
9498#endif
9499
Chuck Leverec011fe2013-10-17 14:12:39 -04009500static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009501 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009502 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009503};
9504
9505#if defined(CONFIG_NFS_V4_1)
9506static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009507 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009508 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009509};
9510#endif /* CONFIG_NFS_V4_1 */
9511
Trond Myklebust97dc1352010-06-16 09:52:26 -04009512static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9513 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009514 .init_caps = NFS_CAP_READDIRPLUS
9515 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009516 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009517 .init_client = nfs40_init_client,
9518 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009519 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009520 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009521 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009522 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009523 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009524 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009525 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9526 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9527 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009528 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009529};
9530
9531#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009532static struct nfs_seqid *
9533nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9534{
9535 return NULL;
9536}
9537
Trond Myklebust97dc1352010-06-16 09:52:26 -04009538static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9539 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009540 .init_caps = NFS_CAP_READDIRPLUS
9541 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009542 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009543 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009544 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009545 .init_client = nfs41_init_client,
9546 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009547 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009548 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009549 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009550 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009551 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009552 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009553 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009554 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9555 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9556 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009557 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009558};
9559#endif
9560
Steve Dickson42c2c422013-05-22 12:50:38 -04009561#if defined(CONFIG_NFS_V4_2)
9562static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9563 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009564 .init_caps = NFS_CAP_READDIRPLUS
9565 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009566 | NFS_CAP_POSIX_LOCK
9567 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009568 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009569 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009570 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009571 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009572 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009573 | NFS_CAP_LAYOUTSTATS
9574 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009575 .init_client = nfs41_init_client,
9576 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009577 .match_stateid = nfs41_match_stateid,
9578 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009579 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009580 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009581 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009582 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009583 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009584 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9585 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9586 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009587 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009588};
9589#endif
9590
Trond Myklebust97dc1352010-06-16 09:52:26 -04009591const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9592 [0] = &nfs_v4_0_minor_ops,
9593#if defined(CONFIG_NFS_V4_1)
9594 [1] = &nfs_v4_1_minor_ops,
9595#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009596#if defined(CONFIG_NFS_V4_2)
9597 [2] = &nfs_v4_2_minor_ops,
9598#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009599};
9600
Trond Myklebust13997822016-07-24 17:10:52 -04009601static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009602{
9603 ssize_t error, error2;
9604
9605 error = generic_listxattr(dentry, list, size);
9606 if (error < 0)
9607 return error;
9608 if (list) {
9609 list += error;
9610 size -= error;
9611 }
9612
9613 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9614 if (error2 < 0)
9615 return error2;
9616 return error + error2;
9617}
9618
Trond Myklebust17f26b12013-08-21 15:48:42 -04009619static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009620 .create = nfs_create,
9621 .lookup = nfs_lookup,
9622 .atomic_open = nfs_atomic_open,
9623 .link = nfs_link,
9624 .unlink = nfs_unlink,
9625 .symlink = nfs_symlink,
9626 .mkdir = nfs_mkdir,
9627 .rmdir = nfs_rmdir,
9628 .mknod = nfs_mknod,
9629 .rename = nfs_rename,
9630 .permission = nfs_permission,
9631 .getattr = nfs_getattr,
9632 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009633 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009634};
9635
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009636static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009637 .permission = nfs_permission,
9638 .getattr = nfs_getattr,
9639 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009640 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009641};
9642
David Howells509de812006-08-22 20:06:11 -04009643const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009644 .version = 4, /* protocol version */
9645 .dentry_ops = &nfs4_dentry_operations,
9646 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009647 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009648 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009650 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009651 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 .getattr = nfs4_proc_getattr,
9653 .setattr = nfs4_proc_setattr,
9654 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009655 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009656 .access = nfs4_proc_access,
9657 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009658 .create = nfs4_proc_create,
9659 .remove = nfs4_proc_remove,
9660 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009661 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009662 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009663 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009664 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009665 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009666 .link = nfs4_proc_link,
9667 .symlink = nfs4_proc_symlink,
9668 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009669 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009670 .readdir = nfs4_proc_readdir,
9671 .mknod = nfs4_proc_mknod,
9672 .statfs = nfs4_proc_statfs,
9673 .fsinfo = nfs4_proc_fsinfo,
9674 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009675 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009676 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009677 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009678 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009679 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009681 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009682 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009683 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009684 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009685 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009686 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009687 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009688 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009689 .have_delegation = nfs4_have_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009690 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009691 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009692 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009693 .create_server = nfs4_create_server,
9694 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009695};
9696
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009697static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009698 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009699 .list = nfs4_xattr_list_nfs4_acl,
9700 .get = nfs4_xattr_get_nfs4_acl,
9701 .set = nfs4_xattr_set_nfs4_acl,
9702};
9703
9704const struct xattr_handler *nfs4_xattr_handlers[] = {
9705 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009706#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9707 &nfs4_xattr_nfs4_label_handler,
9708#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009709 NULL
9710};
9711
Linus Torvalds1da177e2005-04-16 15:20:36 -07009712/*
9713 * Local variables:
9714 * c-basic-offset: 8
9715 * End:
9716 */