blob: 173089f5bf7ee2656e6645612efcc673c655f38b [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/nfs/nfs4proc.c
3 *
4 * Client-side procedure declarations for NFSv4.
5 *
6 * Copyright (c) 2002 The Regents of the University of Michigan.
7 * All rights reserved.
8 *
9 * Kendrick Smith <kmsmith@umich.edu>
10 * Andy Adamson <andros@umich.edu>
11 *
12 * Redistribution and use in source and binary forms, with or without
13 * modification, are permitted provided that the following conditions
14 * are met:
15 *
16 * 1. Redistributions of source code must retain the above copyright
17 * notice, this list of conditions and the following disclaimer.
18 * 2. Redistributions in binary form must reproduce the above copyright
19 * notice, this list of conditions and the following disclaimer in the
20 * documentation and/or other materials provided with the distribution.
21 * 3. Neither the name of the University nor the names of its
22 * contributors may be used to endorse or promote products derived
23 * from this software without specific prior written permission.
24 *
25 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESS OR IMPLIED
26 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
27 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
28 * DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
29 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
32 * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
33 * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
34 * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
35 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include <linux/mm.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070039#include <linux/delay.h>
40#include <linux/errno.h>
41#include <linux/string.h>
Trond Myklebust652f89f2011-12-09 19:05:58 -050042#include <linux/ratelimit.h>
43#include <linux/printk.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090044#include <linux/slab.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070045#include <linux/sunrpc/clnt.h>
46#include <linux/nfs.h>
47#include <linux/nfs4.h>
48#include <linux/nfs_fs.h>
49#include <linux/nfs_page.h>
Bryan Schumaker9b7160c2011-04-13 14:31:30 -040050#include <linux/nfs_mount.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070051#include <linux/namei.h>
Trond Myklebust02a913a2005-10-18 14:20:17 -070052#include <linux/mount.h>
Benny Halevy99fe60d2009-04-01 09:22:29 -040053#include <linux/module.h>
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +000054#include <linux/xattr.h>
Andy Adamsonc7a360b2011-01-25 19:15:32 -050055#include <linux/utsname.h>
Jeff Laytond3103102011-12-01 22:44:39 +010056#include <linux/freezer.h>
Jeff Layton1eb5d982018-01-09 08:21:17 -050057#include <linux/iversion.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070058
Trond Myklebust4ce79712005-06-22 17:16:21 +000059#include "nfs4_fs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070060#include "delegation.h"
Trond Myklebust101070c2008-02-19 20:04:23 -050061#include "internal.h"
Chuck Lever006ea732006-03-20 13:44:14 -050062#include "iostat.h"
Andy Adamsonfc931582009-04-01 09:22:31 -040063#include "callback.h"
Andy Adamsonb1f69b72010-10-20 00:18:03 -040064#include "pnfs.h"
Chuck Leverf0920752012-05-21 22:45:41 -040065#include "netns.h"
Anna Schumaker40c64c22015-04-15 13:00:05 -040066#include "nfs4idmap.h"
Trond Myklebust73e39aa2012-11-26 12:49:34 -050067#include "nfs4session.h"
David Howellsde242c02012-12-20 21:52:38 +000068#include "fscache.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
Trond Myklebustc6d01c62013-08-09 11:51:26 -040070#include "nfs4trace.h"
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072#define NFSDBG_FACILITY NFSDBG_PROC
73
Trond Myklebust2066fe82006-09-15 08:30:46 -040074#define NFS4_POLL_RETRY_MIN (HZ/10)
Linus Torvalds1da177e2005-04-16 15:20:36 -070075#define NFS4_POLL_RETRY_MAX (15*HZ)
76
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +020077/* file attributes which can be mapped to nfs attributes */
78#define NFS4_VALID_ATTRS (ATTR_MODE \
79 | ATTR_UID \
80 | ATTR_GID \
81 | ATTR_SIZE \
82 | ATTR_ATIME \
83 | ATTR_MTIME \
84 | ATTR_CTIME \
85 | ATTR_ATIME_SET \
86 | ATTR_MTIME_SET)
87
Trond Myklebustcdd4e682006-01-03 09:55:12 +010088struct nfs4_opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +010089static int _nfs4_proc_open(struct nfs4_opendata *data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -080090static int _nfs4_recover_proc_open(struct nfs4_opendata *data);
Linus Torvalds1da177e2005-04-16 15:20:36 -070091static int nfs4_do_fsinfo(struct nfs_server *, struct nfs_fh *, struct nfs_fsinfo *);
Chuck Lever81934dd2012-03-01 17:01:57 -050092static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr);
David Quigley1775fd32013-05-22 12:50:42 -040093static int nfs4_proc_getattr(struct nfs_server *, struct nfs_fh *, struct nfs_fattr *, struct nfs4_label *label);
94static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fattr *fattr, struct nfs4_label *label);
Trond Myklebust0ab64e02010-04-16 16:22:51 -040095static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
96 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +110097 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
David Quigley1775fd32013-05-22 12:50:42 -040098 struct nfs4_label *olabel);
Bryan Schumakerf062eb62011-06-02 14:59:10 -040099#ifdef CONFIG_NFS_V4_1
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400100static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
101 struct rpc_cred *cred,
102 struct nfs4_slot *slot,
103 bool is_privileged);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -0400104static int nfs41_test_stateid(struct nfs_server *, nfs4_stateid *,
105 struct rpc_cred *);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -0400106static int nfs41_free_stateid(struct nfs_server *, const nfs4_stateid *,
107 struct rpc_cred *, bool);
Bryan Schumakerf062eb62011-06-02 14:59:10 -0400108#endif
David Quigleyaa9c2662013-05-22 12:50:44 -0400109
110#ifdef CONFIG_NFS_V4_SECURITY_LABEL
111static inline struct nfs4_label *
112nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
113 struct iattr *sattr, struct nfs4_label *label)
114{
115 int err;
116
117 if (label == NULL)
118 return NULL;
119
120 if (nfs_server_capable(dir, NFS_CAP_SECURITY_LABEL) == 0)
121 return NULL;
122
David Quigleyaa9c2662013-05-22 12:50:44 -0400123 err = security_dentry_init_security(dentry, sattr->ia_mode,
124 &dentry->d_name, (void **)&label->label, &label->len);
125 if (err == 0)
126 return label;
127
128 return NULL;
129}
130static inline void
131nfs4_label_release_security(struct nfs4_label *label)
132{
133 if (label)
134 security_release_secctx(label->label, label->len);
135}
136static inline u32 *nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
137{
138 if (label)
139 return server->attr_bitmask;
140
141 return server->attr_bitmask_nl;
142}
143#else
144static inline struct nfs4_label *
145nfs4_label_init_security(struct inode *dir, struct dentry *dentry,
146 struct iattr *sattr, struct nfs4_label *l)
147{ return NULL; }
148static inline void
149nfs4_label_release_security(struct nfs4_label *label)
150{ return; }
151static inline u32 *
152nfs4_bitmask(struct nfs_server *server, struct nfs4_label *label)
153{ return server->attr_bitmask; }
154#endif
155
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156/* Prevent leaks of NFSv4 errors into userland */
WANG Cong46f72f52008-12-30 16:35:55 -0500157static int nfs4_map_errors(int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
Trond Myklebust52567b02009-10-23 14:46:42 -0400159 if (err >= -1000)
160 return err;
161 switch (err) {
162 case -NFS4ERR_RESOURCE:
Weston Andros Adamson30005122013-02-28 20:30:10 -0500163 case -NFS4ERR_LAYOUTTRYLATER:
164 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebust52567b02009-10-23 14:46:42 -0400165 return -EREMOTEIO;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000166 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson88975382013-08-13 16:37:38 -0400167 case -NFS4ERR_WRONG_CRED:
Bryan Schumaker7ebb9312011-03-24 17:12:30 +0000168 return -EPERM;
Trond Myklebust3ddeb7c2011-02-22 15:44:31 -0800169 case -NFS4ERR_BADOWNER:
170 case -NFS4ERR_BADNAME:
171 return -EINVAL;
Trond Myklebustfb13bfa2012-05-28 11:36:28 -0400172 case -NFS4ERR_SHARE_DENIED:
173 return -EACCES;
Steve Dicksonf25efd82012-06-06 14:12:07 -0400174 case -NFS4ERR_MINOR_VERS_MISMATCH:
175 return -EPROTONOSUPPORT;
Trond Myklebust6e3cf242013-03-23 15:22:45 -0400176 case -NFS4ERR_FILE_OPEN:
177 return -EBUSY;
Trond Myklebust52567b02009-10-23 14:46:42 -0400178 default:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700179 dprintk("%s could not handle NFSv4 error %d\n",
Harvey Harrison3110ff82008-05-02 13:42:44 -0700180 __func__, -err);
Trond Myklebust52567b02009-10-23 14:46:42 -0400181 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 }
Trond Myklebust52567b02009-10-23 14:46:42 -0400183 return -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184}
185
186/*
187 * This is our standard bitmap for GETATTR requests.
188 */
Trond Myklebust1549210f2012-06-05 09:16:47 -0400189const u32 nfs4_fattr_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700190 FATTR4_WORD0_TYPE
191 | FATTR4_WORD0_CHANGE
192 | FATTR4_WORD0_SIZE
193 | FATTR4_WORD0_FSID
194 | FATTR4_WORD0_FILEID,
195 FATTR4_WORD1_MODE
196 | FATTR4_WORD1_NUMLINKS
197 | FATTR4_WORD1_OWNER
198 | FATTR4_WORD1_OWNER_GROUP
199 | FATTR4_WORD1_RAWDEV
200 | FATTR4_WORD1_SPACE_USED
201 | FATTR4_WORD1_TIME_ACCESS
202 | FATTR4_WORD1_TIME_METADATA
Anna Schumakerea96d1e2015-04-03 14:35:59 -0400203 | FATTR4_WORD1_TIME_MODIFY
204 | FATTR4_WORD1_MOUNTED_ON_FILEID,
David Quigleyaa9c2662013-05-22 12:50:44 -0400205#ifdef CONFIG_NFS_V4_SECURITY_LABEL
206 FATTR4_WORD2_SECURITY_LABEL
207#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700208};
209
Trond Myklebust1549210f2012-06-05 09:16:47 -0400210static const u32 nfs4_pnfs_open_bitmap[3] = {
211 FATTR4_WORD0_TYPE
212 | FATTR4_WORD0_CHANGE
213 | FATTR4_WORD0_SIZE
214 | FATTR4_WORD0_FSID
215 | FATTR4_WORD0_FILEID,
216 FATTR4_WORD1_MODE
217 | FATTR4_WORD1_NUMLINKS
218 | FATTR4_WORD1_OWNER
219 | FATTR4_WORD1_OWNER_GROUP
220 | FATTR4_WORD1_RAWDEV
221 | FATTR4_WORD1_SPACE_USED
222 | FATTR4_WORD1_TIME_ACCESS
223 | FATTR4_WORD1_TIME_METADATA
224 | FATTR4_WORD1_TIME_MODIFY,
225 FATTR4_WORD2_MDSTHRESHOLD
Trond Myklebust95864c92015-12-26 15:06:03 -0500226#ifdef CONFIG_NFS_V4_SECURITY_LABEL
227 | FATTR4_WORD2_SECURITY_LABEL
228#endif
Trond Myklebust1549210f2012-06-05 09:16:47 -0400229};
230
Andy Adamsone23008e2012-10-02 21:07:32 -0400231static const u32 nfs4_open_noattr_bitmap[3] = {
232 FATTR4_WORD0_TYPE
Andy Adamsone23008e2012-10-02 21:07:32 -0400233 | FATTR4_WORD0_FILEID,
234};
235
David Quigleya09df2c2013-05-22 12:50:41 -0400236const u32 nfs4_statfs_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 FATTR4_WORD0_FILES_AVAIL
238 | FATTR4_WORD0_FILES_FREE
239 | FATTR4_WORD0_FILES_TOTAL,
240 FATTR4_WORD1_SPACE_AVAIL
241 | FATTR4_WORD1_SPACE_FREE
242 | FATTR4_WORD1_SPACE_TOTAL
243};
244
David Quigleya09df2c2013-05-22 12:50:41 -0400245const u32 nfs4_pathconf_bitmap[3] = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700246 FATTR4_WORD0_MAXLINK
247 | FATTR4_WORD0_MAXNAME,
248 0
249};
250
Fred Isamandae100c2011-07-30 20:52:37 -0400251const u32 nfs4_fsinfo_bitmap[3] = { FATTR4_WORD0_MAXFILESIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252 | FATTR4_WORD0_MAXREAD
253 | FATTR4_WORD0_MAXWRITE
254 | FATTR4_WORD0_LEASE_TIME,
Ricardo Labiaga55b6e772010-10-12 16:30:06 -0700255 FATTR4_WORD1_TIME_DELTA
Fred Isamandae100c2011-07-30 20:52:37 -0400256 | FATTR4_WORD1_FS_LAYOUT_TYPES,
257 FATTR4_WORD2_LAYOUT_BLKSIZE
Peng Tao2a92ee92015-09-26 02:24:37 +0800258 | FATTR4_WORD2_CLONE_BLKSIZE
Linus Torvalds1da177e2005-04-16 15:20:36 -0700259};
260
David Quigleya09df2c2013-05-22 12:50:41 -0400261const u32 nfs4_fs_locations_bitmap[3] = {
Chuck Leverc05cefc2017-11-05 15:45:22 -0500262 FATTR4_WORD0_CHANGE
Manoj Naik830b8e32006-06-09 09:34:25 -0400263 | FATTR4_WORD0_SIZE
264 | FATTR4_WORD0_FSID
265 | FATTR4_WORD0_FILEID
266 | FATTR4_WORD0_FS_LOCATIONS,
Chuck Leverc05cefc2017-11-05 15:45:22 -0500267 FATTR4_WORD1_OWNER
Manoj Naik830b8e32006-06-09 09:34:25 -0400268 | FATTR4_WORD1_OWNER_GROUP
269 | FATTR4_WORD1_RAWDEV
270 | FATTR4_WORD1_SPACE_USED
271 | FATTR4_WORD1_TIME_ACCESS
272 | FATTR4_WORD1_TIME_METADATA
273 | FATTR4_WORD1_TIME_MODIFY
David Quigleya09df2c2013-05-22 12:50:41 -0400274 | FATTR4_WORD1_MOUNTED_ON_FILEID,
Manoj Naik830b8e32006-06-09 09:34:25 -0400275};
276
Al Virobc4785c2006-10-19 23:28:51 -0700277static void nfs4_setup_readdir(u64 cookie, __be32 *verifier, struct dentry *dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700278 struct nfs4_readdir_arg *readdir)
279{
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400280 unsigned int attrs = FATTR4_WORD0_FILEID | FATTR4_WORD0_TYPE;
Al Viro0dbb4c62006-10-19 23:28:49 -0700281 __be32 *start, *p;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700282
Linus Torvalds1da177e2005-04-16 15:20:36 -0700283 if (cookie > 2) {
Adrian Bunkb7ef1952005-06-22 17:16:28 +0000284 readdir->cookie = cookie;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700285 memcpy(&readdir->verifier, verifier, sizeof(readdir->verifier));
286 return;
287 }
288
289 readdir->cookie = 0;
290 memset(&readdir->verifier, 0, sizeof(readdir->verifier));
291 if (cookie == 2)
292 return;
293
294 /*
295 * NFSv4 servers do not return entries for '.' and '..'
296 * Therefore, we fake these entries here. We let '.'
297 * have cookie 0 and '..' have cookie 1. Note that
298 * when talking to the server, we always send cookie 0
299 * instead of 1 or 2.
300 */
Cong Wang2b86ce22011-11-25 23:14:33 +0800301 start = p = kmap_atomic(*readdir->pages);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700302
303 if (cookie == 0) {
304 *p++ = xdr_one; /* next */
305 *p++ = xdr_zero; /* cookie, first word */
306 *p++ = xdr_one; /* cookie, second word */
307 *p++ = xdr_one; /* entry len */
308 memcpy(p, ".\0\0\0", 4); /* entry */
309 p++;
310 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400311 *p++ = htonl(attrs); /* bitmap */
312 *p++ = htonl(12); /* attribute buffer length */
313 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000314 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 }
316
317 *p++ = xdr_one; /* next */
318 *p++ = xdr_zero; /* cookie, first word */
319 *p++ = xdr_two; /* cookie, second word */
320 *p++ = xdr_two; /* entry len */
321 memcpy(p, "..\0\0", 4); /* entry */
322 p++;
323 *p++ = xdr_one; /* bitmap length */
Anna Schumaker18fe6a22017-06-16 12:06:59 -0400324 *p++ = htonl(attrs); /* bitmap */
325 *p++ = htonl(12); /* attribute buffer length */
326 *p++ = htonl(NF4DIR);
David Howells2b0143b2015-03-17 22:25:59 +0000327 p = xdr_encode_hyper(p, NFS_FILEID(d_inode(dentry->d_parent)));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328
329 readdir->pgbase = (char *)p - (char *)start;
330 readdir->count -= readdir->pgbase;
Cong Wang2b86ce22011-11-25 23:14:33 +0800331 kunmap_atomic(start);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332}
333
Trond Myklebust26d36302016-09-22 13:39:05 -0400334static void nfs4_test_and_free_stateid(struct nfs_server *server,
335 nfs4_stateid *stateid,
336 struct rpc_cred *cred)
337{
338 const struct nfs4_minor_version_ops *ops = server->nfs_client->cl_mvops;
339
340 ops->test_and_free_expired(server, stateid, cred);
341}
342
343static void __nfs4_free_revoked_stateid(struct nfs_server *server,
344 nfs4_stateid *stateid,
345 struct rpc_cred *cred)
346{
347 stateid->type = NFS4_REVOKED_STATEID_TYPE;
348 nfs4_test_and_free_stateid(server, stateid, cred);
349}
350
351static void nfs4_free_revoked_stateid(struct nfs_server *server,
352 const nfs4_stateid *stateid,
353 struct rpc_cred *cred)
354{
355 nfs4_stateid tmp;
356
357 nfs4_stateid_copy(&tmp, stateid);
358 __nfs4_free_revoked_stateid(server, &tmp, cred);
359}
360
NeilBrown8478eaa2014-09-18 16:09:27 +1000361static long nfs4_update_delay(long *timeout)
362{
363 long ret;
364 if (!timeout)
365 return NFS4_POLL_RETRY_MAX;
366 if (*timeout <= 0)
367 *timeout = NFS4_POLL_RETRY_MIN;
368 if (*timeout > NFS4_POLL_RETRY_MAX)
369 *timeout = NFS4_POLL_RETRY_MAX;
370 ret = *timeout;
371 *timeout <<= 1;
372 return ret;
373}
374
Trond Myklebust65de8722008-12-23 15:21:44 -0500375static int nfs4_delay(struct rpc_clnt *clnt, long *timeout)
376{
377 int res = 0;
378
379 might_sleep();
380
NeilBrown8478eaa2014-09-18 16:09:27 +1000381 freezable_schedule_timeout_killable_unsafe(
382 nfs4_update_delay(timeout));
Trond Myklebust65de8722008-12-23 15:21:44 -0500383 if (fatal_signal_pending(current))
384 res = -ERESTARTSYS;
Trond Myklebust65de8722008-12-23 15:21:44 -0500385 return res;
386}
387
388/* This is the error handling routine for processes that are allowed
389 * to sleep.
390 */
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400391static int nfs4_do_handle_exception(struct nfs_server *server,
392 int errorcode, struct nfs4_exception *exception)
Trond Myklebust65de8722008-12-23 15:21:44 -0500393{
394 struct nfs_client *clp = server->nfs_client;
Trond Myklebust9e33bed2008-12-23 15:21:46 -0500395 struct nfs4_state *state = exception->state;
Trond Myklebust8487c472016-06-26 08:44:35 -0400396 const nfs4_stateid *stateid = exception->stateid;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500397 struct inode *inode = exception->inode;
Trond Myklebust65de8722008-12-23 15:21:44 -0500398 int ret = errorcode;
399
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400400 exception->delay = 0;
401 exception->recovering = 0;
Trond Myklebust65de8722008-12-23 15:21:44 -0500402 exception->retry = 0;
Trond Myklebust272289a2016-09-22 13:39:15 -0400403
404 if (stateid == NULL && state != NULL)
405 stateid = &state->stateid;
406
Trond Myklebust65de8722008-12-23 15:21:44 -0500407 switch(errorcode) {
408 case 0:
409 return 0;
Trond Myklebust5ba12442015-06-16 11:26:35 -0400410 case -NFS4ERR_DELEG_REVOKED:
411 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust272289a2016-09-22 13:39:15 -0400412 case -NFS4ERR_EXPIRED:
Trond Myklebust5ba12442015-06-16 11:26:35 -0400413 case -NFS4ERR_BAD_STATEID:
Trond Myklebust272289a2016-09-22 13:39:15 -0400414 if (inode != NULL && stateid != NULL) {
415 nfs_inode_find_state_and_recover(inode,
416 stateid);
417 goto wait_on_recovery;
418 }
419 case -NFS4ERR_OPENMODE:
Trond Myklebust8487c472016-06-26 08:44:35 -0400420 if (inode) {
421 int err;
422
423 err = nfs_async_inode_return_delegation(inode,
424 stateid);
425 if (err == 0)
426 goto wait_on_recovery;
427 if (stateid != NULL && stateid->type == NFS4_DELEGATION_STATEID_TYPE) {
428 exception->retry = 1;
429 break;
430 }
431 }
Trond Myklebust3114ea72012-03-07 16:39:06 -0500432 if (state == NULL)
433 break;
Trond Myklebust5d422302013-03-14 16:57:48 -0400434 ret = nfs4_schedule_stateid_recovery(server, state);
435 if (ret < 0)
436 break;
Trond Myklebust3114ea72012-03-07 16:39:06 -0500437 goto wait_on_recovery;
Trond Myklebust65de8722008-12-23 15:21:44 -0500438 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500439 case -NFS4ERR_STALE_CLIENTID:
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500440 nfs4_schedule_lease_recovery(clp);
441 goto wait_on_recovery;
Chuck Lever519ae252013-10-17 14:13:19 -0400442 case -NFS4ERR_MOVED:
443 ret = nfs4_schedule_migration_recovery(server);
444 if (ret < 0)
445 break;
446 goto wait_on_recovery;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -0400447 case -NFS4ERR_LEASE_MOVED:
448 nfs4_schedule_lease_moved_recovery(clp);
449 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500450#if defined(CONFIG_NFS_V4_1)
Andy Adamson4745e312009-04-01 09:22:42 -0400451 case -NFS4ERR_BADSESSION:
452 case -NFS4ERR_BADSLOT:
453 case -NFS4ERR_BAD_HIGH_SLOT:
454 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
455 case -NFS4ERR_DEADSESSION:
456 case -NFS4ERR_SEQ_FALSE_RETRY:
457 case -NFS4ERR_SEQ_MISORDERED:
458 dprintk("%s ERROR: %d Reset session\n", __func__,
459 errorcode);
Trond Myklebust9f594792012-05-27 13:02:53 -0400460 nfs4_schedule_session_recovery(clp->cl_session, errorcode);
Bryan Schumaker399f11c2012-10-30 16:06:35 -0400461 goto wait_on_recovery;
Trond Myklebust03391692010-01-26 15:42:38 -0500462#endif /* defined(CONFIG_NFS_V4_1) */
Trond Myklebust65de8722008-12-23 15:21:44 -0500463 case -NFS4ERR_FILE_OPEN:
NeilBrown44ed3552009-12-03 15:58:56 -0500464 if (exception->timeout > HZ) {
465 /* We have retried a decent amount, time to
466 * fail
467 */
468 ret = -EBUSY;
469 break;
470 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500471 case -NFS4ERR_DELAY:
Trond Myklebust2598ed32015-09-20 16:10:18 -0400472 nfs_inc_server_stats(server, NFSIOS_DELAY);
473 case -NFS4ERR_GRACE:
Trond Myklebuste85d7ee2016-07-14 18:46:24 -0400474 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -0400475 case -NFS4ERR_RECALLCONFLICT:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400476 exception->delay = 1;
477 return 0;
478
Andy Adamsona8a4ae32011-05-03 13:43:03 -0400479 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust65de8722008-12-23 15:21:44 -0500480 case -NFS4ERR_OLD_STATEID:
481 exception->retry = 1;
Trond Myklebustb064eca22011-02-22 15:44:32 -0800482 break;
483 case -NFS4ERR_BADOWNER:
484 /* The following works around a Linux server bug! */
485 case -NFS4ERR_BADNAME:
486 if (server->caps & NFS_CAP_UIDGID_NOMAP) {
487 server->caps &= ~NFS_CAP_UIDGID_NOMAP;
488 exception->retry = 1;
489 printk(KERN_WARNING "NFS: v4 server %s "
490 "does not accept raw "
491 "uid/gids. "
492 "Reenabling the idmapper.\n",
493 server->nfs_client->cl_hostname);
494 }
Trond Myklebust65de8722008-12-23 15:21:44 -0500495 }
496 /* We failed to handle the error */
497 return nfs4_map_errors(ret);
Trond Myklebust0400a6b2011-03-09 16:00:53 -0500498wait_on_recovery:
Trond Myklebustb3c2aa02015-09-20 14:32:45 -0400499 exception->recovering = 1;
500 return 0;
501}
502
503/* This is the error handling routine for processes that are allowed
504 * to sleep.
505 */
506int nfs4_handle_exception(struct nfs_server *server, int errorcode, struct nfs4_exception *exception)
507{
508 struct nfs_client *clp = server->nfs_client;
509 int ret;
510
511 ret = nfs4_do_handle_exception(server, errorcode, exception);
512 if (exception->delay) {
513 ret = nfs4_delay(server->client, &exception->timeout);
514 goto out_retry;
515 }
516 if (exception->recovering) {
517 ret = nfs4_wait_clnt_recover(clp);
518 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
519 return -EIO;
520 goto out_retry;
521 }
522 return ret;
523out_retry:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -0500524 if (ret == 0)
525 exception->retry = 1;
526 return ret;
Trond Myklebust65de8722008-12-23 15:21:44 -0500527}
528
Trond Myklebust037fc982015-09-20 15:51:00 -0400529static int
530nfs4_async_handle_exception(struct rpc_task *task, struct nfs_server *server,
531 int errorcode, struct nfs4_exception *exception)
532{
533 struct nfs_client *clp = server->nfs_client;
534 int ret;
535
536 ret = nfs4_do_handle_exception(server, errorcode, exception);
537 if (exception->delay) {
538 rpc_delay(task, nfs4_update_delay(&exception->timeout));
539 goto out_retry;
540 }
541 if (exception->recovering) {
542 rpc_sleep_on(&clp->cl_rpcwaitq, task, NULL);
543 if (test_bit(NFS4CLNT_MANAGER_RUNNING, &clp->cl_state) == 0)
544 rpc_wake_up_queued_task(&clp->cl_rpcwaitq, task);
545 goto out_retry;
546 }
547 if (test_bit(NFS_MIG_FAILED, &server->mig_status))
548 ret = -EIO;
549 return ret;
550out_retry:
551 if (ret == 0)
552 exception->retry = 1;
553 return ret;
554}
555
556static int
557nfs4_async_handle_error(struct rpc_task *task, struct nfs_server *server,
558 struct nfs4_state *state, long *timeout)
559{
560 struct nfs4_exception exception = {
561 .state = state,
562 };
563
564 if (task->tk_status >= 0)
565 return 0;
566 if (timeout)
567 exception.timeout = *timeout;
568 task->tk_status = nfs4_async_handle_exception(task, server,
569 task->tk_status,
570 &exception);
571 if (exception.delay && timeout)
572 *timeout = exception.timeout;
573 if (exception.retry)
574 return -EAGAIN;
575 return 0;
576}
577
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400578/*
579 * Return 'true' if 'clp' is using an rpc_client that is integrity protected
580 * or 'false' otherwise.
581 */
582static bool _nfs4_is_integrity_protected(struct nfs_client *clp)
583{
584 rpc_authflavor_t flavor = clp->cl_rpcclient->cl_auth->au_flavor;
Anna Schumakereeea5362017-01-11 16:01:21 -0500585 return (flavor == RPC_AUTH_GSS_KRB5I) || (flavor == RPC_AUTH_GSS_KRB5P);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -0400586}
Trond Myklebust65de8722008-12-23 15:21:44 -0500587
Trond Myklebust452e9352010-07-31 14:29:06 -0400588static void do_renew_lease(struct nfs_client *clp, unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590 spin_lock(&clp->cl_lock);
591 if (time_before(clp->cl_last_renewal,timestamp))
592 clp->cl_last_renewal = timestamp;
593 spin_unlock(&clp->cl_lock);
594}
595
Trond Myklebust452e9352010-07-31 14:29:06 -0400596static void renew_lease(const struct nfs_server *server, unsigned long timestamp)
597{
Trond Myklebustbe824162015-07-05 14:50:46 -0400598 struct nfs_client *clp = server->nfs_client;
599
600 if (!nfs4_has_session(clp))
601 do_renew_lease(clp, timestamp);
Trond Myklebust452e9352010-07-31 14:29:06 -0400602}
603
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400604struct nfs4_call_sync_data {
605 const struct nfs_server *seq_server;
606 struct nfs4_sequence_args *seq_args;
607 struct nfs4_sequence_res *seq_res;
608};
609
Trond Myklebustbe3a5d22015-06-23 19:51:55 +0800610void nfs4_init_sequence(struct nfs4_sequence_args *args,
611 struct nfs4_sequence_res *res, int cache_reply)
Chuck Levera9c92d62013-08-09 12:48:18 -0400612{
613 args->sa_slot = NULL;
614 args->sa_cache_this = cache_reply;
615 args->sa_privileged = 0;
616
617 res->sr_slot = NULL;
618}
619
620static void nfs4_set_sequence_privileged(struct nfs4_sequence_args *args)
621{
622 args->sa_privileged = 1;
623}
624
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400625static void nfs40_sequence_free_slot(struct nfs4_sequence_res *res)
Chuck Lever3bd23842013-08-09 12:49:19 -0400626{
627 struct nfs4_slot *slot = res->sr_slot;
628 struct nfs4_slot_table *tbl;
629
Chuck Lever3bd23842013-08-09 12:49:19 -0400630 tbl = slot->table;
631 spin_lock(&tbl->slot_tbl_lock);
632 if (!nfs41_wake_and_assign_slot(tbl, slot))
633 nfs4_free_slot(tbl, slot);
634 spin_unlock(&tbl->slot_tbl_lock);
635
636 res->sr_slot = NULL;
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400637}
638
639static int nfs40_sequence_done(struct rpc_task *task,
640 struct nfs4_sequence_res *res)
641{
642 if (res->sr_slot != NULL)
643 nfs40_sequence_free_slot(res);
Chuck Lever3bd23842013-08-09 12:49:19 -0400644 return 1;
645}
646
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400647#if defined(CONFIG_NFS_V4_1)
648
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400649static void nfs41_release_slot(struct nfs4_slot *slot)
Andy Adamson13615872009-04-01 09:22:17 -0400650{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500651 struct nfs4_session *session;
Andy Adamson13615872009-04-01 09:22:17 -0400652 struct nfs4_slot_table *tbl;
Trond Myklebustc10e4492012-11-26 16:16:54 -0500653 bool send_new_highest_used_slotid = false;
Andy Adamson13615872009-04-01 09:22:17 -0400654
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400655 if (!slot)
656 return;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500657 tbl = slot->table;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500658 session = tbl->session;
Andy Adamsonea028ac2009-12-04 15:55:38 -0500659
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400660 /* Bump the slot sequence number */
661 if (slot->seq_done)
662 slot->seq_nr++;
663 slot->seq_done = 0;
664
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500665 spin_lock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500666 /* Be nice to the server: try to ensure that the last transmitted
667 * value for highest_user_slotid <= target_highest_slotid
668 */
669 if (tbl->highest_used_slotid > tbl->target_highest_slotid)
670 send_new_highest_used_slotid = true;
671
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500672 if (nfs41_wake_and_assign_slot(tbl, slot)) {
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500673 send_new_highest_used_slotid = false;
674 goto out_unlock;
675 }
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500676 nfs4_free_slot(tbl, slot);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500677
678 if (tbl->highest_used_slotid != NFS4_NO_SLOT)
679 send_new_highest_used_slotid = false;
Trond Myklebustb75ad4c2012-11-29 17:27:47 -0500680out_unlock:
Trond Myklebust35dc1d72009-12-05 19:32:19 -0500681 spin_unlock(&tbl->slot_tbl_lock);
Trond Myklebustc10e4492012-11-26 16:16:54 -0500682 if (send_new_highest_used_slotid)
Anna Schumaker3f10a6a2015-07-13 14:01:31 -0400683 nfs41_notify_server(session->clp);
Trond Myklebust045d2a62016-08-28 13:25:43 -0400684 if (waitqueue_active(&tbl->slot_waitq))
685 wake_up_all(&tbl->slot_waitq);
Andy Adamson13615872009-04-01 09:22:17 -0400686}
687
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400688static void nfs41_sequence_free_slot(struct nfs4_sequence_res *res)
689{
690 nfs41_release_slot(res->sr_slot);
691 res->sr_slot = NULL;
692}
693
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400694static int nfs41_sequence_process(struct rpc_task *task,
695 struct nfs4_sequence_res *res)
Andy Adamsonb0df8062009-04-01 09:22:18 -0400696{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500697 struct nfs4_session *session;
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500698 struct nfs4_slot *slot = res->sr_slot;
Trond Myklebust14516c32010-07-31 14:29:06 -0400699 struct nfs_client *clp;
Trond Myklebustac20d162012-12-15 15:36:07 -0500700 bool interrupted = false;
Trond Myklebust85563072012-12-11 10:31:12 -0500701 int ret = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400702
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500703 if (slot == NULL)
704 goto out_noaction;
Bryan Schumaker468f8612011-04-18 15:57:32 -0400705 /* don't increment the sequence number if the task wasn't sent */
706 if (!RPC_WAS_SENT(task))
Andy Adamsonb0df8062009-04-01 09:22:18 -0400707 goto out;
708
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500709 session = slot->table->session;
Trond Myklebust933602e2012-11-16 12:12:38 -0500710
Trond Myklebustac20d162012-12-15 15:36:07 -0500711 if (slot->interrupted) {
Olga Kornievskaia88bd4f82017-04-26 14:21:22 -0400712 if (res->sr_status != -NFS4ERR_DELAY)
713 slot->interrupted = 0;
Trond Myklebustac20d162012-12-15 15:36:07 -0500714 interrupted = true;
715 }
716
Trond Myklebust2f92ae32013-08-14 17:58:28 -0400717 trace_nfs4_sequence_done(session, res);
Andy Adamson691daf32009-12-04 15:55:39 -0500718 /* Check the SEQUENCE operation status */
Trond Myklebust14516c32010-07-31 14:29:06 -0400719 switch (res->sr_status) {
720 case 0:
Andy Adamsonb0df8062009-04-01 09:22:18 -0400721 /* Update the slot's sequence and clientid lease timer */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400722 slot->seq_done = 1;
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500723 clp = session->clp;
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500724 do_renew_lease(clp, res->sr_timestamp);
Alexandros Batsakis0629e372009-12-05 13:46:14 -0500725 /* Check sequence flags */
Trond Myklebust0a014a42016-09-22 13:38:51 -0400726 nfs41_handle_sequence_flag_errors(clp, res->sr_status_flags,
727 !!slot->privileged);
Trond Myklebust464ee9f2012-11-20 12:49:27 -0500728 nfs41_update_target_slotid(slot->table, slot, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400729 break;
Trond Myklebustac20d162012-12-15 15:36:07 -0500730 case 1:
731 /*
732 * sr_status remains 1 if an RPC level error occurred.
733 * The server may or may not have processed the sequence
734 * operation..
735 * Mark the slot as having hosted an interrupted RPC call.
736 */
737 slot->interrupted = 1;
738 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400739 case -NFS4ERR_DELAY:
740 /* The server detected a resend of the RPC call and
741 * returned NFS4ERR_DELAY as per Section 2.10.6.2
742 * of RFC5661.
743 */
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500744 dprintk("%s: slot=%u seq=%u: Operation in progress\n",
Benny Halevydfb4f3092010-09-24 09:17:01 -0400745 __func__,
Trond Myklebustdf2fabf2012-11-16 12:45:06 -0500746 slot->slot_nr,
Trond Myklebust933602e2012-11-16 12:12:38 -0500747 slot->seq_nr);
Trond Myklebust14516c32010-07-31 14:29:06 -0400748 goto out_retry;
Trond Myklebust85563072012-12-11 10:31:12 -0500749 case -NFS4ERR_BADSLOT:
750 /*
751 * The slot id we used was probably retired. Try again
752 * using a different slot id.
753 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400754 if (slot->seq_nr < slot->table->target_highest_slotid)
755 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500756 goto retry_nowait;
757 case -NFS4ERR_SEQ_MISORDERED:
758 /*
Trond Myklebustac20d162012-12-15 15:36:07 -0500759 * Was the last operation on this sequence interrupted?
760 * If so, retry after bumping the sequence number.
761 */
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400762 if (interrupted)
763 goto retry_new_seq;
Trond Myklebustac20d162012-12-15 15:36:07 -0500764 /*
Trond Myklebuste8794442012-12-15 13:56:18 -0500765 * Could this slot have been previously retired?
766 * If so, then the server may be expecting seq_nr = 1!
767 */
Trond Myklebust8e63b6a2012-12-15 15:21:52 -0500768 if (slot->seq_nr != 1) {
769 slot->seq_nr = 1;
770 goto retry_nowait;
771 }
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400772 goto session_recover;
Trond Myklebuste8794442012-12-15 13:56:18 -0500773 case -NFS4ERR_SEQ_FALSE_RETRY:
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400774 if (interrupted)
775 goto retry_new_seq;
776 goto session_recover;
Trond Myklebust14516c32010-07-31 14:29:06 -0400777 default:
778 /* Just update the slot sequence no. */
Trond Myklebust07e8dcb2016-08-28 10:28:25 -0400779 slot->seq_done = 1;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400780 }
781out:
782 /* The session may be reset by one of the error handlers. */
783 dprintk("%s: Error %d free the slot \n", __func__, res->sr_status);
Trond Myklebusta13ce7c2014-01-29 12:24:03 -0500784out_noaction:
Trond Myklebust85563072012-12-11 10:31:12 -0500785 return ret;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400786session_recover:
787 nfs4_schedule_session_recovery(session, res->sr_status);
788 goto retry_nowait;
789retry_new_seq:
790 ++slot->seq_nr;
Trond Myklebuste8794442012-12-15 13:56:18 -0500791retry_nowait:
792 if (rpc_restart_call_prepare(task)) {
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400793 nfs41_sequence_free_slot(res);
Trond Myklebuste8794442012-12-15 13:56:18 -0500794 task->tk_status = 0;
795 ret = 0;
796 }
797 goto out;
Trond Myklebust14516c32010-07-31 14:29:06 -0400798out_retry:
Trond Myklebustd05dd4e2010-07-31 14:29:07 -0400799 if (!rpc_restart_call(task))
Trond Myklebust14516c32010-07-31 14:29:06 -0400800 goto out;
801 rpc_delay(task, NFS4_POLL_RETRY_MAX);
802 return 0;
Andy Adamsonb0df8062009-04-01 09:22:18 -0400803}
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400804
805int nfs41_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
806{
807 if (!nfs41_sequence_process(task, res))
808 return 0;
809 if (res->sr_slot != NULL)
810 nfs41_sequence_free_slot(res);
811 return 1;
812
813}
Andy Adamsonf9c96fc2014-01-29 11:34:38 -0500814EXPORT_SYMBOL_GPL(nfs41_sequence_done);
Andy Adamsonb0df8062009-04-01 09:22:18 -0400815
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400816static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
817{
818 if (res->sr_slot == NULL)
819 return 1;
820 if (res->sr_slot->table->session != NULL)
821 return nfs41_sequence_process(task, res);
822 return nfs40_sequence_done(task, res);
823}
824
825static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
826{
827 if (res->sr_slot != NULL) {
828 if (res->sr_slot->table->session != NULL)
829 nfs41_sequence_free_slot(res);
830 else
831 nfs40_sequence_free_slot(res);
832 }
833}
834
Peng Tao2c4b1312014-06-11 05:24:16 +0800835int nfs4_sequence_done(struct rpc_task *task, struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400836{
Trond Myklebuste3725ec2012-11-16 12:25:01 -0500837 if (res->sr_slot == NULL)
Trond Myklebust14516c32010-07-31 14:29:06 -0400838 return 1;
Chuck Lever3bd23842013-08-09 12:49:19 -0400839 if (!res->sr_slot->table->session)
840 return nfs40_sequence_done(task, res);
Trond Myklebust14516c32010-07-31 14:29:06 -0400841 return nfs41_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400842}
Peng Tao2c4b1312014-06-11 05:24:16 +0800843EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Trond Myklebustdf896452010-06-16 09:52:26 -0400844
Andy Adamsonce5039c2009-04-01 09:22:13 -0400845static void nfs41_call_sync_prepare(struct rpc_task *task, void *calldata)
846{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400847 struct nfs4_call_sync_data *data = calldata;
Andy Adamsonce5039c2009-04-01 09:22:13 -0400848
Trond Myklebust035168ab2010-06-16 09:52:26 -0400849 dprintk("--> %s data->seq_server %p\n", __func__, data->seq_server);
850
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500851 nfs4_setup_sequence(data->seq_server->nfs_client,
852 data->seq_args, data->seq_res, task);
Andy Adamsonce5039c2009-04-01 09:22:13 -0400853}
854
Andy Adamson69ab40c2009-04-01 09:22:19 -0400855static void nfs41_call_sync_done(struct rpc_task *task, void *calldata)
856{
Chuck Lever2a3eb2b2013-08-09 12:48:00 -0400857 struct nfs4_call_sync_data *data = calldata;
Andy Adamson69ab40c2009-04-01 09:22:19 -0400858
Trond Myklebust14516c32010-07-31 14:29:06 -0400859 nfs41_sequence_done(task, data->seq_res);
Andy Adamson69ab40c2009-04-01 09:22:19 -0400860}
861
Trond Myklebust17280172012-03-11 13:11:00 -0400862static const struct rpc_call_ops nfs41_call_sync_ops = {
Andy Adamsonce5039c2009-04-01 09:22:13 -0400863 .rpc_call_prepare = nfs41_call_sync_prepare,
Andy Adamson69ab40c2009-04-01 09:22:19 -0400864 .rpc_call_done = nfs41_call_sync_done,
Andy Adamsonce5039c2009-04-01 09:22:13 -0400865};
866
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400867static void
868nfs4_sequence_process_interrupted(struct nfs_client *client,
869 struct nfs4_slot *slot, struct rpc_cred *cred)
870{
871 struct rpc_task *task;
872
873 task = _nfs41_proc_sequence(client, cred, slot, true);
874 if (!IS_ERR(task))
875 rpc_put_task_async(task);
876}
877
Chuck Lever3bd23842013-08-09 12:49:19 -0400878#else /* !CONFIG_NFS_V4_1 */
879
Trond Myklebust2e80dbe2016-08-28 11:50:26 -0400880static int nfs4_sequence_process(struct rpc_task *task, struct nfs4_sequence_res *res)
881{
882 return nfs40_sequence_done(task, res);
883}
884
885static void nfs4_sequence_free_slot(struct nfs4_sequence_res *res)
886{
887 if (res->sr_slot != NULL)
888 nfs40_sequence_free_slot(res);
889}
890
Peng Tao2c4b1312014-06-11 05:24:16 +0800891int nfs4_sequence_done(struct rpc_task *task,
892 struct nfs4_sequence_res *res)
Trond Myklebustdf896452010-06-16 09:52:26 -0400893{
Chuck Lever3bd23842013-08-09 12:49:19 -0400894 return nfs40_sequence_done(task, res);
Trond Myklebustdf896452010-06-16 09:52:26 -0400895}
Peng Tao2c4b1312014-06-11 05:24:16 +0800896EXPORT_SYMBOL_GPL(nfs4_sequence_done);
Chuck Lever3bd23842013-08-09 12:49:19 -0400897
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400898static void
899nfs4_sequence_process_interrupted(struct nfs_client *client,
900 struct nfs4_slot *slot, struct rpc_cred *cred)
901{
902 WARN_ON_ONCE(1);
903 slot->interrupted = 0;
904}
905
Chuck Lever3bd23842013-08-09 12:49:19 -0400906#endif /* !CONFIG_NFS_V4_1 */
Andy Adamsoncccef3b2009-04-01 09:22:03 -0400907
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400908static
909void nfs4_sequence_attach_slot(struct nfs4_sequence_args *args,
910 struct nfs4_sequence_res *res,
911 struct nfs4_slot *slot)
912{
913 if (!slot)
914 return;
915 slot->privileged = args->sa_privileged ? 1 : 0;
916 args->sa_slot = slot;
917
918 res->sr_slot = slot;
919 res->sr_timestamp = jiffies;
920 res->sr_status_flags = 0;
921 res->sr_status = 1;
922
923}
924
925int nfs4_setup_sequence(struct nfs_client *client,
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500926 struct nfs4_sequence_args *args,
927 struct nfs4_sequence_res *res,
928 struct rpc_task *task)
929{
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500930 struct nfs4_session *session = nfs4_get_session(client);
Anna Schumaker76ee0352017-01-10 16:49:31 -0500931 struct nfs4_slot_table *tbl = client->cl_slot_tbl;
Anna Schumaker3d358082017-01-11 10:54:04 -0500932 struct nfs4_slot *slot;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500933
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500934 /* slot already allocated? */
935 if (res->sr_slot != NULL)
936 goto out_start;
937
Anna Schumaker76ee0352017-01-10 16:49:31 -0500938 if (session) {
939 tbl = &session->fc_slot_table;
940 task->tk_timeout = 0;
941 }
942
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400943 for (;;) {
944 spin_lock(&tbl->slot_tbl_lock);
945 /* The state manager will wait until the slot table is empty */
946 if (nfs4_slot_tbl_draining(tbl) && !args->sa_privileged)
947 goto out_sleep;
Anna Schumaker6994cdd2017-01-10 16:13:27 -0500948
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400949 slot = nfs4_alloc_slot(tbl);
950 if (IS_ERR(slot)) {
951 /* Try again in 1/4 second */
952 if (slot == ERR_PTR(-ENOMEM))
953 task->tk_timeout = HZ >> 2;
954 goto out_sleep;
955 }
956 spin_unlock(&tbl->slot_tbl_lock);
957
958 if (likely(!slot->interrupted))
959 break;
960 nfs4_sequence_process_interrupted(client,
961 slot, task->tk_msg.rpc_cred);
Anna Schumaker3d358082017-01-11 10:54:04 -0500962 }
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500963
Trond Myklebust3be0f80b2017-10-19 15:46:45 -0400964 nfs4_sequence_attach_slot(args, res, slot);
Anna Schumaker3d358082017-01-11 10:54:04 -0500965
Anna Schumakerad05cc02017-01-11 13:37:06 -0500966 trace_nfs4_setup_sequence(session, args);
Anna Schumaker9dd9107f2017-01-10 12:01:46 -0500967out_start:
968 rpc_call_start(task);
969 return 0;
Anna Schumaker0dcee8b2017-01-10 16:29:54 -0500970
971out_sleep:
972 if (args->sa_privileged)
973 rpc_sleep_on_priority(&tbl->slot_tbl_waitq, task,
974 NULL, RPC_PRIORITY_PRIVILEGED);
975 else
976 rpc_sleep_on(&tbl->slot_tbl_waitq, task, NULL);
977 spin_unlock(&tbl->slot_tbl_lock);
978 return -EAGAIN;
Anna Schumaker7981c8a2017-01-10 11:39:53 -0500979}
980EXPORT_SYMBOL_GPL(nfs4_setup_sequence);
981
Chuck Lever9915ea72013-08-09 12:48:27 -0400982static void nfs40_call_sync_prepare(struct rpc_task *task, void *calldata)
983{
984 struct nfs4_call_sync_data *data = calldata;
Anna Schumaker42e1cca2017-01-09 15:48:22 -0500985 nfs4_setup_sequence(data->seq_server->nfs_client,
Chuck Lever9915ea72013-08-09 12:48:27 -0400986 data->seq_args, data->seq_res, task);
987}
988
989static void nfs40_call_sync_done(struct rpc_task *task, void *calldata)
990{
991 struct nfs4_call_sync_data *data = calldata;
992 nfs4_sequence_done(task, data->seq_res);
993}
994
995static const struct rpc_call_ops nfs40_call_sync_ops = {
996 .rpc_call_prepare = nfs40_call_sync_prepare,
997 .rpc_call_done = nfs40_call_sync_done,
998};
999
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001000static int nfs4_call_sync_sequence(struct rpc_clnt *clnt,
Trond Myklebustad389da2007-06-05 12:30:00 -04001001 struct nfs_server *server,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001002 struct rpc_message *msg,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001003 struct nfs4_sequence_args *args,
1004 struct nfs4_sequence_res *res)
Trond Myklebustad389da2007-06-05 12:30:00 -04001005{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001006 int ret;
1007 struct rpc_task *task;
Chuck Lever9915ea72013-08-09 12:48:27 -04001008 struct nfs_client *clp = server->nfs_client;
1009 struct nfs4_call_sync_data data = {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001010 .seq_server = server,
1011 .seq_args = args,
1012 .seq_res = res,
1013 };
1014 struct rpc_task_setup task_setup = {
1015 .rpc_client = clnt,
1016 .rpc_message = msg,
Chuck Lever9915ea72013-08-09 12:48:27 -04001017 .callback_ops = clp->cl_mvops->call_sync_ops,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001018 .callback_data = &data
1019 };
1020
1021 task = rpc_run_task(&task_setup);
1022 if (IS_ERR(task))
1023 ret = PTR_ERR(task);
1024 else {
1025 ret = task->tk_status;
Trond Myklebustad389da2007-06-05 12:30:00 -04001026 rpc_put_task(task);
1027 }
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001028 return ret;
1029}
1030
Bryan Schumaker7c513052011-03-24 17:12:24 +00001031int nfs4_call_sync(struct rpc_clnt *clnt,
1032 struct nfs_server *server,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001033 struct rpc_message *msg,
1034 struct nfs4_sequence_args *args,
1035 struct nfs4_sequence_res *res,
1036 int cache_reply)
1037{
Chuck Levera9c92d62013-08-09 12:48:18 -04001038 nfs4_init_sequence(args, res, cache_reply);
Chuck Lever9915ea72013-08-09 12:48:27 -04001039 return nfs4_call_sync_sequence(clnt, server, msg, args, res);
Bryan Schumakere73b83f2011-03-24 17:12:23 +00001040}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001041
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001042static void update_changeattr(struct inode *dir, struct nfs4_change_info *cinfo,
1043 unsigned long timestamp)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044{
1045 struct nfs_inode *nfsi = NFS_I(dir);
1046
1047 spin_lock(&dir->i_lock);
Trond Myklebust359d7d12012-05-28 10:01:34 -04001048 nfsi->cache_validity |= NFS_INO_INVALID_ATTR|NFS_INO_INVALID_DATA;
Jeff Layton1eb5d982018-01-09 08:21:17 -05001049 if (cinfo->atomic && cinfo->before == inode_peek_iversion_raw(dir)) {
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001050 nfsi->cache_validity &= ~NFS_INO_REVAL_PAGECACHE;
1051 nfsi->attrtimeo_timestamp = jiffies;
1052 } else {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 nfs_force_lookup_revalidate(dir);
Jeff Layton1eb5d982018-01-09 08:21:17 -05001054 if (cinfo->before != inode_peek_iversion_raw(dir))
Trond Myklebuste603a4c2016-12-16 16:55:55 -05001055 nfsi->cache_validity |= NFS_INO_INVALID_ACCESS |
1056 NFS_INO_INVALID_ACL;
1057 }
Jeff Layton1eb5d982018-01-09 08:21:17 -05001058 inode_set_iversion_raw(dir, cinfo->after);
Trond Myklebustd3129ef2017-01-11 22:07:28 -05001059 nfsi->read_cache_jiffies = timestamp;
Trond Myklebust3235b402015-02-26 19:52:06 -05001060 nfsi->attr_gencount = nfs_inc_attr_generation_counter();
David Howellsde242c02012-12-20 21:52:38 +00001061 nfs_fscache_invalidate(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 spin_unlock(&dir->i_lock);
1063}
1064
1065struct nfs4_opendata {
1066 struct kref kref;
1067 struct nfs_openargs o_arg;
1068 struct nfs_openres o_res;
1069 struct nfs_open_confirmargs c_arg;
1070 struct nfs_open_confirmres c_res;
Trond Myklebust6926afd12012-01-07 13:22:46 -05001071 struct nfs4_string owner_name;
1072 struct nfs4_string group_name;
Kinglong Meea49c2692015-07-27 15:31:38 +08001073 struct nfs4_label *a_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001074 struct nfs_fattr f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001075 struct nfs4_label *f_label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001076 struct dentry *dir;
Al Viro82a2c1b2011-06-22 18:30:55 -04001077 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 struct nfs4_state_owner *owner;
1079 struct nfs4_state *state;
1080 struct iattr attrs;
1081 unsigned long timestamp;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04001082 bool rpc_done;
1083 bool file_created;
1084 bool is_recover;
1085 bool cancelled;
Trond Myklebustdecf4912005-10-27 22:12:39 -04001086 int rpc_status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087};
Trond Myklebustdecf4912005-10-27 22:12:39 -04001088
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001089struct nfs4_open_createattrs {
1090 struct nfs4_label *label;
1091 struct iattr *sattr;
1092 const __u32 verf[2];
1093};
1094
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001095static bool nfs4_clear_cap_atomic_open_v1(struct nfs_server *server,
1096 int err, struct nfs4_exception *exception)
1097{
1098 if (err != -EINVAL)
1099 return false;
1100 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1101 return false;
1102 server->caps &= ~NFS_CAP_ATOMIC_OPEN_V1;
1103 exception->retry = 1;
1104 return true;
1105}
1106
Trond Myklebust6ae37332015-01-30 14:21:14 -05001107static u32
1108nfs4_map_atomic_open_share(struct nfs_server *server,
1109 fmode_t fmode, int openflags)
1110{
1111 u32 res = 0;
1112
1113 switch (fmode & (FMODE_READ | FMODE_WRITE)) {
1114 case FMODE_READ:
1115 res = NFS4_SHARE_ACCESS_READ;
1116 break;
1117 case FMODE_WRITE:
1118 res = NFS4_SHARE_ACCESS_WRITE;
1119 break;
1120 case FMODE_READ|FMODE_WRITE:
1121 res = NFS4_SHARE_ACCESS_BOTH;
1122 }
1123 if (!(server->caps & NFS_CAP_ATOMIC_OPEN_V1))
1124 goto out;
1125 /* Want no delegation if we're using O_DIRECT */
1126 if (openflags & O_DIRECT)
1127 res |= NFS4_SHARE_WANT_NO_DELEG;
1128out:
1129 return res;
1130}
1131
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001132static enum open_claim_type4
1133nfs4_map_atomic_open_claim(struct nfs_server *server,
1134 enum open_claim_type4 claim)
1135{
1136 if (server->caps & NFS_CAP_ATOMIC_OPEN_V1)
1137 return claim;
1138 switch (claim) {
1139 default:
1140 return claim;
1141 case NFS4_OPEN_CLAIM_FH:
1142 return NFS4_OPEN_CLAIM_NULL;
1143 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1144 return NFS4_OPEN_CLAIM_DELEGATE_CUR;
1145 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
1146 return NFS4_OPEN_CLAIM_DELEGATE_PREV;
1147 }
1148}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001149
1150static void nfs4_init_opendata_res(struct nfs4_opendata *p)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001151{
1152 p->o_res.f_attr = &p->f_attr;
David Quigley1775fd32013-05-22 12:50:42 -04001153 p->o_res.f_label = p->f_label;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001154 p->o_res.seqid = p->o_arg.seqid;
1155 p->c_res.seqid = p->c_arg.seqid;
1156 p->o_res.server = p->o_arg.server;
Andy Adamson5f657532012-10-03 02:39:34 -04001157 p->o_res.access_request = p->o_arg.access;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001158 nfs_fattr_init(&p->f_attr);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001159 nfs_fattr_init_names(&p->f_attr, &p->owner_name, &p->group_name);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001160}
1161
Al Viro82a2c1b2011-06-22 18:30:55 -04001162static struct nfs4_opendata *nfs4_opendata_alloc(struct dentry *dentry,
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001163 struct nfs4_state_owner *sp, fmode_t fmode, int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001164 const struct nfs4_open_createattrs *c,
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001165 enum open_claim_type4 claim,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001166 gfp_t gfp_mask)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001167{
Al Viro82a2c1b2011-06-22 18:30:55 -04001168 struct dentry *parent = dget_parent(dentry);
David Howells2b0143b2015-03-17 22:25:59 +00001169 struct inode *dir = d_inode(parent);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001170 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust63f5f792015-01-23 19:19:25 -05001171 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001172 struct nfs4_label *label = (c != NULL) ? c->label : NULL;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001173 struct nfs4_opendata *p;
1174
Trond Myklebust8535b2b2010-05-13 12:51:01 -04001175 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001176 if (p == NULL)
1177 goto err;
David Quigley14c43f72013-05-22 12:50:43 -04001178
1179 p->f_label = nfs4_label_alloc(server, gfp_mask);
1180 if (IS_ERR(p->f_label))
1181 goto err_free_p;
1182
Kinglong Meea49c2692015-07-27 15:31:38 +08001183 p->a_label = nfs4_label_alloc(server, gfp_mask);
1184 if (IS_ERR(p->a_label))
1185 goto err_free_f;
1186
Trond Myklebust63f5f792015-01-23 19:19:25 -05001187 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
1188 p->o_arg.seqid = alloc_seqid(&sp->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05001189 if (IS_ERR(p->o_arg.seqid))
David Quigley14c43f72013-05-22 12:50:43 -04001190 goto err_free_label;
Al Viro82a2c1b2011-06-22 18:30:55 -04001191 nfs_sb_active(dentry->d_sb);
1192 p->dentry = dget(dentry);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001193 p->dir = parent;
1194 p->owner = sp;
1195 atomic_inc(&sp->so_count);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001196 p->o_arg.open_flags = flags;
1197 p->o_arg.fmode = fmode & (FMODE_READ|FMODE_WRITE);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05001198 p->o_arg.umask = current_umask();
Trond Myklebust536585c2016-11-10 15:40:34 -05001199 p->o_arg.claim = nfs4_map_atomic_open_claim(server, claim);
Trond Myklebust6ae37332015-01-30 14:21:14 -05001200 p->o_arg.share_access = nfs4_map_atomic_open_share(server,
1201 fmode, flags);
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001202 /* don't put an ACCESS op in OPEN compound if O_EXCL, because ACCESS
1203 * will return permission denied for all bits until close */
1204 if (!(flags & O_EXCL)) {
1205 /* ask server to check for all possible rights as results
1206 * are cached */
Trond Myklebust536585c2016-11-10 15:40:34 -05001207 switch (p->o_arg.claim) {
1208 default:
1209 break;
1210 case NFS4_OPEN_CLAIM_NULL:
1211 case NFS4_OPEN_CLAIM_FH:
1212 p->o_arg.access = NFS4_ACCESS_READ |
1213 NFS4_ACCESS_MODIFY |
1214 NFS4_ACCESS_EXTEND |
1215 NFS4_ACCESS_EXECUTE;
1216 }
Weston Andros Adamsonae2bb032012-10-02 14:49:52 -07001217 }
David Howells7539bba2006-08-22 20:06:09 -04001218 p->o_arg.clientid = server->nfs_client->cl_clientid;
Trond Myklebust95b72eb2012-04-20 19:24:51 -04001219 p->o_arg.id.create_time = ktime_to_ns(sp->so_seqid.create_time);
1220 p->o_arg.id.uniquifier = sp->so_seqid.owner_id;
Al Viro82a2c1b2011-06-22 18:30:55 -04001221 p->o_arg.name = &dentry->d_name;
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001222 p->o_arg.server = server;
David Quigleyaa9c2662013-05-22 12:50:44 -04001223 p->o_arg.bitmask = nfs4_bitmask(server, label);
Trond Myklebust1549210f2012-06-05 09:16:47 -04001224 p->o_arg.open_bitmap = &nfs4_fattr_bitmap[0];
Kinglong Meea49c2692015-07-27 15:31:38 +08001225 p->o_arg.label = nfs4_label_copy(p->a_label, label);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04001226 switch (p->o_arg.claim) {
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001227 case NFS4_OPEN_CLAIM_NULL:
1228 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1229 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1230 p->o_arg.fh = NFS_FH(dir);
1231 break;
1232 case NFS4_OPEN_CLAIM_PREVIOUS:
1233 case NFS4_OPEN_CLAIM_FH:
1234 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
1235 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
David Howells2b0143b2015-03-17 22:25:59 +00001236 p->o_arg.fh = NFS_FH(d_inode(dentry));
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001237 }
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001238 if (c != NULL && c->sattr != NULL && c->sattr->ia_valid != 0) {
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001239 p->o_arg.u.attrs = &p->attrs;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001240 memcpy(&p->attrs, c->sattr, sizeof(p->attrs));
Chuck Levercd937102012-03-02 17:14:31 -05001241
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001242 memcpy(p->o_arg.u.verifier.data, c->verf,
Chuck Levercd937102012-03-02 17:14:31 -05001243 sizeof(p->o_arg.u.verifier.data));
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001244 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01001245 p->c_arg.fh = &p->o_res.fh;
1246 p->c_arg.stateid = &p->o_res.stateid;
1247 p->c_arg.seqid = p->o_arg.seqid;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001248 nfs4_init_opendata_res(p);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001249 kref_init(&p->kref);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001250 return p;
David Quigley14c43f72013-05-22 12:50:43 -04001251
1252err_free_label:
Kinglong Meea49c2692015-07-27 15:31:38 +08001253 nfs4_label_free(p->a_label);
1254err_free_f:
David Quigley14c43f72013-05-22 12:50:43 -04001255 nfs4_label_free(p->f_label);
1256err_free_p:
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001257 kfree(p);
1258err:
1259 dput(parent);
1260 return NULL;
1261}
1262
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001263static void nfs4_opendata_free(struct kref *kref)
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001264{
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001265 struct nfs4_opendata *p = container_of(kref,
1266 struct nfs4_opendata, kref);
Al Viro82a2c1b2011-06-22 18:30:55 -04001267 struct super_block *sb = p->dentry->d_sb;
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001268
1269 nfs_free_seqid(p->o_arg.seqid);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001270 nfs4_sequence_free_slot(&p->o_res.seq_res);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001271 if (p->state != NULL)
1272 nfs4_put_open_state(p->state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001273 nfs4_put_state_owner(p->owner);
David Quigley14c43f72013-05-22 12:50:43 -04001274
Kinglong Meea49c2692015-07-27 15:31:38 +08001275 nfs4_label_free(p->a_label);
David Quigley14c43f72013-05-22 12:50:43 -04001276 nfs4_label_free(p->f_label);
1277
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001278 dput(p->dir);
Al Viro82a2c1b2011-06-22 18:30:55 -04001279 dput(p->dentry);
1280 nfs_sb_deactive(sb);
Trond Myklebust6926afd12012-01-07 13:22:46 -05001281 nfs_fattr_free_names(&p->f_attr);
Trond Myklebuste911b812014-03-26 13:24:37 -07001282 kfree(p->f_attr.mdsthreshold);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001283 kfree(p);
1284}
1285
1286static void nfs4_opendata_put(struct nfs4_opendata *p)
1287{
1288 if (p != NULL)
1289 kref_put(&p->kref, nfs4_opendata_free);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01001290}
1291
Trond Myklebust24311f82015-09-20 10:50:17 -04001292static bool nfs4_mode_match_open_stateid(struct nfs4_state *state,
1293 fmode_t fmode)
1294{
1295 switch(fmode & (FMODE_READ|FMODE_WRITE)) {
1296 case FMODE_READ|FMODE_WRITE:
1297 return state->n_rdwr != 0;
1298 case FMODE_WRITE:
1299 return state->n_wronly != 0;
1300 case FMODE_READ:
1301 return state->n_rdonly != 0;
1302 }
1303 WARN_ON_ONCE(1);
1304 return false;
1305}
1306
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001307static int can_open_cached(struct nfs4_state *state, fmode_t mode, int open_mode)
Trond Myklebust6ee41262007-07-08 14:11:36 -04001308{
1309 int ret = 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001310
Trond Myklebust536e43d2012-01-17 22:04:26 -05001311 if (open_mode & (O_EXCL|O_TRUNC))
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001312 goto out;
1313 switch (mode & (FMODE_READ|FMODE_WRITE)) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001314 case FMODE_READ:
Trond Myklebust88069f72009-12-08 08:33:16 -05001315 ret |= test_bit(NFS_O_RDONLY_STATE, &state->flags) != 0
1316 && state->n_rdonly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001317 break;
1318 case FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001319 ret |= test_bit(NFS_O_WRONLY_STATE, &state->flags) != 0
1320 && state->n_wronly != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001321 break;
1322 case FMODE_READ|FMODE_WRITE:
Trond Myklebust88069f72009-12-08 08:33:16 -05001323 ret |= test_bit(NFS_O_RDWR_STATE, &state->flags) != 0
1324 && state->n_rdwr != 0;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001325 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001326out:
Trond Myklebust6ee41262007-07-08 14:11:36 -04001327 return ret;
1328}
1329
Trond Myklebust2a606182015-08-19 22:30:00 -05001330static int can_open_delegated(struct nfs_delegation *delegation, fmode_t fmode,
1331 enum open_claim_type4 claim)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001332{
Trond Myklebust652f89f2011-12-09 19:05:58 -05001333 if (delegation == NULL)
1334 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001335 if ((delegation->type & fmode) != fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001336 return 0;
Trond Myklebustd25be542013-02-05 11:43:28 -05001337 if (test_bit(NFS_DELEGATION_RETURNING, &delegation->flags))
1338 return 0;
Trond Myklebust2a606182015-08-19 22:30:00 -05001339 switch (claim) {
1340 case NFS4_OPEN_CLAIM_NULL:
1341 case NFS4_OPEN_CLAIM_FH:
1342 break;
1343 case NFS4_OPEN_CLAIM_PREVIOUS:
1344 if (!test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags))
1345 break;
1346 default:
1347 return 0;
1348 }
Trond Myklebustb7391f42008-12-23 15:21:52 -05001349 nfs_mark_delegation_referenced(delegation);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001350 return 1;
1351}
1352
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001353static void update_open_stateflags(struct nfs4_state *state, fmode_t fmode)
Trond Myklebuste7616922006-01-03 09:55:13 +01001354{
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001355 switch (fmode) {
Trond Myklebuste7616922006-01-03 09:55:13 +01001356 case FMODE_WRITE:
1357 state->n_wronly++;
1358 break;
1359 case FMODE_READ:
1360 state->n_rdonly++;
1361 break;
1362 case FMODE_READ|FMODE_WRITE:
1363 state->n_rdwr++;
1364 }
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001365 nfs4_state_set_mode_locked(state, state->state | fmode);
Trond Myklebuste7616922006-01-03 09:55:13 +01001366}
1367
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04001368#ifdef CONFIG_NFS_V4_1
1369static bool nfs_open_stateid_recover_openmode(struct nfs4_state *state)
1370{
1371 if (state->n_rdonly && !test_bit(NFS_O_RDONLY_STATE, &state->flags))
1372 return true;
1373 if (state->n_wronly && !test_bit(NFS_O_WRONLY_STATE, &state->flags))
1374 return true;
1375 if (state->n_rdwr && !test_bit(NFS_O_RDWR_STATE, &state->flags))
1376 return true;
1377 return false;
1378}
1379#endif /* CONFIG_NFS_V4_1 */
1380
Trond Myklebustc9399f22017-11-06 15:28:01 -05001381static void nfs_state_log_update_open_stateid(struct nfs4_state *state)
1382{
1383 if (test_and_clear_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
1384 wake_up_all(&state->waitq);
1385}
1386
1387static void nfs_state_log_out_of_order_open_stateid(struct nfs4_state *state,
1388 const nfs4_stateid *stateid)
1389{
1390 u32 state_seqid = be32_to_cpu(state->open_stateid.seqid);
1391 u32 stateid_seqid = be32_to_cpu(stateid->seqid);
1392
1393 if (stateid_seqid == state_seqid + 1U ||
1394 (stateid_seqid == 1U && state_seqid == 0xffffffffU))
1395 nfs_state_log_update_open_stateid(state);
1396 else
1397 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
1398}
1399
Trond Myklebust4f14c192014-02-12 19:15:06 -05001400static void nfs_test_and_clear_all_open_stateid(struct nfs4_state *state)
Trond Myklebust003707c2007-07-05 18:07:55 -04001401{
Trond Myklebust4f14c192014-02-12 19:15:06 -05001402 struct nfs_client *clp = state->owner->so_server->nfs_client;
1403 bool need_recover = false;
1404
1405 if (test_and_clear_bit(NFS_O_RDONLY_STATE, &state->flags) && state->n_rdonly)
1406 need_recover = true;
1407 if (test_and_clear_bit(NFS_O_WRONLY_STATE, &state->flags) && state->n_wronly)
1408 need_recover = true;
1409 if (test_and_clear_bit(NFS_O_RDWR_STATE, &state->flags) && state->n_rdwr)
1410 need_recover = true;
1411 if (need_recover)
1412 nfs4_state_mark_reclaim_nograce(clp, state);
1413}
1414
Trond Myklebustc9399f22017-11-06 15:28:01 -05001415/*
1416 * Check for whether or not the caller may update the open stateid
1417 * to the value passed in by stateid.
1418 *
1419 * Note: This function relies heavily on the server implementing
1420 * RFC7530 Section 9.1.4.2, and RFC5661 Section 8.2.2
1421 * correctly.
1422 * i.e. The stateid seqids have to be initialised to 1, and
1423 * are then incremented on every state transition.
1424 */
Trond Myklebuste999e802014-02-10 18:20:47 -05001425static bool nfs_need_update_open_stateid(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001426 const nfs4_stateid *stateid)
Trond Myklebuste999e802014-02-10 18:20:47 -05001427{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001428 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0 ||
1429 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
1430 if (stateid->seqid == cpu_to_be32(1))
1431 nfs_state_log_update_open_stateid(state);
1432 else
1433 set_bit(NFS_STATE_CHANGE_WAIT, &state->flags);
Trond Myklebuste999e802014-02-10 18:20:47 -05001434 return true;
Trond Myklebust4f14c192014-02-12 19:15:06 -05001435 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001436
1437 if (nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
1438 nfs_state_log_out_of_order_open_stateid(state, stateid);
Trond Myklebuste999e802014-02-10 18:20:47 -05001439 return true;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001440 }
Trond Myklebuste999e802014-02-10 18:20:47 -05001441 return false;
1442}
1443
Trond Myklebustf95549c2015-01-23 18:06:09 -05001444static void nfs_resync_open_stateid_locked(struct nfs4_state *state)
1445{
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001446 if (!(state->n_wronly || state->n_rdonly || state->n_rdwr))
1447 return;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001448 if (state->n_wronly)
1449 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1450 if (state->n_rdonly)
1451 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1452 if (state->n_rdwr)
1453 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust3c38cbe2015-07-22 13:46:13 -04001454 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebustf95549c2015-01-23 18:06:09 -05001455}
1456
Trond Myklebust226056c2014-02-11 10:41:07 -05001457static void nfs_clear_open_stateid_locked(struct nfs4_state *state,
1458 nfs4_stateid *stateid, fmode_t fmode)
1459{
1460 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1461 switch (fmode & (FMODE_READ|FMODE_WRITE)) {
1462 case FMODE_WRITE:
1463 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1464 break;
1465 case FMODE_READ:
1466 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1467 break;
1468 case 0:
1469 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
1470 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1471 clear_bit(NFS_OPEN_STATE, &state->flags);
1472 }
1473 if (stateid == NULL)
1474 return;
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001475 /* Handle OPEN+OPEN_DOWNGRADE races */
1476 if (nfs4_stateid_match_other(stateid, &state->open_stateid) &&
1477 !nfs4_stateid_is_newer(stateid, &state->open_stateid)) {
Trond Myklebustf95549c2015-01-23 18:06:09 -05001478 nfs_resync_open_stateid_locked(state);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001479 goto out;
Trond Myklebustf95549c2015-01-23 18:06:09 -05001480 }
Trond Myklebust003707c2007-07-05 18:07:55 -04001481 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001482 nfs4_stateid_copy(&state->stateid, stateid);
1483 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001484 trace_nfs4_open_stateid_update(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001485out:
1486 nfs_state_log_update_open_stateid(state);
Trond Myklebust226056c2014-02-11 10:41:07 -05001487}
1488
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07001489static void nfs_clear_open_stateid(struct nfs4_state *state,
1490 nfs4_stateid *arg_stateid,
1491 nfs4_stateid *stateid, fmode_t fmode)
Trond Myklebust226056c2014-02-11 10:41:07 -05001492{
1493 write_seqlock(&state->seqlock);
Trond Myklebust3e7dfb12016-11-14 11:19:55 -05001494 /* Ignore, if the CLOSE argment doesn't match the current stateid */
1495 if (nfs4_state_match_open_stateid_other(state, arg_stateid))
1496 nfs_clear_open_stateid_locked(state, stateid, fmode);
Trond Myklebust226056c2014-02-11 10:41:07 -05001497 write_sequnlock(&state->seqlock);
Trond Myklebust4f14c192014-02-12 19:15:06 -05001498 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
1499 nfs4_schedule_state_manager(state->owner->so_server->nfs_client);
Trond Myklebust226056c2014-02-11 10:41:07 -05001500}
1501
Trond Myklebust1393d962016-09-22 13:39:13 -04001502static void nfs_set_open_stateid_locked(struct nfs4_state *state,
Trond Myklebustc9399f22017-11-06 15:28:01 -05001503 const nfs4_stateid *stateid, nfs4_stateid *freeme)
Trond Myklebust003707c2007-07-05 18:07:55 -04001504{
Trond Myklebustc9399f22017-11-06 15:28:01 -05001505 DEFINE_WAIT(wait);
1506 int status = 0;
1507 for (;;) {
1508
1509 if (!nfs_need_update_open_stateid(state, stateid))
1510 return;
1511 if (!test_bit(NFS_STATE_CHANGE_WAIT, &state->flags))
Trond Myklebust003707c2007-07-05 18:07:55 -04001512 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001513 if (status)
Trond Myklebust003707c2007-07-05 18:07:55 -04001514 break;
Trond Myklebustc9399f22017-11-06 15:28:01 -05001515 /* Rely on seqids for serialisation with NFSv4.0 */
1516 if (!nfs4_has_session(NFS_SERVER(state->inode)->nfs_client))
1517 break;
1518
1519 prepare_to_wait(&state->waitq, &wait, TASK_KILLABLE);
1520 /*
1521 * Ensure we process the state changes in the same order
1522 * in which the server processed them by delaying the
1523 * update of the stateid until we are in sequence.
1524 */
1525 write_sequnlock(&state->seqlock);
1526 spin_unlock(&state->owner->so_lock);
1527 rcu_read_unlock();
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001528 trace_nfs4_open_stateid_update_wait(state->inode, stateid, 0);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001529 if (!signal_pending(current)) {
1530 if (schedule_timeout(5*HZ) == 0)
1531 status = -EAGAIN;
1532 else
1533 status = 0;
1534 } else
1535 status = -EINTR;
1536 finish_wait(&state->waitq, &wait);
1537 rcu_read_lock();
1538 spin_lock(&state->owner->so_lock);
1539 write_seqlock(&state->seqlock);
Trond Myklebust003707c2007-07-05 18:07:55 -04001540 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001541
Trond Myklebuste1fff5d2017-11-07 13:10:46 -05001542 if (test_bit(NFS_OPEN_STATE, &state->flags) &&
1543 !nfs4_stateid_match_other(stateid, &state->open_stateid)) {
Trond Myklebustc9399f22017-11-06 15:28:01 -05001544 nfs4_stateid_copy(freeme, &state->open_stateid);
1545 nfs_test_and_clear_all_open_stateid(state);
1546 }
1547
Trond Myklebuste999e802014-02-10 18:20:47 -05001548 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
1549 nfs4_stateid_copy(&state->stateid, stateid);
1550 nfs4_stateid_copy(&state->open_stateid, stateid);
Trond Myklebustad9e02d2017-11-06 15:28:02 -05001551 trace_nfs4_open_stateid_update(state->inode, stateid, status);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001552 nfs_state_log_update_open_stateid(state);
Trond Myklebust003707c2007-07-05 18:07:55 -04001553}
1554
Trond Myklebustc9399f22017-11-06 15:28:01 -05001555static void nfs_state_set_open_stateid(struct nfs4_state *state,
Trond Myklebust1393d962016-09-22 13:39:13 -04001556 const nfs4_stateid *open_stateid,
Trond Myklebust1393d962016-09-22 13:39:13 -04001557 fmode_t fmode,
1558 nfs4_stateid *freeme)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559{
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001560 /*
1561 * Protect the call to nfs4_state_set_mode_locked and
1562 * serialise the stateid update
1563 */
1564 write_seqlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001565 nfs_set_open_stateid_locked(state, open_stateid, freeme);
1566 switch (fmode) {
1567 case FMODE_READ:
1568 set_bit(NFS_O_RDONLY_STATE, &state->flags);
1569 break;
1570 case FMODE_WRITE:
1571 set_bit(NFS_O_WRONLY_STATE, &state->flags);
1572 break;
1573 case FMODE_READ|FMODE_WRITE:
1574 set_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04001575 }
Trond Myklebustc9399f22017-11-06 15:28:01 -05001576 set_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001577 write_sequnlock(&state->seqlock);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001578}
1579
1580static void nfs_state_set_delegation(struct nfs4_state *state,
1581 const nfs4_stateid *deleg_stateid,
1582 fmode_t fmode)
1583{
1584 /*
1585 * Protect the call to nfs4_state_set_mode_locked and
1586 * serialise the stateid update
1587 */
1588 write_seqlock(&state->seqlock);
1589 nfs4_stateid_copy(&state->stateid, deleg_stateid);
1590 set_bit(NFS_DELEGATED_STATE, &state->flags);
1591 write_sequnlock(&state->seqlock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592}
1593
Trond Myklebust1393d962016-09-22 13:39:13 -04001594static int update_open_stateid(struct nfs4_state *state,
1595 const nfs4_stateid *open_stateid,
1596 const nfs4_stateid *delegation,
1597 fmode_t fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001598{
Trond Myklebust1393d962016-09-22 13:39:13 -04001599 struct nfs_server *server = NFS_SERVER(state->inode);
1600 struct nfs_client *clp = server->nfs_client;
Trond Myklebust34310432008-12-23 15:21:38 -05001601 struct nfs_inode *nfsi = NFS_I(state->inode);
1602 struct nfs_delegation *deleg_cur;
Arnd Bergmann83aa3e02016-10-18 17:21:30 +02001603 nfs4_stateid freeme = { };
Trond Myklebust34310432008-12-23 15:21:38 -05001604 int ret = 0;
1605
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001606 fmode &= (FMODE_READ|FMODE_WRITE);
Trond Myklebust34310432008-12-23 15:21:38 -05001607
1608 rcu_read_lock();
Trond Myklebustc9399f22017-11-06 15:28:01 -05001609 spin_lock(&state->owner->so_lock);
1610 if (open_stateid != NULL) {
1611 nfs_state_set_open_stateid(state, open_stateid, fmode, &freeme);
1612 ret = 1;
1613 }
1614
Trond Myklebust34310432008-12-23 15:21:38 -05001615 deleg_cur = rcu_dereference(nfsi->delegation);
1616 if (deleg_cur == NULL)
1617 goto no_delegation;
1618
1619 spin_lock(&deleg_cur->lock);
Trond Myklebust17f26b12013-08-21 15:48:42 -04001620 if (rcu_dereference(nfsi->delegation) != deleg_cur ||
Trond Myklebustd25be542013-02-05 11:43:28 -05001621 test_bit(NFS_DELEGATION_RETURNING, &deleg_cur->flags) ||
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001622 (deleg_cur->type & fmode) != fmode)
Trond Myklebust34310432008-12-23 15:21:38 -05001623 goto no_delegation_unlock;
1624
1625 if (delegation == NULL)
1626 delegation = &deleg_cur->stateid;
Trond Myklebustf597c532012-03-04 18:13:56 -05001627 else if (!nfs4_stateid_match(&deleg_cur->stateid, delegation))
Trond Myklebust34310432008-12-23 15:21:38 -05001628 goto no_delegation_unlock;
1629
Trond Myklebustb7391f42008-12-23 15:21:52 -05001630 nfs_mark_delegation_referenced(deleg_cur);
Trond Myklebustc9399f22017-11-06 15:28:01 -05001631 nfs_state_set_delegation(state, &deleg_cur->stateid, fmode);
Trond Myklebust34310432008-12-23 15:21:38 -05001632 ret = 1;
1633no_delegation_unlock:
1634 spin_unlock(&deleg_cur->lock);
1635no_delegation:
Trond Myklebustc9399f22017-11-06 15:28:01 -05001636 if (ret)
1637 update_open_stateflags(state, fmode);
1638 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001639 rcu_read_unlock();
1640
Trond Myklebust4f14c192014-02-12 19:15:06 -05001641 if (test_bit(NFS_STATE_RECLAIM_NOGRACE, &state->flags))
Trond Myklebust1393d962016-09-22 13:39:13 -04001642 nfs4_schedule_state_manager(clp);
1643 if (freeme.type != 0)
1644 nfs4_test_and_free_stateid(server, &freeme,
1645 state->owner->so_cred);
Trond Myklebust34310432008-12-23 15:21:38 -05001646
1647 return ret;
1648}
1649
Trond Myklebust39071e62015-01-24 15:07:56 -05001650static bool nfs4_update_lock_stateid(struct nfs4_lock_state *lsp,
1651 const nfs4_stateid *stateid)
1652{
1653 struct nfs4_state *state = lsp->ls_state;
1654 bool ret = false;
1655
1656 spin_lock(&state->state_lock);
1657 if (!nfs4_stateid_match_other(stateid, &lsp->ls_stateid))
1658 goto out_noupdate;
1659 if (!nfs4_stateid_is_newer(stateid, &lsp->ls_stateid))
1660 goto out_noupdate;
1661 nfs4_stateid_copy(&lsp->ls_stateid, stateid);
1662 ret = true;
1663out_noupdate:
1664 spin_unlock(&state->state_lock);
1665 return ret;
1666}
Trond Myklebust34310432008-12-23 15:21:38 -05001667
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001668static void nfs4_return_incompatible_delegation(struct inode *inode, fmode_t fmode)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001669{
1670 struct nfs_delegation *delegation;
1671
Trond Myklebustf5086242018-03-20 16:43:13 -04001672 fmode &= FMODE_READ|FMODE_WRITE;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001673 rcu_read_lock();
1674 delegation = rcu_dereference(NFS_I(inode)->delegation);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001675 if (delegation == NULL || (delegation->type & fmode) == fmode) {
Trond Myklebustaac00a82007-07-05 19:02:21 -04001676 rcu_read_unlock();
1677 return;
1678 }
1679 rcu_read_unlock();
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04001680 nfs4_inode_return_delegation(inode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001681}
1682
Trond Myklebust6ee41262007-07-08 14:11:36 -04001683static struct nfs4_state *nfs4_try_open_cached(struct nfs4_opendata *opendata)
Trond Myklebustaac00a82007-07-05 19:02:21 -04001684{
1685 struct nfs4_state *state = opendata->state;
1686 struct nfs_inode *nfsi = NFS_I(state->inode);
1687 struct nfs_delegation *delegation;
Trond Myklebustf448bad2013-05-29 15:36:40 -04001688 int open_mode = opendata->o_arg.open_flags;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001689 fmode_t fmode = opendata->o_arg.fmode;
Trond Myklebust2a606182015-08-19 22:30:00 -05001690 enum open_claim_type4 claim = opendata->o_arg.claim;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001691 nfs4_stateid stateid;
1692 int ret = -EAGAIN;
1693
Trond Myklebustaac00a82007-07-05 19:02:21 -04001694 for (;;) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001695 spin_lock(&state->owner->so_lock);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001696 if (can_open_cached(state, fmode, open_mode)) {
Anna Schumaker61beef72014-09-03 14:15:40 -04001697 update_open_stateflags(state, fmode);
Trond Myklebust6ee41262007-07-08 14:11:36 -04001698 spin_unlock(&state->owner->so_lock);
Anna Schumaker61beef72014-09-03 14:15:40 -04001699 goto out_return_state;
Trond Myklebust6ee41262007-07-08 14:11:36 -04001700 }
Anna Schumaker61beef72014-09-03 14:15:40 -04001701 spin_unlock(&state->owner->so_lock);
Trond Myklebust34310432008-12-23 15:21:38 -05001702 rcu_read_lock();
1703 delegation = rcu_dereference(nfsi->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05001704 if (!can_open_delegated(delegation, fmode, claim)) {
Trond Myklebust34310432008-12-23 15:21:38 -05001705 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04001706 break;
Trond Myklebust34310432008-12-23 15:21:38 -05001707 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001708 /* Save the delegation */
Trond Myklebustf597c532012-03-04 18:13:56 -05001709 nfs4_stateid_copy(&stateid, &delegation->stateid);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001710 rcu_read_unlock();
Trond Myklebustfa332942013-04-09 12:56:52 -04001711 nfs_release_seqid(opendata->o_arg.seqid);
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04001712 if (!opendata->is_recover) {
1713 ret = nfs_may_open(state->inode, state->owner->so_cred, open_mode);
1714 if (ret != 0)
1715 goto out;
1716 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001717 ret = -EAGAIN;
Trond Myklebust34310432008-12-23 15:21:38 -05001718
1719 /* Try to update the stateid using the delegation */
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001720 if (update_open_stateid(state, NULL, &stateid, fmode))
Trond Myklebust34310432008-12-23 15:21:38 -05001721 goto out_return_state;
Trond Myklebustaac00a82007-07-05 19:02:21 -04001722 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04001723out:
1724 return ERR_PTR(ret);
1725out_return_state:
1726 atomic_inc(&state->count);
1727 return state;
1728}
1729
Andy Adamsone23008e2012-10-02 21:07:32 -04001730static void
1731nfs4_opendata_check_deleg(struct nfs4_opendata *data, struct nfs4_state *state)
1732{
1733 struct nfs_client *clp = NFS_SERVER(state->inode)->nfs_client;
1734 struct nfs_delegation *delegation;
1735 int delegation_flags = 0;
1736
1737 rcu_read_lock();
1738 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
1739 if (delegation)
1740 delegation_flags = delegation->flags;
1741 rcu_read_unlock();
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001742 switch (data->o_arg.claim) {
1743 default:
1744 break;
1745 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1746 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04001747 pr_err_ratelimited("NFS: Broken NFSv4 server %s is "
1748 "returning a delegation for "
1749 "OPEN(CLAIM_DELEGATE_CUR)\n",
1750 clp->cl_hostname);
Trond Myklebust72d79ff2015-10-02 11:44:54 -04001751 return;
1752 }
1753 if ((delegation_flags & 1UL<<NFS_DELEGATION_NEED_RECLAIM) == 0)
Andy Adamsone23008e2012-10-02 21:07:32 -04001754 nfs_inode_set_delegation(state->inode,
1755 data->owner->so_cred,
1756 &data->o_res);
1757 else
1758 nfs_inode_reclaim_delegation(state->inode,
1759 data->owner->so_cred,
1760 &data->o_res);
1761}
1762
1763/*
1764 * Check the inode attributes against the CLAIM_PREVIOUS returned attributes
1765 * and update the nfs4_state.
1766 */
1767static struct nfs4_state *
1768_nfs4_opendata_reclaim_to_nfs4_state(struct nfs4_opendata *data)
1769{
1770 struct inode *inode = data->state->inode;
1771 struct nfs4_state *state = data->state;
1772 int ret;
1773
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001774 if (!data->rpc_done) {
Anna Schumaker37a84842017-01-11 16:08:35 -05001775 if (data->rpc_status)
1776 return ERR_PTR(data->rpc_status);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001777 /* cached opens have already been processed */
1778 goto update;
Andy Adamsone23008e2012-10-02 21:07:32 -04001779 }
1780
Andy Adamsone23008e2012-10-02 21:07:32 -04001781 ret = nfs_refresh_inode(inode, &data->f_attr);
1782 if (ret)
Anna Schumaker37a84842017-01-11 16:08:35 -05001783 return ERR_PTR(ret);
Andy Adamsone23008e2012-10-02 21:07:32 -04001784
1785 if (data->o_res.delegation_type != 0)
1786 nfs4_opendata_check_deleg(data, state);
Weston Andros Adamsond2bfda22013-10-21 13:10:13 -04001787update:
Andy Adamsone23008e2012-10-02 21:07:32 -04001788 update_open_stateid(state, &data->o_res.stateid, NULL,
1789 data->o_arg.fmode);
Trond Myklebustd49f0422013-10-28 14:57:12 -04001790 atomic_inc(&state->count);
Andy Adamsone23008e2012-10-02 21:07:32 -04001791
1792 return state;
Andy Adamsone23008e2012-10-02 21:07:32 -04001793}
1794
Trond Myklebust4e2fcac2017-08-08 09:06:18 -04001795static struct inode *
1796nfs4_opendata_get_inode(struct nfs4_opendata *data)
1797{
1798 struct inode *inode;
1799
1800 switch (data->o_arg.claim) {
1801 case NFS4_OPEN_CLAIM_NULL:
1802 case NFS4_OPEN_CLAIM_DELEGATE_CUR:
1803 case NFS4_OPEN_CLAIM_DELEGATE_PREV:
1804 if (!(data->f_attr.valid & NFS_ATTR_FATTR))
1805 return ERR_PTR(-EAGAIN);
1806 inode = nfs_fhget(data->dir->d_sb, &data->o_res.fh,
1807 &data->f_attr, data->f_label);
1808 break;
1809 default:
1810 inode = d_inode(data->dentry);
1811 ihold(inode);
1812 nfs_refresh_inode(inode, &data->f_attr);
1813 }
1814 return inode;
1815}
1816
Andy Adamsone23008e2012-10-02 21:07:32 -04001817static struct nfs4_state *
Trond Myklebust75e8c482017-08-08 10:38:07 -04001818nfs4_opendata_find_nfs4_state(struct nfs4_opendata *data)
1819{
1820 struct nfs4_state *state;
1821 struct inode *inode;
1822
1823 inode = nfs4_opendata_get_inode(data);
1824 if (IS_ERR(inode))
1825 return ERR_CAST(inode);
1826 if (data->state != NULL && data->state->inode == inode) {
1827 state = data->state;
1828 atomic_inc(&state->count);
1829 } else
1830 state = nfs4_get_open_state(inode, data->owner);
1831 iput(inode);
1832 if (state == NULL)
1833 state = ERR_PTR(-ENOMEM);
1834 return state;
1835}
1836
Andy Adamsone23008e2012-10-02 21:07:32 -04001837static struct nfs4_state *
1838_nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001839{
Trond Myklebust75e8c482017-08-08 10:38:07 -04001840 struct nfs4_state *state;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001841
Trond Myklebustaac00a82007-07-05 19:02:21 -04001842 if (!data->rpc_done) {
Trond Myklebust6ee41262007-07-08 14:11:36 -04001843 state = nfs4_try_open_cached(data);
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05001844 trace_nfs4_cached_open(data->state);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001845 goto out;
1846 }
1847
Trond Myklebust75e8c482017-08-08 10:38:07 -04001848 state = nfs4_opendata_find_nfs4_state(data);
1849 if (IS_ERR(state))
1850 goto out;
1851
Andy Adamsone23008e2012-10-02 21:07:32 -04001852 if (data->o_res.delegation_type != 0)
1853 nfs4_opendata_check_deleg(data, state);
Trond Myklebust34310432008-12-23 15:21:38 -05001854 update_open_stateid(state, &data->o_res.stateid, NULL,
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001855 data->o_arg.fmode);
Trond Myklebustaac00a82007-07-05 19:02:21 -04001856out:
Trond Myklebust7aa262b52013-02-28 16:19:59 -08001857 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01001858 return state;
1859}
1860
Andy Adamsone23008e2012-10-02 21:07:32 -04001861static struct nfs4_state *
1862nfs4_opendata_to_nfs4_state(struct nfs4_opendata *data)
1863{
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001864 struct nfs4_state *ret;
1865
Andy Adamsone23008e2012-10-02 21:07:32 -04001866 if (data->o_arg.claim == NFS4_OPEN_CLAIM_PREVIOUS)
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04001867 ret =_nfs4_opendata_reclaim_to_nfs4_state(data);
1868 else
1869 ret = _nfs4_opendata_to_nfs4_state(data);
1870 nfs4_sequence_free_slot(&data->o_res.seq_res);
1871 return ret;
Andy Adamsone23008e2012-10-02 21:07:32 -04001872}
1873
Trond Myklebust864472e2006-01-03 09:55:15 +01001874static struct nfs_open_context *nfs4_state_find_open_context(struct nfs4_state *state)
1875{
1876 struct nfs_inode *nfsi = NFS_I(state->inode);
1877 struct nfs_open_context *ctx;
1878
1879 spin_lock(&state->inode->i_lock);
1880 list_for_each_entry(ctx, &nfsi->open_files, list) {
1881 if (ctx->state != state)
1882 continue;
1883 get_nfs_open_context(ctx);
1884 spin_unlock(&state->inode->i_lock);
1885 return ctx;
1886 }
1887 spin_unlock(&state->inode->i_lock);
1888 return ERR_PTR(-ENOENT);
1889}
1890
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001891static struct nfs4_opendata *nfs4_open_recoverdata_alloc(struct nfs_open_context *ctx,
1892 struct nfs4_state *state, enum open_claim_type4 claim)
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001893{
1894 struct nfs4_opendata *opendata;
1895
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001896 opendata = nfs4_opendata_alloc(ctx->dentry, state->owner, 0, 0,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05001897 NULL, claim, GFP_NOFS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001898 if (opendata == NULL)
1899 return ERR_PTR(-ENOMEM);
1900 opendata->state = state;
1901 atomic_inc(&state->count);
1902 return opendata;
1903}
1904
Trond Myklebust24311f82015-09-20 10:50:17 -04001905static int nfs4_open_recover_helper(struct nfs4_opendata *opendata,
1906 fmode_t fmode)
Trond Myklebust864472e2006-01-03 09:55:15 +01001907{
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001908 struct nfs4_state *newstate;
Trond Myklebust864472e2006-01-03 09:55:15 +01001909 int ret;
1910
Trond Myklebust24311f82015-09-20 10:50:17 -04001911 if (!nfs4_mode_match_open_stateid(opendata->state, fmode))
NeilBrown39f897f2015-06-29 14:28:54 +10001912 return 0;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001913 opendata->o_arg.open_flags = 0;
1914 opendata->o_arg.fmode = fmode;
Trond Myklebustbe36e182015-02-27 17:04:17 -05001915 opendata->o_arg.share_access = nfs4_map_atomic_open_share(
1916 NFS_SB(opendata->dentry->d_sb),
1917 fmode, 0);
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001918 memset(&opendata->o_res, 0, sizeof(opendata->o_res));
1919 memset(&opendata->c_res, 0, sizeof(opendata->c_res));
1920 nfs4_init_opendata_res(opendata);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08001921 ret = _nfs4_recover_proc_open(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01001922 if (ret != 0)
1923 return ret;
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001924 newstate = nfs4_opendata_to_nfs4_state(opendata);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04001925 if (IS_ERR(newstate))
1926 return PTR_ERR(newstate);
Trond Myklebust24311f82015-09-20 10:50:17 -04001927 if (newstate != opendata->state)
1928 ret = -ESTALE;
Al Viro643168c2011-06-22 18:20:23 -04001929 nfs4_close_state(newstate, fmode);
Trond Myklebust24311f82015-09-20 10:50:17 -04001930 return ret;
Trond Myklebust864472e2006-01-03 09:55:15 +01001931}
1932
1933static int nfs4_open_recover(struct nfs4_opendata *opendata, struct nfs4_state *state)
1934{
Trond Myklebust864472e2006-01-03 09:55:15 +01001935 int ret;
1936
Trond Myklebust4f14c192014-02-12 19:15:06 -05001937 /* Don't trigger recovery in nfs_test_and_clear_all_open_stateid */
1938 clear_bit(NFS_O_RDWR_STATE, &state->flags);
1939 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
1940 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001941 /* memory barrier prior to reading state->n_* */
Trond Myklebust2ced46c2007-07-03 23:48:13 -04001942 clear_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04001943 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust864472e2006-01-03 09:55:15 +01001944 smp_rmb();
Trond Myklebust24311f82015-09-20 10:50:17 -04001945 ret = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
1946 if (ret != 0)
1947 return ret;
1948 ret = nfs4_open_recover_helper(opendata, FMODE_WRITE);
1949 if (ret != 0)
1950 return ret;
1951 ret = nfs4_open_recover_helper(opendata, FMODE_READ);
1952 if (ret != 0)
1953 return ret;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001954 /*
1955 * We may have performed cached opens for all three recoveries.
1956 * Check if we need to update the current stateid.
1957 */
1958 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0 &&
Trond Myklebustf597c532012-03-04 18:13:56 -05001959 !nfs4_stateid_match(&state->stateid, &state->open_stateid)) {
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001960 write_seqlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001961 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0)
Trond Myklebustf597c532012-03-04 18:13:56 -05001962 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
Trond Myklebust8bda4e42007-07-09 10:45:42 -04001963 write_sequnlock(&state->seqlock);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001964 }
Trond Myklebust864472e2006-01-03 09:55:15 +01001965 return 0;
1966}
1967
Linus Torvalds1da177e2005-04-16 15:20:36 -07001968/*
1969 * OPEN_RECLAIM:
1970 * reclaim state on the server after a reboot.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001971 */
Trond Myklebust539cd032007-06-05 11:46:42 -04001972static int _nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973{
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001974 struct nfs_delegation *delegation;
Trond Myklebust864472e2006-01-03 09:55:15 +01001975 struct nfs4_opendata *opendata;
Trond Myklebustdc0b0272008-12-23 15:21:56 -05001976 fmode_t delegation_type = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001977 int status;
1978
Trond Myklebust4a1c0892013-03-15 14:57:33 -04001979 opendata = nfs4_open_recoverdata_alloc(ctx, state,
1980 NFS4_OPEN_CLAIM_PREVIOUS);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04001981 if (IS_ERR(opendata))
1982 return PTR_ERR(opendata);
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001983 rcu_read_lock();
1984 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust15c831b2008-12-23 15:21:39 -05001985 if (delegation != NULL && test_bit(NFS_DELEGATION_NEED_RECLAIM, &delegation->flags) != 0)
Trond Myklebust65bbf6b2007-08-27 09:57:46 -04001986 delegation_type = delegation->type;
Trond Myklebust1ac7e2f2007-07-08 21:04:15 -04001987 rcu_read_unlock();
Trond Myklebust864472e2006-01-03 09:55:15 +01001988 opendata->o_arg.u.delegation_type = delegation_type;
1989 status = nfs4_open_recover(opendata, state);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04001990 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991 return status;
1992}
1993
Trond Myklebust539cd032007-06-05 11:46:42 -04001994static int nfs4_do_open_reclaim(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001995{
1996 struct nfs_server *server = NFS_SERVER(state->inode);
1997 struct nfs4_exception exception = { };
1998 int err;
1999 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002000 err = _nfs4_do_open_reclaim(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002001 trace_nfs4_open_reclaim(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002002 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2003 continue;
Trond Myklebust168667c2010-10-19 19:47:49 -04002004 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002005 break;
2006 nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 } while (exception.retry);
2008 return err;
2009}
2010
Trond Myklebust864472e2006-01-03 09:55:15 +01002011static int nfs4_open_reclaim(struct nfs4_state_owner *sp, struct nfs4_state *state)
2012{
2013 struct nfs_open_context *ctx;
2014 int ret;
2015
2016 ctx = nfs4_state_find_open_context(state);
2017 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002018 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002019 ret = nfs4_do_open_reclaim(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002020 put_nfs_open_context(ctx);
2021 return ret;
2022}
2023
NeilBrowndce26302017-12-13 09:57:09 +11002024static int nfs4_handle_delegation_recall_error(struct nfs_server *server, struct nfs4_state *state, const nfs4_stateid *stateid, struct file_lock *fl, int err)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025{
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002026 switch (err) {
2027 default:
2028 printk(KERN_ERR "NFS: %s: unhandled error "
2029 "%d.\n", __func__, err);
2030 case 0:
2031 case -ENOENT:
Trond Myklebust8eee52a2015-06-04 13:51:13 -04002032 case -EAGAIN:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002033 case -ESTALE:
2034 break;
2035 case -NFS4ERR_BADSESSION:
2036 case -NFS4ERR_BADSLOT:
2037 case -NFS4ERR_BAD_HIGH_SLOT:
2038 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
2039 case -NFS4ERR_DEADSESSION:
2040 set_bit(NFS_DELEGATED_STATE, &state->flags);
2041 nfs4_schedule_session_recovery(server->nfs_client->cl_session, err);
2042 return -EAGAIN;
2043 case -NFS4ERR_STALE_CLIENTID:
2044 case -NFS4ERR_STALE_STATEID:
2045 set_bit(NFS_DELEGATED_STATE, &state->flags);
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002046 /* Don't recall a delegation if it was lost */
2047 nfs4_schedule_lease_recovery(server->nfs_client);
2048 return -EAGAIN;
Chuck Lever352297b2013-10-17 14:13:24 -04002049 case -NFS4ERR_MOVED:
2050 nfs4_schedule_migration_recovery(server);
2051 return -EAGAIN;
Chuck Lever8ef2f8d2013-10-17 14:13:41 -04002052 case -NFS4ERR_LEASE_MOVED:
2053 nfs4_schedule_lease_moved_recovery(server->nfs_client);
2054 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002055 case -NFS4ERR_DELEG_REVOKED:
2056 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebust404ea3562016-09-22 13:39:08 -04002057 case -NFS4ERR_EXPIRED:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002058 case -NFS4ERR_BAD_STATEID:
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002059 case -NFS4ERR_OPENMODE:
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002060 nfs_inode_find_state_and_recover(state->inode,
2061 stateid);
2062 nfs4_schedule_stateid_recovery(server, state);
Trond Myklebust869f9df2014-11-10 18:43:56 -05002063 return -EAGAIN;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002064 case -NFS4ERR_DELAY:
2065 case -NFS4ERR_GRACE:
2066 set_bit(NFS_DELEGATED_STATE, &state->flags);
2067 ssleep(1);
2068 return -EAGAIN;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002069 case -ENOMEM:
2070 case -NFS4ERR_DENIED:
NeilBrowndce26302017-12-13 09:57:09 +11002071 if (fl) {
2072 struct nfs4_lock_state *lsp = fl->fl_u.nfs4_fl.owner;
2073 if (lsp)
2074 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2075 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002076 return 0;
Trond Myklebustbe76b5b2013-04-01 15:40:44 -04002077 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002078 return err;
2079}
2080
Trond Myklebust24311f82015-09-20 10:50:17 -04002081int nfs4_open_delegation_recall(struct nfs_open_context *ctx,
2082 struct nfs4_state *state, const nfs4_stateid *stateid,
2083 fmode_t type)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002084{
2085 struct nfs_server *server = NFS_SERVER(state->inode);
2086 struct nfs4_opendata *opendata;
Trond Myklebust24311f82015-09-20 10:50:17 -04002087 int err = 0;
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002088
2089 opendata = nfs4_open_recoverdata_alloc(ctx, state,
2090 NFS4_OPEN_CLAIM_DELEG_CUR_FH);
2091 if (IS_ERR(opendata))
2092 return PTR_ERR(opendata);
2093 nfs4_stateid_copy(&opendata->o_arg.u.delegation, stateid);
Jeff Layton5e99b532015-10-02 13:14:37 -04002094 write_seqlock(&state->seqlock);
2095 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2096 write_sequnlock(&state->seqlock);
Trond Myklebust24311f82015-09-20 10:50:17 -04002097 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2098 switch (type & (FMODE_READ|FMODE_WRITE)) {
2099 case FMODE_READ|FMODE_WRITE:
2100 case FMODE_WRITE:
2101 err = nfs4_open_recover_helper(opendata, FMODE_READ|FMODE_WRITE);
2102 if (err)
2103 break;
2104 err = nfs4_open_recover_helper(opendata, FMODE_WRITE);
2105 if (err)
2106 break;
2107 case FMODE_READ:
2108 err = nfs4_open_recover_helper(opendata, FMODE_READ);
2109 }
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002110 nfs4_opendata_put(opendata);
NeilBrowndce26302017-12-13 09:57:09 +11002111 return nfs4_handle_delegation_recall_error(server, state, stateid, NULL, err);
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04002112}
2113
Chuck Leverbe05c862013-08-09 12:49:47 -04002114static void nfs4_open_confirm_prepare(struct rpc_task *task, void *calldata)
2115{
2116 struct nfs4_opendata *data = calldata;
2117
Anna Schumaker7981c8a2017-01-10 11:39:53 -05002118 nfs4_setup_sequence(data->o_arg.server->nfs_client,
2119 &data->c_arg.seq_args, &data->c_res.seq_res, task);
Chuck Leverbe05c862013-08-09 12:49:47 -04002120}
2121
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002122static void nfs4_open_confirm_done(struct rpc_task *task, void *calldata)
2123{
2124 struct nfs4_opendata *data = calldata;
2125
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002126 nfs40_sequence_done(task, &data->c_res.seq_res);
Chuck Leverbe05c862013-08-09 12:49:47 -04002127
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002128 data->rpc_status = task->tk_status;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002129 if (data->rpc_status == 0) {
Trond Myklebustf597c532012-03-04 18:13:56 -05002130 nfs4_stateid_copy(&data->o_res.stateid, &data->c_res.stateid);
Trond Myklebustbb226292008-01-02 15:19:18 -05002131 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust26e976a2006-01-03 09:55:21 +01002132 renew_lease(data->o_res.server, data->timestamp);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002133 data->rpc_done = true;
Trond Myklebust26e976a2006-01-03 09:55:21 +01002134 }
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002135}
2136
2137static void nfs4_open_confirm_release(void *calldata)
2138{
2139 struct nfs4_opendata *data = calldata;
2140 struct nfs4_state *state = NULL;
2141
2142 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002143 if (!data->cancelled)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002144 goto out_free;
2145 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002146 if (!data->rpc_done)
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002147 goto out_free;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002148 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002149 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002150 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002151out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002152 nfs4_opendata_put(data);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002153}
2154
2155static const struct rpc_call_ops nfs4_open_confirm_ops = {
Chuck Leverbe05c862013-08-09 12:49:47 -04002156 .rpc_call_prepare = nfs4_open_confirm_prepare,
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002157 .rpc_call_done = nfs4_open_confirm_done,
2158 .rpc_release = nfs4_open_confirm_release,
2159};
2160
2161/*
2162 * Note: On error, nfs4_proc_open_confirm will free the struct nfs4_opendata
2163 */
2164static int _nfs4_proc_open_confirm(struct nfs4_opendata *data)
2165{
David Howells2b0143b2015-03-17 22:25:59 +00002166 struct nfs_server *server = NFS_SERVER(d_inode(data->dir));
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002167 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002168 struct rpc_message msg = {
2169 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_CONFIRM],
2170 .rpc_argp = &data->c_arg,
2171 .rpc_resp = &data->c_res,
2172 .rpc_cred = data->owner->so_cred,
2173 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002174 struct rpc_task_setup task_setup_data = {
2175 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002176 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002177 .callback_ops = &nfs4_open_confirm_ops,
2178 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002179 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002180 .flags = RPC_TASK_ASYNC,
2181 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 int status;
2183
Trond Myklebust17ead6c2014-02-01 14:53:23 -05002184 nfs4_init_sequence(&data->c_arg.seq_args, &data->c_res.seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002185 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002186 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002187 data->rpc_status = 0;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002188 data->timestamp = jiffies;
Benjamin Coddingtone92c1e02015-10-01 09:17:33 -04002189 if (data->is_recover)
2190 nfs4_set_sequence_privileged(&data->c_arg.seq_args);
Trond Myklebustc970aa82007-07-14 15:39:59 -04002191 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05002192 if (IS_ERR(task))
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002193 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002194 status = rpc_wait_for_completion_task(task);
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002195 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002196 data->cancelled = true;
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002197 smp_wmb();
2198 } else
2199 status = data->rpc_status;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05002200 rpc_put_task(task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002201 return status;
2202}
2203
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002204static void nfs4_open_prepare(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002205{
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002206 struct nfs4_opendata *data = calldata;
2207 struct nfs4_state_owner *sp = data->owner;
Trond Myklebust549b19c2013-04-16 18:42:34 -04002208 struct nfs_client *clp = sp->so_server->nfs_client;
Trond Myklebust2a606182015-08-19 22:30:00 -05002209 enum open_claim_type4 claim = data->o_arg.claim;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002210
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002211 if (nfs_wait_on_sequence(data->o_arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002212 goto out_wait;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002213 /*
2214 * Check if we still need to send an OPEN call, or if we can use
2215 * a delegation instead.
2216 */
2217 if (data->state != NULL) {
2218 struct nfs_delegation *delegation;
2219
Trond Myklebustdc0b0272008-12-23 15:21:56 -05002220 if (can_open_cached(data->state, data->o_arg.fmode, data->o_arg.open_flags))
Trond Myklebust6ee41262007-07-08 14:11:36 -04002221 goto out_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002222 rcu_read_lock();
2223 delegation = rcu_dereference(NFS_I(data->state->inode)->delegation);
Trond Myklebust2a606182015-08-19 22:30:00 -05002224 if (can_open_delegated(delegation, data->o_arg.fmode, claim))
Trond Myklebust652f89f2011-12-09 19:05:58 -05002225 goto unlock_no_action;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002226 rcu_read_unlock();
2227 }
Trond Myklebust95b72eb2012-04-20 19:24:51 -04002228 /* Update client id. */
Trond Myklebust549b19c2013-04-16 18:42:34 -04002229 data->o_arg.clientid = clp->cl_clientid;
Trond Myklebust2a606182015-08-19 22:30:00 -05002230 switch (claim) {
2231 default:
2232 break;
Trond Myklebust8188df12013-04-23 14:31:19 -04002233 case NFS4_OPEN_CLAIM_PREVIOUS:
2234 case NFS4_OPEN_CLAIM_DELEG_CUR_FH:
2235 case NFS4_OPEN_CLAIM_DELEG_PREV_FH:
Andy Adamsone23008e2012-10-02 21:07:32 -04002236 data->o_arg.open_bitmap = &nfs4_open_noattr_bitmap[0];
Trond Myklebust8188df12013-04-23 14:31:19 -04002237 case NFS4_OPEN_CLAIM_FH:
2238 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_NOATTR];
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002239 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002240 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05002241 if (nfs4_setup_sequence(data->o_arg.server->nfs_client,
Andy Adamsond8985282009-04-01 09:22:21 -04002242 &data->o_arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04002243 &data->o_res.seq_res,
2244 task) != 0)
2245 nfs_release_seqid(data->o_arg.seqid);
Trond Myklebust549b19c2013-04-16 18:42:34 -04002246
2247 /* Set the create mode (note dependency on the session type) */
2248 data->o_arg.createmode = NFS4_CREATE_UNCHECKED;
2249 if (data->o_arg.open_flags & O_EXCL) {
2250 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE;
2251 if (nfs4_has_persistent_session(clp))
2252 data->o_arg.createmode = NFS4_CREATE_GUARDED;
2253 else if (clp->cl_mvops->minor_version > 0)
2254 data->o_arg.createmode = NFS4_CREATE_EXCLUSIVE4_1;
2255 }
Trond Myklebust6ee41262007-07-08 14:11:36 -04002256 return;
Trond Myklebust652f89f2011-12-09 19:05:58 -05002257unlock_no_action:
Olga Kornievskaia9759b0f2015-11-24 13:29:42 -05002258 trace_nfs4_cached_open(data->state);
Trond Myklebust652f89f2011-12-09 19:05:58 -05002259 rcu_read_unlock();
Trond Myklebust6ee41262007-07-08 14:11:36 -04002260out_no_action:
2261 task->tk_action = NULL;
Trond Myklebustc8da19b2013-02-11 19:01:21 -05002262out_wait:
Trond Myklebustb75ad4c2012-11-29 17:27:47 -05002263 nfs4_sequence_done(task, &data->o_res.seq_res);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002264}
2265
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002266static void nfs4_open_done(struct rpc_task *task, void *calldata)
2267{
2268 struct nfs4_opendata *data = calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002269
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002270 data->rpc_status = task->tk_status;
Andy Adamsond8985282009-04-01 09:22:21 -04002271
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04002272 if (!nfs4_sequence_process(task, &data->o_res.seq_res))
Trond Myklebust14516c32010-07-31 14:29:06 -04002273 return;
Andy Adamsond8985282009-04-01 09:22:21 -04002274
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002275 if (task->tk_status == 0) {
Trond Myklebust807d66d82012-10-02 17:09:00 -07002276 if (data->o_res.f_attr->valid & NFS_ATTR_FATTR_TYPE) {
2277 switch (data->o_res.f_attr->mode & S_IFMT) {
Trond Myklebust6f926b52005-10-18 14:20:18 -07002278 case S_IFREG:
2279 break;
2280 case S_IFLNK:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002281 data->rpc_status = -ELOOP;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002282 break;
2283 case S_IFDIR:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002284 data->rpc_status = -EISDIR;
Trond Myklebust6f926b52005-10-18 14:20:18 -07002285 break;
2286 default:
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002287 data->rpc_status = -ENOTDIR;
Trond Myklebust807d66d82012-10-02 17:09:00 -07002288 }
Trond Myklebust6f926b52005-10-18 14:20:18 -07002289 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01002290 renew_lease(data->o_res.server, data->timestamp);
Trond Myklebust0f9f95e2007-07-08 16:19:56 -04002291 if (!(data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM))
2292 nfs_confirm_seqid(&data->owner->so_seqid, 0);
Trond Myklebust6f926b52005-10-18 14:20:18 -07002293 }
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002294 data->rpc_done = true;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002295}
Trond Myklebust6f926b52005-10-18 14:20:18 -07002296
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002297static void nfs4_open_release(void *calldata)
2298{
2299 struct nfs4_opendata *data = calldata;
2300 struct nfs4_state *state = NULL;
2301
2302 /* If this request hasn't been cancelled, do nothing */
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002303 if (!data->cancelled)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002304 goto out_free;
2305 /* In case of error, no cleanup! */
Trond Myklebust3e309912007-07-07 13:19:59 -04002306 if (data->rpc_status != 0 || !data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002307 goto out_free;
2308 /* In case we need an open_confirm, no cleanup! */
2309 if (data->o_res.rflags & NFS4_OPEN_RESULT_CONFIRM)
2310 goto out_free;
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002311 state = nfs4_opendata_to_nfs4_state(data);
Trond Myklebust1b370bc2007-07-07 08:04:47 -04002312 if (!IS_ERR(state))
Al Viro643168c2011-06-22 18:20:23 -04002313 nfs4_close_state(state, data->o_arg.fmode);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002314out_free:
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002315 nfs4_opendata_put(data);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002316}
2317
2318static const struct rpc_call_ops nfs4_open_ops = {
2319 .rpc_call_prepare = nfs4_open_prepare,
2320 .rpc_call_done = nfs4_open_done,
2321 .rpc_release = nfs4_open_release,
2322};
2323
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002324static int nfs4_run_open_task(struct nfs4_opendata *data, int isrecover)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002325{
David Howells2b0143b2015-03-17 22:25:59 +00002326 struct inode *dir = d_inode(data->dir);
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002327 struct nfs_server *server = NFS_SERVER(dir);
2328 struct nfs_openargs *o_arg = &data->o_arg;
2329 struct nfs_openres *o_res = &data->o_res;
2330 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04002331 struct rpc_message msg = {
2332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN],
2333 .rpc_argp = o_arg,
2334 .rpc_resp = o_res,
2335 .rpc_cred = data->owner->so_cred,
2336 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04002337 struct rpc_task_setup task_setup_data = {
2338 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04002339 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002340 .callback_ops = &nfs4_open_ops,
2341 .callback_data = data,
Trond Myklebust101070c2008-02-19 20:04:23 -05002342 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04002343 .flags = RPC_TASK_ASYNC,
2344 };
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002345 int status;
2346
Chuck Levera9c92d62013-08-09 12:48:18 -04002347 nfs4_init_sequence(&o_arg->seq_args, &o_res->seq_res, 1);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002348 kref_get(&data->kref);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002349 data->rpc_done = false;
Trond Myklebust3e309912007-07-07 13:19:59 -04002350 data->rpc_status = 0;
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002351 data->cancelled = false;
2352 data->is_recover = false;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002353 if (isrecover) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04002354 nfs4_set_sequence_privileged(&o_arg->seq_args);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002355 data->is_recover = true;
Trond Myklebustbdeca1b2013-04-23 14:52:44 -04002356 }
Trond Myklebustc970aa82007-07-14 15:39:59 -04002357 task = rpc_run_task(&task_setup_data);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002358 if (IS_ERR(task))
2359 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05002360 status = rpc_wait_for_completion_task(task);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002361 if (status != 0) {
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002362 data->cancelled = true;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002363 smp_wmb();
2364 } else
2365 status = data->rpc_status;
2366 rpc_put_task(task);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002367
2368 return status;
2369}
2370
2371static int _nfs4_recover_proc_open(struct nfs4_opendata *data)
2372{
David Howells2b0143b2015-03-17 22:25:59 +00002373 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002374 struct nfs_openres *o_res = &data->o_res;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05002375 int status;
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002376
2377 status = nfs4_run_open_task(data, 1);
2378 if (status != 0 || !data->rpc_done)
2379 return status;
2380
Trond Myklebust6926afd12012-01-07 13:22:46 -05002381 nfs_fattr_map_and_free_names(NFS_SERVER(dir), &data->f_attr);
2382
Anna Schumakerd7e98252017-01-11 16:13:29 -05002383 if (o_res->rflags & NFS4_OPEN_RESULT_CONFIRM)
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002384 status = _nfs4_proc_open_confirm(data);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002385
2386 return status;
2387}
2388
Trond Myklebustf3792d62014-07-10 08:54:32 -04002389/*
2390 * Additional permission checks in order to distinguish between an
2391 * open for read, and an open for execute. This works around the
2392 * fact that NFSv4 OPEN treats read and execute permissions as being
2393 * the same.
2394 * Note that in the non-execute case, we want to turn off permission
2395 * checking if we just created a new file (POSIX open() semantics).
2396 */
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002397static int nfs4_opendata_access(struct rpc_cred *cred,
2398 struct nfs4_opendata *opendata,
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002399 struct nfs4_state *state, fmode_t fmode,
2400 int openflags)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002401{
2402 struct nfs_access_entry cache;
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002403 u32 mask, flags;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002404
2405 /* access call failed or for some reason the server doesn't
2406 * support any access modes -- defer access call until later */
2407 if (opendata->o_res.access_supported == 0)
2408 return 0;
2409
2410 mask = 0;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002411 /*
2412 * Use openflags to check for exec, because fmode won't
2413 * always have FMODE_EXEC set when file open for exec.
2414 */
Weston Andros Adamsonf8d9a892013-01-03 16:42:29 -05002415 if (openflags & __FMODE_EXEC) {
2416 /* ONLY check for exec rights */
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002417 if (S_ISDIR(state->inode->i_mode))
2418 mask = NFS4_ACCESS_LOOKUP;
2419 else
2420 mask = NFS4_ACCESS_EXECUTE;
Trond Myklebustf3792d62014-07-10 08:54:32 -04002421 } else if ((fmode & FMODE_READ) && !opendata->file_created)
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002422 mask = NFS4_ACCESS_READ;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002423
2424 cache.cred = cred;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002425 nfs_access_set_mask(&cache, opendata->o_res.access_result);
2426 nfs_access_add_cache(state->inode, &cache);
2427
Anna Schumaker1e6f2092017-07-25 16:10:47 -04002428 flags = NFS4_ACCESS_READ | NFS4_ACCESS_EXECUTE | NFS4_ACCESS_LOOKUP;
2429 if ((mask & ~cache.mask & flags) == 0)
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002430 return 0;
2431
Weston Andros Adamson998f40b2012-11-02 18:00:56 -04002432 return -EACCES;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002433}
2434
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002435/*
2436 * Note: On error, nfs4_proc_open will free the struct nfs4_opendata
2437 */
2438static int _nfs4_proc_open(struct nfs4_opendata *data)
2439{
David Howells2b0143b2015-03-17 22:25:59 +00002440 struct inode *dir = d_inode(data->dir);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08002441 struct nfs_server *server = NFS_SERVER(dir);
2442 struct nfs_openargs *o_arg = &data->o_arg;
2443 struct nfs_openres *o_res = &data->o_res;
2444 int status;
2445
2446 status = nfs4_run_open_task(data, 0);
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002447 if (!data->rpc_done)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002448 return status;
Trond Myklebust08ef7bd2011-10-18 16:11:49 -07002449 if (status != 0) {
2450 if (status == -NFS4ERR_BADNAME &&
2451 !(o_arg->open_flags & O_CREAT))
2452 return -ENOENT;
2453 return status;
2454 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002455
Trond Myklebust6926afd12012-01-07 13:22:46 -05002456 nfs_fattr_map_and_free_names(server, &data->f_attr);
2457
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002458 if (o_arg->open_flags & O_CREAT) {
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002459 if (o_arg->open_flags & O_EXCL)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002460 data->file_created = true;
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002461 else if (o_res->cinfo.before != o_res->cinfo.after)
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04002462 data->file_created = true;
Jeff Layton1eb5d982018-01-09 08:21:17 -05002463 if (data->file_created ||
2464 inode_peek_iversion_raw(dir) != o_res->cinfo.after)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05002465 update_changeattr(dir, &o_res->cinfo,
2466 o_res->f_attr->time_start);
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002467 }
Trond Myklebust0df5dd42010-04-11 16:48:44 -04002468 if ((o_res->rflags & NFS4_OPEN_RESULT_LOCKTYPE_POSIX) == 0)
2469 server->caps &= ~NFS_CAP_POSIX_LOCK;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002470 if(o_res->rflags & NFS4_OPEN_RESULT_CONFIRM) {
Trond Myklebustcdd4e682006-01-03 09:55:12 +01002471 status = _nfs4_proc_open_confirm(data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002472 if (status != 0)
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002473 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002474 }
Trond Myklebust56e0d712017-04-15 19:20:01 -04002475 if (!(o_res->f_attr->valid & NFS_ATTR_FATTR)) {
2476 nfs4_sequence_free_slot(&o_res->seq_res);
Andy Adamson8935ef62014-05-23 06:22:59 -07002477 nfs4_proc_getattr(server, &o_res->fh, o_res->f_attr, o_res->f_label);
Trond Myklebust56e0d712017-04-15 19:20:01 -04002478 }
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002479 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480}
2481
Linus Torvalds1da177e2005-04-16 15:20:36 -07002482/*
2483 * OPEN_EXPIRED:
2484 * reclaim state on the server after a network partition.
2485 * Assumes caller holds the appropriate lock
2486 */
Trond Myklebust539cd032007-06-05 11:46:42 -04002487static int _nfs4_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002488{
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002489 struct nfs4_opendata *opendata;
Trond Myklebust864472e2006-01-03 09:55:15 +01002490 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002491
Trond Myklebust4a1c0892013-03-15 14:57:33 -04002492 opendata = nfs4_open_recoverdata_alloc(ctx, state,
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002493 NFS4_OPEN_CLAIM_FH);
Trond Myklebust6f220ed2007-07-17 21:50:45 -04002494 if (IS_ERR(opendata))
2495 return PTR_ERR(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002496 ret = nfs4_open_recover(opendata, state);
Trond Myklebust35d05772008-04-05 15:54:17 -04002497 if (ret == -ESTALE)
Al Viro3d4ff432011-06-22 18:40:12 -04002498 d_drop(ctx->dentry);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002499 nfs4_opendata_put(opendata);
Trond Myklebust864472e2006-01-03 09:55:15 +01002500 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002501}
2502
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002503static int nfs4_do_open_expired(struct nfs_open_context *ctx, struct nfs4_state *state)
Trond Myklebust202b50d2005-06-22 17:16:29 +00002504{
Trond Myklebust539cd032007-06-05 11:46:42 -04002505 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust202b50d2005-06-22 17:16:29 +00002506 struct nfs4_exception exception = { };
2507 int err;
2508
2509 do {
Trond Myklebust539cd032007-06-05 11:46:42 -04002510 err = _nfs4_open_expired(ctx, state);
Trond Myklebust42113a72013-08-12 16:19:27 -04002511 trace_nfs4_open_expired(ctx, 0, err);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002512 if (nfs4_clear_cap_atomic_open_v1(server, err, &exception))
2513 continue;
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002514 switch (err) {
2515 default:
2516 goto out;
2517 case -NFS4ERR_GRACE:
2518 case -NFS4ERR_DELAY:
2519 nfs4_handle_exception(server, err, &exception);
2520 err = 0;
2521 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00002522 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05002523out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00002524 return err;
2525}
2526
Linus Torvalds1da177e2005-04-16 15:20:36 -07002527static int nfs4_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2528{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002529 struct nfs_open_context *ctx;
Trond Myklebust864472e2006-01-03 09:55:15 +01002530 int ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002531
Trond Myklebust864472e2006-01-03 09:55:15 +01002532 ctx = nfs4_state_find_open_context(state);
2533 if (IS_ERR(ctx))
Trond Myklebust91876b12013-03-28 14:01:33 -04002534 return -EAGAIN;
Trond Myklebust539cd032007-06-05 11:46:42 -04002535 ret = nfs4_do_open_expired(ctx, state);
Trond Myklebust864472e2006-01-03 09:55:15 +01002536 put_nfs_open_context(ctx);
2537 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538}
2539
Trond Myklebust41020b62016-09-22 13:38:58 -04002540static void nfs_finish_clear_delegation_stateid(struct nfs4_state *state,
2541 const nfs4_stateid *stateid)
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002542{
Trond Myklebust41020b62016-09-22 13:38:58 -04002543 nfs_remove_bad_delegation(state->inode, stateid);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002544 write_seqlock(&state->seqlock);
2545 nfs4_stateid_copy(&state->stateid, &state->open_stateid);
2546 write_sequnlock(&state->seqlock);
2547 clear_bit(NFS_DELEGATED_STATE, &state->flags);
2548}
2549
2550static void nfs40_clear_delegation_stateid(struct nfs4_state *state)
2551{
2552 if (rcu_access_pointer(NFS_I(state->inode)->delegation) != NULL)
Trond Myklebust41020b62016-09-22 13:38:58 -04002553 nfs_finish_clear_delegation_stateid(state, NULL);
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03002554}
2555
2556static int nfs40_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2557{
2558 /* NFSv4.0 doesn't allow for delegation recovery on open expire */
2559 nfs40_clear_delegation_stateid(state);
2560 return nfs4_open_expired(sp, state);
2561}
2562
Trond Myklebust45870d62016-09-22 13:38:59 -04002563static int nfs40_test_and_free_expired_stateid(struct nfs_server *server,
2564 nfs4_stateid *stateid,
2565 struct rpc_cred *cred)
2566{
2567 return -NFS4ERR_BAD_STATEID;
2568}
2569
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002570#if defined(CONFIG_NFS_V4_1)
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002571static int nfs41_test_and_free_expired_stateid(struct nfs_server *server,
2572 nfs4_stateid *stateid,
2573 struct rpc_cred *cred)
2574{
2575 int status;
2576
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002577 switch (stateid->type) {
2578 default:
2579 break;
2580 case NFS4_INVALID_STATEID_TYPE:
2581 case NFS4_SPECIAL_STATEID_TYPE:
2582 return -NFS4ERR_BAD_STATEID;
2583 case NFS4_REVOKED_STATEID_TYPE:
2584 goto out_free;
2585 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002586
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002587 status = nfs41_test_stateid(server, stateid, cred);
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002588 switch (status) {
2589 case -NFS4ERR_EXPIRED:
2590 case -NFS4ERR_ADMIN_REVOKED:
2591 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002592 break;
2593 default:
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002594 return status;
2595 }
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002596out_free:
2597 /* Ack the revoked state to the server */
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04002598 nfs41_free_stateid(server, stateid, cred, true);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002599 return -NFS4ERR_EXPIRED;
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002600}
2601
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002602static void nfs41_check_delegation_stateid(struct nfs4_state *state)
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002603{
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002604 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002605 nfs4_stateid stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002606 struct nfs_delegation *delegation;
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002607 struct rpc_cred *cred;
2608 int status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002609
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002610 /* Get the delegation credential for use by test/free_stateid */
2611 rcu_read_lock();
2612 delegation = rcu_dereference(NFS_I(state->inode)->delegation);
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002613 if (delegation == NULL) {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002614 rcu_read_unlock();
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002615 return;
2616 }
2617
2618 nfs4_stateid_copy(&stateid, &delegation->stateid);
Olga Kornievskaia0e3d3e52017-03-30 13:49:03 -04002619 if (test_bit(NFS_DELEGATION_REVOKED, &delegation->flags) ||
2620 !test_and_clear_bit(NFS_DELEGATION_TEST_EXPIRED,
2621 &delegation->flags)) {
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002622 rcu_read_unlock();
Trond Myklebust41020b62016-09-22 13:38:58 -04002623 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebust4c8e5442016-09-22 13:38:55 -04002624 return;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002625 }
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002626
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002627 cred = get_rpccred(delegation->cred);
2628 rcu_read_unlock();
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002629 status = nfs41_test_and_free_expired_stateid(server, &stateid, cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002630 trace_nfs4_test_delegation_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002631 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID)
Trond Myklebust41020b62016-09-22 13:38:58 -04002632 nfs_finish_clear_delegation_stateid(state, &stateid);
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002633
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002634 put_rpccred(cred);
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002635}
2636
2637/**
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002638 * nfs41_check_expired_locks - possibly free a lock stateid
2639 *
2640 * @state: NFSv4 state for an inode
2641 *
2642 * Returns NFS_OK if recovery for this stateid is now finished.
2643 * Otherwise a negative NFS4ERR value is returned.
2644 */
2645static int nfs41_check_expired_locks(struct nfs4_state *state)
2646{
2647 int status, ret = NFS_OK;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002648 struct nfs4_lock_state *lsp, *prev = NULL;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002649 struct nfs_server *server = NFS_SERVER(state->inode);
2650
2651 if (!test_bit(LK_STATE_IN_USE, &state->flags))
2652 goto out;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002653
2654 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002655 list_for_each_entry(lsp, &state->lock_states, ls_locks) {
2656 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags)) {
2657 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
2658
Elena Reshetova194bc1f2017-10-20 12:53:36 +03002659 refcount_inc(&lsp->ls_count);
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002660 spin_unlock(&state->state_lock);
2661
2662 nfs4_put_lock_state(prev);
2663 prev = lsp;
2664
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002665 status = nfs41_test_and_free_expired_stateid(server,
2666 &lsp->ls_stateid,
2667 cred);
2668 trace_nfs4_test_lock_stateid(state, lsp, status);
2669 if (status == -NFS4ERR_EXPIRED ||
2670 status == -NFS4ERR_BAD_STATEID) {
2671 clear_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002672 lsp->ls_stateid.type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002673 if (!recover_lost_locks)
2674 set_bit(NFS_LOCK_LOST, &lsp->ls_flags);
2675 } else if (status != NFS_OK) {
2676 ret = status;
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002677 nfs4_put_lock_state(prev);
2678 goto out;
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002679 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002680 spin_lock(&state->state_lock);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002681 }
Benjamin Coddingtond75a6a02016-11-18 21:11:39 -05002682 }
2683 spin_unlock(&state->state_lock);
2684 nfs4_put_lock_state(prev);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002685out:
2686 return ret;
2687}
2688
2689/**
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002690 * nfs41_check_open_stateid - possibly free an open stateid
2691 *
2692 * @state: NFSv4 state for an inode
2693 *
2694 * Returns NFS_OK if recovery for this stateid is now finished.
2695 * Otherwise a negative NFS4ERR value is returned.
2696 */
2697static int nfs41_check_open_stateid(struct nfs4_state *state)
2698{
2699 struct nfs_server *server = NFS_SERVER(state->inode);
Bryan Schumakerfcb6d9c2012-09-26 15:25:53 -04002700 nfs4_stateid *stateid = &state->open_stateid;
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04002701 struct rpc_cred *cred = state->owner->so_cred;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002702 int status;
2703
Trond Myklebustb134fc42016-09-22 13:39:16 -04002704 if (test_bit(NFS_OPEN_STATE, &state->flags) == 0) {
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002705 if (test_bit(NFS_DELEGATED_STATE, &state->flags) == 0) {
2706 if (nfs4_have_delegation(state->inode, state->state))
2707 return NFS_OK;
2708 return -NFS4ERR_OPENMODE;
2709 }
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002710 return -NFS4ERR_BAD_STATEID;
Trond Myklebustb134fc42016-09-22 13:39:16 -04002711 }
Trond Myklebust4586f6e2016-09-22 13:38:57 -04002712 status = nfs41_test_and_free_expired_stateid(server, stateid, cred);
Trond Myklebust08cb47f2013-08-20 21:59:40 -04002713 trace_nfs4_test_open_stateid(state, NULL, status);
Trond Myklebustf7a62ad2016-09-22 13:39:02 -04002714 if (status == -NFS4ERR_EXPIRED || status == -NFS4ERR_BAD_STATEID) {
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002715 clear_bit(NFS_O_RDONLY_STATE, &state->flags);
2716 clear_bit(NFS_O_WRONLY_STATE, &state->flags);
2717 clear_bit(NFS_O_RDWR_STATE, &state->flags);
Trond Myklebustfd068b22013-04-22 11:29:51 -04002718 clear_bit(NFS_OPEN_STATE, &state->flags);
Trond Myklebust67dd4832016-09-22 13:39:17 -04002719 stateid->type = NFS4_INVALID_STATEID_TYPE;
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002720 return status;
Trond Myklebustc0ca0e52017-08-08 21:39:28 -04002721 }
Trond Myklebust8a64c4e2016-09-22 13:39:21 -04002722 if (nfs_open_stateid_recover_openmode(state))
2723 return -NFS4ERR_OPENMODE;
2724 return NFS_OK;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002725}
2726
2727static int nfs41_open_expired(struct nfs4_state_owner *sp, struct nfs4_state *state)
2728{
Chuck Levereb64cf92012-07-11 16:30:05 -04002729 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05002730
Trond Myklebust0c116ca2014-11-12 14:44:49 -05002731 nfs41_check_delegation_stateid(state);
Trond Myklebustc5896fc2016-09-22 13:39:03 -04002732 status = nfs41_check_expired_locks(state);
2733 if (status != NFS_OK)
2734 return status;
Chuck Lever3e60ffd2012-07-11 16:30:14 -04002735 status = nfs41_check_open_stateid(state);
Chuck Levereb64cf92012-07-11 16:30:05 -04002736 if (status != NFS_OK)
2737 status = nfs4_open_expired(sp, state);
2738 return status;
Bryan Schumakerf062eb62011-06-02 14:59:10 -04002739}
2740#endif
2741
Linus Torvalds1da177e2005-04-16 15:20:36 -07002742/*
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002743 * on an EXCLUSIVE create, the server should send back a bitmask with FATTR4-*
2744 * fields corresponding to attributes that were used to store the verifier.
2745 * Make sure we clobber those fields in the later setattr call
2746 */
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002747static inline void nfs4_exclusive_attrset(struct nfs4_opendata *opendata,
2748 struct iattr *sattr, struct nfs4_label **label)
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002749{
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002750 const u32 *attrset = opendata->o_res.attrset;
2751
2752 if ((attrset[1] & FATTR4_WORD1_TIME_ACCESS) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002753 !(sattr->ia_valid & ATTR_ATIME_SET))
2754 sattr->ia_valid |= ATTR_ATIME;
2755
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002756 if ((attrset[1] & FATTR4_WORD1_TIME_MODIFY) &&
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002757 !(sattr->ia_valid & ATTR_MTIME_SET))
2758 sattr->ia_valid |= ATTR_MTIME;
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002759
2760 /* Except MODE, it seems harmless of setting twice. */
Benjamin Coddingtona4306072017-01-24 11:34:20 -05002761 if (opendata->o_arg.createmode != NFS4_CREATE_EXCLUSIVE &&
Benjamin Coddington501e7a42017-06-02 11:21:34 -04002762 (attrset[1] & FATTR4_WORD1_MODE ||
2763 attrset[2] & FATTR4_WORD2_MODE_UMASK))
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002764 sattr->ia_valid &= ~ATTR_MODE;
2765
2766 if (attrset[2] & FATTR4_WORD2_SECURITY_LABEL)
2767 *label = NULL;
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002768}
2769
Trond Myklebustc21443c2013-02-07 14:26:21 -05002770static int _nfs4_open_and_get_state(struct nfs4_opendata *opendata,
2771 fmode_t fmode,
2772 int flags,
Trond Myklebust3efb9722013-05-29 13:17:04 -04002773 struct nfs_open_context *ctx)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002774{
2775 struct nfs4_state_owner *sp = opendata->owner;
2776 struct nfs_server *server = sp->so_server;
Trond Myklebust275bb302013-05-29 13:11:28 -04002777 struct dentry *dentry;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002778 struct nfs4_state *state;
2779 unsigned int seq;
2780 int ret;
2781
2782 seq = raw_seqcount_begin(&sp->so_reclaim_seqcount);
2783
2784 ret = _nfs4_proc_open(opendata);
Trond Myklebustdca780012014-10-23 19:23:03 +03002785 if (ret != 0)
Trond Myklebustc21443c2013-02-07 14:26:21 -05002786 goto out;
2787
2788 state = nfs4_opendata_to_nfs4_state(opendata);
2789 ret = PTR_ERR(state);
2790 if (IS_ERR(state))
2791 goto out;
Trond Myklebusta974dee2017-02-08 11:29:46 -05002792 ctx->state = state;
Trond Myklebustc21443c2013-02-07 14:26:21 -05002793 if (server->caps & NFS_CAP_POSIX_LOCK)
2794 set_bit(NFS_STATE_POSIX_LOCKS, &state->flags);
Jeff Laytona8ce3772016-09-17 18:17:35 -04002795 if (opendata->o_res.rflags & NFS4_OPEN_RESULT_MAY_NOTIFY_LOCK)
2796 set_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags);
Trond Myklebustc21443c2013-02-07 14:26:21 -05002797
Trond Myklebust275bb302013-05-29 13:11:28 -04002798 dentry = opendata->dentry;
David Howells2b0143b2015-03-17 22:25:59 +00002799 if (d_really_is_negative(dentry)) {
Al Viro668d0cd2016-03-08 12:44:17 -05002800 struct dentry *alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002801 d_drop(dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002802 alias = d_exact_alias(dentry, state->inode);
2803 if (!alias)
2804 alias = d_splice_alias(igrab(state->inode), dentry);
2805 /* d_splice_alias() can't fail here - it's a non-directory */
2806 if (alias) {
Trond Myklebust275bb302013-05-29 13:11:28 -04002807 dput(ctx->dentry);
Al Viro668d0cd2016-03-08 12:44:17 -05002808 ctx->dentry = dentry = alias;
Trond Myklebust275bb302013-05-29 13:11:28 -04002809 }
2810 nfs_set_verifier(dentry,
David Howells2b0143b2015-03-17 22:25:59 +00002811 nfs_save_change_attribute(d_inode(opendata->dir)));
Trond Myklebust275bb302013-05-29 13:11:28 -04002812 }
2813
Trond Myklebustc21443c2013-02-07 14:26:21 -05002814 ret = nfs4_opendata_access(sp->so_cred, opendata, state, fmode, flags);
2815 if (ret != 0)
2816 goto out;
2817
David Howells2b0143b2015-03-17 22:25:59 +00002818 if (d_inode(dentry) == state->inode) {
Trond Myklebustc45ffdd2013-05-29 13:34:46 -04002819 nfs_inode_attach_open_context(ctx);
2820 if (read_seqcount_retry(&sp->so_reclaim_seqcount, seq))
2821 nfs4_schedule_stateid_recovery(server, state);
2822 }
Trond Myklebustc21443c2013-02-07 14:26:21 -05002823out:
2824 return ret;
2825}
2826
Jeff Laytonaa53ed52007-06-05 14:49:03 -04002827/*
Trond Myklebust24ac23a2006-01-03 09:55:11 +01002828 * Returns a referenced nfs4_state
Linus Torvalds1da177e2005-04-16 15:20:36 -07002829 */
Andy Adamson82be4172012-05-23 05:02:35 -04002830static int _nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002831 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002832 int flags,
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002833 const struct nfs4_open_createattrs *c,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002834 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835{
2836 struct nfs4_state_owner *sp;
2837 struct nfs4_state *state = NULL;
2838 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002839 struct nfs4_opendata *opendata;
Trond Myklebust4197a052013-05-29 12:37:49 -04002840 struct dentry *dentry = ctx->dentry;
2841 struct rpc_cred *cred = ctx->cred;
2842 struct nfs4_threshold **ctx_th = &ctx->mdsthreshold;
2843 fmode_t fmode = ctx->mode & (FMODE_READ|FMODE_WRITE|FMODE_EXEC);
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002844 enum open_claim_type4 claim = NFS4_OPEN_CLAIM_NULL;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002845 struct iattr *sattr = c->sattr;
2846 struct nfs4_label *label = c->label;
David Quigley1775fd32013-05-22 12:50:42 -04002847 struct nfs4_label *olabel = NULL;
Trond Myklebustaac00a82007-07-05 19:02:21 -04002848 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849
2850 /* Protect against reboot recovery conflicts */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 status = -ENOMEM;
Trond Myklebustd1e284d2012-01-17 22:04:24 -05002852 sp = nfs4_get_state_owner(server, cred, GFP_KERNEL);
2853 if (sp == NULL) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002854 dprintk("nfs4_do_open: nfs4_get_state_owner failed!\n");
2855 goto out_err;
2856 }
Anna Schumaker334f87d2017-01-11 16:17:17 -05002857 status = nfs4_client_recover_expired_lease(server->nfs_client);
Trond Myklebust58d97142006-01-03 09:55:24 +01002858 if (status != 0)
Trond Myklebustb4454fe2006-01-03 09:55:25 +01002859 goto err_put_state_owner;
David Howells2b0143b2015-03-17 22:25:59 +00002860 if (d_really_is_positive(dentry))
2861 nfs4_return_incompatible_delegation(d_inode(dentry), fmode);
Trond Myklebust58d97142006-01-03 09:55:24 +01002862 status = -ENOMEM;
David Howells2b0143b2015-03-17 22:25:59 +00002863 if (d_really_is_positive(dentry))
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002864 claim = NFS4_OPEN_CLAIM_FH;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002865 opendata = nfs4_opendata_alloc(dentry, sp, fmode, flags,
2866 c, claim, GFP_KERNEL);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002867 if (opendata == NULL)
Trond Myklebust95d35cb2008-12-23 15:21:45 -05002868 goto err_put_state_owner;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002869
David Quigley14c43f72013-05-22 12:50:43 -04002870 if (label) {
2871 olabel = nfs4_label_alloc(server, GFP_KERNEL);
2872 if (IS_ERR(olabel)) {
2873 status = PTR_ERR(olabel);
2874 goto err_opendata_put;
2875 }
2876 }
2877
Trond Myklebuste911b812014-03-26 13:24:37 -07002878 if (server->attr_bitmask[2] & FATTR4_WORD2_MDSTHRESHOLD) {
2879 if (!opendata->f_attr.mdsthreshold) {
2880 opendata->f_attr.mdsthreshold = pnfs_mdsthreshold_alloc();
2881 if (!opendata->f_attr.mdsthreshold)
2882 goto err_free_label;
2883 }
Trond Myklebust1549210f2012-06-05 09:16:47 -04002884 opendata->o_arg.open_bitmap = &nfs4_pnfs_open_bitmap[0];
Andy Adamson82be4172012-05-23 05:02:35 -04002885 }
David Howells2b0143b2015-03-17 22:25:59 +00002886 if (d_really_is_positive(dentry))
2887 opendata->state = nfs4_get_open_state(d_inode(dentry), sp);
Trond Myklebustaac00a82007-07-05 19:02:21 -04002888
Trond Myklebust3efb9722013-05-29 13:17:04 -04002889 status = _nfs4_open_and_get_state(opendata, fmode, flags, ctx);
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002890 if (status != 0)
David Quigley14c43f72013-05-22 12:50:43 -04002891 goto err_free_label;
Trond Myklebust3efb9722013-05-29 13:17:04 -04002892 state = ctx->state;
Weston Andros Adamson6168f622012-09-10 14:00:46 -04002893
NeilBrownefcbc042015-07-30 13:00:56 +10002894 if ((opendata->o_arg.open_flags & (O_CREAT|O_EXCL)) == (O_CREAT|O_EXCL) &&
Trond Myklebust549b19c2013-04-16 18:42:34 -04002895 (opendata->o_arg.createmode != NFS4_CREATE_GUARDED)) {
Kinglong Mee5334c5b2015-08-26 21:13:37 +08002896 nfs4_exclusive_attrset(opendata, sattr, &label);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002897 /*
2898 * send create attributes which was not set by open
2899 * with an extra setattr.
2900 */
2901 if (sattr->ia_valid & NFS4_VALID_ATTRS) {
2902 nfs_fattr_init(opendata->o_res.f_attr);
2903 status = nfs4_do_setattr(state->inode, cred,
2904 opendata->o_res.f_attr, sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11002905 ctx, label, olabel);
Tigran Mkrtchyana1d1c4f2016-05-12 11:16:38 +02002906 if (status == 0) {
2907 nfs_setattr_update_inode(state->inode, sattr,
2908 opendata->o_res.f_attr);
2909 nfs_setsecurity(state->inode, opendata->o_res.f_attr, olabel);
2910 }
David Quigley1775fd32013-05-22 12:50:42 -04002911 }
Trond Myklebust0ab64e02010-04-16 16:22:51 -04002912 }
Kinglong Meec5c3fb52015-08-26 21:11:39 +08002913 if (opened && opendata->file_created)
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002914 *opened |= FILE_CREATED;
Andy Adamson82be4172012-05-23 05:02:35 -04002915
Trond Myklebuste911b812014-03-26 13:24:37 -07002916 if (pnfs_use_threshold(ctx_th, opendata->f_attr.mdsthreshold, server)) {
Andy Adamson82be4172012-05-23 05:02:35 -04002917 *ctx_th = opendata->f_attr.mdsthreshold;
Trond Myklebuste911b812014-03-26 13:24:37 -07002918 opendata->f_attr.mdsthreshold = NULL;
2919 }
Andy Adamson82be4172012-05-23 05:02:35 -04002920
David Quigley14c43f72013-05-22 12:50:43 -04002921 nfs4_label_free(olabel);
2922
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002923 nfs4_opendata_put(opendata);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002924 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002925 return 0;
David Quigley14c43f72013-05-22 12:50:43 -04002926err_free_label:
2927 nfs4_label_free(olabel);
Trond Myklebustc6d00e62007-06-17 16:02:44 -04002928err_opendata_put:
2929 nfs4_opendata_put(opendata);
Trond Myklebuste56e0b782006-01-03 09:55:08 +01002930err_put_state_owner:
2931 nfs4_put_state_owner(sp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002932out_err:
Linus Torvalds1da177e2005-04-16 15:20:36 -07002933 return status;
2934}
2935
2936
Andy Adamson82be4172012-05-23 05:02:35 -04002937static struct nfs4_state *nfs4_do_open(struct inode *dir,
Trond Myklebust4197a052013-05-29 12:37:49 -04002938 struct nfs_open_context *ctx,
Andy Adamson82be4172012-05-23 05:02:35 -04002939 int flags,
2940 struct iattr *sattr,
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04002941 struct nfs4_label *label,
2942 int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002943{
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002944 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002945 struct nfs4_exception exception = { };
2946 struct nfs4_state *res;
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002947 struct nfs4_open_createattrs c = {
2948 .label = label,
2949 .sattr = sattr,
2950 .verf = {
2951 [0] = (__u32)jiffies,
2952 [1] = (__u32)current->pid,
2953 },
2954 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07002955 int status;
2956
2957 do {
Trond Myklebust8fd1ab72017-11-06 15:28:03 -05002958 status = _nfs4_do_open(dir, ctx, flags, &c, opened);
Trond Myklebust3efb9722013-05-29 13:17:04 -04002959 res = ctx->state;
Trond Myklebust42113a72013-08-12 16:19:27 -04002960 trace_nfs4_open_file(ctx, flags, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002961 if (status == 0)
2962 break;
2963 /* NOTE: BAD_SEQID means the server and client disagree about the
2964 * book-keeping w.r.t. state-changing operations
2965 * (OPEN/CLOSE/LOCK/LOCKU...)
2966 * It is actually a sign of a bug on the client or on the server.
2967 *
2968 * If we receive a BAD_SEQID error in the particular case of
Trond Myklebustcee54fc2005-10-18 14:20:12 -07002969 * doing an OPEN, we assume that nfs_increment_open_seqid() will
Linus Torvalds1da177e2005-04-16 15:20:36 -07002970 * have unhashed the old state_owner for us, and that we can
2971 * therefore safely retry using a new one. We should still warn
2972 * the user though...
2973 */
2974 if (status == -NFS4ERR_BAD_SEQID) {
Trond Myklebust9a3ba432012-03-12 18:01:48 -04002975 pr_warn_ratelimited("NFS: v4 server %s "
Trond Myklebust6f43ddc2007-07-08 16:49:11 -04002976 " returned a bad sequence-id error!\n",
2977 NFS_SERVER(dir)->nfs_client->cl_hostname);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002978 exception.retry = 1;
2979 continue;
2980 }
Trond Myklebust550f5742005-10-18 14:20:21 -07002981 /*
2982 * BAD_STATEID on OPEN means that the server cancelled our
2983 * state before it received the OPEN_CONFIRM.
2984 * Recover by retrying the request as per the discussion
2985 * on Page 181 of RFC3530.
2986 */
2987 if (status == -NFS4ERR_BAD_STATEID) {
2988 exception.retry = 1;
2989 continue;
2990 }
Trond Myklebustaac00a82007-07-05 19:02:21 -04002991 if (status == -EAGAIN) {
2992 /* We must have found a delegation */
2993 exception.retry = 1;
2994 continue;
2995 }
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04002996 if (nfs4_clear_cap_atomic_open_v1(server, status, &exception))
2997 continue;
2998 res = ERR_PTR(nfs4_handle_exception(server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07002999 status, &exception));
3000 } while (exception.retry);
3001 return res;
3002}
3003
Trond Myklebust8487c472016-06-26 08:44:35 -04003004static int _nfs4_do_setattr(struct inode *inode,
3005 struct nfs_setattrargs *arg,
3006 struct nfs_setattrres *res,
3007 struct rpc_cred *cred,
NeilBrown29b59f92016-10-13 15:26:47 +11003008 struct nfs_open_context *ctx)
Trond Myklebust8487c472016-06-26 08:44:35 -04003009{
3010 struct nfs_server *server = NFS_SERVER(inode);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003011 struct rpc_message msg = {
Trond Myklebust8487c472016-06-26 08:44:35 -04003012 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
3013 .rpc_argp = arg,
3014 .rpc_resp = res,
3015 .rpc_cred = cred,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003016 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003017 struct rpc_cred *delegation_cred = NULL;
3018 unsigned long timestamp = jiffies;
3019 fmode_t fmode;
3020 bool truncate;
3021 int status;
3022
3023 nfs_fattr_init(res->fattr);
3024
3025 /* Servers should only apply open mode checks for file size changes */
3026 truncate = (arg->iap->ia_valid & ATTR_SIZE) ? true : false;
3027 fmode = truncate ? FMODE_WRITE : FMODE_READ;
3028
3029 if (nfs4_copy_delegation_stateid(inode, fmode, &arg->stateid, &delegation_cred)) {
3030 /* Use that stateid */
NeilBrown29b59f92016-10-13 15:26:47 +11003031 } else if (truncate && ctx != NULL) {
NeilBrown17393472016-10-13 15:26:47 +11003032 struct nfs_lock_context *l_ctx;
NeilBrown29b59f92016-10-13 15:26:47 +11003033 if (!nfs4_valid_open_stateid(ctx->state))
Trond Myklebust8487c472016-06-26 08:44:35 -04003034 return -EBADF;
NeilBrown17393472016-10-13 15:26:47 +11003035 l_ctx = nfs_get_lock_context(ctx);
3036 if (IS_ERR(l_ctx))
3037 return PTR_ERR(l_ctx);
NeilBrown7a0566b2016-12-06 15:50:06 -05003038 status = nfs4_select_rw_stateid(ctx->state, FMODE_WRITE, l_ctx,
3039 &arg->stateid, &delegation_cred);
3040 nfs_put_lock_context(l_ctx);
3041 if (status == -EIO)
Trond Myklebust8487c472016-06-26 08:44:35 -04003042 return -EBADF;
3043 } else
3044 nfs4_stateid_copy(&arg->stateid, &zero_stateid);
3045 if (delegation_cred)
3046 msg.rpc_cred = delegation_cred;
3047
3048 status = nfs4_call_sync(server->client, server, &msg, &arg->seq_args, &res->seq_res, 1);
3049
3050 put_rpccred(delegation_cred);
NeilBrown29b59f92016-10-13 15:26:47 +11003051 if (status == 0 && ctx != NULL)
Trond Myklebust8487c472016-06-26 08:44:35 -04003052 renew_lease(server, timestamp);
3053 trace_nfs4_setattr(inode, &arg->stateid, status);
3054 return status;
3055}
3056
3057static int nfs4_do_setattr(struct inode *inode, struct rpc_cred *cred,
3058 struct nfs_fattr *fattr, struct iattr *sattr,
NeilBrown29b59f92016-10-13 15:26:47 +11003059 struct nfs_open_context *ctx, struct nfs4_label *ilabel,
Trond Myklebust8487c472016-06-26 08:44:35 -04003060 struct nfs4_label *olabel)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003061{
Trond Myklebust3e4f6292006-03-20 13:44:46 -05003062 struct nfs_server *server = NFS_SERVER(inode);
NeilBrown29b59f92016-10-13 15:26:47 +11003063 struct nfs4_state *state = ctx ? ctx->state : NULL;
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003064 struct nfs_setattrargs arg = {
3065 .fh = NFS_FH(inode),
3066 .iap = sattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003067 .server = server,
3068 .bitmask = server->attr_bitmask,
David Quigley1775fd32013-05-22 12:50:42 -04003069 .label = ilabel,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003070 };
3071 struct nfs_setattrres res = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07003072 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003073 .label = olabel,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003074 .server = server,
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003075 };
Trond Myklebust8487c472016-06-26 08:44:35 -04003076 struct nfs4_exception exception = {
3077 .state = state,
3078 .inode = inode,
3079 .stateid = &arg.stateid,
3080 };
3081 int err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003082
David Quigleyaa9c2662013-05-22 12:50:44 -04003083 arg.bitmask = nfs4_bitmask(server, ilabel);
3084 if (ilabel)
3085 arg.bitmask = nfs4_bitmask(server, olabel);
3086
Linus Torvalds1da177e2005-04-16 15:20:36 -07003087 do {
NeilBrown29b59f92016-10-13 15:26:47 +11003088 err = _nfs4_do_setattr(inode, &arg, &res, cred, ctx);
Trond Myklebust451146b2012-04-18 16:29:11 -04003089 switch (err) {
3090 case -NFS4ERR_OPENMODE:
Trond Myklebust721ccfb2013-04-29 11:11:58 -04003091 if (!(sattr->ia_valid & ATTR_SIZE)) {
3092 pr_warn_once("NFSv4: server %s is incorrectly "
3093 "applying open mode checks to "
3094 "a SETATTR that is not "
3095 "changing file size.\n",
3096 server->nfs_client->cl_hostname);
3097 }
Trond Myklebust451146b2012-04-18 16:29:11 -04003098 if (state && !(state->state & FMODE_WRITE)) {
3099 err = -EBADF;
3100 if (sattr->ia_valid & ATTR_OPEN)
3101 err = -EACCES;
3102 goto out;
3103 }
3104 }
3105 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003106 } while (exception.retry);
Trond Myklebust451146b2012-04-18 16:29:11 -04003107out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003108 return err;
3109}
3110
Peng Tao500d7012015-09-22 11:35:22 +08003111static bool
3112nfs4_wait_on_layoutreturn(struct inode *inode, struct rpc_task *task)
3113{
3114 if (inode == NULL || !nfs_have_layout(inode))
3115 return false;
3116
3117 return pnfs_wait_on_layoutreturn(inode, task);
3118}
3119
Linus Torvalds1da177e2005-04-16 15:20:36 -07003120struct nfs4_closedata {
3121 struct inode *inode;
3122 struct nfs4_state *state;
3123 struct nfs_closeargs arg;
3124 struct nfs_closeres res;
Trond Myklebustcf805162016-11-15 14:56:07 -05003125 struct {
3126 struct nfs4_layoutreturn_args arg;
3127 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003128 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebustcf805162016-11-15 14:56:07 -05003129 u32 roc_barrier;
3130 bool roc;
3131 } lr;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003132 struct nfs_fattr fattr;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003133 unsigned long timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003134};
3135
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003136static void nfs4_free_closedata(void *data)
Trond Myklebust95121352005-10-18 14:20:12 -07003137{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003138 struct nfs4_closedata *calldata = data;
3139 struct nfs4_state_owner *sp = calldata->state->owner;
Al Viro643168c2011-06-22 18:20:23 -04003140 struct super_block *sb = calldata->state->inode->i_sb;
Trond Myklebust95121352005-10-18 14:20:12 -07003141
Trond Myklebustcf805162016-11-15 14:56:07 -05003142 if (calldata->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003143 pnfs_roc_release(&calldata->lr.arg, &calldata->lr.res,
3144 calldata->res.lr_ret);
Trond Myklebust95121352005-10-18 14:20:12 -07003145 nfs4_put_open_state(calldata->state);
3146 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust95121352005-10-18 14:20:12 -07003147 nfs4_put_state_owner(sp);
Trond Myklebust322b2b92013-01-11 16:39:51 -05003148 nfs_sb_deactive(sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003149 kfree(calldata);
3150}
3151
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003152static void nfs4_close_done(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003153{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003154 struct nfs4_closedata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003155 struct nfs4_state *state = calldata->state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003156 struct nfs_server *server = NFS_SERVER(calldata->inode);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003157 nfs4_stateid *res_stateid = NULL;
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003158 struct nfs4_exception exception = {
3159 .state = state,
3160 .inode = calldata->inode,
3161 .stateid = &calldata->arg.stateid,
3162 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003163
Chuck Levera3ca5652012-03-01 17:00:40 -05003164 dprintk("%s: begin!\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04003165 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
3166 return;
Trond Myklebust42113a72013-08-12 16:19:27 -04003167 trace_nfs4_close(state, &calldata->arg, &calldata->res, task->tk_status);
Trond Myklebustcf805162016-11-15 14:56:07 -05003168
3169 /* Handle Layoutreturn errors */
3170 if (calldata->arg.lr_args && task->tk_status != 0) {
3171 switch (calldata->res.lr_ret) {
3172 default:
3173 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
3174 break;
3175 case 0:
3176 calldata->arg.lr_args = NULL;
3177 calldata->res.lr_res = NULL;
3178 break;
Trond Myklebust73800202017-11-06 15:28:07 -05003179 case -NFS4ERR_OLD_STATEID:
3180 if (nfs4_refresh_layout_stateid(&calldata->arg.lr_args->stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003181 calldata->inode))
3182 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05003183 /* Fallthrough */
Trond Myklebustcf805162016-11-15 14:56:07 -05003184 case -NFS4ERR_ADMIN_REVOKED:
3185 case -NFS4ERR_DELEG_REVOKED:
3186 case -NFS4ERR_EXPIRED:
3187 case -NFS4ERR_BAD_STATEID:
Trond Myklebustcf805162016-11-15 14:56:07 -05003188 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
3189 case -NFS4ERR_WRONG_CRED:
3190 calldata->arg.lr_args = NULL;
3191 calldata->res.lr_res = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003192 goto lr_restart;
Trond Myklebustcf805162016-11-15 14:56:07 -05003193 }
3194 }
3195
Anna Schumakerd9b67e12017-01-11 15:04:25 -05003196 /* hmm. we are done with the inode, and in the process of freeing
Linus Torvalds1da177e2005-04-16 15:20:36 -07003197 * the state_owner. we keep this around to process errors
3198 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07003199 switch (task->tk_status) {
3200 case 0:
Trond Myklebust412f6c42014-08-25 22:09:08 -04003201 res_stateid = &calldata->res.stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003202 renew_lease(server, calldata->timestamp);
Trond Myklebust412f6c42014-08-25 22:09:08 -04003203 break;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003204 case -NFS4ERR_ACCESS:
3205 if (calldata->arg.bitmask != NULL) {
3206 calldata->arg.bitmask = NULL;
3207 calldata->res.fattr = NULL;
Trond Myklebust91b30d22017-11-06 15:28:09 -05003208 goto out_restart;
Trond Myklebustf07d4a32016-12-19 10:34:14 -05003209
3210 }
3211 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003212 case -NFS4ERR_OLD_STATEID:
3213 /* Did we race with OPEN? */
3214 if (nfs4_refresh_open_stateid(&calldata->arg.stateid,
Trond Myklebust91b30d22017-11-06 15:28:09 -05003215 state))
3216 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05003217 goto out_release;
Trond Myklebust69794ad2013-11-20 12:57:19 -05003218 case -NFS4ERR_ADMIN_REVOKED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003219 case -NFS4ERR_STALE_STATEID:
Trond Myklebust26d36302016-09-22 13:39:05 -04003220 case -NFS4ERR_EXPIRED:
3221 nfs4_free_revoked_stateid(server,
3222 &calldata->arg.stateid,
3223 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05003224 /* Fallthrough */
Trond Myklebust9e33bed2008-12-23 15:21:46 -05003225 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc82bac62017-11-06 15:28:06 -05003226 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003227 default:
Trond Myklebustb8b8d222017-11-07 10:51:37 -05003228 task->tk_status = nfs4_async_handle_exception(task,
3229 server, task->tk_status, &exception);
3230 if (exception.retry)
Trond Myklebust91b30d22017-11-06 15:28:09 -05003231 goto out_restart;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003232 }
Trond Myklebust4a1e2fe2015-08-30 18:37:59 -07003233 nfs_clear_open_stateid(state, &calldata->arg.stateid,
3234 res_stateid, calldata->arg.fmode);
Trond Myklebust69794ad2013-11-20 12:57:19 -05003235out_release:
Trond Myklebust91b30d22017-11-06 15:28:09 -05003236 task->tk_status = 0;
Trond Myklebust72211db2009-12-15 14:47:36 -05003237 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustd8d84982016-12-19 12:14:44 -05003238 nfs_refresh_inode(calldata->inode, &calldata->fattr);
Chuck Levera3ca5652012-03-01 17:00:40 -05003239 dprintk("%s: done, ret = %d!\n", __func__, task->tk_status);
Trond Myklebust91b30d22017-11-06 15:28:09 -05003240 return;
3241lr_restart:
3242 calldata->res.lr_ret = 0;
3243out_restart:
3244 task->tk_status = 0;
3245 rpc_restart_call_prepare(task);
3246 goto out_release;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003247}
3248
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003249static void nfs4_close_prepare(struct rpc_task *task, void *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003250{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003251 struct nfs4_closedata *calldata = data;
Trond Myklebust95121352005-10-18 14:20:12 -07003252 struct nfs4_state *state = calldata->state;
Trond Myklebust7fdab062012-09-20 20:15:57 -04003253 struct inode *inode = calldata->inode;
Trond Myklebustaee7af32014-08-25 22:33:12 -04003254 bool is_rdonly, is_wronly, is_rdwr;
Trond Myklebust88069f72009-12-08 08:33:16 -05003255 int call_close = 0;
Trond Myklebust95121352005-10-18 14:20:12 -07003256
Chuck Levera3ca5652012-03-01 17:00:40 -05003257 dprintk("%s: begin!\n", __func__);
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003258 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003259 goto out_wait;
Trond Myklebust003707c2007-07-05 18:07:55 -04003260
Trond Myklebust88069f72009-12-08 08:33:16 -05003261 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_OPEN_DOWNGRADE];
Trond Myklebust4cecb762005-11-04 15:32:58 -05003262 spin_lock(&state->owner->so_lock);
Trond Myklebustaee7af32014-08-25 22:33:12 -04003263 is_rdwr = test_bit(NFS_O_RDWR_STATE, &state->flags);
3264 is_rdonly = test_bit(NFS_O_RDONLY_STATE, &state->flags);
3265 is_wronly = test_bit(NFS_O_WRONLY_STATE, &state->flags);
Trond Myklebust003707c2007-07-05 18:07:55 -04003266 /* Calculate the change in open mode */
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003267 calldata->arg.fmode = 0;
Trond Myklebuste7616922006-01-03 09:55:13 +01003268 if (state->n_rdwr == 0) {
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003269 if (state->n_rdonly == 0)
3270 call_close |= is_rdonly;
3271 else if (is_rdonly)
3272 calldata->arg.fmode |= FMODE_READ;
3273 if (state->n_wronly == 0)
3274 call_close |= is_wronly;
3275 else if (is_wronly)
3276 calldata->arg.fmode |= FMODE_WRITE;
Trond Myklebuste547f262016-06-25 19:19:28 -04003277 if (calldata->arg.fmode != (FMODE_READ|FMODE_WRITE))
3278 call_close |= is_rdwr;
Trond Myklebustcd9288f2014-09-18 11:51:32 -04003279 } else if (is_rdwr)
3280 calldata->arg.fmode |= FMODE_READ|FMODE_WRITE;
3281
Trond Myklebust5cc78612016-11-14 11:19:56 -05003282 if (!nfs4_valid_open_stateid(state) ||
Trond Myklebustc82bac62017-11-06 15:28:06 -05003283 !nfs4_refresh_open_stateid(&calldata->arg.stateid, state))
Trond Myklebust5d422302013-03-14 16:57:48 -04003284 call_close = 0;
Trond Myklebust4cecb762005-11-04 15:32:58 -05003285 spin_unlock(&state->owner->so_lock);
Trond Myklebust88069f72009-12-08 08:33:16 -05003286
3287 if (!call_close) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003288 /* Note: exit _without_ calling nfs4_close_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003289 goto out_no_action;
Trond Myklebust95121352005-10-18 14:20:12 -07003290 }
Trond Myklebust88069f72009-12-08 08:33:16 -05003291
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003292 if (!calldata->lr.roc && nfs4_wait_on_layoutreturn(inode, task)) {
Peng Tao500d7012015-09-22 11:35:22 +08003293 nfs_release_seqid(calldata->arg.seqid);
3294 goto out_wait;
3295 }
3296
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003297 if (calldata->arg.fmode == 0)
Trond Myklebust88069f72009-12-08 08:33:16 -05003298 task->tk_msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE];
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003299
Trond Myklebust9413a1a2016-12-19 11:36:41 -05003300 if (calldata->arg.fmode == 0 || calldata->arg.fmode == FMODE_READ) {
Trond Myklebust3ecefc92016-10-27 18:25:04 -04003301 /* Close-to-open cache consistency revalidation */
3302 if (!nfs4_have_delegation(inode, FMODE_READ))
3303 calldata->arg.bitmask = NFS_SERVER(inode)->cache_consistency_bitmask;
3304 else
3305 calldata->arg.bitmask = NULL;
3306 }
Trond Myklebust3c13cb52015-08-18 23:45:13 -05003307
Trond Myklebust6ae37332015-01-30 14:21:14 -05003308 calldata->arg.share_access =
3309 nfs4_map_atomic_open_share(NFS_SERVER(inode),
3310 calldata->arg.fmode, 0);
Trond Myklebust88069f72009-12-08 08:33:16 -05003311
Trond Myklebustd8d84982016-12-19 12:14:44 -05003312 if (calldata->res.fattr == NULL)
3313 calldata->arg.bitmask = NULL;
3314 else if (calldata->arg.bitmask == NULL)
3315 calldata->res.fattr = NULL;
Trond Myklebust26e976a2006-01-03 09:55:21 +01003316 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05003317 if (nfs4_setup_sequence(NFS_SERVER(inode)->nfs_client,
Trond Myklebust9d12b212012-01-17 22:04:25 -05003318 &calldata->arg.seq_args,
3319 &calldata->res.seq_res,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04003320 task) != 0)
3321 nfs_release_seqid(calldata->arg.seqid);
Chuck Levera3ca5652012-03-01 17:00:40 -05003322 dprintk("%s: done!\n", __func__);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05003323 return;
3324out_no_action:
3325 task->tk_action = NULL;
3326out_wait:
3327 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003328}
3329
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003330static const struct rpc_call_ops nfs4_close_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01003331 .rpc_call_prepare = nfs4_close_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01003332 .rpc_call_done = nfs4_close_done,
3333 .rpc_release = nfs4_free_closedata,
3334};
3335
Linus Torvalds1da177e2005-04-16 15:20:36 -07003336/*
3337 * It is possible for data to be read/written from a mem-mapped file
3338 * after the sys_close call (which hits the vfs layer as a flush).
3339 * This means that we can't safely call nfsv4 close on a file until
3340 * the inode is cleared. This in turn means that we are not good
3341 * NFSv4 citizens - we do not indicate to the server to update the file's
3342 * share state even when we are done with one of the three share
3343 * stateid's in the inode.
3344 *
3345 * NOTE: Caller must be holding the sp->so_owner semaphore!
3346 */
Trond Myklebust1f7977c2012-09-20 20:31:51 -04003347int nfs4_do_close(struct nfs4_state *state, gfp_t gfp_mask, int wait)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003348{
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003349 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust63f5f792015-01-23 19:19:25 -05003350 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003351 struct nfs4_closedata *calldata;
Trond Myklebustb39e6252007-06-11 23:05:07 -04003352 struct nfs4_state_owner *sp = state->owner;
3353 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04003354 struct rpc_message msg = {
3355 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CLOSE],
3356 .rpc_cred = state->owner->so_cred,
3357 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04003358 struct rpc_task_setup task_setup_data = {
3359 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04003360 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003361 .callback_ops = &nfs4_close_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05003362 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04003363 .flags = RPC_TASK_ASYNC,
3364 };
Trond Myklebust95121352005-10-18 14:20:12 -07003365 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003366
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04003367 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_CLEANUP,
3368 &task_setup_data.rpc_client, &msg);
3369
Trond Myklebust8535b2b2010-05-13 12:51:01 -04003370 calldata = kzalloc(sizeof(*calldata), gfp_mask);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003371 if (calldata == NULL)
Trond Myklebust95121352005-10-18 14:20:12 -07003372 goto out;
Chuck Levera9c92d62013-08-09 12:48:18 -04003373 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 1);
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003374 calldata->inode = state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003375 calldata->state = state;
Trond Myklebust4a35bd42007-06-05 10:31:33 -04003376 calldata->arg.fh = NFS_FH(state->inode);
Trond Myklebustc82bac62017-11-06 15:28:06 -05003377 if (!nfs4_copy_open_stateid(&calldata->arg.stateid, state))
3378 goto out_free_calldata;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003379 /* Serialization for the sequence id */
Trond Myklebust63f5f792015-01-23 19:19:25 -05003380 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
3381 calldata->arg.seqid = alloc_seqid(&state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05003382 if (IS_ERR(calldata->arg.seqid))
Trond Myklebust95121352005-10-18 14:20:12 -07003383 goto out_free_calldata;
Trond Myklebustd8d84982016-12-19 12:14:44 -05003384 nfs_fattr_init(&calldata->fattr);
Trond Myklebustdc0b0272008-12-23 15:21:56 -05003385 calldata->arg.fmode = 0;
Trond Myklebust4d796d72016-09-23 11:38:08 -04003386 calldata->lr.arg.ld_private = &calldata->lr.ld_private;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003387 calldata->res.fattr = &calldata->fattr;
Trond Myklebustc1d51932008-04-07 13:20:54 -04003388 calldata->res.seqid = calldata->arg.seqid;
Trond Myklebust516a6af2005-10-27 22:12:41 -04003389 calldata->res.server = server;
Trond Myklebustcf805162016-11-15 14:56:07 -05003390 calldata->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05003391 calldata->lr.roc = pnfs_roc(state->inode,
3392 &calldata->lr.arg, &calldata->lr.res, msg.rpc_cred);
3393 if (calldata->lr.roc) {
3394 calldata->arg.lr_args = &calldata->lr.arg;
3395 calldata->res.lr_res = &calldata->lr.res;
3396 }
Al Viro643168c2011-06-22 18:20:23 -04003397 nfs_sb_active(calldata->inode->i_sb);
Trond Myklebust95121352005-10-18 14:20:12 -07003398
Trond Myklebust1174dd12010-12-21 10:52:24 -05003399 msg.rpc_argp = &calldata->arg;
3400 msg.rpc_resp = &calldata->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04003401 task_setup_data.callback_data = calldata;
3402 task = rpc_run_task(&task_setup_data);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003403 if (IS_ERR(task))
3404 return PTR_ERR(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003405 status = 0;
3406 if (wait)
3407 status = rpc_wait_for_completion_task(task);
Trond Myklebustb39e6252007-06-11 23:05:07 -04003408 rpc_put_task(task);
Trond Myklebusta49c3c72007-10-18 18:03:27 -04003409 return status;
Trond Myklebust95121352005-10-18 14:20:12 -07003410out_free_calldata:
3411 kfree(calldata);
3412out:
Trond Myklebustb39e6252007-06-11 23:05:07 -04003413 nfs4_put_open_state(state);
3414 nfs4_put_state_owner(sp);
Trond Myklebust95121352005-10-18 14:20:12 -07003415 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003416}
3417
Trond Myklebust2b484292010-09-17 10:56:51 -04003418static struct inode *
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003419nfs4_atomic_open(struct inode *dir, struct nfs_open_context *ctx,
3420 int open_flags, struct iattr *attr, int *opened)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003421{
Linus Torvalds1da177e2005-04-16 15:20:36 -07003422 struct nfs4_state *state;
David Quigleyaa9c2662013-05-22 12:50:44 -04003423 struct nfs4_label l = {0, 0, 0, NULL}, *label = NULL;
3424
3425 label = nfs4_label_init_security(dir, ctx->dentry, attr, &l);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003426
Trond Myklebust565277f2007-10-15 18:17:53 -04003427 /* Protect against concurrent sillydeletes */
Trond Myklebust5bc2afc2013-09-23 18:01:28 -04003428 state = nfs4_do_open(dir, ctx, open_flags, attr, label, opened);
David Quigleyaa9c2662013-05-22 12:50:44 -04003429
3430 nfs4_label_release_security(label);
3431
Trond Myklebustf46e0bd2010-09-17 10:56:50 -04003432 if (IS_ERR(state))
3433 return ERR_CAST(state);
Trond Myklebust275bb302013-05-29 13:11:28 -04003434 return state->inode;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003435}
3436
Trond Myklebust1185a552009-12-03 15:54:02 -05003437static void nfs4_close_context(struct nfs_open_context *ctx, int is_sync)
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003438{
3439 if (ctx->state == NULL)
3440 return;
3441 if (is_sync)
Al Viro643168c2011-06-22 18:20:23 -04003442 nfs4_close_sync(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003443 else
Al Viro643168c2011-06-22 18:20:23 -04003444 nfs4_close_state(ctx->state, ctx->mode);
Trond Myklebust7fe5c392009-03-19 15:35:50 -04003445}
Linus Torvalds1da177e2005-04-16 15:20:36 -07003446
Trond Myklebustb944dba2013-11-04 15:20:20 -05003447#define FATTR4_WORD1_NFS40_MASK (2*FATTR4_WORD1_MOUNTED_ON_FILEID - 1UL)
3448#define FATTR4_WORD2_NFS41_MASK (2*FATTR4_WORD2_SUPPATTR_EXCLCREAT - 1UL)
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05003449#define FATTR4_WORD2_NFS42_MASK (2*FATTR4_WORD2_MODE_UMASK - 1UL)
Trond Myklebustb944dba2013-11-04 15:20:20 -05003450
Linus Torvalds1da177e2005-04-16 15:20:36 -07003451static int _nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
3452{
Kinglong Mee8c612822015-08-26 21:12:58 +08003453 u32 bitmask[3] = {}, minorversion = server->nfs_client->cl_minorversion;
Benny Halevy43652ad2009-04-01 09:21:54 -04003454 struct nfs4_server_caps_arg args = {
3455 .fhandle = fhandle,
Kinglong Mee8c612822015-08-26 21:12:58 +08003456 .bitmask = bitmask,
Benny Halevy43652ad2009-04-01 09:21:54 -04003457 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07003458 struct nfs4_server_caps_res res = {};
3459 struct rpc_message msg = {
3460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SERVER_CAPS],
Benny Halevy43652ad2009-04-01 09:21:54 -04003461 .rpc_argp = &args,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003462 .rpc_resp = &res,
3463 };
3464 int status;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003465 int i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003466
Kinglong Mee8c612822015-08-26 21:12:58 +08003467 bitmask[0] = FATTR4_WORD0_SUPPORTED_ATTRS |
3468 FATTR4_WORD0_FH_EXPIRE_TYPE |
3469 FATTR4_WORD0_LINK_SUPPORT |
3470 FATTR4_WORD0_SYMLINK_SUPPORT |
3471 FATTR4_WORD0_ACLSUPPORT;
3472 if (minorversion)
3473 bitmask[2] = FATTR4_WORD2_SUPPATTR_EXCLCREAT;
3474
Bryan Schumaker7c513052011-03-24 17:12:24 +00003475 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003476 if (status == 0) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003477 /* Sanity check the server answers */
Kinglong Mee8c612822015-08-26 21:12:58 +08003478 switch (minorversion) {
Trond Myklebustb944dba2013-11-04 15:20:20 -05003479 case 0:
3480 res.attr_bitmask[1] &= FATTR4_WORD1_NFS40_MASK;
3481 res.attr_bitmask[2] = 0;
3482 break;
3483 case 1:
3484 res.attr_bitmask[2] &= FATTR4_WORD2_NFS41_MASK;
3485 break;
3486 case 2:
3487 res.attr_bitmask[2] &= FATTR4_WORD2_NFS42_MASK;
3488 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003489 memcpy(server->attr_bitmask, res.attr_bitmask, sizeof(server->attr_bitmask));
Trond Myklebust62ab4602009-08-09 15:06:19 -04003490 server->caps &= ~(NFS_CAP_ACLS|NFS_CAP_HARDLINKS|
3491 NFS_CAP_SYMLINKS|NFS_CAP_FILEID|
3492 NFS_CAP_MODE|NFS_CAP_NLINK|NFS_CAP_OWNER|
3493 NFS_CAP_OWNER_GROUP|NFS_CAP_ATIME|
Trond Myklebustb944dba2013-11-04 15:20:20 -05003494 NFS_CAP_CTIME|NFS_CAP_MTIME|
3495 NFS_CAP_SECURITY_LABEL);
Malahal Naineni7dd7d952014-01-23 08:54:55 -06003496 if (res.attr_bitmask[0] & FATTR4_WORD0_ACL &&
3497 res.acl_bitmask & ACL4_SUPPORT_ALLOW_ACL)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003498 server->caps |= NFS_CAP_ACLS;
3499 if (res.has_links != 0)
3500 server->caps |= NFS_CAP_HARDLINKS;
3501 if (res.has_symlinks != 0)
3502 server->caps |= NFS_CAP_SYMLINKS;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003503 if (res.attr_bitmask[0] & FATTR4_WORD0_FILEID)
3504 server->caps |= NFS_CAP_FILEID;
3505 if (res.attr_bitmask[1] & FATTR4_WORD1_MODE)
3506 server->caps |= NFS_CAP_MODE;
3507 if (res.attr_bitmask[1] & FATTR4_WORD1_NUMLINKS)
3508 server->caps |= NFS_CAP_NLINK;
3509 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER)
3510 server->caps |= NFS_CAP_OWNER;
3511 if (res.attr_bitmask[1] & FATTR4_WORD1_OWNER_GROUP)
3512 server->caps |= NFS_CAP_OWNER_GROUP;
3513 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_ACCESS)
3514 server->caps |= NFS_CAP_ATIME;
3515 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_METADATA)
3516 server->caps |= NFS_CAP_CTIME;
3517 if (res.attr_bitmask[1] & FATTR4_WORD1_TIME_MODIFY)
3518 server->caps |= NFS_CAP_MTIME;
David Quigleyaa9c2662013-05-22 12:50:44 -04003519#ifdef CONFIG_NFS_V4_SECURITY_LABEL
3520 if (res.attr_bitmask[2] & FATTR4_WORD2_SECURITY_LABEL)
3521 server->caps |= NFS_CAP_SECURITY_LABEL;
3522#endif
3523 memcpy(server->attr_bitmask_nl, res.attr_bitmask,
3524 sizeof(server->attr_bitmask));
Trond Myklebustb944dba2013-11-04 15:20:20 -05003525 server->attr_bitmask_nl[2] &= ~FATTR4_WORD2_SECURITY_LABEL;
Trond Myklebust62ab4602009-08-09 15:06:19 -04003526
Trond Myklebusta65318b2009-03-11 14:10:28 -04003527 memcpy(server->cache_consistency_bitmask, res.attr_bitmask, sizeof(server->cache_consistency_bitmask));
3528 server->cache_consistency_bitmask[0] &= FATTR4_WORD0_CHANGE|FATTR4_WORD0_SIZE;
3529 server->cache_consistency_bitmask[1] &= FATTR4_WORD1_TIME_METADATA|FATTR4_WORD1_TIME_MODIFY;
Trond Myklebustb944dba2013-11-04 15:20:20 -05003530 server->cache_consistency_bitmask[2] = 0;
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003531
3532 /* Avoid a regression due to buggy server */
3533 for (i = 0; i < ARRAY_SIZE(res.exclcreat_bitmask); i++)
3534 res.exclcreat_bitmask[i] &= res.attr_bitmask[i];
Kinglong Mee8c612822015-08-26 21:12:58 +08003535 memcpy(server->exclcreat_bitmask, res.exclcreat_bitmask,
3536 sizeof(server->exclcreat_bitmask));
Trond Myklebustf4b23de2017-05-09 15:47:15 -04003537
Linus Torvalds1da177e2005-04-16 15:20:36 -07003538 server->acl_bitmask = res.acl_bitmask;
Chuck Lever264e6352012-03-01 17:02:05 -05003539 server->fh_expire_type = res.fh_expire_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003540 }
Andy Adamsoncccef3b2009-04-01 09:22:03 -04003541
Linus Torvalds1da177e2005-04-16 15:20:36 -07003542 return status;
3543}
3544
Trond Myklebust55a97592006-06-09 09:34:19 -04003545int nfs4_server_capabilities(struct nfs_server *server, struct nfs_fh *fhandle)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003546{
3547 struct nfs4_exception exception = { };
3548 int err;
3549 do {
3550 err = nfs4_handle_exception(server,
3551 _nfs4_server_capabilities(server, fhandle),
3552 &exception);
3553 } while (exception.retry);
3554 return err;
3555}
3556
3557static int _nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3558 struct nfs_fsinfo *info)
3559{
David Quigleyaa9c2662013-05-22 12:50:44 -04003560 u32 bitmask[3];
Linus Torvalds1da177e2005-04-16 15:20:36 -07003561 struct nfs4_lookup_root_arg args = {
David Quigleyaa9c2662013-05-22 12:50:44 -04003562 .bitmask = bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003563 };
3564 struct nfs4_lookup_res res = {
3565 .server = server,
Trond Myklebust0e574af2005-10-27 22:12:38 -04003566 .fattr = info->fattr,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003567 .fh = fhandle,
3568 };
3569 struct rpc_message msg = {
3570 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP_ROOT],
3571 .rpc_argp = &args,
3572 .rpc_resp = &res,
3573 };
Benny Halevy008f55d2009-04-01 09:22:50 -04003574
David Quigleyaa9c2662013-05-22 12:50:44 -04003575 bitmask[0] = nfs4_fattr_bitmap[0];
3576 bitmask[1] = nfs4_fattr_bitmap[1];
3577 /*
3578 * Process the label in the upcoming getfattr
3579 */
3580 bitmask[2] = nfs4_fattr_bitmap[2] & ~FATTR4_WORD2_SECURITY_LABEL;
3581
Trond Myklebust0e574af2005-10-27 22:12:38 -04003582 nfs_fattr_init(info->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003583 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003584}
3585
3586static int nfs4_lookup_root(struct nfs_server *server, struct nfs_fh *fhandle,
3587 struct nfs_fsinfo *info)
3588{
3589 struct nfs4_exception exception = { };
3590 int err;
3591 do {
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003592 err = _nfs4_lookup_root(server, fhandle, info);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003593 trace_nfs4_lookup_root(server, fhandle, info->fattr, err);
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003594 switch (err) {
3595 case 0:
3596 case -NFS4ERR_WRONGSEC:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003597 goto out;
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003598 default:
3599 err = nfs4_handle_exception(server, err, &exception);
3600 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07003601 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04003602out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07003603 return err;
3604}
3605
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003606static int nfs4_lookup_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
3607 struct nfs_fsinfo *info, rpc_authflavor_t flavor)
3608{
Trond Myklebustc2190662013-08-26 19:23:04 -04003609 struct rpc_auth_create_args auth_args = {
3610 .pseudoflavor = flavor,
3611 };
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003612 struct rpc_auth *auth;
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003613
Trond Myklebustc2190662013-08-26 19:23:04 -04003614 auth = rpcauth_create(&auth_args, server->client);
Anna Schumaker9df13362017-01-11 16:30:08 -05003615 if (IS_ERR(auth))
3616 return -EACCES;
3617 return nfs4_lookup_root(server, fhandle, info);
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003618}
3619
Chuck Lever9a744ba2013-03-16 15:56:02 -04003620/*
3621 * Retry pseudoroot lookup with various security flavors. We do this when:
3622 *
3623 * NFSv4.0: the PUTROOTFH operation returns NFS4ERR_WRONGSEC
3624 * NFSv4.1: the server does not support the SECINFO_NO_NAME operation
3625 *
3626 * Returns zero on success, or a negative NFS4ERR value, or a
3627 * negative errno value.
3628 */
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003629static int nfs4_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
David Howells54ceac42006-08-22 20:06:13 -04003630 struct nfs_fsinfo *info)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003631{
Chuck Lever9a744ba2013-03-16 15:56:02 -04003632 /* Per 3530bis 15.33.5 */
3633 static const rpc_authflavor_t flav_array[] = {
3634 RPC_AUTH_GSS_KRB5P,
3635 RPC_AUTH_GSS_KRB5I,
3636 RPC_AUTH_GSS_KRB5,
Chuck Leverc4eafe12013-03-16 15:56:11 -04003637 RPC_AUTH_UNIX, /* courtesy */
Chuck Lever9a744ba2013-03-16 15:56:02 -04003638 RPC_AUTH_NULL,
3639 };
3640 int status = -EPERM;
3641 size_t i;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003642
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04003643 if (server->auth_info.flavor_len > 0) {
3644 /* try each flavor specified by user */
3645 for (i = 0; i < server->auth_info.flavor_len; i++) {
3646 status = nfs4_lookup_root_sec(server, fhandle, info,
3647 server->auth_info.flavors[i]);
3648 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3649 continue;
3650 break;
3651 }
3652 } else {
3653 /* no flavors specified by user, try default list */
3654 for (i = 0; i < ARRAY_SIZE(flav_array); i++) {
3655 status = nfs4_lookup_root_sec(server, fhandle, info,
3656 flav_array[i]);
3657 if (status == -NFS4ERR_WRONGSEC || status == -EACCES)
3658 continue;
3659 break;
3660 }
Bryan Schumaker8f70e952011-03-24 17:12:31 +00003661 }
Chuck Lever9a744ba2013-03-16 15:56:02 -04003662
Bryan Schumakerfb8a5ba2011-04-18 16:52:25 -04003663 /*
3664 * -EACCESS could mean that the user doesn't have correct permissions
3665 * to access the mount. It could also mean that we tried to mount
3666 * with a gss auth flavor, but rpc.gssd isn't running. Either way,
3667 * existing mount programs don't handle -EACCES very well so it should
3668 * be mapped to -EPERM instead.
3669 */
3670 if (status == -EACCES)
3671 status = -EPERM;
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003672 return status;
3673}
3674
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003675/**
3676 * nfs4_proc_get_rootfh - get file handle for server's pseudoroot
3677 * @server: initialized nfs_server handle
3678 * @fhandle: we fill in the pseudo-fs root file handle
3679 * @info: we fill in an FSINFO struct
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003680 * @auth_probe: probe the auth flavours
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003681 *
3682 * Returns zero on success, or a negative errno.
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003683 */
Bryan Schumaker3028eb22012-05-10 15:07:30 -04003684int nfs4_proc_get_rootfh(struct nfs_server *server, struct nfs_fh *fhandle,
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003685 struct nfs_fsinfo *info,
3686 bool auth_probe)
Bryan Schumaker801a16dc2011-04-13 14:31:30 -04003687{
Andre Przywarac7757072015-04-23 17:17:40 +01003688 int status = 0;
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003689
Andre Przywarac7757072015-04-23 17:17:40 +01003690 if (!auth_probe)
Trond Myklebust5e6b1992013-09-07 12:58:57 -04003691 status = nfs4_lookup_root(server, fhandle, info);
Andre Przywarac7757072015-04-23 17:17:40 +01003692
3693 if (auth_probe || status == NFS4ERR_WRONGSEC)
Trond Myklebust698c9372016-07-25 13:31:14 -04003694 status = server->nfs_client->cl_mvops->find_root_sec(server,
3695 fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003696
Linus Torvalds1da177e2005-04-16 15:20:36 -07003697 if (status == 0)
3698 status = nfs4_server_capabilities(server, fhandle);
3699 if (status == 0)
3700 status = nfs4_do_fsinfo(server, fhandle, info);
Chuck Lever2ed4b95b2013-03-16 15:55:45 -04003701
Trond Myklebustc12e87f2006-03-13 21:20:47 -08003702 return nfs4_map_errors(status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003703}
3704
Bryan Schumakerbae36242012-05-10 15:07:31 -04003705static int nfs4_proc_get_root(struct nfs_server *server, struct nfs_fh *mntfh,
3706 struct nfs_fsinfo *info)
3707{
3708 int error;
3709 struct nfs_fattr *fattr = info->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04003710 struct nfs4_label *label = NULL;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003711
3712 error = nfs4_server_capabilities(server, mntfh);
3713 if (error < 0) {
3714 dprintk("nfs4_get_root: getcaps error = %d\n", -error);
3715 return error;
3716 }
3717
David Quigley14c43f72013-05-22 12:50:43 -04003718 label = nfs4_label_alloc(server, GFP_KERNEL);
3719 if (IS_ERR(label))
3720 return PTR_ERR(label);
3721
David Quigley1775fd32013-05-22 12:50:42 -04003722 error = nfs4_proc_getattr(server, mntfh, fattr, label);
Bryan Schumakerbae36242012-05-10 15:07:31 -04003723 if (error < 0) {
3724 dprintk("nfs4_get_root: getattr error = %d\n", -error);
David Quigley14c43f72013-05-22 12:50:43 -04003725 goto err_free_label;
Bryan Schumakerbae36242012-05-10 15:07:31 -04003726 }
3727
3728 if (fattr->valid & NFS_ATTR_FATTR_FSID &&
3729 !nfs_fsid_equal(&server->fsid, &fattr->fsid))
3730 memcpy(&server->fsid, &fattr->fsid, sizeof(server->fsid));
3731
David Quigley14c43f72013-05-22 12:50:43 -04003732err_free_label:
3733 nfs4_label_free(label);
3734
Bryan Schumakerbae36242012-05-10 15:07:31 -04003735 return error;
3736}
3737
Manoj Naik6b97fd32006-06-09 09:34:29 -04003738/*
3739 * Get locations and (maybe) other attributes of a referral.
3740 * Note that we'll actually follow the referral later when
3741 * we detect fsid mismatch in inode revalidation
3742 */
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003743static int nfs4_get_referral(struct rpc_clnt *client, struct inode *dir,
3744 const struct qstr *name, struct nfs_fattr *fattr,
3745 struct nfs_fh *fhandle)
Manoj Naik6b97fd32006-06-09 09:34:29 -04003746{
3747 int status = -ENOMEM;
3748 struct page *page = NULL;
3749 struct nfs4_fs_locations *locations = NULL;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003750
3751 page = alloc_page(GFP_KERNEL);
3752 if (page == NULL)
3753 goto out;
3754 locations = kmalloc(sizeof(struct nfs4_fs_locations), GFP_KERNEL);
3755 if (locations == NULL)
3756 goto out;
3757
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003758 status = nfs4_proc_fs_locations(client, dir, name, locations, page);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003759 if (status != 0)
3760 goto out;
Chuck Lever519ae252013-10-17 14:13:19 -04003761
3762 /*
3763 * If the fsid didn't change, this is a migration event, not a
3764 * referral. Cause us to drop into the exception handler, which
3765 * will kick off migration recovery.
3766 */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003767 if (nfs_fsid_equal(&NFS_SERVER(dir)->fsid, &locations->fattr.fsid)) {
Andy Adamson533eb462011-06-13 18:25:56 -04003768 dprintk("%s: server did not return a different fsid for"
3769 " a referral at %s\n", __func__, name->name);
Chuck Lever519ae252013-10-17 14:13:19 -04003770 status = -NFS4ERR_MOVED;
Manoj Naik6b97fd32006-06-09 09:34:29 -04003771 goto out;
3772 }
Andy Adamson533eb462011-06-13 18:25:56 -04003773 /* Fixup attributes for the nfs_lookup() call to nfs_fhget() */
3774 nfs_fixup_referral_attributes(&locations->fattr);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003775
Andy Adamson533eb462011-06-13 18:25:56 -04003776 /* replace the lookup nfs_fattr with the locations nfs_fattr */
Manoj Naik6b97fd32006-06-09 09:34:29 -04003777 memcpy(fattr, &locations->fattr, sizeof(struct nfs_fattr));
Manoj Naik6b97fd32006-06-09 09:34:29 -04003778 memset(fhandle, 0, sizeof(struct nfs_fh));
3779out:
3780 if (page)
3781 __free_page(page);
Davidlohr Bueso5d7ca352010-08-11 12:42:15 -04003782 kfree(locations);
Manoj Naik6b97fd32006-06-09 09:34:29 -04003783 return status;
3784}
3785
David Quigley1775fd32013-05-22 12:50:42 -04003786static int _nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3787 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003788{
3789 struct nfs4_getattr_arg args = {
3790 .fh = fhandle,
3791 .bitmask = server->attr_bitmask,
3792 };
3793 struct nfs4_getattr_res res = {
3794 .fattr = fattr,
David Quigley1775fd32013-05-22 12:50:42 -04003795 .label = label,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003796 .server = server,
3797 };
3798 struct rpc_message msg = {
3799 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
3800 .rpc_argp = &args,
3801 .rpc_resp = &res,
3802 };
David Quigleyaa9c2662013-05-22 12:50:44 -04003803
3804 args.bitmask = nfs4_bitmask(server, label);
3805
Trond Myklebust0e574af2005-10-27 22:12:38 -04003806 nfs_fattr_init(fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00003807 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003808}
3809
David Quigley1775fd32013-05-22 12:50:42 -04003810static int nfs4_proc_getattr(struct nfs_server *server, struct nfs_fh *fhandle,
3811 struct nfs_fattr *fattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003812{
3813 struct nfs4_exception exception = { };
3814 int err;
3815 do {
Trond Myklebustb5f875a2013-08-13 13:01:39 -04003816 err = _nfs4_proc_getattr(server, fhandle, fattr, label);
3817 trace_nfs4_getattr(server, fhandle, fattr, err);
3818 err = nfs4_handle_exception(server, err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07003819 &exception);
3820 } while (exception.retry);
3821 return err;
3822}
3823
3824/*
3825 * The file is not closed if it is opened due to the a request to change
3826 * the size of the file. The open call will not be needed once the
3827 * VFS layer lookup-intents are implemented.
3828 *
3829 * Close is called when the inode is destroyed.
3830 * If we haven't opened the file for O_WRONLY, we
3831 * need to in the size_change case to obtain a stateid.
3832 *
3833 * Got race?
3834 * Because OPEN is always done by name in nfsv4, it is
3835 * possible that we opened a different file by the same
3836 * name. We can recognize this race condition, but we
3837 * can't do anything about it besides returning an error.
3838 *
3839 * This will be fixed with VFS changes (lookup-intent).
3840 */
3841static int
3842nfs4_proc_setattr(struct dentry *dentry, struct nfs_fattr *fattr,
3843 struct iattr *sattr)
3844{
David Howells2b0143b2015-03-17 22:25:59 +00003845 struct inode *inode = d_inode(dentry);
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003846 struct rpc_cred *cred = NULL;
NeilBrown29b59f92016-10-13 15:26:47 +11003847 struct nfs_open_context *ctx = NULL;
David Quigley14c43f72013-05-22 12:50:43 -04003848 struct nfs4_label *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003849 int status;
3850
Peng Tao88ac8152014-09-12 11:04:10 +08003851 if (pnfs_ld_layoutret_on_setattr(inode) &&
3852 sattr->ia_valid & ATTR_SIZE &&
3853 sattr->ia_size < i_size_read(inode))
Trond Myklebust24028672013-03-20 13:23:33 -04003854 pnfs_commit_and_return_layout(inode);
Benny Halevy8a1636c2010-07-14 15:43:57 -04003855
Trond Myklebust0e574af2005-10-27 22:12:38 -04003856 nfs_fattr_init(fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003857
Andy Adamson26699402012-05-30 16:12:24 -04003858 /* Deal with open(O_TRUNC) */
3859 if (sattr->ia_valid & ATTR_OPEN)
Nadav Shemercc7936f2013-07-21 17:21:43 +03003860 sattr->ia_valid &= ~(ATTR_MTIME|ATTR_CTIME);
Andy Adamson26699402012-05-30 16:12:24 -04003861
3862 /* Optimization: if the end result is no change, don't RPC */
Nadav Shemercc7936f2013-07-21 17:21:43 +03003863 if ((sattr->ia_valid & ~(ATTR_FILE|ATTR_OPEN)) == 0)
Andy Adamson26699402012-05-30 16:12:24 -04003864 return 0;
3865
Trond Myklebustd5308382005-11-04 15:33:38 -05003866 /* Search for an existing open(O_WRITE) file */
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003867 if (sattr->ia_valid & ATTR_FILE) {
Trond Myklebust08e9eac2005-06-22 17:16:29 +00003868
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003869 ctx = nfs_file_open_context(sattr->ia_file);
NeilBrown29b59f92016-10-13 15:26:47 +11003870 if (ctx)
Neil Brown504e5182008-10-16 14:15:16 +11003871 cred = ctx->cred;
Trond Myklebust659bfcd2008-06-10 19:39:41 -04003872 }
3873
David Quigley14c43f72013-05-22 12:50:43 -04003874 label = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
3875 if (IS_ERR(label))
3876 return PTR_ERR(label);
3877
Trond Myklebust199366f2018-03-20 16:43:18 -04003878 /* Return any delegations if we're going to change ACLs */
3879 if ((sattr->ia_valid & (ATTR_MODE|ATTR_UID|ATTR_GID)) != 0)
3880 nfs4_inode_return_delegation(inode);
3881
NeilBrown29b59f92016-10-13 15:26:47 +11003882 status = nfs4_do_setattr(inode, cred, fattr, sattr, ctx, NULL, label);
David Quigleyaa9c2662013-05-22 12:50:44 -04003883 if (status == 0) {
Trond Myklebustf0446362015-02-26 16:09:04 -05003884 nfs_setattr_update_inode(inode, sattr, fattr);
David Quigleyaa9c2662013-05-22 12:50:44 -04003885 nfs_setsecurity(inode, fattr, label);
3886 }
David Quigley14c43f72013-05-22 12:50:43 -04003887 nfs4_label_free(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003888 return status;
3889}
3890
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003891static int _nfs4_proc_lookup(struct rpc_clnt *clnt, struct inode *dir,
3892 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003893 struct nfs_fattr *fattr, struct nfs4_label *label)
David Howells2b3de442006-08-22 20:06:09 -04003894{
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003895 struct nfs_server *server = NFS_SERVER(dir);
David Howells2b3de442006-08-22 20:06:09 -04003896 int status;
3897 struct nfs4_lookup_arg args = {
3898 .bitmask = server->attr_bitmask,
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003899 .dir_fh = NFS_FH(dir),
David Howells2b3de442006-08-22 20:06:09 -04003900 .name = name,
3901 };
3902 struct nfs4_lookup_res res = {
3903 .server = server,
3904 .fattr = fattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04003905 .label = label,
David Howells2b3de442006-08-22 20:06:09 -04003906 .fh = fhandle,
3907 };
3908 struct rpc_message msg = {
3909 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUP],
3910 .rpc_argp = &args,
3911 .rpc_resp = &res,
3912 };
3913
David Quigleyaa9c2662013-05-22 12:50:44 -04003914 args.bitmask = nfs4_bitmask(server, label);
3915
David Howells2b3de442006-08-22 20:06:09 -04003916 nfs_fattr_init(fattr);
3917
Linus Torvalds1da177e2005-04-16 15:20:36 -07003918 dprintk("NFS call lookup %s\n", name->name);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003919 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07003920 dprintk("NFS reply lookup: %d\n", status);
3921 return status;
3922}
3923
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003924static void nfs_fixup_secinfo_attributes(struct nfs_fattr *fattr)
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003925{
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003926 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003927 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_MOUNTPOINT;
Bryan Schumaker7ebb9312011-03-24 17:12:30 +00003928 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
3929 fattr->nlink = 2;
3930}
3931
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003932static int nfs4_proc_lookup_common(struct rpc_clnt **clnt, struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04003933 const struct qstr *name, struct nfs_fh *fhandle,
David Quigley1775fd32013-05-22 12:50:42 -04003934 struct nfs_fattr *fattr, struct nfs4_label *label)
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003935{
3936 struct nfs4_exception exception = { };
3937 struct rpc_clnt *client = *clnt;
3938 int err;
3939 do {
David Quigley1775fd32013-05-22 12:50:42 -04003940 err = _nfs4_proc_lookup(client, dir, name, fhandle, fattr, label);
Trond Myklebust078ea3d2013-08-12 16:45:55 -04003941 trace_nfs4_lookup(dir, name, err);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003942 switch (err) {
3943 case -NFS4ERR_BADNAME:
3944 err = -ENOENT;
3945 goto out;
3946 case -NFS4ERR_MOVED:
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003947 err = nfs4_get_referral(client, dir, name, fattr, fhandle);
Dominique Martinetc86c90c2015-06-04 17:04:17 +02003948 if (err == -NFS4ERR_MOVED)
3949 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003950 goto out;
3951 case -NFS4ERR_WRONGSEC:
3952 err = -EPERM;
3953 if (client != *clnt)
3954 goto out;
Andy Adamson66b06862014-06-12 15:02:32 -04003955 client = nfs4_negotiate_security(client, dir, name);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003956 if (IS_ERR(client))
3957 return PTR_ERR(client);
3958
3959 exception.retry = 1;
3960 break;
3961 default:
3962 err = nfs4_handle_exception(NFS_SERVER(dir), err, &exception);
3963 }
3964 } while (exception.retry);
3965
3966out:
3967 if (err == 0)
3968 *clnt = client;
3969 else if (client != *clnt)
3970 rpc_shutdown_client(client);
3971
3972 return err;
3973}
3974
Al Virobeffb8f2016-07-20 16:34:42 -04003975static int nfs4_proc_lookup(struct inode *dir, const struct qstr *name,
David Quigley1775fd32013-05-22 12:50:42 -04003976 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
3977 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07003978{
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003979 int status;
3980 struct rpc_clnt *client = NFS_CLIENT(dir);
Trond Myklebust0c2e53f2011-10-18 16:11:22 -07003981
David Quigley1775fd32013-05-22 12:50:42 -04003982 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, label);
Bryan Schumaker72de53e2012-04-27 13:27:40 -04003983 if (client != NFS_CLIENT(dir)) {
3984 rpc_shutdown_client(client);
3985 nfs_fixup_secinfo_attributes(fattr);
3986 }
3987 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07003988}
3989
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003990struct rpc_clnt *
Al Virobeffb8f2016-07-20 16:34:42 -04003991nfs4_proc_lookup_mountpoint(struct inode *dir, const struct qstr *name,
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003992 struct nfs_fh *fhandle, struct nfs_fattr *fattr)
3993{
Trond Myklebustb72888c2013-08-07 20:38:07 -04003994 struct rpc_clnt *client = NFS_CLIENT(dir);
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003995 int status;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003996
David Quigley1775fd32013-05-22 12:50:42 -04003997 status = nfs4_proc_lookup_common(&client, dir, name, fhandle, fattr, NULL);
Trond Myklebustb72888c2013-08-07 20:38:07 -04003998 if (status < 0)
Bryan Schumakerf05d1472012-04-27 13:27:41 -04003999 return ERR_PTR(status);
Trond Myklebustb72888c2013-08-07 20:38:07 -04004000 return (client == NFS_CLIENT(dir)) ? rpc_clone_client(client) : client;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04004001}
4002
Jeff Layton5b5faaf2017-06-29 06:34:52 -07004003static int _nfs4_proc_lookupp(struct inode *inode,
4004 struct nfs_fh *fhandle, struct nfs_fattr *fattr,
4005 struct nfs4_label *label)
4006{
4007 struct rpc_clnt *clnt = NFS_CLIENT(inode);
4008 struct nfs_server *server = NFS_SERVER(inode);
4009 int status;
4010 struct nfs4_lookupp_arg args = {
4011 .bitmask = server->attr_bitmask,
4012 .fh = NFS_FH(inode),
4013 };
4014 struct nfs4_lookupp_res res = {
4015 .server = server,
4016 .fattr = fattr,
4017 .label = label,
4018 .fh = fhandle,
4019 };
4020 struct rpc_message msg = {
4021 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOOKUPP],
4022 .rpc_argp = &args,
4023 .rpc_resp = &res,
4024 };
4025
4026 args.bitmask = nfs4_bitmask(server, label);
4027
4028 nfs_fattr_init(fattr);
4029
4030 dprintk("NFS call lookupp ino=0x%lx\n", inode->i_ino);
4031 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
4032 &res.seq_res, 0);
4033 dprintk("NFS reply lookupp: %d\n", status);
4034 return status;
4035}
4036
4037static int nfs4_proc_lookupp(struct inode *inode, struct nfs_fh *fhandle,
4038 struct nfs_fattr *fattr, struct nfs4_label *label)
4039{
4040 struct nfs4_exception exception = { };
4041 int err;
4042 do {
4043 err = _nfs4_proc_lookupp(inode, fhandle, fattr, label);
4044 trace_nfs4_lookupp(inode, err);
4045 err = nfs4_handle_exception(NFS_SERVER(inode), err,
4046 &exception);
4047 } while (exception.retry);
4048 return err;
4049}
4050
Linus Torvalds1da177e2005-04-16 15:20:36 -07004051static int _nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4052{
Trond Myklebust76b32992007-08-10 17:45:11 -04004053 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004054 struct nfs4_accessargs args = {
4055 .fh = NFS_FH(inode),
Trond Myklebusta4980e72012-01-30 15:43:56 -05004056 .bitmask = server->cache_consistency_bitmask,
Anna Schumaker1750d922017-07-26 12:00:21 -04004057 .access = entry->mask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004058 };
Trond Myklebust76b32992007-08-10 17:45:11 -04004059 struct nfs4_accessres res = {
4060 .server = server,
Trond Myklebust76b32992007-08-10 17:45:11 -04004061 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004062 struct rpc_message msg = {
4063 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_ACCESS],
4064 .rpc_argp = &args,
4065 .rpc_resp = &res,
4066 .rpc_cred = entry->cred,
4067 };
David Quigleyaa9c2662013-05-22 12:50:44 -04004068 int status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004069
Trond Myklebustc407d412010-04-16 16:22:48 -04004070 res.fattr = nfs_alloc_fattr();
4071 if (res.fattr == NULL)
4072 return -ENOMEM;
4073
Bryan Schumaker7c513052011-03-24 17:12:24 +00004074 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004075 if (!status) {
Weston Andros Adamson6168f622012-09-10 14:00:46 -04004076 nfs_access_set_mask(entry, res.access);
Trond Myklebustc407d412010-04-16 16:22:48 -04004077 nfs_refresh_inode(inode, res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004078 }
Trond Myklebustc407d412010-04-16 16:22:48 -04004079 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004080 return status;
4081}
4082
4083static int nfs4_proc_access(struct inode *inode, struct nfs_access_entry *entry)
4084{
4085 struct nfs4_exception exception = { };
4086 int err;
4087 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004088 err = _nfs4_proc_access(inode, entry);
4089 trace_nfs4_access(inode, err);
4090 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004091 &exception);
4092 } while (exception.retry);
4093 return err;
4094}
4095
4096/*
4097 * TODO: For the time being, we don't try to get any attributes
4098 * along with any of the zero-copy operations READ, READDIR,
4099 * READLINK, WRITE.
4100 *
4101 * In the case of the first three, we want to put the GETATTR
4102 * after the read-type operation -- this is because it is hard
4103 * to predict the length of a GETATTR response in v4, and thus
4104 * align the READ data correctly. This means that the GETATTR
4105 * may end up partially falling into the page cache, and we should
4106 * shift it into the 'tail' of the xdr_buf before processing.
4107 * To do this efficiently, we need to know the total length
4108 * of data received, which doesn't seem to be available outside
4109 * of the RPC layer.
4110 *
4111 * In the case of WRITE, we also want to put the GETATTR after
4112 * the operation -- in this case because we want to make sure
Trond Myklebust140150d2012-06-05 15:20:25 -04004113 * we get the post-operation mtime and size.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004114 *
4115 * Both of these changes to the XDR layer would in fact be quite
4116 * minor, but I decided to leave them for a subsequent patch.
4117 */
4118static int _nfs4_proc_readlink(struct inode *inode, struct page *page,
4119 unsigned int pgbase, unsigned int pglen)
4120{
4121 struct nfs4_readlink args = {
4122 .fh = NFS_FH(inode),
4123 .pgbase = pgbase,
4124 .pglen = pglen,
4125 .pages = &page,
4126 };
Benny Halevyf50c7002009-04-01 09:21:55 -04004127 struct nfs4_readlink_res res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004128 struct rpc_message msg = {
4129 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READLINK],
4130 .rpc_argp = &args,
Benny Halevyf50c7002009-04-01 09:21:55 -04004131 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004132 };
4133
Bryan Schumaker7c513052011-03-24 17:12:24 +00004134 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 -07004135}
4136
4137static int nfs4_proc_readlink(struct inode *inode, struct page *page,
4138 unsigned int pgbase, unsigned int pglen)
4139{
4140 struct nfs4_exception exception = { };
4141 int err;
4142 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004143 err = _nfs4_proc_readlink(inode, page, pgbase, pglen);
4144 trace_nfs4_readlink(inode, err);
4145 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004146 &exception);
4147 } while (exception.retry);
4148 return err;
4149}
4150
Linus Torvalds1da177e2005-04-16 15:20:36 -07004151/*
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004152 * This is just for mknod. open(O_CREAT) will always do ->open_context().
Linus Torvalds1da177e2005-04-16 15:20:36 -07004153 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07004154static int
4155nfs4_proc_create(struct inode *dir, struct dentry *dentry, struct iattr *sattr,
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004156 int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004157{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004158 struct nfs_server *server = NFS_SERVER(dir);
David Quigleyaa9c2662013-05-22 12:50:44 -04004159 struct nfs4_label l, *ilabel = NULL;
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004160 struct nfs_open_context *ctx;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004161 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004162 int status = 0;
4163
NeilBrown532d4de2016-10-13 15:26:47 +11004164 ctx = alloc_nfs_open_context(dentry, FMODE_READ, NULL);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004165 if (IS_ERR(ctx))
4166 return PTR_ERR(ctx);
4167
David Quigleyaa9c2662013-05-22 12:50:44 -04004168 ilabel = nfs4_label_init_security(dir, dentry, sattr, &l);
4169
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004170 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4171 sattr->ia_mode &= ~current_umask();
Kinglong Meec5c3fb52015-08-26 21:11:39 +08004172 state = nfs4_do_open(dir, ctx, flags, sattr, ilabel, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004173 if (IS_ERR(state)) {
4174 status = PTR_ERR(state);
Trond Myklebustc0204fd2010-09-17 10:56:51 -04004175 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004176 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004177out:
David Quigleyaa9c2662013-05-22 12:50:44 -04004178 nfs4_label_release_security(ilabel);
Miklos Szeredi8867fe52012-06-05 15:10:19 +02004179 put_nfs_open_context(ctx);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004180 return status;
4181}
4182
Al Virobeffb8f2016-07-20 16:34:42 -04004183static int _nfs4_proc_remove(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004184{
Trond Myklebust16e42952005-10-27 22:12:44 -04004185 struct nfs_server *server = NFS_SERVER(dir);
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004186 struct nfs_removeargs args = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07004187 .fh = NFS_FH(dir),
Linus Torvalds26fe5752012-05-10 13:14:12 -07004188 .name = *name,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004189 };
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004190 struct nfs_removeres res = {
Trond Myklebust16e42952005-10-27 22:12:44 -04004191 .server = server,
Trond Myklebust16e42952005-10-27 22:12:44 -04004192 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004193 struct rpc_message msg = {
Trond Myklebust4fdc17b2007-07-14 15:39:57 -04004194 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE],
4195 .rpc_argp = &args,
4196 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004197 };
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004198 unsigned long timestamp = jiffies;
Trond Myklebust778d2812012-04-27 13:48:19 -04004199 int status;
Trond Myklebustd3468902010-04-16 16:22:50 -04004200
Bryan Schumaker7c513052011-03-24 17:12:24 +00004201 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 1);
Trond Myklebust778d2812012-04-27 13:48:19 -04004202 if (status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004203 update_changeattr(dir, &res.cinfo, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004204 return status;
4205}
4206
Trond Myklebust912678d2018-03-20 16:43:15 -04004207static int nfs4_proc_remove(struct inode *dir, struct dentry *dentry)
4208{
4209 struct nfs4_exception exception = { };
4210 struct inode *inode = d_inode(dentry);
4211 int err;
4212
4213 if (inode)
4214 nfs4_inode_return_delegation(inode);
4215 do {
4216 err = _nfs4_proc_remove(dir, &dentry->d_name);
4217 trace_nfs4_remove(dir, &dentry->d_name, err);
4218 err = nfs4_handle_exception(NFS_SERVER(dir), err,
4219 &exception);
4220 } while (exception.retry);
4221 return err;
4222}
4223
4224static int nfs4_proc_rmdir(struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004225{
4226 struct nfs4_exception exception = { };
4227 int err;
Trond Myklebust912678d2018-03-20 16:43:15 -04004228
Linus Torvalds1da177e2005-04-16 15:20:36 -07004229 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004230 err = _nfs4_proc_remove(dir, name);
4231 trace_nfs4_remove(dir, name, err);
4232 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004233 &exception);
4234 } while (exception.retry);
4235 return err;
4236}
4237
Trond Myklebust977fcc22018-03-20 16:43:17 -04004238static void nfs4_proc_unlink_setup(struct rpc_message *msg, struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004239{
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004240 struct nfs_removeargs *args = msg->rpc_argp;
4241 struct nfs_removeres *res = msg->rpc_resp;
Trond Myklebust977fcc22018-03-20 16:43:17 -04004242 struct inode *inode = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004243
Trond Myklebust977fcc22018-03-20 16:43:17 -04004244 res->server = NFS_SB(dentry->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004245 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_REMOVE];
Chuck Levera9c92d62013-08-09 12:48:18 -04004246 nfs4_init_sequence(&args->seq_args, &res->seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04004247
4248 nfs_fattr_init(res->dir_attr);
Trond Myklebust977fcc22018-03-20 16:43:17 -04004249
4250 if (inode)
4251 nfs4_inode_return_delegation(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004252}
4253
Bryan Schumaker34e137c2012-03-19 14:54:41 -04004254static void nfs4_proc_unlink_rpc_prepare(struct rpc_task *task, struct nfs_unlinkdata *data)
4255{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004256 nfs4_setup_sequence(NFS_SB(data->dentry->d_sb)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004257 &data->args.seq_args,
4258 &data->res.seq_res,
4259 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004260}
4261
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004262static int nfs4_proc_unlink_done(struct rpc_task *task, struct inode *dir)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004263{
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004264 struct nfs_unlinkdata *data = task->tk_calldata;
4265 struct nfs_removeres *res = &data->res;
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004266
Trond Myklebust14516c32010-07-31 14:29:06 -04004267 if (!nfs4_sequence_done(task, &res->seq_res))
4268 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004269 if (nfs4_async_handle_error(task, res->server, NULL,
4270 &data->timeout) == -EAGAIN)
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004271 return 0;
Trond Myklebustc40d52f2017-01-11 12:36:11 -05004272 if (task->tk_status == 0)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004273 update_changeattr(dir, &res->cinfo, res->dir_attr->time_start);
Trond Myklebuste4eff1a2007-07-14 15:39:58 -04004274 return 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004275}
4276
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004277static void nfs4_proc_rename_setup(struct rpc_message *msg,
4278 struct dentry *old_dentry,
4279 struct dentry *new_dentry)
Jeff Laytond3d41522010-09-17 17:31:57 -04004280{
Jeff Laytond3d41522010-09-17 17:31:57 -04004281 struct nfs_renameargs *arg = msg->rpc_argp;
4282 struct nfs_renameres *res = msg->rpc_resp;
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004283 struct inode *old_inode = d_inode(old_dentry);
4284 struct inode *new_inode = d_inode(new_dentry);
Jeff Laytond3d41522010-09-17 17:31:57 -04004285
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004286 if (old_inode)
4287 nfs4_inode_return_delegation(old_inode);
4288 if (new_inode)
4289 nfs4_inode_return_delegation(new_inode);
Jeff Laytond3d41522010-09-17 17:31:57 -04004290 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENAME];
Trond Myklebustf2c2c552018-03-20 16:43:16 -04004291 res->server = NFS_SB(old_dentry->d_sb);
Chuck Levera9c92d62013-08-09 12:48:18 -04004292 nfs4_init_sequence(&arg->seq_args, &res->seq_res, 1);
Jeff Laytond3d41522010-09-17 17:31:57 -04004293}
4294
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04004295static void nfs4_proc_rename_rpc_prepare(struct rpc_task *task, struct nfs_renamedata *data)
4296{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004297 nfs4_setup_sequence(NFS_SERVER(data->old_dir)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004298 &data->args.seq_args,
4299 &data->res.seq_res,
4300 task);
Jeff Laytond3d41522010-09-17 17:31:57 -04004301}
4302
4303static int nfs4_proc_rename_done(struct rpc_task *task, struct inode *old_dir,
4304 struct inode *new_dir)
4305{
Trond Myklebustfbc6f7c2013-08-12 17:08:26 -04004306 struct nfs_renamedata *data = task->tk_calldata;
4307 struct nfs_renameres *res = &data->res;
Jeff Laytond3d41522010-09-17 17:31:57 -04004308
4309 if (!nfs4_sequence_done(task, &res->seq_res))
4310 return 0;
NeilBrown8478eaa2014-09-18 16:09:27 +10004311 if (nfs4_async_handle_error(task, res->server, NULL, &data->timeout) == -EAGAIN)
Jeff Laytond3d41522010-09-17 17:31:57 -04004312 return 0;
4313
Trond Myklebustc733c492017-01-11 12:32:26 -05004314 if (task->tk_status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004315 update_changeattr(old_dir, &res->old_cinfo, res->old_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004316 if (new_dir != old_dir)
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004317 update_changeattr(new_dir, &res->new_cinfo, res->new_fattr->time_start);
Trond Myklebustc733c492017-01-11 12:32:26 -05004318 }
Jeff Laytond3d41522010-09-17 17:31:57 -04004319 return 1;
4320}
4321
Al Virobeffb8f2016-07-20 16:34:42 -04004322static int _nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004323{
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004324 struct nfs_server *server = NFS_SERVER(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004325 struct nfs4_link_arg arg = {
4326 .fh = NFS_FH(inode),
4327 .dir_fh = NFS_FH(dir),
4328 .name = name,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004329 .bitmask = server->attr_bitmask,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004330 };
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004331 struct nfs4_link_res res = {
4332 .server = server,
David Quigley1775fd32013-05-22 12:50:42 -04004333 .label = NULL,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004334 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004335 struct rpc_message msg = {
4336 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LINK],
4337 .rpc_argp = &arg,
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004338 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004339 };
Trond Myklebust136f2622010-04-16 16:22:49 -04004340 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004341
Trond Myklebust136f2622010-04-16 16:22:49 -04004342 res.fattr = nfs_alloc_fattr();
Trond Myklebust778d2812012-04-27 13:48:19 -04004343 if (res.fattr == NULL)
Trond Myklebust136f2622010-04-16 16:22:49 -04004344 goto out;
4345
David Quigley14c43f72013-05-22 12:50:43 -04004346 res.label = nfs4_label_alloc(server, GFP_KERNEL);
4347 if (IS_ERR(res.label)) {
4348 status = PTR_ERR(res.label);
4349 goto out;
4350 }
David Quigleyaa9c2662013-05-22 12:50:44 -04004351 arg.bitmask = nfs4_bitmask(server, res.label);
David Quigley14c43f72013-05-22 12:50:43 -04004352
Trond Myklebust9f768272018-03-20 16:43:14 -04004353 nfs4_inode_return_delegation(inode);
4354
Bryan Schumaker7c513052011-03-24 17:12:24 +00004355 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004356 if (!status) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004357 update_changeattr(dir, &res.cinfo, res.fattr->time_start);
David Quigleyaa9c2662013-05-22 12:50:44 -04004358 status = nfs_post_op_update_inode(inode, res.fattr);
4359 if (!status)
4360 nfs_setsecurity(inode, res.fattr, res.label);
Trond Myklebust91ba2ee2005-10-27 22:12:42 -04004361 }
David Quigley14c43f72013-05-22 12:50:43 -04004362
4363
4364 nfs4_label_free(res.label);
4365
Trond Myklebust136f2622010-04-16 16:22:49 -04004366out:
Trond Myklebust136f2622010-04-16 16:22:49 -04004367 nfs_free_fattr(res.fattr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004368 return status;
4369}
4370
Al Virobeffb8f2016-07-20 16:34:42 -04004371static int nfs4_proc_link(struct inode *inode, struct inode *dir, const struct qstr *name)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004372{
4373 struct nfs4_exception exception = { };
4374 int err;
4375 do {
4376 err = nfs4_handle_exception(NFS_SERVER(inode),
4377 _nfs4_proc_link(inode, dir, name),
4378 &exception);
4379 } while (exception.retry);
4380 return err;
4381}
4382
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004383struct nfs4_createdata {
4384 struct rpc_message msg;
4385 struct nfs4_create_arg arg;
4386 struct nfs4_create_res res;
4387 struct nfs_fh fh;
4388 struct nfs_fattr fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004389 struct nfs4_label *label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004390};
4391
4392static struct nfs4_createdata *nfs4_alloc_createdata(struct inode *dir,
Al Virobeffb8f2016-07-20 16:34:42 -04004393 const struct qstr *name, struct iattr *sattr, u32 ftype)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004394{
4395 struct nfs4_createdata *data;
4396
4397 data = kzalloc(sizeof(*data), GFP_KERNEL);
4398 if (data != NULL) {
4399 struct nfs_server *server = NFS_SERVER(dir);
4400
David Quigley14c43f72013-05-22 12:50:43 -04004401 data->label = nfs4_label_alloc(server, GFP_KERNEL);
4402 if (IS_ERR(data->label))
4403 goto out_free;
4404
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004405 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE];
4406 data->msg.rpc_argp = &data->arg;
4407 data->msg.rpc_resp = &data->res;
4408 data->arg.dir_fh = NFS_FH(dir);
4409 data->arg.server = server;
4410 data->arg.name = name;
4411 data->arg.attrs = sattr;
4412 data->arg.ftype = ftype;
David Quigleyaa9c2662013-05-22 12:50:44 -04004413 data->arg.bitmask = nfs4_bitmask(server, data->label);
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004414 data->arg.umask = current_umask();
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004415 data->res.server = server;
4416 data->res.fh = &data->fh;
4417 data->res.fattr = &data->fattr;
David Quigley1775fd32013-05-22 12:50:42 -04004418 data->res.label = data->label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004419 nfs_fattr_init(data->res.fattr);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004420 }
4421 return data;
David Quigley14c43f72013-05-22 12:50:43 -04004422out_free:
4423 kfree(data);
4424 return NULL;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004425}
4426
4427static int nfs4_do_create(struct inode *dir, struct dentry *dentry, struct nfs4_createdata *data)
4428{
Bryan Schumaker7c513052011-03-24 17:12:24 +00004429 int status = nfs4_call_sync(NFS_SERVER(dir)->client, NFS_SERVER(dir), &data->msg,
Bryan Schumakere73b83f2011-03-24 17:12:23 +00004430 &data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004431 if (status == 0) {
Trond Myklebustd3129ef2017-01-11 22:07:28 -05004432 update_changeattr(dir, &data->res.dir_cinfo,
4433 data->res.fattr->time_start);
David Quigley1775fd32013-05-22 12:50:42 -04004434 status = nfs_instantiate(dentry, data->res.fh, data->res.fattr, data->res.label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004435 }
4436 return status;
4437}
4438
4439static void nfs4_free_createdata(struct nfs4_createdata *data)
4440{
David Quigley14c43f72013-05-22 12:50:43 -04004441 nfs4_label_free(data->label);
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004442 kfree(data);
4443}
4444
Chuck Lever4f390c12006-08-22 20:06:22 -04004445static int _nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004446 struct page *page, unsigned int len, struct iattr *sattr,
4447 struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004448{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004449 struct nfs4_createdata *data;
4450 int status = -ENAMETOOLONG;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004451
Chuck Lever94a6d752006-08-22 20:06:23 -04004452 if (len > NFS4_MAXPATHLEN)
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004453 goto out;
Chuck Lever4f390c12006-08-22 20:06:22 -04004454
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004455 status = -ENOMEM;
4456 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4LNK);
4457 if (data == NULL)
4458 goto out;
4459
4460 data->msg.rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SYMLINK];
4461 data->arg.u.symlink.pages = &page;
4462 data->arg.u.symlink.len = len;
David Quigley1775fd32013-05-22 12:50:42 -04004463 data->arg.label = label;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004464
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004465 status = nfs4_do_create(dir, dentry, data);
4466
4467 nfs4_free_createdata(data);
4468out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004469 return status;
4470}
4471
Chuck Lever4f390c12006-08-22 20:06:22 -04004472static int nfs4_proc_symlink(struct inode *dir, struct dentry *dentry,
Chuck Lever94a6d752006-08-22 20:06:23 -04004473 struct page *page, unsigned int len, struct iattr *sattr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004474{
4475 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004476 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004477 int err;
David Quigleyaa9c2662013-05-22 12:50:44 -04004478
4479 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4480
Linus Torvalds1da177e2005-04-16 15:20:36 -07004481 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004482 err = _nfs4_proc_symlink(dir, dentry, page, len, sattr, label);
4483 trace_nfs4_symlink(dir, &dentry->d_name, err);
4484 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004485 &exception);
4486 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004487
4488 nfs4_label_release_security(label);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004489 return err;
4490}
4491
4492static int _nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
David Quigley1775fd32013-05-22 12:50:42 -04004493 struct iattr *sattr, struct nfs4_label *label)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004494{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004495 struct nfs4_createdata *data;
4496 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004497
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004498 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4DIR);
4499 if (data == NULL)
4500 goto out;
4501
David Quigley1775fd32013-05-22 12:50:42 -04004502 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004503 status = nfs4_do_create(dir, dentry, data);
4504
4505 nfs4_free_createdata(data);
4506out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004507 return status;
4508}
4509
4510static int nfs4_proc_mkdir(struct inode *dir, struct dentry *dentry,
4511 struct iattr *sattr)
4512{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004513 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004514 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004515 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004516 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004517
David Quigleyaa9c2662013-05-22 12:50:44 -04004518 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4519
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004520 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4521 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004522 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004523 err = _nfs4_proc_mkdir(dir, dentry, sattr, label);
4524 trace_nfs4_mkdir(dir, &dentry->d_name, err);
4525 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004526 &exception);
4527 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004528 nfs4_label_release_security(label);
4529
Linus Torvalds1da177e2005-04-16 15:20:36 -07004530 return err;
4531}
4532
4533static int _nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004534 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004535{
David Howells2b0143b2015-03-17 22:25:59 +00004536 struct inode *dir = d_inode(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004537 struct nfs4_readdir_arg args = {
4538 .fh = NFS_FH(dir),
Bryan Schumaker56e4ebf2010-10-20 15:44:37 -04004539 .pages = pages,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004540 .pgbase = 0,
4541 .count = count,
David Howells2b0143b2015-03-17 22:25:59 +00004542 .bitmask = NFS_SERVER(d_inode(dentry))->attr_bitmask,
Bryan Schumaker82f2e542010-10-21 16:33:18 -04004543 .plus = plus,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004544 };
4545 struct nfs4_readdir_res res;
4546 struct rpc_message msg = {
4547 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READDIR],
4548 .rpc_argp = &args,
4549 .rpc_resp = &res,
4550 .rpc_cred = cred,
4551 };
4552 int status;
4553
Al Viro6de14722013-09-16 10:53:17 -04004554 dprintk("%s: dentry = %pd2, cookie = %Lu\n", __func__,
4555 dentry,
Trond Myklebusteadf4592005-06-22 17:16:39 +00004556 (unsigned long long)cookie);
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004557 nfs4_setup_readdir(cookie, NFS_I(dir)->cookieverf, dentry, &args);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004558 res.pgbase = args.pgbase;
Bryan Schumaker7c513052011-03-24 17:12:24 +00004559 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 -05004560 if (status >= 0) {
Trond Myklebustc3f52af2012-09-03 14:56:02 -04004561 memcpy(NFS_I(dir)->cookieverf, res.verifier.data, NFS4_VERIFIER_SIZE);
Trond Myklebustac396122010-11-15 20:26:22 -05004562 status += args.pgbase;
4563 }
Trond Myklebustc4812992007-09-28 17:11:45 -04004564
4565 nfs_invalidate_atime(dir);
4566
Harvey Harrison3110ff82008-05-02 13:42:44 -07004567 dprintk("%s: returns %d\n", __func__, status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004568 return status;
4569}
4570
4571static int nfs4_proc_readdir(struct dentry *dentry, struct rpc_cred *cred,
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04004572 u64 cookie, struct page **pages, unsigned int count, bool plus)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004573{
4574 struct nfs4_exception exception = { };
4575 int err;
4576 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04004577 err = _nfs4_proc_readdir(dentry, cred, cookie,
4578 pages, count, plus);
David Howells2b0143b2015-03-17 22:25:59 +00004579 trace_nfs4_readdir(d_inode(dentry), err);
4580 err = nfs4_handle_exception(NFS_SERVER(d_inode(dentry)), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004581 &exception);
4582 } while (exception.retry);
4583 return err;
4584}
4585
4586static int _nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
David Quigleyaa9c2662013-05-22 12:50:44 -04004587 struct iattr *sattr, struct nfs4_label *label, dev_t rdev)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004588{
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004589 struct nfs4_createdata *data;
4590 int mode = sattr->ia_mode;
4591 int status = -ENOMEM;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004592
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004593 data = nfs4_alloc_createdata(dir, &dentry->d_name, sattr, NF4SOCK);
4594 if (data == NULL)
4595 goto out;
4596
Linus Torvalds1da177e2005-04-16 15:20:36 -07004597 if (S_ISFIFO(mode))
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004598 data->arg.ftype = NF4FIFO;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004599 else if (S_ISBLK(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004600 data->arg.ftype = NF4BLK;
4601 data->arg.u.device.specdata1 = MAJOR(rdev);
4602 data->arg.u.device.specdata2 = MINOR(rdev);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004603 }
4604 else if (S_ISCHR(mode)) {
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004605 data->arg.ftype = NF4CHR;
4606 data->arg.u.device.specdata1 = MAJOR(rdev);
4607 data->arg.u.device.specdata2 = MINOR(rdev);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004608 } else if (!S_ISSOCK(mode)) {
4609 status = -EINVAL;
4610 goto out_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004611 }
David Quigley1775fd32013-05-22 12:50:42 -04004612
David Quigleyaa9c2662013-05-22 12:50:44 -04004613 data->arg.label = label;
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004614 status = nfs4_do_create(dir, dentry, data);
Trond Myklebust4ea8fed2012-10-15 15:47:41 -04004615out_free:
Trond Myklebust57dc9a52008-06-20 15:35:32 -04004616 nfs4_free_createdata(data);
4617out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07004618 return status;
4619}
4620
4621static int nfs4_proc_mknod(struct inode *dir, struct dentry *dentry,
4622 struct iattr *sattr, dev_t rdev)
4623{
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004624 struct nfs_server *server = NFS_SERVER(dir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004625 struct nfs4_exception exception = { };
David Quigleyaa9c2662013-05-22 12:50:44 -04004626 struct nfs4_label l, *label = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004627 int err;
Aneesh Kumar K.Va8a5da92010-12-09 11:35:14 +00004628
David Quigleyaa9c2662013-05-22 12:50:44 -04004629 label = nfs4_label_init_security(dir, dentry, sattr, &l);
4630
Andreas Gruenbacherdff25dd2016-12-02 22:53:30 -05004631 if (!(server->attr_bitmask[2] & FATTR4_WORD2_MODE_UMASK))
4632 sattr->ia_mode &= ~current_umask();
Linus Torvalds1da177e2005-04-16 15:20:36 -07004633 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04004634 err = _nfs4_proc_mknod(dir, dentry, sattr, label, rdev);
4635 trace_nfs4_mknod(dir, &dentry->d_name, err);
4636 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004637 &exception);
4638 } while (exception.retry);
David Quigleyaa9c2662013-05-22 12:50:44 -04004639
4640 nfs4_label_release_security(label);
4641
Linus Torvalds1da177e2005-04-16 15:20:36 -07004642 return err;
4643}
4644
4645static int _nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle,
4646 struct nfs_fsstat *fsstat)
4647{
4648 struct nfs4_statfs_arg args = {
4649 .fh = fhandle,
4650 .bitmask = server->attr_bitmask,
4651 };
Benny Halevy24ad1482009-04-01 09:21:56 -04004652 struct nfs4_statfs_res res = {
4653 .fsstat = fsstat,
4654 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004655 struct rpc_message msg = {
4656 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_STATFS],
4657 .rpc_argp = &args,
Benny Halevy24ad1482009-04-01 09:21:56 -04004658 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004659 };
4660
Trond Myklebust0e574af2005-10-27 22:12:38 -04004661 nfs_fattr_init(fsstat->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004662 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004663}
4664
4665static int nfs4_proc_statfs(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsstat *fsstat)
4666{
4667 struct nfs4_exception exception = { };
4668 int err;
4669 do {
4670 err = nfs4_handle_exception(server,
4671 _nfs4_proc_statfs(server, fhandle, fsstat),
4672 &exception);
4673 } while (exception.retry);
4674 return err;
4675}
4676
4677static int _nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle,
4678 struct nfs_fsinfo *fsinfo)
4679{
4680 struct nfs4_fsinfo_arg args = {
4681 .fh = fhandle,
4682 .bitmask = server->attr_bitmask,
4683 };
Benny Halevy3dda5e42009-04-01 09:21:57 -04004684 struct nfs4_fsinfo_res res = {
4685 .fsinfo = fsinfo,
4686 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004687 struct rpc_message msg = {
4688 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSINFO],
4689 .rpc_argp = &args,
Benny Halevy3dda5e42009-04-01 09:21:57 -04004690 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004691 };
4692
Bryan Schumaker7c513052011-03-24 17:12:24 +00004693 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004694}
4695
4696static int nfs4_do_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4697{
4698 struct nfs4_exception exception = { };
Chuck Lever83ca7f52013-03-16 15:55:53 -04004699 unsigned long now = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004700 int err;
4701
4702 do {
Chuck Lever83ca7f52013-03-16 15:55:53 -04004703 err = _nfs4_do_fsinfo(server, fhandle, fsinfo);
Trond Myklebustb5f875a2013-08-13 13:01:39 -04004704 trace_nfs4_fsinfo(server, fhandle, fsinfo->fattr, err);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004705 if (err == 0) {
Trond Myklebustfb10fb62016-08-05 19:13:08 -04004706 nfs4_set_lease_period(server->nfs_client,
4707 fsinfo->lease_time * HZ,
4708 now);
Chuck Lever83ca7f52013-03-16 15:55:53 -04004709 break;
4710 }
4711 err = nfs4_handle_exception(server, err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004712 } while (exception.retry);
4713 return err;
4714}
4715
4716static int nfs4_proc_fsinfo(struct nfs_server *server, struct nfs_fh *fhandle, struct nfs_fsinfo *fsinfo)
4717{
Bryan Schumakere38eb652012-06-20 15:53:40 -04004718 int error;
4719
Trond Myklebust0e574af2005-10-27 22:12:38 -04004720 nfs_fattr_init(fsinfo->fattr);
Bryan Schumakere38eb652012-06-20 15:53:40 -04004721 error = nfs4_do_fsinfo(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004722 if (error == 0) {
4723 /* block layout checks this! */
4724 server->pnfs_blksize = fsinfo->blksize;
Jeff Laytonca440c32016-09-15 14:40:49 -04004725 set_pnfs_layoutdriver(server, fhandle, fsinfo);
Peng Taodc182542012-08-24 00:27:49 +08004726 }
Bryan Schumakere38eb652012-06-20 15:53:40 -04004727
4728 return error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004729}
4730
4731static int _nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4732 struct nfs_pathconf *pathconf)
4733{
4734 struct nfs4_pathconf_arg args = {
4735 .fh = fhandle,
4736 .bitmask = server->attr_bitmask,
4737 };
Benny Halevyd45b2982009-04-01 09:21:58 -04004738 struct nfs4_pathconf_res res = {
4739 .pathconf = pathconf,
4740 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07004741 struct rpc_message msg = {
4742 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_PATHCONF],
4743 .rpc_argp = &args,
Benny Halevyd45b2982009-04-01 09:21:58 -04004744 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07004745 };
4746
4747 /* None of the pathconf attributes are mandatory to implement */
4748 if ((args.bitmask[0] & nfs4_pathconf_bitmap[0]) == 0) {
4749 memset(pathconf, 0, sizeof(*pathconf));
4750 return 0;
4751 }
4752
Trond Myklebust0e574af2005-10-27 22:12:38 -04004753 nfs_fattr_init(pathconf->fattr);
Bryan Schumaker7c513052011-03-24 17:12:24 +00004754 return nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004755}
4756
4757static int nfs4_proc_pathconf(struct nfs_server *server, struct nfs_fh *fhandle,
4758 struct nfs_pathconf *pathconf)
4759{
4760 struct nfs4_exception exception = { };
4761 int err;
4762
4763 do {
4764 err = nfs4_handle_exception(server,
4765 _nfs4_proc_pathconf(server, fhandle, pathconf),
4766 &exception);
4767 } while (exception.retry);
4768 return err;
4769}
4770
Trond Myklebust5521abf2013-03-16 20:54:34 -04004771int nfs4_set_rw_stateid(nfs4_stateid *stateid,
Trond Myklebust9b206142013-03-17 15:52:00 -04004772 const struct nfs_open_context *ctx,
4773 const struct nfs_lock_context *l_ctx,
4774 fmode_t fmode)
4775{
NeilBrown17393472016-10-13 15:26:47 +11004776 return nfs4_select_rw_stateid(ctx->state, fmode, l_ctx, stateid, NULL);
Trond Myklebust9b206142013-03-17 15:52:00 -04004777}
4778EXPORT_SYMBOL_GPL(nfs4_set_rw_stateid);
4779
Trond Myklebust5521abf2013-03-16 20:54:34 -04004780static bool nfs4_stateid_is_current(nfs4_stateid *stateid,
4781 const struct nfs_open_context *ctx,
4782 const struct nfs_lock_context *l_ctx,
4783 fmode_t fmode)
4784{
4785 nfs4_stateid current_stateid;
4786
Trond Myklebuste1253be2014-03-05 08:44:23 -05004787 /* If the current stateid represents a lost lock, then exit */
4788 if (nfs4_set_rw_stateid(&current_stateid, ctx, l_ctx, fmode) == -EIO)
4789 return true;
Trond Myklebust5521abf2013-03-16 20:54:34 -04004790 return nfs4_stateid_match(stateid, &current_stateid);
4791}
4792
4793static bool nfs4_error_stateid_expired(int err)
4794{
4795 switch (err) {
4796 case -NFS4ERR_DELEG_REVOKED:
4797 case -NFS4ERR_ADMIN_REVOKED:
4798 case -NFS4ERR_BAD_STATEID:
4799 case -NFS4ERR_STALE_STATEID:
4800 case -NFS4ERR_OLD_STATEID:
4801 case -NFS4ERR_OPENMODE:
4802 case -NFS4ERR_EXPIRED:
4803 return true;
4804 }
4805 return false;
4806}
4807
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004808static int nfs4_read_done_cb(struct rpc_task *task, struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004809{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004810 struct nfs_server *server = NFS_SERVER(hdr->inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004811
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004812 trace_nfs4_read(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004813 if (task->tk_status < 0) {
4814 struct nfs4_exception exception = {
4815 .inode = hdr->inode,
4816 .state = hdr->args.context->state,
4817 .stateid = &hdr->args.stateid,
4818 };
4819 task->tk_status = nfs4_async_handle_exception(task,
4820 server, task->tk_status, &exception);
4821 if (exception.retry) {
4822 rpc_restart_call_prepare(task);
4823 return -EAGAIN;
4824 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004825 }
Trond Myklebust8850df92007-09-28 17:20:07 -04004826
Linus Torvalds1da177e2005-04-16 15:20:36 -07004827 if (task->tk_status > 0)
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004828 renew_lease(server, hdr->timestamp);
Trond Myklebustec06c092006-03-20 13:44:27 -05004829 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004830}
4831
Trond Myklebust5521abf2013-03-16 20:54:34 -04004832static bool nfs4_read_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004833 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004834{
4835
4836 if (!nfs4_error_stateid_expired(task->tk_status) ||
4837 nfs4_stateid_is_current(&args->stateid,
4838 args->context,
4839 args->lock_context,
4840 FMODE_READ))
4841 return false;
4842 rpc_restart_call_prepare(task);
4843 return true;
4844}
4845
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004846static int nfs4_read_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004847{
4848
4849 dprintk("--> %s\n", __func__);
4850
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004851 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004852 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004853 if (nfs4_read_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004854 return -EAGAIN;
Trond Myklebustbfc505d2016-09-15 18:26:05 -04004855 if (task->tk_status > 0)
4856 nfs_invalidate_atime(hdr->inode);
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004857 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4858 nfs4_read_done_cb(task, hdr);
Andy Adamsoncbdabc72011-03-01 01:34:20 +00004859}
4860
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004861static void nfs4_proc_read_setup(struct nfs_pgio_header *hdr,
4862 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004863{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004864 hdr->timestamp = jiffies;
Trond Myklebustca857cc2016-06-28 13:54:09 -04004865 if (!hdr->pgio_done_cb)
4866 hdr->pgio_done_cb = nfs4_read_done_cb;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004867 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_READ];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004868 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004869}
4870
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004871static int nfs4_proc_pgio_rpc_prepare(struct rpc_task *task,
4872 struct nfs_pgio_header *hdr)
Bryan Schumakerea7c3302012-03-19 14:54:40 -04004873{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004874 if (nfs4_setup_sequence(NFS_SERVER(hdr->inode)->nfs_client,
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004875 &hdr->args.seq_args,
4876 &hdr->res.seq_res,
Trond Myklebust9b206142013-03-17 15:52:00 -04004877 task))
NeilBrownef1820f2013-09-04 17:04:49 +10004878 return 0;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004879 if (nfs4_set_rw_stateid(&hdr->args.stateid, hdr->args.context,
4880 hdr->args.lock_context,
Benjamin Coddingtonfbe77c32017-04-19 10:11:35 -04004881 hdr->rw_mode) == -EIO)
NeilBrownef1820f2013-09-04 17:04:49 +10004882 return -EIO;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004883 if (unlikely(test_bit(NFS_CONTEXT_BAD, &hdr->args.context->flags)))
NeilBrownef1820f2013-09-04 17:04:49 +10004884 return -EIO;
4885 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004886}
4887
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004888static int nfs4_write_done_cb(struct rpc_task *task,
4889 struct nfs_pgio_header *hdr)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004890{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004891 struct inode *inode = hdr->inode;
NeilBrown8478eaa2014-09-18 16:09:27 +10004892
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004893 trace_nfs4_write(hdr, task->tk_status);
Trond Myklebust9c278692016-09-22 13:39:11 -04004894 if (task->tk_status < 0) {
4895 struct nfs4_exception exception = {
4896 .inode = hdr->inode,
4897 .state = hdr->args.context->state,
4898 .stateid = &hdr->args.stateid,
4899 };
4900 task->tk_status = nfs4_async_handle_exception(task,
4901 NFS_SERVER(inode), task->tk_status,
4902 &exception);
4903 if (exception.retry) {
4904 rpc_restart_call_prepare(task);
4905 return -EAGAIN;
4906 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07004907 }
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004908 if (task->tk_status >= 0) {
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004909 renew_lease(NFS_SERVER(inode), hdr->timestamp);
Trond Myklebusta08a8cd2015-02-26 17:36:09 -05004910 nfs_writeback_update_inode(hdr);
Trond Myklebust4f9838c2005-10-27 22:12:44 -04004911 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004912 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004913}
4914
Trond Myklebust5521abf2013-03-16 20:54:34 -04004915static bool nfs4_write_stateid_changed(struct rpc_task *task,
Anna Schumaker3c6b8992014-05-06 09:12:24 -04004916 struct nfs_pgio_args *args)
Trond Myklebust5521abf2013-03-16 20:54:34 -04004917{
4918
4919 if (!nfs4_error_stateid_expired(task->tk_status) ||
4920 nfs4_stateid_is_current(&args->stateid,
4921 args->context,
4922 args->lock_context,
4923 FMODE_WRITE))
4924 return false;
4925 rpc_restart_call_prepare(task);
4926 return true;
4927}
4928
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004929static int nfs4_write_done(struct rpc_task *task, struct nfs_pgio_header *hdr)
Fred Isamanb029bc92011-03-03 15:13:42 +00004930{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004931 if (!nfs4_sequence_done(task, &hdr->res.seq_res))
Fred Isamanb029bc92011-03-03 15:13:42 +00004932 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004933 if (nfs4_write_stateid_changed(task, &hdr->args))
Trond Myklebust5521abf2013-03-16 20:54:34 -04004934 return -EAGAIN;
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004935 return hdr->pgio_done_cb ? hdr->pgio_done_cb(task, hdr) :
4936 nfs4_write_done_cb(task, hdr);
Fred Isamanb029bc92011-03-03 15:13:42 +00004937}
4938
Trond Myklebust5a37f852012-04-28 14:55:16 -04004939static
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004940bool nfs4_write_need_cache_consistency_data(struct nfs_pgio_header *hdr)
Fred Isamana69aef12011-03-03 15:13:47 +00004941{
Trond Myklebust5a37f852012-04-28 14:55:16 -04004942 /* Don't request attributes for pNFS or O_DIRECT writes */
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004943 if (hdr->ds_clp != NULL || hdr->dreq != NULL)
Trond Myklebust5a37f852012-04-28 14:55:16 -04004944 return false;
4945 /* Otherwise, request attributes if and only if we don't hold
4946 * a delegation
4947 */
Bryan Schumaker011e2a72012-06-20 15:53:43 -04004948 return nfs4_have_delegation(hdr->inode, FMODE_READ) == 0;
Fred Isamana69aef12011-03-03 15:13:47 +00004949}
Fred Isamana69aef12011-03-03 15:13:47 +00004950
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004951static void nfs4_proc_write_setup(struct nfs_pgio_header *hdr,
4952 struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004953{
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004954 struct nfs_server *server = NFS_SERVER(hdr->inode);
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004955
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004956 if (!nfs4_write_need_cache_consistency_data(hdr)) {
4957 hdr->args.bitmask = NULL;
4958 hdr->res.fattr = NULL;
Fred Isaman7ffd1062011-03-03 15:13:46 +00004959 } else
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004960 hdr->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebust5a37f852012-04-28 14:55:16 -04004961
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004962 if (!hdr->pgio_done_cb)
4963 hdr->pgio_done_cb = nfs4_write_done_cb;
4964 hdr->res.server = server;
4965 hdr->timestamp = jiffies;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004966
Trond Myklebustbdc7f022007-07-14 15:40:00 -04004967 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_WRITE];
Weston Andros Adamsond45f60c2014-06-09 11:48:35 -04004968 nfs4_init_sequence(&hdr->args.seq_args, &hdr->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004969}
4970
Fred Isaman0b7c0152012-04-20 14:47:39 -04004971static void nfs4_proc_commit_rpc_prepare(struct rpc_task *task, struct nfs_commit_data *data)
4972{
Anna Schumaker42e1cca2017-01-09 15:48:22 -05004973 nfs4_setup_sequence(NFS_SERVER(data->inode)->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04004974 &data->args.seq_args,
4975 &data->res.seq_res,
4976 task);
Linus Torvalds1da177e2005-04-16 15:20:36 -07004977}
4978
Fred Isaman0b7c0152012-04-20 14:47:39 -04004979static int nfs4_commit_done_cb(struct rpc_task *task, struct nfs_commit_data *data)
Linus Torvalds1da177e2005-04-16 15:20:36 -07004980{
Linus Torvalds1da177e2005-04-16 15:20:36 -07004981 struct inode *inode = data->inode;
Trond Myklebust14516c32010-07-31 14:29:06 -04004982
Trond Myklebustcc668ab2013-08-14 15:31:28 -04004983 trace_nfs4_commit(data, task->tk_status);
NeilBrown8478eaa2014-09-18 16:09:27 +10004984 if (nfs4_async_handle_error(task, NFS_SERVER(inode),
4985 NULL, NULL) == -EAGAIN) {
Trond Myklebustd00c5d42011-10-19 12:17:29 -07004986 rpc_restart_call_prepare(task);
Trond Myklebust788e7a82006-03-20 13:44:27 -05004987 return -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004988 }
Trond Myklebust788e7a82006-03-20 13:44:27 -05004989 return 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07004990}
4991
Fred Isaman0b7c0152012-04-20 14:47:39 -04004992static int nfs4_commit_done(struct rpc_task *task, struct nfs_commit_data *data)
Fred Isaman5f452432011-03-23 13:27:46 +00004993{
4994 if (!nfs4_sequence_done(task, &data->res.seq_res))
4995 return -EAGAIN;
Fred Isaman0b7c0152012-04-20 14:47:39 -04004996 return data->commit_done_cb(task, data);
Fred Isaman5f452432011-03-23 13:27:46 +00004997}
4998
Fred Isaman0b7c0152012-04-20 14:47:39 -04004999static void nfs4_proc_commit_setup(struct nfs_commit_data *data, struct rpc_message *msg)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005000{
Trond Myklebust788e7a82006-03-20 13:44:27 -05005001 struct nfs_server *server = NFS_SERVER(data->inode);
Fred Isaman988b6dc2011-03-23 13:27:52 +00005002
Fred Isaman0b7c0152012-04-20 14:47:39 -04005003 if (data->commit_done_cb == NULL)
5004 data->commit_done_cb = nfs4_commit_done_cb;
Trond Myklebust4f9838c2005-10-27 22:12:44 -04005005 data->res.server = server;
Trond Myklebustbdc7f022007-07-14 15:40:00 -04005006 msg->rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_COMMIT];
Chuck Levera9c92d62013-08-09 12:48:18 -04005007 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005008}
5009
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005010struct nfs4_renewdata {
5011 struct nfs_client *client;
5012 unsigned long timestamp;
5013};
5014
Linus Torvalds1da177e2005-04-16 15:20:36 -07005015/*
5016 * nfs4_proc_async_renew(): This is not one of the nfs_rpc_ops; it is a special
5017 * standalone procedure for queueing an asynchronous RENEW.
5018 */
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005019static void nfs4_renew_release(void *calldata)
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005020{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005021 struct nfs4_renewdata *data = calldata;
5022 struct nfs_client *clp = data->client;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005023
Elena Reshetova212bf412017-10-20 12:53:38 +03005024 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005025 nfs4_schedule_state_renewal(clp);
5026 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005027 kfree(data);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005028}
5029
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005030static void nfs4_renew_done(struct rpc_task *task, void *calldata)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005031{
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005032 struct nfs4_renewdata *data = calldata;
5033 struct nfs_client *clp = data->client;
5034 unsigned long timestamp = data->timestamp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005035
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005036 trace_nfs4_renew_async(clp, task->tk_status);
Chuck Leverf8aba1e2013-10-17 14:13:53 -04005037 switch (task->tk_status) {
5038 case 0:
5039 break;
5040 case -NFS4ERR_LEASE_MOVED:
5041 nfs4_schedule_lease_moved_recovery(clp);
5042 break;
5043 default:
Trond Myklebust95baa252009-05-26 14:51:00 -04005044 /* Unless we're shutting down, schedule state recovery! */
Trond Myklebust042b60b2011-08-24 15:07:37 -04005045 if (test_bit(NFS_CS_RENEWD, &clp->cl_res_state) == 0)
5046 return;
5047 if (task->tk_status != NFS4ERR_CB_PATH_DOWN) {
Trond Myklebust0400a6b2011-03-09 16:00:53 -05005048 nfs4_schedule_lease_recovery(clp);
Trond Myklebust042b60b2011-08-24 15:07:37 -04005049 return;
5050 }
5051 nfs4_schedule_path_down_recovery(clp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005052 }
Trond Myklebust452e9352010-07-31 14:29:06 -04005053 do_renew_lease(clp, timestamp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005054}
5055
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005056static const struct rpc_call_ops nfs4_renew_ops = {
5057 .rpc_call_done = nfs4_renew_done,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08005058 .rpc_release = nfs4_renew_release,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01005059};
5060
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005061static int nfs4_proc_async_renew(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005062{
5063 struct rpc_message msg = {
5064 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5065 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005066 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005067 };
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005068 struct nfs4_renewdata *data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005069
Trond Myklebust2f60ea62011-08-24 15:07:37 -04005070 if (renew_flags == 0)
5071 return 0;
Elena Reshetova212bf412017-10-20 12:53:38 +03005072 if (!refcount_inc_not_zero(&clp->cl_count))
Alexandros Batsakis0851de062010-02-05 03:45:06 -08005073 return -EIO;
Trond Myklebustb569ad32011-08-24 15:07:35 -04005074 data = kmalloc(sizeof(*data), GFP_NOFS);
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005075 if (data == NULL) {
5076 nfs_put_client(clp);
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005077 return -ENOMEM;
Dave Wysochanski5c737cb2017-04-27 10:45:15 -04005078 }
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005079 data->client = clp;
5080 data->timestamp = jiffies;
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005081 return rpc_call_async(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT,
Chuck Lever9bc4e3c2010-05-07 13:34:17 -04005082 &nfs4_renew_ops, data);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005083}
5084
Trond Myklebust8534d4e2011-08-24 15:07:37 -04005085static int nfs4_proc_renew(struct nfs_client *clp, struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005086{
5087 struct rpc_message msg = {
5088 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RENEW],
5089 .rpc_argp = clp,
Trond Myklebustb4454fe2006-01-03 09:55:25 +01005090 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005091 };
5092 unsigned long now = jiffies;
5093 int status;
5094
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04005095 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005096 if (status < 0)
5097 return status;
Trond Myklebust452e9352010-07-31 14:29:06 -04005098 do_renew_lease(clp, now);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005099 return 0;
5100}
5101
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005102static inline int nfs4_server_supports_acls(struct nfs_server *server)
5103{
Malahal Naineni7dd7d952014-01-23 08:54:55 -06005104 return server->caps & NFS_CAP_ACLS;
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005105}
5106
Trond Myklebust21f498c2012-08-24 10:59:25 -04005107/* Assuming that XATTR_SIZE_MAX is a multiple of PAGE_SIZE, and that
5108 * it's OK to put sizeof(void) * (XATTR_SIZE_MAX/PAGE_SIZE) bytes on
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005109 * the stack.
5110 */
Trond Myklebust21f498c2012-08-24 10:59:25 -04005111#define NFS4ACL_MAXPAGES DIV_ROUND_UP(XATTR_SIZE_MAX, PAGE_SIZE)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005112
Neil Hormane9e3d722011-03-04 19:26:03 -05005113static int buf_to_pages_noslab(const void *buf, size_t buflen,
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005114 struct page **pages)
Neil Hormane9e3d722011-03-04 19:26:03 -05005115{
5116 struct page *newpage, **spages;
5117 int rc = 0;
5118 size_t len;
5119 spages = pages;
5120
5121 do {
Trond Myklebust21f498c2012-08-24 10:59:25 -04005122 len = min_t(size_t, PAGE_SIZE, buflen);
Neil Hormane9e3d722011-03-04 19:26:03 -05005123 newpage = alloc_page(GFP_KERNEL);
5124
5125 if (newpage == NULL)
5126 goto unwind;
5127 memcpy(page_address(newpage), buf, len);
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005128 buf += len;
5129 buflen -= len;
Neil Hormane9e3d722011-03-04 19:26:03 -05005130 *pages++ = newpage;
5131 rc++;
5132 } while (buflen != 0);
5133
5134 return rc;
5135
5136unwind:
5137 for(; rc > 0; rc--)
5138 __free_page(spages[rc-1]);
5139 return -ENOMEM;
5140}
5141
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005142struct nfs4_cached_acl {
5143 int cached;
5144 size_t len;
Andrew Morton3e9d4152005-06-22 17:16:28 +00005145 char data[0];
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005146};
5147
5148static void nfs4_set_cached_acl(struct inode *inode, struct nfs4_cached_acl *acl)
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005149{
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005150 struct nfs_inode *nfsi = NFS_I(inode);
5151
5152 spin_lock(&inode->i_lock);
5153 kfree(nfsi->nfs4_acl);
5154 nfsi->nfs4_acl = acl;
5155 spin_unlock(&inode->i_lock);
5156}
5157
5158static void nfs4_zap_acl_attr(struct inode *inode)
5159{
5160 nfs4_set_cached_acl(inode, NULL);
5161}
5162
5163static inline ssize_t nfs4_read_cached_acl(struct inode *inode, char *buf, size_t buflen)
5164{
5165 struct nfs_inode *nfsi = NFS_I(inode);
5166 struct nfs4_cached_acl *acl;
5167 int ret = -ENOENT;
5168
5169 spin_lock(&inode->i_lock);
5170 acl = nfsi->nfs4_acl;
5171 if (acl == NULL)
5172 goto out;
5173 if (buf == NULL) /* user is just asking for length */
5174 goto out_len;
5175 if (acl->cached == 0)
5176 goto out;
5177 ret = -ERANGE; /* see getxattr(2) man page */
5178 if (acl->len > buflen)
5179 goto out;
5180 memcpy(buf, acl->data, acl->len);
5181out_len:
5182 ret = acl->len;
5183out:
5184 spin_unlock(&inode->i_lock);
5185 return ret;
5186}
5187
Sachin Prabhu5794d212012-04-17 14:36:40 +01005188static 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 +00005189{
5190 struct nfs4_cached_acl *acl;
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005191 size_t buflen = sizeof(*acl) + acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005192
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005193 if (buflen <= PAGE_SIZE) {
Trond Myklebustb291f1b2012-08-14 18:30:41 -04005194 acl = kmalloc(buflen, GFP_KERNEL);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005195 if (acl == NULL)
5196 goto out;
5197 acl->cached = 1;
Sachin Prabhu5794d212012-04-17 14:36:40 +01005198 _copy_from_pages(acl->data, pages, pgbase, acl_len);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005199 } else {
5200 acl = kmalloc(sizeof(*acl), GFP_KERNEL);
5201 if (acl == NULL)
5202 goto out;
5203 acl->cached = 0;
5204 }
5205 acl->len = acl_len;
5206out:
5207 nfs4_set_cached_acl(inode, acl);
5208}
5209
Andy Adamsonbf118a32011-12-07 11:55:27 -05005210/*
5211 * The getxattr API returns the required buffer length when called with a
5212 * NULL buf. The NFSv4 acl tool then calls getxattr again after allocating
5213 * the required buf. On a NULL buf, we send a page of data to the server
5214 * guessing that the ACL request can be serviced by a page. If so, we cache
5215 * up to the page of ACL data, and the 2nd call to getxattr is serviced by
5216 * the cache. If not so, we throw away the page, and cache the required
5217 * length. The next getxattr call will then produce another round trip to
5218 * the server, this time with the input buf of the required size.
5219 */
Trond Myklebust16b42892006-08-24 12:27:15 -04005220static ssize_t __nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005221{
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005222 struct page *pages[NFS4ACL_MAXPAGES + 1] = {NULL, };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005223 struct nfs_getaclargs args = {
5224 .fh = NFS_FH(inode),
5225 .acl_pages = pages,
5226 .acl_len = buflen,
5227 };
Benny Halevy663c79b2009-04-01 09:21:59 -04005228 struct nfs_getaclres res = {
5229 .acl_len = buflen,
5230 };
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005231 struct rpc_message msg = {
5232 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETACL],
5233 .rpc_argp = &args,
Benny Halevy663c79b2009-04-01 09:21:59 -04005234 .rpc_resp = &res,
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005235 };
Weston Andros Adamsoned92d8c2017-02-23 14:54:21 -05005236 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE) + 1;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005237 int ret = -ENOMEM, i;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005238
Trond Myklebust21f498c2012-08-24 10:59:25 -04005239 if (npages > ARRAY_SIZE(pages))
5240 return -ERANGE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005241
Andy Adamsonbf118a32011-12-07 11:55:27 -05005242 for (i = 0; i < npages; i++) {
5243 pages[i] = alloc_page(GFP_KERNEL);
5244 if (!pages[i])
5245 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005246 }
Sachin Prabhu5a006892012-04-17 14:35:39 +01005247
5248 /* for decoding across pages */
5249 res.acl_scratch = alloc_page(GFP_KERNEL);
5250 if (!res.acl_scratch)
5251 goto out_free;
5252
Andy Adamsonbf118a32011-12-07 11:55:27 -05005253 args.acl_len = npages * PAGE_SIZE;
Sachin Prabhu5a006892012-04-17 14:35:39 +01005254
Peng Taode040be2012-01-10 22:42:47 +08005255 dprintk("%s buf %p buflen %zu npages %d args.acl_len %zu\n",
Andy Adamsonbf118a32011-12-07 11:55:27 -05005256 __func__, buf, buflen, npages, args.acl_len);
5257 ret = nfs4_call_sync(NFS_SERVER(inode)->client, NFS_SERVER(inode),
5258 &msg, &args.seq_args, &res.seq_res, 0);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005259 if (ret)
5260 goto out_free;
Andy Adamsonbf118a32011-12-07 11:55:27 -05005261
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005262 /* Handle the case where the passed-in buffer is too short */
5263 if (res.acl_flags & NFS4_ACL_TRUNC) {
5264 /* Did the user only issue a request for the acl length? */
5265 if (buf == NULL)
5266 goto out_ok;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005267 ret = -ERANGE;
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005268 goto out_free;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005269 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005270 nfs4_write_cached_acl(inode, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005271 if (buf) {
5272 if (res.acl_len > buflen) {
5273 ret = -ERANGE;
5274 goto out_free;
5275 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005276 _copy_from_pages(buf, pages, res.acl_data_offset, res.acl_len);
Sven Wegener7d3e91a2012-12-08 15:30:18 +01005277 }
Trond Myklebust1f1ea6c2012-08-26 11:44:43 -07005278out_ok:
5279 ret = res.acl_len;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005280out_free:
Andy Adamsonbf118a32011-12-07 11:55:27 -05005281 for (i = 0; i < npages; i++)
5282 if (pages[i])
5283 __free_page(pages[i]);
Trond Myklebust331818f2012-02-03 18:30:53 -05005284 if (res.acl_scratch)
5285 __free_page(res.acl_scratch);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005286 return ret;
5287}
5288
Trond Myklebust16b42892006-08-24 12:27:15 -04005289static ssize_t nfs4_get_acl_uncached(struct inode *inode, void *buf, size_t buflen)
5290{
5291 struct nfs4_exception exception = { };
5292 ssize_t ret;
5293 do {
5294 ret = __nfs4_get_acl_uncached(inode, buf, buflen);
Trond Myklebustc1578b72013-08-12 16:58:42 -04005295 trace_nfs4_get_acl(inode, ret);
Trond Myklebust16b42892006-08-24 12:27:15 -04005296 if (ret >= 0)
5297 break;
5298 ret = nfs4_handle_exception(NFS_SERVER(inode), ret, &exception);
5299 } while (exception.retry);
5300 return ret;
5301}
5302
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005303static ssize_t nfs4_proc_get_acl(struct inode *inode, void *buf, size_t buflen)
5304{
5305 struct nfs_server *server = NFS_SERVER(inode);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005306 int ret;
5307
5308 if (!nfs4_server_supports_acls(server))
5309 return -EOPNOTSUPP;
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005310 ret = nfs_revalidate_inode(server, inode);
5311 if (ret < 0)
5312 return ret;
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005313 if (NFS_I(inode)->cache_validity & NFS_INO_INVALID_ACL)
5314 nfs_zap_acl_cache(inode);
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005315 ret = nfs4_read_cached_acl(inode, buf, buflen);
5316 if (ret != -ENOENT)
Andy Adamsonbf118a32011-12-07 11:55:27 -05005317 /* -ENOENT is returned if there is no ACL or if there is an ACL
5318 * but no cached acl data, just the acl length */
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00005319 return ret;
5320 return nfs4_get_acl_uncached(inode, buf, buflen);
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00005321}
5322
Trond Myklebust16b42892006-08-24 12:27:15 -04005323static int __nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005324{
5325 struct nfs_server *server = NFS_SERVER(inode);
5326 struct page *pages[NFS4ACL_MAXPAGES];
5327 struct nfs_setaclargs arg = {
5328 .fh = NFS_FH(inode),
5329 .acl_pages = pages,
5330 .acl_len = buflen,
5331 };
Benny Halevy73c403a2009-04-01 09:22:01 -04005332 struct nfs_setaclres res;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005333 struct rpc_message msg = {
5334 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETACL],
5335 .rpc_argp = &arg,
Benny Halevy73c403a2009-04-01 09:22:01 -04005336 .rpc_resp = &res,
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005337 };
Trond Myklebust21f498c2012-08-24 10:59:25 -04005338 unsigned int npages = DIV_ROUND_UP(buflen, PAGE_SIZE);
Neil Hormane9e3d722011-03-04 19:26:03 -05005339 int ret, i;
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005340
5341 if (!nfs4_server_supports_acls(server))
5342 return -EOPNOTSUPP;
Trond Myklebust21f498c2012-08-24 10:59:25 -04005343 if (npages > ARRAY_SIZE(pages))
5344 return -ERANGE;
Andreas Gruenbacher8fbcf232015-11-03 18:25:34 +01005345 i = buf_to_pages_noslab(buf, buflen, arg.acl_pages);
Neil Hormane9e3d722011-03-04 19:26:03 -05005346 if (i < 0)
5347 return i;
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04005348 nfs4_inode_return_delegation(inode);
Bryan Schumaker7c513052011-03-24 17:12:24 +00005349 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Neil Hormane9e3d722011-03-04 19:26:03 -05005350
5351 /*
5352 * Free each page after tx, so the only ref left is
5353 * held by the network stack
5354 */
5355 for (; i > 0; i--)
5356 put_page(pages[i-1]);
5357
Aneesh Kumar K.V08a22b32010-12-01 10:42:16 +00005358 /*
5359 * Acl update can result in inode attribute update.
5360 * so mark the attribute cache invalid.
5361 */
5362 spin_lock(&inode->i_lock);
5363 NFS_I(inode)->cache_validity |= NFS_INO_INVALID_ATTR;
5364 spin_unlock(&inode->i_lock);
Trond Myklebustf41f7412008-06-11 17:39:04 -04005365 nfs_access_zap_cache(inode);
5366 nfs_zap_acl_cache(inode);
J. Bruce Fields4b580ee2005-06-22 17:16:23 +00005367 return ret;
5368}
5369
Trond Myklebust16b42892006-08-24 12:27:15 -04005370static int nfs4_proc_set_acl(struct inode *inode, const void *buf, size_t buflen)
5371{
5372 struct nfs4_exception exception = { };
5373 int err;
5374 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005375 err = __nfs4_proc_set_acl(inode, buf, buflen);
5376 trace_nfs4_set_acl(inode, err);
5377 err = nfs4_handle_exception(NFS_SERVER(inode), err,
Trond Myklebust16b42892006-08-24 12:27:15 -04005378 &exception);
5379 } while (exception.retry);
5380 return err;
5381}
5382
David Quigleyaa9c2662013-05-22 12:50:44 -04005383#ifdef CONFIG_NFS_V4_SECURITY_LABEL
5384static int _nfs4_get_security_label(struct inode *inode, void *buf,
5385 size_t buflen)
5386{
5387 struct nfs_server *server = NFS_SERVER(inode);
5388 struct nfs_fattr fattr;
5389 struct nfs4_label label = {0, 0, buflen, buf};
5390
5391 u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005392 struct nfs4_getattr_arg arg = {
David Quigleyaa9c2662013-05-22 12:50:44 -04005393 .fh = NFS_FH(inode),
5394 .bitmask = bitmask,
5395 };
5396 struct nfs4_getattr_res res = {
5397 .fattr = &fattr,
5398 .label = &label,
5399 .server = server,
5400 };
5401 struct rpc_message msg = {
5402 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETATTR],
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005403 .rpc_argp = &arg,
David Quigleyaa9c2662013-05-22 12:50:44 -04005404 .rpc_resp = &res,
5405 };
5406 int ret;
5407
5408 nfs_fattr_init(&fattr);
5409
Trond Myklebustfcb63a92013-11-01 12:42:25 -04005410 ret = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 0);
David Quigleyaa9c2662013-05-22 12:50:44 -04005411 if (ret)
5412 return ret;
5413 if (!(fattr.valid & NFS_ATTR_FATTR_V4_SECURITY_LABEL))
5414 return -ENOENT;
5415 if (buflen < label.len)
5416 return -ERANGE;
5417 return 0;
5418}
5419
5420static int nfs4_get_security_label(struct inode *inode, void *buf,
5421 size_t buflen)
5422{
5423 struct nfs4_exception exception = { };
5424 int err;
5425
5426 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5427 return -EOPNOTSUPP;
5428
5429 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005430 err = _nfs4_get_security_label(inode, buf, buflen);
5431 trace_nfs4_get_security_label(inode, err);
5432 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005433 &exception);
5434 } while (exception.retry);
5435 return err;
5436}
5437
5438static int _nfs4_do_set_security_label(struct inode *inode,
5439 struct nfs4_label *ilabel,
5440 struct nfs_fattr *fattr,
5441 struct nfs4_label *olabel)
5442{
5443
5444 struct iattr sattr = {0};
5445 struct nfs_server *server = NFS_SERVER(inode);
5446 const u32 bitmask[3] = { 0, 0, FATTR4_WORD2_SECURITY_LABEL };
Jeff Layton12207f62013-11-01 10:49:32 -04005447 struct nfs_setattrargs arg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005448 .fh = NFS_FH(inode),
5449 .iap = &sattr,
David Quigleyaa9c2662013-05-22 12:50:44 -04005450 .server = server,
5451 .bitmask = bitmask,
5452 .label = ilabel,
5453 };
5454 struct nfs_setattrres res = {
5455 .fattr = fattr,
5456 .label = olabel,
5457 .server = server,
5458 };
5459 struct rpc_message msg = {
Anna Schumakerd9b67e12017-01-11 15:04:25 -05005460 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETATTR],
5461 .rpc_argp = &arg,
5462 .rpc_resp = &res,
David Quigleyaa9c2662013-05-22 12:50:44 -04005463 };
5464 int status;
5465
Jeff Layton12207f62013-11-01 10:49:32 -04005466 nfs4_stateid_copy(&arg.stateid, &zero_stateid);
David Quigleyaa9c2662013-05-22 12:50:44 -04005467
Jeff Layton12207f62013-11-01 10:49:32 -04005468 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
David Quigleyaa9c2662013-05-22 12:50:44 -04005469 if (status)
5470 dprintk("%s failed: %d\n", __func__, status);
5471
5472 return status;
5473}
5474
5475static int nfs4_do_set_security_label(struct inode *inode,
5476 struct nfs4_label *ilabel,
5477 struct nfs_fattr *fattr,
5478 struct nfs4_label *olabel)
5479{
5480 struct nfs4_exception exception = { };
5481 int err;
5482
5483 do {
Trond Myklebustc1578b72013-08-12 16:58:42 -04005484 err = _nfs4_do_set_security_label(inode, ilabel,
5485 fattr, olabel);
5486 trace_nfs4_set_security_label(inode, err);
5487 err = nfs4_handle_exception(NFS_SERVER(inode), err,
David Quigleyaa9c2662013-05-22 12:50:44 -04005488 &exception);
5489 } while (exception.retry);
5490 return err;
5491}
5492
5493static int
Al Viro59301222016-05-27 10:19:30 -04005494nfs4_set_security_label(struct inode *inode, const void *buf, size_t buflen)
David Quigleyaa9c2662013-05-22 12:50:44 -04005495{
5496 struct nfs4_label ilabel, *olabel = NULL;
5497 struct nfs_fattr fattr;
5498 struct rpc_cred *cred;
David Quigleyaa9c2662013-05-22 12:50:44 -04005499 int status;
5500
5501 if (!nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL))
5502 return -EOPNOTSUPP;
5503
5504 nfs_fattr_init(&fattr);
5505
5506 ilabel.pi = 0;
5507 ilabel.lfs = 0;
5508 ilabel.label = (char *)buf;
5509 ilabel.len = buflen;
5510
5511 cred = rpc_lookup_cred();
5512 if (IS_ERR(cred))
5513 return PTR_ERR(cred);
5514
5515 olabel = nfs4_label_alloc(NFS_SERVER(inode), GFP_KERNEL);
5516 if (IS_ERR(olabel)) {
5517 status = -PTR_ERR(olabel);
5518 goto out;
5519 }
5520
5521 status = nfs4_do_set_security_label(inode, &ilabel, &fattr, olabel);
5522 if (status == 0)
5523 nfs_setsecurity(inode, &fattr, olabel);
5524
5525 nfs4_label_free(olabel);
5526out:
5527 put_rpccred(cred);
5528 return status;
5529}
5530#endif /* CONFIG_NFS_V4_SECURITY_LABEL */
5531
5532
Chuck Leverf0920752012-05-21 22:45:41 -04005533static void nfs4_init_boot_verifier(const struct nfs_client *clp,
5534 nfs4_verifier *bootverf)
Chuck Levercd937102012-03-02 17:14:31 -05005535{
5536 __be32 verf[2];
5537
Chuck Lever2c820d92012-05-21 22:45:33 -04005538 if (test_bit(NFS4CLNT_PURGE_STATE, &clp->cl_state)) {
5539 /* An impossible timestamp guarantees this value
5540 * will never match a generated boot time. */
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005541 verf[0] = cpu_to_be32(U32_MAX);
5542 verf[1] = cpu_to_be32(U32_MAX);
Chuck Lever2c820d92012-05-21 22:45:33 -04005543 } else {
Chuck Leverf0920752012-05-21 22:45:41 -04005544 struct nfs_net *nn = net_generic(clp->cl_net, nfs_net_id);
Deepa Dinamani2f86e092016-10-01 16:46:26 -07005545 u64 ns = ktime_to_ns(nn->boot_time);
5546
5547 verf[0] = cpu_to_be32(ns >> 32);
5548 verf[1] = cpu_to_be32(ns);
Chuck Lever2c820d92012-05-21 22:45:33 -04005549 }
Chuck Levercd937102012-03-02 17:14:31 -05005550 memcpy(bootverf->data, verf, sizeof(bootverf->data));
5551}
5552
Jeff Laytona3192682015-06-09 19:43:59 -04005553static int
5554nfs4_init_nonuniform_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005555{
Jeff Laytona3192682015-06-09 19:43:59 -04005556 size_t len;
5557 char *str;
Chuck Levere984a552012-09-14 17:24:21 -04005558
Trond Myklebustceb3a162015-01-03 15:16:04 -05005559 if (clp->cl_owner_id != NULL)
Jeff Laytona3192682015-06-09 19:43:59 -04005560 return 0;
Kinglong Mee4a3e5772015-08-31 10:53:43 +08005561
Jeff Laytona3192682015-06-09 19:43:59 -04005562 rcu_read_lock();
Kinglong Mee4a703162015-08-31 10:53:33 +08005563 len = 14 + strlen(clp->cl_ipaddr) + 1 +
Jeff Laytona3192682015-06-09 19:43:59 -04005564 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR)) +
5565 1 +
5566 strlen(rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO)) +
5567 1;
5568 rcu_read_unlock();
5569
5570 if (len > NFS4_OPAQUE_LIMIT + 1)
5571 return -EINVAL;
5572
5573 /*
5574 * Since this string is allocated at mount time, and held until the
5575 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5576 * about a memory-reclaim deadlock.
5577 */
5578 str = kmalloc(len, GFP_KERNEL);
5579 if (!str)
5580 return -ENOMEM;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005581
Chuck Levere984a552012-09-14 17:24:21 -04005582 rcu_read_lock();
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005583 scnprintf(str, len, "Linux NFSv4.0 %s/%s %s",
Jeff Laytona3192682015-06-09 19:43:59 -04005584 clp->cl_ipaddr,
5585 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_ADDR),
5586 rpc_peeraddr2str(clp->cl_rpcclient, RPC_DISPLAY_PROTO));
Chuck Levere984a552012-09-14 17:24:21 -04005587 rcu_read_unlock();
Jeff Laytona3192682015-06-09 19:43:59 -04005588
Jeff Laytona3192682015-06-09 19:43:59 -04005589 clp->cl_owner_id = str;
5590 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005591}
5592
Jeff Layton873e3852015-06-09 19:44:00 -04005593static int
5594nfs4_init_uniquifier_client_string(struct nfs_client *clp)
Chuck Levere984a552012-09-14 17:24:21 -04005595{
Jeff Layton873e3852015-06-09 19:44:00 -04005596 size_t len;
5597 char *str;
5598
5599 len = 10 + 10 + 1 + 10 + 1 +
5600 strlen(nfs4_client_id_uniquifier) + 1 +
5601 strlen(clp->cl_rpcclient->cl_nodename) + 1;
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;
5614
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005615 scnprintf(str, len, "Linux NFSv%u.%u %s/%s",
Jeff Layton873e3852015-06-09 19:44:00 -04005616 clp->rpc_ops->version, clp->cl_minorversion,
5617 nfs4_client_id_uniquifier,
5618 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005619 clp->cl_owner_id = str;
5620 return 0;
5621}
5622
5623static int
5624nfs4_init_uniform_client_string(struct nfs_client *clp)
5625{
Jeff Layton873e3852015-06-09 19:44:00 -04005626 size_t len;
5627 char *str;
Trond Myklebustceb3a162015-01-03 15:16:04 -05005628
5629 if (clp->cl_owner_id != NULL)
Jeff Layton873e3852015-06-09 19:44:00 -04005630 return 0;
Chuck Lever6f2ea7f2012-09-14 17:24:41 -04005631
5632 if (nfs4_client_id_uniquifier[0] != '\0')
Jeff Layton873e3852015-06-09 19:44:00 -04005633 return nfs4_init_uniquifier_client_string(clp);
5634
5635 len = 10 + 10 + 1 + 10 + 1 +
5636 strlen(clp->cl_rpcclient->cl_nodename) + 1;
5637
5638 if (len > NFS4_OPAQUE_LIMIT + 1)
5639 return -EINVAL;
5640
5641 /*
5642 * Since this string is allocated at mount time, and held until the
5643 * nfs_client is destroyed, we can use GFP_KERNEL here w/o worrying
5644 * about a memory-reclaim deadlock.
5645 */
5646 str = kmalloc(len, GFP_KERNEL);
5647 if (!str)
5648 return -ENOMEM;
5649
Trond Myklebustf2dd4362015-10-08 11:33:17 -04005650 scnprintf(str, len, "Linux NFSv%u.%u %s",
Jeff Layton873e3852015-06-09 19:44:00 -04005651 clp->rpc_ops->version, clp->cl_minorversion,
5652 clp->cl_rpcclient->cl_nodename);
Jeff Layton873e3852015-06-09 19:44:00 -04005653 clp->cl_owner_id = str;
5654 return 0;
Chuck Levere984a552012-09-14 17:24:21 -04005655}
5656
Chuck Lever706cb8d2014-03-12 12:51:47 -04005657/*
5658 * nfs4_callback_up_net() starts only "tcp" and "tcp6" callback
5659 * services. Advertise one based on the address family of the
5660 * clientaddr.
5661 */
5662static unsigned int
5663nfs4_init_callback_netid(const struct nfs_client *clp, char *buf, size_t len)
5664{
5665 if (strchr(clp->cl_ipaddr, ':') != NULL)
5666 return scnprintf(buf, len, "tcp6");
5667 else
5668 return scnprintf(buf, len, "tcp");
5669}
5670
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005671static void nfs4_setclientid_done(struct rpc_task *task, void *calldata)
5672{
5673 struct nfs4_setclientid *sc = calldata;
5674
5675 if (task->tk_status == 0)
5676 sc->sc_cred = get_rpccred(task->tk_rqstp->rq_cred);
5677}
5678
5679static const struct rpc_call_ops nfs4_setclientid_ops = {
5680 .rpc_call_done = nfs4_setclientid_done,
5681};
5682
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005683/**
5684 * nfs4_proc_setclientid - Negotiate client ID
5685 * @clp: state data structure
5686 * @program: RPC program for NFSv4 callback service
5687 * @port: IP port number for NFS4 callback service
5688 * @cred: RPC credential to use for this call
5689 * @res: where to place the result
5690 *
5691 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5692 */
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005693int nfs4_proc_setclientid(struct nfs_client *clp, u32 program,
5694 unsigned short port, struct rpc_cred *cred,
5695 struct nfs4_setclientid_res *res)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005696{
5697 nfs4_verifier sc_verifier;
5698 struct nfs4_setclientid setclientid = {
5699 .sc_verifier = &sc_verifier,
5700 .sc_prog = program,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005701 .sc_clnt = clp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005702 };
5703 struct rpc_message msg = {
5704 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID],
5705 .rpc_argp = &setclientid,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005706 .rpc_resp = res,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005707 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005708 };
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005709 struct rpc_task *task;
5710 struct rpc_task_setup task_setup_data = {
5711 .rpc_client = clp->cl_rpcclient,
5712 .rpc_message = &msg,
5713 .callback_ops = &nfs4_setclientid_ops,
5714 .callback_data = &setclientid,
5715 .flags = RPC_TASK_TIMEOUT,
5716 };
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005717 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005718
Chuck Leverde734832012-07-11 16:30:50 -04005719 /* nfs_client_id4 */
Chuck Leverf0920752012-05-21 22:45:41 -04005720 nfs4_init_boot_verifier(clp, &sc_verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04005721
5722 if (test_bit(NFS_CS_MIGRATION, &clp->cl_flags))
5723 status = nfs4_init_uniform_client_string(clp);
5724 else
Jeff Laytona3192682015-06-09 19:43:59 -04005725 status = nfs4_init_nonuniform_client_string(clp);
Jeff Layton873e3852015-06-09 19:44:00 -04005726
5727 if (status)
5728 goto out;
Jeff Layton3a6bb732015-06-09 19:43:57 -04005729
Chuck Leverde734832012-07-11 16:30:50 -04005730 /* cb_client4 */
Chuck Lever706cb8d2014-03-12 12:51:47 -04005731 setclientid.sc_netid_len =
5732 nfs4_init_callback_netid(clp,
5733 setclientid.sc_netid,
5734 sizeof(setclientid.sc_netid));
Chuck Leverde734832012-07-11 16:30:50 -04005735 setclientid.sc_uaddr_len = scnprintf(setclientid.sc_uaddr,
Chuck Leverd4d3c502007-12-10 14:57:09 -05005736 sizeof(setclientid.sc_uaddr), "%s.%u.%u",
Linus Torvalds1da177e2005-04-16 15:20:36 -07005737 clp->cl_ipaddr, port >> 8, port & 255);
5738
Jeff Layton3a6bb732015-06-09 19:43:57 -04005739 dprintk("NFS call setclientid auth=%s, '%s'\n",
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005740 clp->cl_rpcclient->cl_auth->au_ops->au_name,
Jeff Layton3a6bb732015-06-09 19:43:57 -04005741 clp->cl_owner_id);
Jeff Laytonf11b2a12014-06-21 20:52:17 -04005742 task = rpc_run_task(&task_setup_data);
5743 if (IS_ERR(task)) {
5744 status = PTR_ERR(task);
5745 goto out;
5746 }
5747 status = task->tk_status;
5748 if (setclientid.sc_cred) {
5749 clp->cl_acceptor = rpcauth_stringify_acceptor(setclientid.sc_cred);
5750 put_rpccred(setclientid.sc_cred);
5751 }
5752 rpc_put_task(task);
5753out:
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005754 trace_nfs4_setclientid(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005755 dprintk("NFS reply setclientid: %d\n", status);
5756 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005757}
5758
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005759/**
5760 * nfs4_proc_setclientid_confirm - Confirm client ID
5761 * @clp: state data structure
5762 * @res: result of a previous SETCLIENTID
5763 * @cred: RPC credential to use for this call
5764 *
5765 * Returns zero, a negative errno, or a negative NFS4ERR status code.
5766 */
Trond Myklebustfd954ae2011-04-24 14:28:18 -04005767int nfs4_proc_setclientid_confirm(struct nfs_client *clp,
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005768 struct nfs4_setclientid_res *arg,
5769 struct rpc_cred *cred)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005770{
Linus Torvalds1da177e2005-04-16 15:20:36 -07005771 struct rpc_message msg = {
5772 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SETCLIENTID_CONFIRM],
Trond Myklebustbb8b27e2010-04-16 16:43:06 -04005773 .rpc_argp = arg,
Trond Myklebust286d7d62006-01-03 09:55:26 +01005774 .rpc_cred = cred,
Linus Torvalds1da177e2005-04-16 15:20:36 -07005775 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07005776 int status;
5777
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005778 dprintk("NFS call setclientid_confirm auth=%s, (client ID %llx)\n",
5779 clp->cl_rpcclient->cl_auth->au_ops->au_name,
5780 clp->cl_clientid);
Trond Myklebust1bd714f2011-04-24 14:29:33 -04005781 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04005782 trace_nfs4_setclientid_confirm(clp, status);
Chuck Lever6bbb4ae2012-07-11 16:30:59 -04005783 dprintk("NFS reply setclientid_confirm: %d\n", status);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005784 return status;
5785}
5786
Trond Myklebustfe650402006-01-03 09:55:18 +01005787struct nfs4_delegreturndata {
5788 struct nfs4_delegreturnargs args;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005789 struct nfs4_delegreturnres res;
Trond Myklebustfe650402006-01-03 09:55:18 +01005790 struct nfs_fh fh;
5791 nfs4_stateid stateid;
Trond Myklebust26e976a2006-01-03 09:55:21 +01005792 unsigned long timestamp;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005793 struct {
5794 struct nfs4_layoutreturn_args arg;
5795 struct nfs4_layoutreturn_res res;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005796 struct nfs4_xdr_opaque_data ld_private;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005797 u32 roc_barrier;
5798 bool roc;
5799 } lr;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005800 struct nfs_fattr fattr;
Trond Myklebustfe650402006-01-03 09:55:18 +01005801 int rpc_status;
Peng Tao039b7562014-07-03 13:05:02 +08005802 struct inode *inode;
Trond Myklebustfe650402006-01-03 09:55:18 +01005803};
5804
Trond Myklebustfe650402006-01-03 09:55:18 +01005805static void nfs4_delegreturn_done(struct rpc_task *task, void *calldata)
5806{
5807 struct nfs4_delegreturndata *data = calldata;
Trond Myklebuste0dba012017-11-07 11:02:32 -05005808 struct nfs4_exception exception = {
5809 .inode = data->inode,
5810 .stateid = &data->stateid,
5811 };
Andy Adamson938e1012009-04-01 09:22:28 -04005812
Trond Myklebust14516c32010-07-31 14:29:06 -04005813 if (!nfs4_sequence_done(task, &data->res.seq_res))
5814 return;
Andy Adamson938e1012009-04-01 09:22:28 -04005815
Trond Myklebustca8acf82013-08-13 10:36:56 -04005816 trace_nfs4_delegreturn_exit(&data->args, &data->res, task->tk_status);
Trond Myklebust586f1c32016-11-15 15:03:33 -05005817
5818 /* Handle Layoutreturn errors */
5819 if (data->args.lr_args && task->tk_status != 0) {
5820 switch(data->res.lr_ret) {
5821 default:
5822 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
5823 break;
5824 case 0:
5825 data->args.lr_args = NULL;
5826 data->res.lr_res = NULL;
5827 break;
Trond Myklebust73800202017-11-06 15:28:07 -05005828 case -NFS4ERR_OLD_STATEID:
5829 if (nfs4_refresh_layout_stateid(&data->args.lr_args->stateid,
Trond Myklebust140087fd2017-11-06 15:28:10 -05005830 data->inode))
5831 goto lr_restart;
Trond Myklebust73800202017-11-06 15:28:07 -05005832 /* Fallthrough */
Trond Myklebust586f1c32016-11-15 15:03:33 -05005833 case -NFS4ERR_ADMIN_REVOKED:
5834 case -NFS4ERR_DELEG_REVOKED:
5835 case -NFS4ERR_EXPIRED:
5836 case -NFS4ERR_BAD_STATEID:
Trond Myklebust586f1c32016-11-15 15:03:33 -05005837 case -NFS4ERR_UNKNOWN_LAYOUTTYPE:
5838 case -NFS4ERR_WRONG_CRED:
5839 data->args.lr_args = NULL;
5840 data->res.lr_res = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005841 goto lr_restart;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005842 }
5843 }
5844
Ricardo Labiaga79708862009-12-07 09:23:21 -05005845 switch (task->tk_status) {
Ricardo Labiaga79708862009-12-07 09:23:21 -05005846 case 0:
Trond Myklebustfa178f22006-01-03 09:55:38 +01005847 renew_lease(data->res.server, data->timestamp);
Trond Myklebust23ea44c2016-11-10 16:06:28 -05005848 break;
Trond Myklebustc97cf602013-11-19 16:34:14 -05005849 case -NFS4ERR_ADMIN_REVOKED:
5850 case -NFS4ERR_DELEG_REVOKED:
Trond Myklebust26d36302016-09-22 13:39:05 -04005851 case -NFS4ERR_EXPIRED:
5852 nfs4_free_revoked_stateid(data->res.server,
5853 data->args.stateid,
5854 task->tk_msg.rpc_cred);
Trond Myklebust12f275c2017-11-06 15:28:05 -05005855 /* Fallthrough */
Trond Myklebustc97cf602013-11-19 16:34:14 -05005856 case -NFS4ERR_BAD_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005857 case -NFS4ERR_STALE_STATEID:
Trond Myklebustc97cf602013-11-19 16:34:14 -05005858 task->tk_status = 0;
5859 break;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005860 case -NFS4ERR_OLD_STATEID:
Trond Myklebust140087fd2017-11-06 15:28:10 -05005861 if (nfs4_refresh_delegation_stateid(&data->stateid, data->inode))
5862 goto out_restart;
Trond Myklebust12f275c2017-11-06 15:28:05 -05005863 task->tk_status = 0;
5864 break;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005865 case -NFS4ERR_ACCESS:
5866 if (data->args.bitmask) {
5867 data->args.bitmask = NULL;
5868 data->res.fattr = NULL;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005869 goto out_restart;
Trond Myklebust8ac2b4222016-12-19 10:23:10 -05005870 }
Trond Myklebust140087fd2017-11-06 15:28:10 -05005871 /* Fallthrough */
Ricardo Labiaga79708862009-12-07 09:23:21 -05005872 default:
Trond Myklebuste0dba012017-11-07 11:02:32 -05005873 task->tk_status = nfs4_async_handle_exception(task,
5874 data->res.server, task->tk_status,
5875 &exception);
5876 if (exception.retry)
Trond Myklebust140087fd2017-11-06 15:28:10 -05005877 goto out_restart;
Ricardo Labiaga79708862009-12-07 09:23:21 -05005878 }
5879 data->rpc_status = task->tk_status;
Trond Myklebust140087fd2017-11-06 15:28:10 -05005880 return;
5881lr_restart:
5882 data->res.lr_ret = 0;
5883out_restart:
5884 task->tk_status = 0;
5885 rpc_restart_call_prepare(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005886}
5887
5888static void nfs4_delegreturn_release(void *calldata)
5889{
Peng Tao039b7562014-07-03 13:05:02 +08005890 struct nfs4_delegreturndata *data = calldata;
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005891 struct inode *inode = data->inode;
Peng Tao039b7562014-07-03 13:05:02 +08005892
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005893 if (inode) {
Trond Myklebust586f1c32016-11-15 15:03:33 -05005894 if (data->lr.roc)
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005895 pnfs_roc_release(&data->lr.arg, &data->lr.res,
5896 data->res.lr_ret);
Trond Myklebust0bc2c9b2016-12-16 19:48:09 -05005897 nfs_post_op_update_inode_force_wcc(inode, &data->fattr);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005898 nfs_iput_and_deactive(inode);
5899 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005900 kfree(calldata);
5901}
5902
Andy Adamson938e1012009-04-01 09:22:28 -04005903static void nfs4_delegreturn_prepare(struct rpc_task *task, void *data)
5904{
5905 struct nfs4_delegreturndata *d_data;
5906
5907 d_data = (struct nfs4_delegreturndata *)data;
5908
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005909 if (!d_data->lr.roc && nfs4_wait_on_layoutreturn(d_data->inode, task))
Peng Tao500d7012015-09-22 11:35:22 +08005910 return;
5911
Anna Schumaker42e1cca2017-01-09 15:48:22 -05005912 nfs4_setup_sequence(d_data->res.server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04005913 &d_data->args.seq_args,
5914 &d_data->res.seq_res,
5915 task);
Andy Adamson938e1012009-04-01 09:22:28 -04005916}
Andy Adamson938e1012009-04-01 09:22:28 -04005917
Jesper Juhlc8d149f2006-03-20 13:44:07 -05005918static const struct rpc_call_ops nfs4_delegreturn_ops = {
Andy Adamson938e1012009-04-01 09:22:28 -04005919 .rpc_call_prepare = nfs4_delegreturn_prepare,
Trond Myklebustfe650402006-01-03 09:55:18 +01005920 .rpc_call_done = nfs4_delegreturn_done,
5921 .rpc_release = nfs4_delegreturn_release,
5922};
5923
Trond Myklebuste6f81072008-01-24 18:14:34 -05005924static int _nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Trond Myklebustfe650402006-01-03 09:55:18 +01005925{
5926 struct nfs4_delegreturndata *data;
Trond Myklebustfa178f22006-01-03 09:55:38 +01005927 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustfe650402006-01-03 09:55:18 +01005928 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005929 struct rpc_message msg = {
5930 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DELEGRETURN],
5931 .rpc_cred = cred,
5932 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04005933 struct rpc_task_setup task_setup_data = {
5934 .rpc_client = server->client,
Trond Myklebust5138fde2007-07-14 15:40:01 -04005935 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04005936 .callback_ops = &nfs4_delegreturn_ops,
5937 .flags = RPC_TASK_ASYNC,
5938 };
Trond Myklebuste6f81072008-01-24 18:14:34 -05005939 int status = 0;
Trond Myklebustfe650402006-01-03 09:55:18 +01005940
Trond Myklebust8535b2b2010-05-13 12:51:01 -04005941 data = kzalloc(sizeof(*data), GFP_NOFS);
Trond Myklebustfe650402006-01-03 09:55:18 +01005942 if (data == NULL)
5943 return -ENOMEM;
Chuck Levera9c92d62013-08-09 12:48:18 -04005944 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andrew Elble99ade3c2015-12-02 09:39:51 -05005945
5946 nfs4_state_protect(server->nfs_client,
5947 NFS_SP4_MACH_CRED_CLEANUP,
5948 &task_setup_data.rpc_client, &msg);
5949
Trond Myklebustfe650402006-01-03 09:55:18 +01005950 data->args.fhandle = &data->fh;
5951 data->args.stateid = &data->stateid;
Trond Myklebust9e907fe2012-04-27 13:48:17 -04005952 data->args.bitmask = server->cache_consistency_bitmask;
Trond Myklebustfe650402006-01-03 09:55:18 +01005953 nfs_copy_fh(&data->fh, NFS_FH(inode));
Trond Myklebustf597c532012-03-04 18:13:56 -05005954 nfs4_stateid_copy(&data->stateid, stateid);
Trond Myklebustfa178f22006-01-03 09:55:38 +01005955 data->res.fattr = &data->fattr;
5956 data->res.server = server;
Trond Myklebust586f1c32016-11-15 15:03:33 -05005957 data->res.lr_ret = -NFS4ERR_NOMATCHING_LAYOUT;
Trond Myklebust4d796d72016-09-23 11:38:08 -04005958 data->lr.arg.ld_private = &data->lr.ld_private;
Trond Myklebust5138fde2007-07-14 15:40:01 -04005959 nfs_fattr_init(data->res.fattr);
Trond Myklebust26e976a2006-01-03 09:55:21 +01005960 data->timestamp = jiffies;
Trond Myklebustfe650402006-01-03 09:55:18 +01005961 data->rpc_status = 0;
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005962 data->lr.roc = pnfs_roc(inode, &data->lr.arg, &data->lr.res, cred);
Trond Myklebustea7c38f2015-02-05 15:13:24 -05005963 data->inode = nfs_igrab_and_active(inode);
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005964 if (data->inode) {
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005965 if (data->lr.roc) {
5966 data->args.lr_args = &data->lr.arg;
5967 data->res.lr_res = &data->lr.res;
5968 }
Trond Myklebust53e6fc82016-11-19 08:48:47 -05005969 } else if (data->lr.roc) {
5970 pnfs_roc_release(&data->lr.arg, &data->lr.res, 0);
5971 data->lr.roc = false;
Trond Myklebust1c5bd76d2016-11-16 01:11:25 -05005972 }
Trond Myklebustfe650402006-01-03 09:55:18 +01005973
Trond Myklebustc970aa82007-07-14 15:39:59 -04005974 task_setup_data.callback_data = data;
Trond Myklebust1174dd12010-12-21 10:52:24 -05005975 msg.rpc_argp = &data->args;
5976 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04005977 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05005978 if (IS_ERR(task))
Trond Myklebustfe650402006-01-03 09:55:18 +01005979 return PTR_ERR(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005980 if (!issync)
5981 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05005982 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6f81072008-01-24 18:14:34 -05005983 if (status != 0)
5984 goto out;
5985 status = data->rpc_status;
Trond Myklebuste6f81072008-01-24 18:14:34 -05005986out:
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05005987 rpc_put_task(task);
Trond Myklebustfe650402006-01-03 09:55:18 +01005988 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07005989}
5990
Trond Myklebuste6f81072008-01-24 18:14:34 -05005991int nfs4_proc_delegreturn(struct inode *inode, struct rpc_cred *cred, const nfs4_stateid *stateid, int issync)
Linus Torvalds1da177e2005-04-16 15:20:36 -07005992{
5993 struct nfs_server *server = NFS_SERVER(inode);
5994 struct nfs4_exception exception = { };
5995 int err;
5996 do {
Trond Myklebuste6f81072008-01-24 18:14:34 -05005997 err = _nfs4_proc_delegreturn(inode, cred, stateid, issync);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05005998 trace_nfs4_delegreturn(inode, stateid, err);
Linus Torvalds1da177e2005-04-16 15:20:36 -07005999 switch (err) {
6000 case -NFS4ERR_STALE_STATEID:
6001 case -NFS4ERR_EXPIRED:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006002 case 0:
6003 return 0;
6004 }
6005 err = nfs4_handle_exception(server, err, &exception);
6006 } while (exception.retry);
6007 return err;
6008}
6009
Linus Torvalds1da177e2005-04-16 15:20:36 -07006010static int _nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6011{
6012 struct inode *inode = state->inode;
6013 struct nfs_server *server = NFS_SERVER(inode);
David Howells7539bba2006-08-22 20:06:09 -04006014 struct nfs_client *clp = server->nfs_client;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006015 struct nfs_lockt_args arg = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07006016 .fh = NFS_FH(inode),
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006017 .fl = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006018 };
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006019 struct nfs_lockt_res res = {
6020 .denied = request,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006021 };
6022 struct rpc_message msg = {
6023 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKT],
Anna Schumakerd9b67e12017-01-11 15:04:25 -05006024 .rpc_argp = &arg,
6025 .rpc_resp = &res,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006026 .rpc_cred = state->owner->so_cred,
6027 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006028 struct nfs4_lock_state *lsp;
6029 int status;
6030
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006031 arg.lock_owner.clientid = clp->cl_clientid;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006032 status = nfs4_set_lock_state(state, request);
6033 if (status != 0)
6034 goto out;
6035 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006036 arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006037 arg.lock_owner.s_dev = server->s_dev;
Bryan Schumaker7c513052011-03-24 17:12:24 +00006038 status = nfs4_call_sync(server->client, server, &msg, &arg.seq_args, &res.seq_res, 1);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006039 switch (status) {
6040 case 0:
6041 request->fl_type = F_UNLCK;
6042 break;
6043 case -NFS4ERR_DENIED:
6044 status = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006045 }
J. Bruce Fields70cc6482007-02-22 18:48:53 -05006046 request->fl_ops->fl_release_private(request);
Trond Myklebusta6f951d2013-10-01 14:24:58 -04006047 request->fl_ops = NULL;
Trond Myklebust8d0a8a92005-06-22 17:16:32 +00006048out:
Linus Torvalds1da177e2005-04-16 15:20:36 -07006049 return status;
6050}
6051
6052static int nfs4_proc_getlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6053{
6054 struct nfs4_exception exception = { };
6055 int err;
6056
6057 do {
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006058 err = _nfs4_proc_getlk(state, cmd, request);
6059 trace_nfs4_get_lock(request, state, cmd, err);
6060 err = nfs4_handle_exception(NFS_SERVER(state->inode), err,
Linus Torvalds1da177e2005-04-16 15:20:36 -07006061 &exception);
6062 } while (exception.retry);
6063 return err;
6064}
6065
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006066struct nfs4_unlockdata {
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006067 struct nfs_locku_args arg;
6068 struct nfs_locku_res res;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006069 struct nfs4_lock_state *lsp;
6070 struct nfs_open_context *ctx;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006071 struct nfs_lock_context *l_ctx;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006072 struct file_lock fl;
Trond Myklebust516285eb2015-09-20 16:15:24 -04006073 struct nfs_server *server;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006074 unsigned long timestamp;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006075};
6076
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006077static struct nfs4_unlockdata *nfs4_alloc_unlockdata(struct file_lock *fl,
6078 struct nfs_open_context *ctx,
6079 struct nfs4_lock_state *lsp,
6080 struct nfs_seqid *seqid)
6081{
6082 struct nfs4_unlockdata *p;
6083 struct inode *inode = lsp->ls_state->inode;
6084
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006085 p = kzalloc(sizeof(*p), GFP_NOFS);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006086 if (p == NULL)
6087 return NULL;
6088 p->arg.fh = NFS_FH(inode);
6089 p->arg.fl = &p->fl;
6090 p->arg.seqid = seqid;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006091 p->res.seqid = seqid;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006092 p->lsp = lsp;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006093 refcount_inc(&lsp->ls_count);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006094 /* Ensure we don't close file until we're done freeing locks! */
6095 p->ctx = get_nfs_open_context(ctx);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006096 p->l_ctx = nfs_get_lock_context(ctx);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006097 memcpy(&p->fl, fl, sizeof(p->fl));
6098 p->server = NFS_SERVER(inode);
6099 return p;
6100}
6101
Trond Myklebust06f814a2006-01-03 09:55:07 +01006102static void nfs4_locku_release_calldata(void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006103{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006104 struct nfs4_unlockdata *calldata = data;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006105 nfs_free_seqid(calldata->arg.seqid);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006106 nfs4_put_lock_state(calldata->lsp);
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006107 nfs_put_lock_context(calldata->l_ctx);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006108 put_nfs_open_context(calldata->ctx);
6109 kfree(calldata);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006110}
6111
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006112static void nfs4_locku_done(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006113{
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006114 struct nfs4_unlockdata *calldata = data;
Trond Myklebust82571552017-11-07 11:14:49 -05006115 struct nfs4_exception exception = {
6116 .inode = calldata->lsp->ls_state->inode,
6117 .stateid = &calldata->arg.stateid,
6118 };
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006119
Trond Myklebust14516c32010-07-31 14:29:06 -04006120 if (!nfs4_sequence_done(task, &calldata->res.seq_res))
6121 return;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006122 switch (task->tk_status) {
6123 case 0:
Trond Myklebust26e976a2006-01-03 09:55:21 +01006124 renew_lease(calldata->server, calldata->timestamp);
Jeff Layton75575dd2016-09-17 18:17:32 -04006125 locks_lock_inode_wait(calldata->lsp->ls_state->inode, &calldata->fl);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006126 if (nfs4_update_lock_stateid(calldata->lsp,
6127 &calldata->res.stateid))
6128 break;
Trond Myklebust26d36302016-09-22 13:39:05 -04006129 case -NFS4ERR_ADMIN_REVOKED:
6130 case -NFS4ERR_EXPIRED:
6131 nfs4_free_revoked_stateid(calldata->server,
6132 &calldata->arg.stateid,
6133 task->tk_msg.rpc_cred);
Trond Myklebust9e33bed2008-12-23 15:21:46 -05006134 case -NFS4ERR_BAD_STATEID:
6135 case -NFS4ERR_OLD_STATEID:
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006136 case -NFS4ERR_STALE_STATEID:
Trond Myklebust425c1d42015-01-24 14:57:53 -05006137 if (!nfs4_stateid_match(&calldata->arg.stateid,
6138 &calldata->lsp->ls_stateid))
6139 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006140 break;
6141 default:
Trond Myklebust82571552017-11-07 11:14:49 -05006142 task->tk_status = nfs4_async_handle_exception(task,
6143 calldata->server, task->tk_status,
6144 &exception);
6145 if (exception.retry)
Trond Myklebustd00c5d42011-10-19 12:17:29 -07006146 rpc_restart_call_prepare(task);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006147 }
Trond Myklebust2b1bc302012-10-29 18:53:23 -04006148 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006149}
6150
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006151static void nfs4_locku_prepare(struct rpc_task *task, void *data)
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006152{
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006153 struct nfs4_unlockdata *calldata = data;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006154
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006155 if (test_bit(NFS_CONTEXT_UNLOCK, &calldata->l_ctx->open_context->flags) &&
6156 nfs_async_iocounter_wait(task, calldata->l_ctx))
6157 return;
6158
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006159 if (nfs_wait_on_sequence(calldata->arg.seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006160 goto out_wait;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006161 nfs4_stateid_copy(&calldata->arg.stateid, &calldata->lsp->ls_stateid);
Trond Myklebust795a88c2012-09-10 13:26:49 -04006162 if (test_bit(NFS_LOCK_INITIALIZED, &calldata->lsp->ls_flags) == 0) {
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006163 /* Note: exit _without_ running nfs4_locku_done */
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006164 goto out_no_action;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006165 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006166 calldata->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006167 if (nfs4_setup_sequence(calldata->server->nfs_client,
Andy Adamsona8936932009-04-01 09:22:23 -04006168 &calldata->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006169 &calldata->res.seq_res,
6170 task) != 0)
6171 nfs_release_seqid(calldata->arg.seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006172 return;
6173out_no_action:
6174 task->tk_action = NULL;
6175out_wait:
6176 nfs4_sequence_done(task, &calldata->res.seq_res);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006177}
6178
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006179static const struct rpc_call_ops nfs4_locku_ops = {
Trond Myklebust4ce70ad2006-01-03 09:55:05 +01006180 .rpc_call_prepare = nfs4_locku_prepare,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006181 .rpc_call_done = nfs4_locku_done,
Trond Myklebust06f814a2006-01-03 09:55:07 +01006182 .rpc_release = nfs4_locku_release_calldata,
Trond Myklebust963d8fe2006-01-03 09:55:04 +01006183};
6184
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006185static struct rpc_task *nfs4_do_unlck(struct file_lock *fl,
6186 struct nfs_open_context *ctx,
6187 struct nfs4_lock_state *lsp,
6188 struct nfs_seqid *seqid)
6189{
6190 struct nfs4_unlockdata *data;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006191 struct rpc_message msg = {
6192 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCKU],
6193 .rpc_cred = ctx->cred,
6194 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006195 struct rpc_task_setup task_setup_data = {
6196 .rpc_client = NFS_CLIENT(lsp->ls_state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006197 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006198 .callback_ops = &nfs4_locku_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006199 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006200 .flags = RPC_TASK_ASYNC,
6201 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006202
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04006203 nfs4_state_protect(NFS_SERVER(lsp->ls_state->inode)->nfs_client,
6204 NFS_SP4_MACH_CRED_CLEANUP, &task_setup_data.rpc_client, &msg);
6205
Frank Filz137d6ac2007-07-09 15:32:29 -07006206 /* Ensure this is an unlock - when canceling a lock, the
6207 * canceled lock is passed in, and it won't be an unlock.
6208 */
6209 fl->fl_type = F_UNLCK;
Benjamin Coddingtonf30cb752017-04-11 12:50:12 -04006210 if (fl->fl_flags & FL_CLOSE)
6211 set_bit(NFS_CONTEXT_UNLOCK, &ctx->flags);
Frank Filz137d6ac2007-07-09 15:32:29 -07006212
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006213 data = nfs4_alloc_unlockdata(fl, ctx, lsp, seqid);
6214 if (data == NULL) {
6215 nfs_free_seqid(seqid);
6216 return ERR_PTR(-ENOMEM);
6217 }
6218
Chuck Levera9c92d62013-08-09 12:48:18 -04006219 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006220 msg.rpc_argp = &data->arg;
6221 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006222 task_setup_data.callback_data = data;
6223 return rpc_run_task(&task_setup_data);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006224}
6225
Linus Torvalds1da177e2005-04-16 15:20:36 -07006226static int nfs4_proc_unlck(struct nfs4_state *state, int cmd, struct file_lock *request)
6227{
Trond Myklebust65b62a22013-02-07 10:54:07 -05006228 struct inode *inode = state->inode;
6229 struct nfs4_state_owner *sp = state->owner;
6230 struct nfs_inode *nfsi = NFS_I(inode);
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006231 struct nfs_seqid *seqid;
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006232 struct nfs4_lock_state *lsp;
Trond Myklebust06f814a2006-01-03 09:55:07 +01006233 struct rpc_task *task;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006234 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebust06f814a2006-01-03 09:55:07 +01006235 int status = 0;
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006236 unsigned char fl_flags = request->fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006237
Trond Myklebust9b073572006-06-29 16:38:34 -04006238 status = nfs4_set_lock_state(state, request);
6239 /* Unlock _before_ we do the RPC call */
6240 request->fl_flags |= FL_EXISTS;
Trond Myklebust65b62a22013-02-07 10:54:07 -05006241 /* Exclude nfs_delegation_claim_locks() */
6242 mutex_lock(&sp->so_delegreturn_mutex);
6243 /* Exclude nfs4_reclaim_open_stateid() - note nesting! */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006244 down_read(&nfsi->rwsem);
Jeff Layton75575dd2016-09-17 18:17:32 -04006245 if (locks_lock_inode_wait(inode, request) == -ENOENT) {
Trond Myklebust19e03c52008-12-23 15:21:44 -05006246 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006247 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006248 goto out;
Trond Myklebust19e03c52008-12-23 15:21:44 -05006249 }
6250 up_read(&nfsi->rwsem);
Trond Myklebust65b62a22013-02-07 10:54:07 -05006251 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebust9b073572006-06-29 16:38:34 -04006252 if (status != 0)
6253 goto out;
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006254 /* Is this a delegated lock? */
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006255 lsp = request->fl_u.nfs4_fl.owner;
Trond Myklebustc5a2a152013-04-30 12:43:42 -04006256 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) == 0)
6257 goto out;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006258 alloc_seqid = NFS_SERVER(inode)->nfs_client->cl_mvops->alloc_seqid;
6259 seqid = alloc_seqid(&lsp->ls_seqid, GFP_KERNEL);
Trond Myklebust9b073572006-06-29 16:38:34 -04006260 status = -ENOMEM;
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006261 if (IS_ERR(seqid))
Trond Myklebust9b073572006-06-29 16:38:34 -04006262 goto out;
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006263 task = nfs4_do_unlck(request, nfs_file_open_context(request->fl_file), lsp, seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006264 status = PTR_ERR(task);
6265 if (IS_ERR(task))
Trond Myklebust9b073572006-06-29 16:38:34 -04006266 goto out;
Anna Schumaker820bf852017-01-11 15:01:43 -05006267 status = rpc_wait_for_completion_task(task);
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006268 rpc_put_task(task);
Trond Myklebust9b073572006-06-29 16:38:34 -04006269out:
Trond Myklebust536ff0f2008-04-04 15:08:02 -04006270 request->fl_flags = fl_flags;
Trond Myklebustd1b748a2013-08-12 16:35:20 -04006271 trace_nfs4_unlock(request, state, F_SETLK, status);
Trond Myklebustfaf5f492005-10-18 14:20:15 -07006272 return status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006273}
6274
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006275struct nfs4_lockdata {
6276 struct nfs_lock_args arg;
Trond Myklebust911d1aa2006-01-03 09:55:16 +01006277 struct nfs_lock_res res;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006278 struct nfs4_lock_state *lsp;
6279 struct nfs_open_context *ctx;
6280 struct file_lock fl;
Trond Myklebust26e976a2006-01-03 09:55:21 +01006281 unsigned long timestamp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006282 int rpc_status;
6283 int cancelled;
Andy Adamson66179ef2009-04-01 09:22:22 -04006284 struct nfs_server *server;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006285};
6286
6287static struct nfs4_lockdata *nfs4_alloc_lockdata(struct file_lock *fl,
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006288 struct nfs_open_context *ctx, struct nfs4_lock_state *lsp,
6289 gfp_t gfp_mask)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006290{
6291 struct nfs4_lockdata *p;
6292 struct inode *inode = lsp->ls_state->inode;
6293 struct nfs_server *server = NFS_SERVER(inode);
Trond Myklebustb4019c02015-01-24 14:19:19 -05006294 struct nfs_seqid *(*alloc_seqid)(struct nfs_seqid_counter *, gfp_t);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006295
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006296 p = kzalloc(sizeof(*p), gfp_mask);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006297 if (p == NULL)
6298 return NULL;
6299
6300 p->arg.fh = NFS_FH(inode);
6301 p->arg.fl = &p->fl;
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006302 p->arg.open_seqid = nfs_alloc_seqid(&lsp->ls_state->owner->so_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006303 if (IS_ERR(p->arg.open_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006304 goto out_free;
Trond Myklebustb4019c02015-01-24 14:19:19 -05006305 alloc_seqid = server->nfs_client->cl_mvops->alloc_seqid;
6306 p->arg.lock_seqid = alloc_seqid(&lsp->ls_seqid, gfp_mask);
Trond Myklebustbadc76d2015-01-23 18:48:00 -05006307 if (IS_ERR(p->arg.lock_seqid))
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006308 goto out_free_seqid;
David Howells7539bba2006-08-22 20:06:09 -04006309 p->arg.lock_owner.clientid = server->nfs_client->cl_clientid;
Trond Myklebust48c22eb2012-01-17 22:04:25 -05006310 p->arg.lock_owner.id = lsp->ls_seqid.owner_id;
Trond Myklebustd035c362010-12-21 10:45:27 -05006311 p->arg.lock_owner.s_dev = server->s_dev;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006312 p->res.lock_seqid = p->arg.lock_seqid;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006313 p->lsp = lsp;
Andy Adamson66179ef2009-04-01 09:22:22 -04006314 p->server = server;
Elena Reshetova194bc1f2017-10-20 12:53:36 +03006315 refcount_inc(&lsp->ls_count);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006316 p->ctx = get_nfs_open_context(ctx);
6317 memcpy(&p->fl, fl, sizeof(p->fl));
6318 return p;
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006319out_free_seqid:
6320 nfs_free_seqid(p->arg.open_seqid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006321out_free:
6322 kfree(p);
6323 return NULL;
6324}
6325
6326static void nfs4_lock_prepare(struct rpc_task *task, void *calldata)
6327{
6328 struct nfs4_lockdata *data = calldata;
6329 struct nfs4_state *state = data->lsp->ls_state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006330
Harvey Harrison3110ff82008-05-02 13:42:44 -07006331 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006332 if (nfs_wait_on_sequence(data->arg.lock_seqid, task) != 0)
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006333 goto out_wait;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006334 /* Do we need to do an open_to_lock_owner? */
Trond Myklebust6b447532015-01-24 18:38:15 -05006335 if (!test_bit(NFS_LOCK_INITIALIZED, &data->lsp->ls_flags)) {
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006336 if (nfs_wait_on_sequence(data->arg.open_seqid, task) != 0) {
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006337 goto out_release_lock_seqid;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006338 }
Trond Myklebust425c1d42015-01-24 14:57:53 -05006339 nfs4_stateid_copy(&data->arg.open_stateid,
6340 &state->open_stateid);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006341 data->arg.new_lock_owner = 1;
Trond Myklebustc1d51932008-04-07 13:20:54 -04006342 data->res.open_seqid = data->arg.open_seqid;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006343 } else {
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006344 data->arg.new_lock_owner = 0;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006345 nfs4_stateid_copy(&data->arg.lock_stateid,
6346 &data->lsp->ls_stateid);
6347 }
Trond Myklebust5d422302013-03-14 16:57:48 -04006348 if (!nfs4_valid_open_stateid(state)) {
6349 data->rpc_status = -EBADF;
6350 task->tk_action = NULL;
6351 goto out_release_open_seqid;
6352 }
Trond Myklebust26e976a2006-01-03 09:55:21 +01006353 data->timestamp = jiffies;
Anna Schumaker42e1cca2017-01-09 15:48:22 -05006354 if (nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust035168ab2010-06-16 09:52:26 -04006355 &data->arg.seq_args,
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006356 &data->res.seq_res,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04006357 task) == 0)
Andy Adamson66179ef2009-04-01 09:22:22 -04006358 return;
Trond Myklebust5d422302013-03-14 16:57:48 -04006359out_release_open_seqid:
Trond Myklebust2240a9e2012-10-29 18:37:40 -04006360 nfs_release_seqid(data->arg.open_seqid);
6361out_release_lock_seqid:
6362 nfs_release_seqid(data->arg.lock_seqid);
Trond Myklebustc8da19b2013-02-11 19:01:21 -05006363out_wait:
6364 nfs4_sequence_done(task, &data->res.seq_res);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006365 dprintk("%s: done!, ret = %d\n", __func__, data->rpc_status);
Alexandros Batsakisb2579572009-12-14 21:27:57 -08006366}
6367
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006368static void nfs4_lock_done(struct rpc_task *task, void *calldata)
6369{
6370 struct nfs4_lockdata *data = calldata;
Trond Myklebust39071e62015-01-24 15:07:56 -05006371 struct nfs4_lock_state *lsp = data->lsp;
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006372
Harvey Harrison3110ff82008-05-02 13:42:44 -07006373 dprintk("%s: begin!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006374
Trond Myklebust14516c32010-07-31 14:29:06 -04006375 if (!nfs4_sequence_done(task, &data->res.seq_res))
6376 return;
Andy Adamson66179ef2009-04-01 09:22:22 -04006377
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006378 data->rpc_status = task->tk_status;
Trond Myklebust425c1d42015-01-24 14:57:53 -05006379 switch (task->tk_status) {
6380 case 0:
David Howells2b0143b2015-03-17 22:25:59 +00006381 renew_lease(NFS_SERVER(d_inode(data->ctx->dentry)),
Trond Myklebust39071e62015-01-24 15:07:56 -05006382 data->timestamp);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006383 if (data->arg.new_lock) {
6384 data->fl.fl_flags &= ~(FL_SLEEP | FL_ACCESS);
Jeff Layton75575dd2016-09-17 18:17:32 -04006385 if (locks_lock_inode_wait(lsp->ls_state->inode, &data->fl) < 0) {
Trond Myklebustc69899a2015-01-24 16:03:52 -05006386 rpc_restart_call_prepare(task);
6387 break;
6388 }
6389 }
Trond Myklebust39071e62015-01-24 15:07:56 -05006390 if (data->arg.new_lock_owner != 0) {
6391 nfs_confirm_seqid(&lsp->ls_seqid, 0);
6392 nfs4_stateid_copy(&lsp->ls_stateid, &data->res.stateid);
6393 set_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags);
6394 } else if (!nfs4_update_lock_stateid(lsp, &data->res.stateid))
6395 rpc_restart_call_prepare(task);
Trond Myklebust425c1d42015-01-24 14:57:53 -05006396 break;
6397 case -NFS4ERR_BAD_STATEID:
6398 case -NFS4ERR_OLD_STATEID:
6399 case -NFS4ERR_STALE_STATEID:
6400 case -NFS4ERR_EXPIRED:
6401 if (data->arg.new_lock_owner != 0) {
6402 if (!nfs4_stateid_match(&data->arg.open_stateid,
6403 &lsp->ls_state->open_stateid))
6404 rpc_restart_call_prepare(task);
6405 } else if (!nfs4_stateid_match(&data->arg.lock_stateid,
6406 &lsp->ls_stateid))
6407 rpc_restart_call_prepare(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006408 }
Harvey Harrison3110ff82008-05-02 13:42:44 -07006409 dprintk("%s: done, ret = %d!\n", __func__, data->rpc_status);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006410}
6411
6412static void nfs4_lock_release(void *calldata)
6413{
6414 struct nfs4_lockdata *data = calldata;
6415
Harvey Harrison3110ff82008-05-02 13:42:44 -07006416 dprintk("%s: begin!\n", __func__);
Trond Myklebust2f74c0a2008-01-08 17:56:07 -05006417 nfs_free_seqid(data->arg.open_seqid);
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006418 if (data->cancelled) {
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006419 struct rpc_task *task;
6420 task = nfs4_do_unlck(&data->fl, data->ctx, data->lsp,
6421 data->arg.lock_seqid);
6422 if (!IS_ERR(task))
Trond Myklebustbf294b42011-02-21 11:05:41 -08006423 rpc_put_task_async(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006424 dprintk("%s: cancelling lock!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006425 } else
6426 nfs_free_seqid(data->arg.lock_seqid);
6427 nfs4_put_lock_state(data->lsp);
6428 put_nfs_open_context(data->ctx);
6429 kfree(data);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006430 dprintk("%s: done!\n", __func__);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006431}
6432
6433static const struct rpc_call_ops nfs4_lock_ops = {
6434 .rpc_call_prepare = nfs4_lock_prepare,
6435 .rpc_call_done = nfs4_lock_done,
6436 .rpc_release = nfs4_lock_release,
6437};
6438
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006439static void nfs4_handle_setlk_error(struct nfs_server *server, struct nfs4_lock_state *lsp, int new_lock_owner, int error)
6440{
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006441 switch (error) {
6442 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustd7f3e4b2016-09-22 13:39:09 -04006443 case -NFS4ERR_EXPIRED:
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006444 case -NFS4ERR_BAD_STATEID:
Trond Myklebustecac7992011-03-09 16:00:56 -05006445 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006446 if (new_lock_owner != 0 ||
Trond Myklebust795a88c2012-09-10 13:26:49 -04006447 test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) != 0)
Trond Myklebustecac7992011-03-09 16:00:56 -05006448 nfs4_schedule_stateid_recovery(server, lsp->ls_state);
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006449 break;
6450 case -NFS4ERR_STALE_STATEID:
Trond Myklebusta2c0b9e2010-01-26 15:42:47 -05006451 lsp->ls_seqid.flags &= ~NFS_SEQID_CONFIRMED;
Trond Myklebustecac7992011-03-09 16:00:56 -05006452 nfs4_schedule_lease_recovery(server->nfs_client);
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006453 };
6454}
6455
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006456static int _nfs4_do_setlk(struct nfs4_state *state, int cmd, struct file_lock *fl, int recovery_type)
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006457{
6458 struct nfs4_lockdata *data;
6459 struct rpc_task *task;
Trond Myklebust5138fde2007-07-14 15:40:01 -04006460 struct rpc_message msg = {
6461 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LOCK],
6462 .rpc_cred = state->owner->so_cred,
6463 };
Trond Myklebustc970aa82007-07-14 15:39:59 -04006464 struct rpc_task_setup task_setup_data = {
6465 .rpc_client = NFS_CLIENT(state->inode),
Trond Myklebust5138fde2007-07-14 15:40:01 -04006466 .rpc_message = &msg,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006467 .callback_ops = &nfs4_lock_ops,
Trond Myklebust101070c2008-02-19 20:04:23 -05006468 .workqueue = nfsiod_workqueue,
Trond Myklebustc970aa82007-07-14 15:39:59 -04006469 .flags = RPC_TASK_ASYNC,
6470 };
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006471 int ret;
6472
Harvey Harrison3110ff82008-05-02 13:42:44 -07006473 dprintk("%s: begin!\n", __func__);
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006474 data = nfs4_alloc_lockdata(fl, nfs_file_open_context(fl->fl_file),
Trond Myklebust8535b2b2010-05-13 12:51:01 -04006475 fl->fl_u.nfs4_fl.owner,
6476 recovery_type == NFS_LOCK_NEW ? GFP_KERNEL : GFP_NOFS);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006477 if (data == NULL)
6478 return -ENOMEM;
6479 if (IS_SETLKW(cmd))
6480 data->arg.block = 1;
Chuck Levera9c92d62013-08-09 12:48:18 -04006481 nfs4_init_sequence(&data->arg.seq_args, &data->res.seq_res, 1);
Trond Myklebust1174dd12010-12-21 10:52:24 -05006482 msg.rpc_argp = &data->arg;
6483 msg.rpc_resp = &data->res;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006484 task_setup_data.callback_data = data;
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04006485 if (recovery_type > NFS_LOCK_NEW) {
6486 if (recovery_type == NFS_LOCK_RECLAIM)
6487 data->arg.reclaim = NFS_LOCK_RECLAIM;
6488 nfs4_set_sequence_privileged(&data->arg.seq_args);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006489 } else
6490 data->arg.new_lock = 1;
Trond Myklebustc970aa82007-07-14 15:39:59 -04006491 task = rpc_run_task(&task_setup_data);
Trond Myklebust7a1218a2006-03-20 18:11:10 -05006492 if (IS_ERR(task))
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006493 return PTR_ERR(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05006494 ret = rpc_wait_for_completion_task(task);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006495 if (ret == 0) {
6496 ret = data->rpc_status;
Trond Myklebust2bee72a2010-01-26 15:42:21 -05006497 if (ret)
6498 nfs4_handle_setlk_error(data->server, data->lsp,
6499 data->arg.new_lock_owner, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006500 } else
Benjamin Coddingtona7a3b1e2017-06-20 08:33:44 -04006501 data->cancelled = true;
Trond Myklebuste6b3c4d2006-11-11 22:18:03 -05006502 rpc_put_task(task);
Harvey Harrison3110ff82008-05-02 13:42:44 -07006503 dprintk("%s: done, ret = %d!\n", __func__, ret);
Olga Kornievskaia48c95792015-11-24 13:29:41 -05006504 trace_nfs4_set_lock(fl, state, &data->res.stateid, cmd, ret);
Trond Myklebusta5d16a42006-01-03 09:55:17 +01006505 return ret;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006506}
6507
6508static int nfs4_lock_reclaim(struct nfs4_state *state, struct file_lock *request)
6509{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006510 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006511 struct nfs4_exception exception = {
6512 .inode = state->inode,
6513 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006514 int err;
6515
6516 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006517 /* Cache the lock if possible... */
6518 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6519 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006520 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_RECLAIM);
Trond Myklebust168667c2010-10-19 19:47:49 -04006521 if (err != -NFS4ERR_DELAY)
Trond Myklebust202b50d2005-06-22 17:16:29 +00006522 break;
6523 nfs4_handle_exception(server, err, &exception);
6524 } while (exception.retry);
6525 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006526}
6527
6528static int nfs4_lock_expired(struct nfs4_state *state, struct file_lock *request)
6529{
Trond Myklebust202b50d2005-06-22 17:16:29 +00006530 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust05ffe242012-04-18 12:20:10 -04006531 struct nfs4_exception exception = {
6532 .inode = state->inode,
6533 };
Trond Myklebust202b50d2005-06-22 17:16:29 +00006534 int err;
6535
Trond Myklebust6bfc93e2005-11-04 15:39:36 -05006536 err = nfs4_set_lock_state(state, request);
6537 if (err != 0)
6538 return err;
Trond Myklebustf6de7a32013-09-04 10:08:54 -04006539 if (!recover_lost_locks) {
NeilBrownef1820f2013-09-04 17:04:49 +10006540 set_bit(NFS_LOCK_LOST, &request->fl_u.nfs4_fl.owner->ls_flags);
6541 return 0;
6542 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006543 do {
Trond Myklebust42a2d132006-06-29 16:38:36 -04006544 if (test_bit(NFS_DELEGATED_STATE, &state->flags) != 0)
6545 return 0;
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006546 err = _nfs4_do_setlk(state, F_SETLK, request, NFS_LOCK_EXPIRED);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006547 switch (err) {
6548 default:
6549 goto out;
6550 case -NFS4ERR_GRACE:
6551 case -NFS4ERR_DELAY:
6552 nfs4_handle_exception(server, err, &exception);
6553 err = 0;
6554 }
Trond Myklebust202b50d2005-06-22 17:16:29 +00006555 } while (exception.retry);
Trond Myklebusta9ed2e22009-12-03 15:53:21 -05006556out:
Trond Myklebust202b50d2005-06-22 17:16:29 +00006557 return err;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006558}
6559
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006560#if defined(CONFIG_NFS_V4_1)
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006561static int nfs41_lock_expired(struct nfs4_state *state, struct file_lock *request)
6562{
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006563 struct nfs4_lock_state *lsp;
6564 int status;
Bryan Schumakerb01dd1d2012-01-31 10:39:30 -05006565
Trond Myklebustc5896fc2016-09-22 13:39:03 -04006566 status = nfs4_set_lock_state(state, request);
6567 if (status != 0)
6568 return status;
6569 lsp = request->fl_u.nfs4_fl.owner;
6570 if (test_bit(NFS_LOCK_INITIALIZED, &lsp->ls_flags) ||
6571 test_bit(NFS_LOCK_LOST, &lsp->ls_flags))
6572 return 0;
Anna Schumaker81b68de2017-01-11 16:41:34 -05006573 return nfs4_lock_expired(state, request);
Bryan Schumakerf062eb62011-06-02 14:59:10 -04006574}
6575#endif
6576
Linus Torvalds1da177e2005-04-16 15:20:36 -07006577static int _nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6578{
Trond Myklebust19e03c52008-12-23 15:21:44 -05006579 struct nfs_inode *nfsi = NFS_I(state->inode);
Chuck Lever11476e92016-04-11 16:20:22 -04006580 struct nfs4_state_owner *sp = state->owner;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006581 unsigned char fl_flags = request->fl_flags;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006582 int status;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006583
Trond Myklebust01c3b862006-06-29 16:38:39 -04006584 request->fl_flags |= FL_ACCESS;
Jeff Layton75575dd2016-09-17 18:17:32 -04006585 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006586 if (status < 0)
6587 goto out;
Chuck Lever11476e92016-04-11 16:20:22 -04006588 mutex_lock(&sp->so_delegreturn_mutex);
Trond Myklebust19e03c52008-12-23 15:21:44 -05006589 down_read(&nfsi->rwsem);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006590 if (test_bit(NFS_DELEGATED_STATE, &state->flags)) {
Trond Myklebust01c3b862006-06-29 16:38:39 -04006591 /* Yes: cache locks! */
Trond Myklebust01c3b862006-06-29 16:38:39 -04006592 /* ...but avoid races with delegation recall... */
Trond Myklebust19e03c52008-12-23 15:21:44 -05006593 request->fl_flags = fl_flags & ~FL_SLEEP;
Jeff Layton75575dd2016-09-17 18:17:32 -04006594 status = locks_lock_inode_wait(state->inode, request);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006595 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006596 mutex_unlock(&sp->so_delegreturn_mutex);
Trond Myklebustc69899a2015-01-24 16:03:52 -05006597 goto out;
Trond Myklebust01c3b862006-06-29 16:38:39 -04006598 }
Trond Myklebust9a99af42013-02-04 20:17:49 -05006599 up_read(&nfsi->rwsem);
Chuck Lever11476e92016-04-11 16:20:22 -04006600 mutex_unlock(&sp->so_delegreturn_mutex);
Alexandros Batsakisafe6c272009-12-09 01:50:14 -08006601 status = _nfs4_do_setlk(state, cmd, request, NFS_LOCK_NEW);
Trond Myklebust01c3b862006-06-29 16:38:39 -04006602out:
6603 request->fl_flags = fl_flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006604 return status;
6605}
6606
6607static int nfs4_proc_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6608{
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006609 struct nfs4_exception exception = {
6610 .state = state,
Trond Myklebust05ffe242012-04-18 12:20:10 -04006611 .inode = state->inode,
Trond Myklebusta1d0b5e2012-03-05 19:56:44 -05006612 };
Linus Torvalds1da177e2005-04-16 15:20:36 -07006613 int err;
6614
6615 do {
Trond Myklebust965b5d62009-06-17 13:22:59 -07006616 err = _nfs4_proc_setlk(state, cmd, request);
6617 if (err == -NFS4ERR_DENIED)
6618 err = -EAGAIN;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006619 err = nfs4_handle_exception(NFS_SERVER(state->inode),
Trond Myklebust965b5d62009-06-17 13:22:59 -07006620 err, &exception);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006621 } while (exception.retry);
6622 return err;
6623}
6624
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006625#define NFS4_LOCK_MINTIMEOUT (1 * HZ)
6626#define NFS4_LOCK_MAXTIMEOUT (30 * HZ)
6627
6628static int
Jeff Laytona1d617d82016-09-17 18:17:39 -04006629nfs4_retry_setlk_simple(struct nfs4_state *state, int cmd,
6630 struct file_lock *request)
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006631{
6632 int status = -ERESTARTSYS;
6633 unsigned long timeout = NFS4_LOCK_MINTIMEOUT;
6634
6635 while(!signalled()) {
6636 status = nfs4_proc_setlk(state, cmd, request);
6637 if ((status != -EAGAIN) || IS_SETLK(cmd))
6638 break;
6639 freezable_schedule_timeout_interruptible(timeout);
6640 timeout *= 2;
6641 timeout = min_t(unsigned long, NFS4_LOCK_MAXTIMEOUT, timeout);
6642 status = -ERESTARTSYS;
6643 }
6644 return status;
6645}
6646
Jeff Laytona1d617d82016-09-17 18:17:39 -04006647#ifdef CONFIG_NFS_V4_1
6648struct nfs4_lock_waiter {
6649 struct task_struct *task;
6650 struct inode *inode;
6651 struct nfs_lowner *owner;
6652 bool notified;
6653};
6654
6655static int
Ingo Molnarac6424b2017-06-20 12:06:13 +02006656nfs4_wake_lock_waiter(wait_queue_entry_t *wait, unsigned int mode, int flags, void *key)
Jeff Laytona1d617d82016-09-17 18:17:39 -04006657{
6658 int ret;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006659 struct nfs4_lock_waiter *waiter = wait->private;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006660
Jeff Layton57174592018-03-18 08:37:03 -04006661 /* NULL key means to wake up everyone */
6662 if (key) {
6663 struct cb_notify_lock_args *cbnl = key;
6664 struct nfs_lowner *lowner = &cbnl->cbnl_owner,
6665 *wowner = waiter->owner;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006666
Jeff Layton57174592018-03-18 08:37:03 -04006667 /* Only wake if the callback was for the same owner. */
6668 if (lowner->id != wowner->id || lowner->s_dev != wowner->s_dev)
6669 return 0;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006670
Jeff Layton57174592018-03-18 08:37:03 -04006671 /* Make sure it's for the right inode */
6672 if (nfs_compare_fh(NFS_FH(waiter->inode), &cbnl->cbnl_fh))
6673 return 0;
6674
6675 waiter->notified = true;
6676 }
Jeff Laytona1d617d82016-09-17 18:17:39 -04006677
6678 /* override "private" so we can use default_wake_function */
6679 wait->private = waiter->task;
6680 ret = autoremove_wake_function(wait, mode, flags, key);
6681 wait->private = waiter;
6682 return ret;
6683}
6684
6685static int
6686nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6687{
6688 int status = -ERESTARTSYS;
6689 unsigned long flags;
6690 struct nfs4_lock_state *lsp = request->fl_u.nfs4_fl.owner;
6691 struct nfs_server *server = NFS_SERVER(state->inode);
6692 struct nfs_client *clp = server->nfs_client;
6693 wait_queue_head_t *q = &clp->cl_lock_waitq;
6694 struct nfs_lowner owner = { .clientid = clp->cl_clientid,
6695 .id = lsp->ls_seqid.owner_id,
6696 .s_dev = server->s_dev };
6697 struct nfs4_lock_waiter waiter = { .task = current,
6698 .inode = state->inode,
6699 .owner = &owner,
6700 .notified = false };
Ingo Molnarac6424b2017-06-20 12:06:13 +02006701 wait_queue_entry_t wait;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006702
6703 /* Don't bother with waitqueue if we don't expect a callback */
6704 if (!test_bit(NFS_STATE_MAY_NOTIFY_LOCK, &state->flags))
6705 return nfs4_retry_setlk_simple(state, cmd, request);
6706
6707 init_wait(&wait);
6708 wait.private = &waiter;
6709 wait.func = nfs4_wake_lock_waiter;
6710 add_wait_queue(q, &wait);
6711
6712 while(!signalled()) {
Jeff Layton41a74622018-03-18 08:37:01 -04006713 waiter.notified = false;
Jeff Laytona1d617d82016-09-17 18:17:39 -04006714 status = nfs4_proc_setlk(state, cmd, request);
6715 if ((status != -EAGAIN) || IS_SETLK(cmd))
6716 break;
6717
6718 status = -ERESTARTSYS;
6719 spin_lock_irqsave(&q->lock, flags);
6720 if (waiter.notified) {
6721 spin_unlock_irqrestore(&q->lock, flags);
6722 continue;
6723 }
6724 set_current_state(TASK_INTERRUPTIBLE);
6725 spin_unlock_irqrestore(&q->lock, flags);
6726
Benjamin Coddingtonb7dbcc02017-07-28 12:33:54 -04006727 freezable_schedule_timeout(NFS4_LOCK_MAXTIMEOUT);
Jeff Laytona1d617d82016-09-17 18:17:39 -04006728 }
6729
6730 finish_wait(q, &wait);
6731 return status;
6732}
6733#else /* !CONFIG_NFS_V4_1 */
6734static inline int
6735nfs4_retry_setlk(struct nfs4_state *state, int cmd, struct file_lock *request)
6736{
6737 return nfs4_retry_setlk_simple(state, cmd, request);
6738}
6739#endif
6740
Linus Torvalds1da177e2005-04-16 15:20:36 -07006741static int
6742nfs4_proc_lock(struct file *filp, int cmd, struct file_lock *request)
6743{
6744 struct nfs_open_context *ctx;
6745 struct nfs4_state *state;
Linus Torvalds1da177e2005-04-16 15:20:36 -07006746 int status;
6747
6748 /* verify open state */
Trond Myklebustcd3758e2007-08-10 17:44:32 -04006749 ctx = nfs_file_open_context(filp);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006750 state = ctx->state;
6751
Trond Myklebustd9531262009-07-21 19:22:38 -04006752 if (IS_GETLK(cmd)) {
6753 if (state != NULL)
6754 return nfs4_proc_getlk(state, F_GETLK, request);
6755 return 0;
6756 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006757
6758 if (!(IS_SETLK(cmd) || IS_SETLKW(cmd)))
6759 return -EINVAL;
6760
Trond Myklebustd9531262009-07-21 19:22:38 -04006761 if (request->fl_type == F_UNLCK) {
6762 if (state != NULL)
6763 return nfs4_proc_unlck(state, cmd, request);
6764 return 0;
6765 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07006766
Trond Myklebustd9531262009-07-21 19:22:38 -04006767 if (state == NULL)
6768 return -ENOLCK;
Jeff Layton1ea67db2016-09-17 18:17:37 -04006769
6770 if ((request->fl_flags & FL_POSIX) &&
6771 !test_bit(NFS_STATE_POSIX_LOCKS, &state->flags))
6772 return -ENOLCK;
6773
Benjamin Coddingtonfcfa4472017-11-10 06:27:49 -05006774 /*
6775 * Don't rely on the VFS having checked the file open mode,
6776 * since it won't do this for flock() locks.
6777 */
6778 switch (request->fl_type) {
6779 case F_RDLCK:
6780 if (!(filp->f_mode & FMODE_READ))
6781 return -EBADF;
6782 break;
6783 case F_WRLCK:
6784 if (!(filp->f_mode & FMODE_WRITE))
6785 return -EBADF;
6786 }
6787
Jeff Layton1ea67db2016-09-17 18:17:37 -04006788 status = nfs4_set_lock_state(state, request);
6789 if (status != 0)
6790 return status;
6791
Jeff Laytond2f3a7f2016-09-17 18:17:38 -04006792 return nfs4_retry_setlk(state, cmd, request);
Linus Torvalds1da177e2005-04-16 15:20:36 -07006793}
6794
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006795int nfs4_lock_delegation_recall(struct file_lock *fl, struct nfs4_state *state, const nfs4_stateid *stateid)
Trond Myklebust888e6942005-11-04 15:38:11 -05006796{
6797 struct nfs_server *server = NFS_SERVER(state->inode);
Trond Myklebust888e6942005-11-04 15:38:11 -05006798 int err;
6799
6800 err = nfs4_set_lock_state(state, fl);
6801 if (err != 0)
Trond Myklebustdb4f2e62013-04-01 15:56:46 -04006802 return err;
Trond Myklebust4a706fa2013-04-01 14:47:22 -04006803 err = _nfs4_do_setlk(state, F_SETLK, fl, NFS_LOCK_NEW);
NeilBrowndce26302017-12-13 09:57:09 +11006804 return nfs4_handle_delegation_recall_error(server, state, stateid, fl, err);
Trond Myklebust888e6942005-11-04 15:38:11 -05006805}
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006806
Trond Myklebustcf470c32012-03-07 13:49:12 -05006807struct nfs_release_lockowner_data {
6808 struct nfs4_lock_state *lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006809 struct nfs_server *server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006810 struct nfs_release_lockowner_args args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006811 struct nfs_release_lockowner_res res;
Chuck Lever60ea6812013-10-17 14:13:47 -04006812 unsigned long timestamp;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006813};
6814
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006815static void nfs4_release_lockowner_prepare(struct rpc_task *task, void *calldata)
6816{
6817 struct nfs_release_lockowner_data *data = calldata;
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006818 struct nfs_server *server = data->server;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05006819 nfs4_setup_sequence(server->nfs_client, &data->args.seq_args,
6820 &data->res.seq_res, task);
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006821 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
Chuck Lever60ea6812013-10-17 14:13:47 -04006822 data->timestamp = jiffies;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006823}
6824
6825static void nfs4_release_lockowner_done(struct rpc_task *task, void *calldata)
6826{
6827 struct nfs_release_lockowner_data *data = calldata;
Chuck Lever60ea6812013-10-17 14:13:47 -04006828 struct nfs_server *server = data->server;
6829
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006830 nfs40_sequence_done(task, &data->res.seq_res);
Chuck Lever60ea6812013-10-17 14:13:47 -04006831
6832 switch (task->tk_status) {
6833 case 0:
6834 renew_lease(server, data->timestamp);
6835 break;
6836 case -NFS4ERR_STALE_CLIENTID:
6837 case -NFS4ERR_EXPIRED:
Kinglong Mee5b53dc82014-08-04 16:18:16 +08006838 nfs4_schedule_lease_recovery(server->nfs_client);
6839 break;
Chuck Lever60ea6812013-10-17 14:13:47 -04006840 case -NFS4ERR_LEASE_MOVED:
6841 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10006842 if (nfs4_async_handle_error(task, server,
6843 NULL, NULL) == -EAGAIN)
Chuck Lever60ea6812013-10-17 14:13:47 -04006844 rpc_restart_call_prepare(task);
6845 }
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006846}
6847
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006848static void nfs4_release_lockowner_release(void *calldata)
6849{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006850 struct nfs_release_lockowner_data *data = calldata;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006851 nfs4_free_lock_state(data->server, data->lsp);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006852 kfree(calldata);
6853}
6854
Trond Myklebust17280172012-03-11 13:11:00 -04006855static const struct rpc_call_ops nfs4_release_lockowner_ops = {
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006856 .rpc_call_prepare = nfs4_release_lockowner_prepare,
6857 .rpc_call_done = nfs4_release_lockowner_done,
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006858 .rpc_release = nfs4_release_lockowner_release,
6859};
6860
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006861static void
6862nfs4_release_lockowner(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006863{
Trond Myklebustcf470c32012-03-07 13:49:12 -05006864 struct nfs_release_lockowner_data *data;
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006865 struct rpc_message msg = {
6866 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RELEASE_LOCKOWNER],
6867 };
6868
6869 if (server->nfs_client->cl_mvops->minor_version != 0)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006870 return;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006871
Trond Myklebustcf470c32012-03-07 13:49:12 -05006872 data = kmalloc(sizeof(*data), GFP_NOFS);
6873 if (!data)
Jeff Laytonf1cdae82014-05-01 06:28:47 -04006874 return;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006875 data->lsp = lsp;
Trond Myklebust5ae67c42012-03-19 16:17:18 -04006876 data->server = server;
Trond Myklebustcf470c32012-03-07 13:49:12 -05006877 data->args.lock_owner.clientid = server->nfs_client->cl_clientid;
6878 data->args.lock_owner.id = lsp->ls_seqid.owner_id;
6879 data->args.lock_owner.s_dev = server->s_dev;
Chuck Leverfbd4bfd2013-08-09 12:49:38 -04006880
Trond Myklebustcf470c32012-03-07 13:49:12 -05006881 msg.rpc_argp = &data->args;
Trond Myklebustb7e63a12014-02-26 11:19:14 -08006882 msg.rpc_resp = &data->res;
6883 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 0);
Trond Myklebustcf470c32012-03-07 13:49:12 -05006884 rpc_call_async(server->client, &msg, 0, &nfs4_release_lockowner_ops, data);
Trond Myklebustd3c7b7c2010-07-01 12:49:01 -04006885}
6886
J. Bruce Fieldsaa1870a2005-06-22 17:16:22 +00006887#define XATTR_NAME_NFSV4_ACL "system.nfs4_acl"
6888
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006889static int nfs4_xattr_set_nfs4_acl(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006890 struct dentry *unused, struct inode *inode,
6891 const char *key, const void *buf,
6892 size_t buflen, int flags)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006893{
Al Viro59301222016-05-27 10:19:30 -04006894 return nfs4_proc_set_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006895}
6896
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006897static int nfs4_xattr_get_nfs4_acl(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006898 struct dentry *unused, struct inode *inode,
6899 const char *key, void *buf, size_t buflen)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006900{
Al Virob2968212016-04-10 20:48:24 -04006901 return nfs4_proc_get_acl(inode, buf, buflen);
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006902}
6903
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006904static bool nfs4_xattr_list_nfs4_acl(struct dentry *dentry)
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006905{
Andreas Gruenbacher764a5c62015-12-02 14:44:43 +01006906 return nfs4_server_supports_acls(NFS_SERVER(d_inode(dentry)));
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00006907}
6908
David Quigleyc9bccef2013-05-22 12:50:45 -04006909#ifdef CONFIG_NFS_V4_SECURITY_LABEL
David Quigleyc9bccef2013-05-22 12:50:45 -04006910
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006911static int nfs4_xattr_set_nfs4_label(const struct xattr_handler *handler,
Al Viro59301222016-05-27 10:19:30 -04006912 struct dentry *unused, struct inode *inode,
6913 const char *key, const void *buf,
6914 size_t buflen, int flags)
David Quigleyc9bccef2013-05-22 12:50:45 -04006915{
6916 if (security_ismaclabel(key))
Al Viro59301222016-05-27 10:19:30 -04006917 return nfs4_set_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006918
6919 return -EOPNOTSUPP;
6920}
6921
Andreas Gruenbacherd9a82a02015-10-04 19:18:51 +02006922static int nfs4_xattr_get_nfs4_label(const struct xattr_handler *handler,
Al Virob2968212016-04-10 20:48:24 -04006923 struct dentry *unused, struct inode *inode,
6924 const char *key, void *buf, size_t buflen)
David Quigleyc9bccef2013-05-22 12:50:45 -04006925{
6926 if (security_ismaclabel(key))
Al Virob2968212016-04-10 20:48:24 -04006927 return nfs4_get_security_label(inode, buf, buflen);
David Quigleyc9bccef2013-05-22 12:50:45 -04006928 return -EOPNOTSUPP;
6929}
6930
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006931static ssize_t
6932nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
David Quigleyc9bccef2013-05-22 12:50:45 -04006933{
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006934 int len = 0;
David Quigleyc9bccef2013-05-22 12:50:45 -04006935
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006936 if (nfs_server_capable(inode, NFS_CAP_SECURITY_LABEL)) {
6937 len = security_inode_listsecurity(inode, list, list_len);
6938 if (list_len && len > list_len)
6939 return -ERANGE;
David Quigleyc9bccef2013-05-22 12:50:45 -04006940 }
6941 return len;
6942}
6943
6944static const struct xattr_handler nfs4_xattr_nfs4_label_handler = {
6945 .prefix = XATTR_SECURITY_PREFIX,
David Quigleyc9bccef2013-05-22 12:50:45 -04006946 .get = nfs4_xattr_get_nfs4_label,
6947 .set = nfs4_xattr_set_nfs4_label,
6948};
David Quigleyc9bccef2013-05-22 12:50:45 -04006949
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01006950#else
6951
6952static ssize_t
6953nfs4_listxattr_nfs4_label(struct inode *inode, char *list, size_t list_len)
6954{
6955 return 0;
6956}
6957
6958#endif
David Quigleyc9bccef2013-05-22 12:50:45 -04006959
Andy Adamson533eb462011-06-13 18:25:56 -04006960/*
6961 * nfs_fhget will use either the mounted_on_fileid or the fileid
6962 */
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006963static void nfs_fixup_referral_attributes(struct nfs_fattr *fattr)
6964{
Andy Adamson533eb462011-06-13 18:25:56 -04006965 if (!(((fattr->valid & NFS_ATTR_FATTR_MOUNTED_ON_FILEID) ||
6966 (fattr->valid & NFS_ATTR_FATTR_FILEID)) &&
6967 (fattr->valid & NFS_ATTR_FATTR_FSID) &&
Chuck Lever81934dd2012-03-01 17:01:57 -05006968 (fattr->valid & NFS_ATTR_FATTR_V4_LOCATIONS)))
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006969 return;
6970
6971 fattr->valid |= NFS_ATTR_FATTR_TYPE | NFS_ATTR_FATTR_MODE |
Chuck Lever81934dd2012-03-01 17:01:57 -05006972 NFS_ATTR_FATTR_NLINK | NFS_ATTR_FATTR_V4_REFERRAL;
Trond Myklebust69aaaae2009-03-11 14:10:28 -04006973 fattr->mode = S_IFDIR | S_IRUGO | S_IXUGO;
6974 fattr->nlink = 2;
6975}
6976
Bryan Schumakerf05d1472012-04-27 13:27:41 -04006977static int _nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
6978 const struct qstr *name,
6979 struct nfs4_fs_locations *fs_locations,
6980 struct page *page)
Trond Myklebust683b57b2006-06-09 09:34:22 -04006981{
6982 struct nfs_server *server = NFS_SERVER(dir);
Chuck Leverc05cefc2017-11-05 15:45:22 -05006983 u32 bitmask[3];
Trond Myklebust683b57b2006-06-09 09:34:22 -04006984 struct nfs4_fs_locations_arg args = {
6985 .dir_fh = NFS_FH(dir),
Trond Myklebustc228fd32007-01-13 02:28:11 -05006986 .name = name,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006987 .page = page,
6988 .bitmask = bitmask,
6989 };
Benny Halevy22958462009-04-01 09:22:02 -04006990 struct nfs4_fs_locations_res res = {
6991 .fs_locations = fs_locations,
6992 };
Trond Myklebust683b57b2006-06-09 09:34:22 -04006993 struct rpc_message msg = {
6994 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
6995 .rpc_argp = &args,
Benny Halevy22958462009-04-01 09:22:02 -04006996 .rpc_resp = &res,
Trond Myklebust683b57b2006-06-09 09:34:22 -04006997 };
6998 int status;
6999
Harvey Harrison3110ff82008-05-02 13:42:44 -07007000 dprintk("%s: start\n", __func__);
Andy Adamson533eb462011-06-13 18:25:56 -04007001
Chuck Leverc05cefc2017-11-05 15:45:22 -05007002 bitmask[0] = nfs4_fattr_bitmap[0] | FATTR4_WORD0_FS_LOCATIONS;
7003 bitmask[1] = nfs4_fattr_bitmap[1];
7004
Andy Adamson533eb462011-06-13 18:25:56 -04007005 /* Ask for the fileid of the absent filesystem if mounted_on_fileid
7006 * is not supported */
7007 if (NFS_SERVER(dir)->attr_bitmask[1] & FATTR4_WORD1_MOUNTED_ON_FILEID)
Chuck Leverc05cefc2017-11-05 15:45:22 -05007008 bitmask[0] &= ~FATTR4_WORD0_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007009 else
Chuck Leverc05cefc2017-11-05 15:45:22 -05007010 bitmask[1] &= ~FATTR4_WORD1_MOUNTED_ON_FILEID;
Andy Adamson533eb462011-06-13 18:25:56 -04007011
Trond Myklebustc228fd32007-01-13 02:28:11 -05007012 nfs_fattr_init(&fs_locations->fattr);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007013 fs_locations->server = server;
Manoj Naik830b8e32006-06-09 09:34:25 -04007014 fs_locations->nlocations = 0;
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007015 status = nfs4_call_sync(client, server, &msg, &args.seq_args, &res.seq_res, 0);
Harvey Harrison3110ff82008-05-02 13:42:44 -07007016 dprintk("%s: returned status = %d\n", __func__, status);
Trond Myklebust683b57b2006-06-09 09:34:22 -04007017 return status;
7018}
7019
Bryan Schumakerf05d1472012-04-27 13:27:41 -04007020int nfs4_proc_fs_locations(struct rpc_clnt *client, struct inode *dir,
7021 const struct qstr *name,
7022 struct nfs4_fs_locations *fs_locations,
7023 struct page *page)
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007024{
7025 struct nfs4_exception exception = { };
7026 int err;
7027 do {
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007028 err = _nfs4_proc_fs_locations(client, dir, name,
7029 fs_locations, page);
7030 trace_nfs4_get_fs_locations(dir, name, err);
7031 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumakerdb0a9592012-04-27 13:27:39 -04007032 &exception);
7033 } while (exception.retry);
7034 return err;
7035}
7036
Chuck Leverb03d7352013-10-17 14:12:50 -04007037/*
7038 * This operation also signals the server that this client is
7039 * performing migration recovery. The server can stop returning
7040 * NFS4ERR_LEASE_MOVED to this client. A RENEW operation is
7041 * appended to this compound to identify the client ID which is
7042 * performing recovery.
7043 */
7044static int _nfs40_proc_get_locations(struct inode *inode,
7045 struct nfs4_fs_locations *locations,
7046 struct page *page, struct rpc_cred *cred)
7047{
7048 struct nfs_server *server = NFS_SERVER(inode);
7049 struct rpc_clnt *clnt = server->client;
7050 u32 bitmask[2] = {
7051 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7052 };
7053 struct nfs4_fs_locations_arg args = {
7054 .clientid = server->nfs_client->cl_clientid,
7055 .fh = NFS_FH(inode),
7056 .page = page,
7057 .bitmask = bitmask,
7058 .migration = 1, /* skip LOOKUP */
7059 .renew = 1, /* append RENEW */
7060 };
7061 struct nfs4_fs_locations_res res = {
7062 .fs_locations = locations,
7063 .migration = 1,
7064 .renew = 1,
7065 };
7066 struct rpc_message msg = {
7067 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7068 .rpc_argp = &args,
7069 .rpc_resp = &res,
7070 .rpc_cred = cred,
7071 };
7072 unsigned long now = jiffies;
7073 int status;
7074
7075 nfs_fattr_init(&locations->fattr);
7076 locations->server = server;
7077 locations->nlocations = 0;
7078
7079 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7080 nfs4_set_sequence_privileged(&args.seq_args);
7081 status = nfs4_call_sync_sequence(clnt, server, &msg,
7082 &args.seq_args, &res.seq_res);
7083 if (status)
7084 return status;
7085
7086 renew_lease(server, now);
7087 return 0;
7088}
7089
7090#ifdef CONFIG_NFS_V4_1
7091
7092/*
7093 * This operation also signals the server that this client is
7094 * performing migration recovery. The server can stop asserting
7095 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID
7096 * performing this operation is identified in the SEQUENCE
7097 * operation in this compound.
7098 *
7099 * When the client supports GETATTR(fs_locations_info), it can
7100 * be plumbed in here.
7101 */
7102static int _nfs41_proc_get_locations(struct inode *inode,
7103 struct nfs4_fs_locations *locations,
7104 struct page *page, struct rpc_cred *cred)
7105{
7106 struct nfs_server *server = NFS_SERVER(inode);
7107 struct rpc_clnt *clnt = server->client;
7108 u32 bitmask[2] = {
7109 [0] = FATTR4_WORD0_FSID | FATTR4_WORD0_FS_LOCATIONS,
7110 };
7111 struct nfs4_fs_locations_arg args = {
7112 .fh = NFS_FH(inode),
7113 .page = page,
7114 .bitmask = bitmask,
7115 .migration = 1, /* skip LOOKUP */
7116 };
7117 struct nfs4_fs_locations_res res = {
7118 .fs_locations = locations,
7119 .migration = 1,
7120 };
7121 struct rpc_message msg = {
7122 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FS_LOCATIONS],
7123 .rpc_argp = &args,
7124 .rpc_resp = &res,
7125 .rpc_cred = cred,
7126 };
7127 int status;
7128
7129 nfs_fattr_init(&locations->fattr);
7130 locations->server = server;
7131 locations->nlocations = 0;
7132
7133 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7134 nfs4_set_sequence_privileged(&args.seq_args);
7135 status = nfs4_call_sync_sequence(clnt, server, &msg,
7136 &args.seq_args, &res.seq_res);
7137 if (status == NFS4_OK &&
7138 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7139 status = -NFS4ERR_LEASE_MOVED;
7140 return status;
7141}
7142
7143#endif /* CONFIG_NFS_V4_1 */
7144
7145/**
7146 * nfs4_proc_get_locations - discover locations for a migrated FSID
7147 * @inode: inode on FSID that is migrating
7148 * @locations: result of query
7149 * @page: buffer
7150 * @cred: credential to use for this operation
7151 *
7152 * Returns NFS4_OK on success, a negative NFS4ERR status code if the
7153 * operation failed, or a negative errno if a local error occurred.
7154 *
7155 * On success, "locations" is filled in, but if the server has
7156 * no locations information, NFS_ATTR_FATTR_V4_LOCATIONS is not
7157 * asserted.
7158 *
7159 * -NFS4ERR_LEASE_MOVED is returned if the server still has leases
7160 * from this client that require migration recovery.
7161 */
7162int nfs4_proc_get_locations(struct inode *inode,
7163 struct nfs4_fs_locations *locations,
7164 struct page *page, struct rpc_cred *cred)
7165{
7166 struct nfs_server *server = NFS_SERVER(inode);
7167 struct nfs_client *clp = server->nfs_client;
7168 const struct nfs4_mig_recovery_ops *ops =
7169 clp->cl_mvops->mig_recovery_ops;
7170 struct nfs4_exception exception = { };
7171 int status;
7172
7173 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7174 (unsigned long long)server->fsid.major,
7175 (unsigned long long)server->fsid.minor,
7176 clp->cl_hostname);
7177 nfs_display_fhandle(NFS_FH(inode), __func__);
7178
7179 do {
7180 status = ops->get_locations(inode, locations, page, cred);
7181 if (status != -NFS4ERR_DELAY)
7182 break;
7183 nfs4_handle_exception(server, status, &exception);
7184 } while (exception.retry);
7185 return status;
7186}
7187
Chuck Lever44c99932013-10-17 14:13:30 -04007188/*
7189 * This operation also signals the server that this client is
7190 * performing "lease moved" recovery. The server can stop
7191 * returning NFS4ERR_LEASE_MOVED to this client. A RENEW operation
7192 * is appended to this compound to identify the client ID which is
7193 * performing recovery.
7194 */
7195static int _nfs40_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7196{
7197 struct nfs_server *server = NFS_SERVER(inode);
7198 struct nfs_client *clp = NFS_SERVER(inode)->nfs_client;
7199 struct rpc_clnt *clnt = server->client;
7200 struct nfs4_fsid_present_arg args = {
7201 .fh = NFS_FH(inode),
7202 .clientid = clp->cl_clientid,
7203 .renew = 1, /* append RENEW */
7204 };
7205 struct nfs4_fsid_present_res res = {
7206 .renew = 1,
7207 };
7208 struct rpc_message msg = {
7209 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7210 .rpc_argp = &args,
7211 .rpc_resp = &res,
7212 .rpc_cred = cred,
7213 };
7214 unsigned long now = jiffies;
7215 int status;
7216
7217 res.fh = nfs_alloc_fhandle();
7218 if (res.fh == NULL)
7219 return -ENOMEM;
7220
7221 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7222 nfs4_set_sequence_privileged(&args.seq_args);
7223 status = nfs4_call_sync_sequence(clnt, server, &msg,
7224 &args.seq_args, &res.seq_res);
7225 nfs_free_fhandle(res.fh);
7226 if (status)
7227 return status;
7228
7229 do_renew_lease(clp, now);
7230 return 0;
7231}
7232
7233#ifdef CONFIG_NFS_V4_1
7234
7235/*
7236 * This operation also signals the server that this client is
7237 * performing "lease moved" recovery. The server can stop asserting
7238 * SEQ4_STATUS_LEASE_MOVED for this client. The client ID performing
7239 * this operation is identified in the SEQUENCE operation in this
7240 * compound.
7241 */
7242static int _nfs41_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7243{
7244 struct nfs_server *server = NFS_SERVER(inode);
7245 struct rpc_clnt *clnt = server->client;
7246 struct nfs4_fsid_present_arg args = {
7247 .fh = NFS_FH(inode),
7248 };
7249 struct nfs4_fsid_present_res res = {
7250 };
7251 struct rpc_message msg = {
7252 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FSID_PRESENT],
7253 .rpc_argp = &args,
7254 .rpc_resp = &res,
7255 .rpc_cred = cred,
7256 };
7257 int status;
7258
7259 res.fh = nfs_alloc_fhandle();
7260 if (res.fh == NULL)
7261 return -ENOMEM;
7262
7263 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
7264 nfs4_set_sequence_privileged(&args.seq_args);
7265 status = nfs4_call_sync_sequence(clnt, server, &msg,
7266 &args.seq_args, &res.seq_res);
7267 nfs_free_fhandle(res.fh);
7268 if (status == NFS4_OK &&
7269 res.seq_res.sr_status_flags & SEQ4_STATUS_LEASE_MOVED)
7270 status = -NFS4ERR_LEASE_MOVED;
7271 return status;
7272}
7273
7274#endif /* CONFIG_NFS_V4_1 */
7275
7276/**
7277 * nfs4_proc_fsid_present - Is this FSID present or absent on server?
7278 * @inode: inode on FSID to check
7279 * @cred: credential to use for this operation
7280 *
7281 * Server indicates whether the FSID is present, moved, or not
7282 * recognized. This operation is necessary to clear a LEASE_MOVED
7283 * condition for this client ID.
7284 *
7285 * Returns NFS4_OK if the FSID is present on this server,
7286 * -NFS4ERR_MOVED if the FSID is no longer present, a negative
7287 * NFS4ERR code if some error occurred on the server, or a
7288 * negative errno if a local failure occurred.
7289 */
7290int nfs4_proc_fsid_present(struct inode *inode, struct rpc_cred *cred)
7291{
7292 struct nfs_server *server = NFS_SERVER(inode);
7293 struct nfs_client *clp = server->nfs_client;
7294 const struct nfs4_mig_recovery_ops *ops =
7295 clp->cl_mvops->mig_recovery_ops;
7296 struct nfs4_exception exception = { };
7297 int status;
7298
7299 dprintk("%s: FSID %llx:%llx on \"%s\"\n", __func__,
7300 (unsigned long long)server->fsid.major,
7301 (unsigned long long)server->fsid.minor,
7302 clp->cl_hostname);
7303 nfs_display_fhandle(NFS_FH(inode), __func__);
7304
7305 do {
7306 status = ops->fsid_present(inode, cred);
7307 if (status != -NFS4ERR_DELAY)
7308 break;
7309 nfs4_handle_exception(server, status, &exception);
7310 } while (exception.retry);
7311 return status;
7312}
7313
Andy Adamson5ec16a82013-08-08 10:57:55 -04007314/**
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007315 * If 'use_integrity' is true and the state managment nfs_client
7316 * cl_rpcclient is using krb5i/p, use the integrity protected cl_rpcclient
7317 * and the machine credential as per RFC3530bis and RFC5661 Security
7318 * Considerations sections. Otherwise, just use the user cred with the
7319 * filesystem's rpc_client.
Andy Adamson5ec16a82013-08-08 10:57:55 -04007320 */
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007321static 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 +00007322{
7323 int status;
7324 struct nfs4_secinfo_arg args = {
7325 .dir_fh = NFS_FH(dir),
7326 .name = name,
7327 };
7328 struct nfs4_secinfo_res res = {
7329 .flavors = flavors,
7330 };
7331 struct rpc_message msg = {
7332 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO],
7333 .rpc_argp = &args,
7334 .rpc_resp = &res,
7335 };
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007336 struct rpc_clnt *clnt = NFS_SERVER(dir)->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007337 struct rpc_cred *cred = NULL;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007338
7339 if (use_integrity) {
7340 clnt = NFS_SERVER(dir)->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007341 cred = nfs4_get_clid_cred(NFS_SERVER(dir)->nfs_client);
7342 msg.rpc_cred = cred;
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007343 }
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007344
7345 dprintk("NFS call secinfo %s\n", name->name);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007346
7347 nfs4_state_protect(NFS_SERVER(dir)->nfs_client,
7348 NFS_SP4_MACH_CRED_SECINFO, &clnt, &msg);
7349
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007350 status = nfs4_call_sync(clnt, NFS_SERVER(dir), &msg, &args.seq_args,
7351 &res.seq_res, 0);
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007352 dprintk("NFS reply secinfo: %d\n", status);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007353
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04007354 if (cred)
7355 put_rpccred(cred);
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007356
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007357 return status;
7358}
7359
Bryan Schumaker72de53e2012-04-27 13:27:40 -04007360int nfs4_proc_secinfo(struct inode *dir, const struct qstr *name,
7361 struct nfs4_secinfo_flavors *flavors)
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007362{
7363 struct nfs4_exception exception = { };
7364 int err;
7365 do {
Weston Andros Adamsona5250de2013-09-03 15:18:49 -04007366 err = -NFS4ERR_WRONGSEC;
7367
7368 /* try to use integrity protection with machine cred */
7369 if (_nfs4_is_integrity_protected(NFS_SERVER(dir)->nfs_client))
7370 err = _nfs4_proc_secinfo(dir, name, flavors, true);
7371
7372 /*
7373 * if unable to use integrity protection, or SECINFO with
7374 * integrity protection returns NFS4ERR_WRONGSEC (which is
7375 * disallowed by spec, but exists in deployed servers) use
7376 * the current filesystem's rpc_client and the user cred.
7377 */
7378 if (err == -NFS4ERR_WRONGSEC)
7379 err = _nfs4_proc_secinfo(dir, name, flavors, false);
7380
Trond Myklebust078ea3d2013-08-12 16:45:55 -04007381 trace_nfs4_secinfo(dir, name, err);
7382 err = nfs4_handle_exception(NFS_SERVER(dir), err,
Bryan Schumaker5a5ea0d2011-03-24 17:12:29 +00007383 &exception);
7384 } while (exception.retry);
7385 return err;
7386}
7387
Andy Adamson557134a2009-04-01 09:21:53 -04007388#ifdef CONFIG_NFS_V4_1
Benny Halevy99fe60d2009-04-01 09:22:29 -04007389/*
Andy Adamson357f54d2010-12-14 10:11:57 -05007390 * Check the exchange flags returned by the server for invalid flags, having
7391 * both PNFS and NON_PNFS flags set, and not having one of NON_PNFS, PNFS, or
7392 * DS flags set.
7393 */
7394static int nfs4_check_cl_exchange_flags(u32 flags)
7395{
7396 if (flags & ~EXCHGID4_FLAG_MASK_R)
7397 goto out_inval;
7398 if ((flags & EXCHGID4_FLAG_USE_PNFS_MDS) &&
7399 (flags & EXCHGID4_FLAG_USE_NON_PNFS))
7400 goto out_inval;
7401 if (!(flags & (EXCHGID4_FLAG_MASK_PNFS)))
7402 goto out_inval;
7403 return NFS_OK;
7404out_inval:
7405 return -NFS4ERR_INVAL;
7406}
7407
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007408static bool
Chuck Lever79d4e1f2012-05-21 22:44:31 -04007409nfs41_same_server_scope(struct nfs41_server_scope *a,
7410 struct nfs41_server_scope *b)
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007411{
Anna Schumaker49ad0142017-01-11 16:51:59 -05007412 if (a->server_scope_sz != b->server_scope_sz)
7413 return false;
7414 return memcmp(a->server_scope, b->server_scope, a->server_scope_sz) == 0;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007415}
7416
Andy Adamson02a95de2016-02-05 16:08:37 -05007417static void
7418nfs4_bind_one_conn_to_session_done(struct rpc_task *task, void *calldata)
7419{
7420}
7421
7422static const struct rpc_call_ops nfs4_bind_one_conn_to_session_ops = {
7423 .rpc_call_done = &nfs4_bind_one_conn_to_session_done,
7424};
7425
Andy Adamson357f54d2010-12-14 10:11:57 -05007426/*
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007427 * nfs4_proc_bind_one_conn_to_session()
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007428 *
7429 * The 4.1 client currently uses the same TCP connection for the
7430 * fore and backchannel.
7431 */
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007432static
7433int nfs4_proc_bind_one_conn_to_session(struct rpc_clnt *clnt,
7434 struct rpc_xprt *xprt,
7435 struct nfs_client *clp,
7436 struct rpc_cred *cred)
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007437{
7438 int status;
Trond Myklebust71a097c2015-02-18 09:27:18 -08007439 struct nfs41_bind_conn_to_session_args args = {
7440 .client = clp,
7441 .dir = NFS4_CDFC4_FORE_OR_BOTH,
7442 };
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007443 struct nfs41_bind_conn_to_session_res res;
7444 struct rpc_message msg = {
7445 .rpc_proc =
7446 &nfs4_procedures[NFSPROC4_CLNT_BIND_CONN_TO_SESSION],
Trond Myklebust71a097c2015-02-18 09:27:18 -08007447 .rpc_argp = &args,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007448 .rpc_resp = &res,
Trond Myklebust2cf047c2012-05-25 17:57:41 -04007449 .rpc_cred = cred,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007450 };
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007451 struct rpc_task_setup task_setup_data = {
7452 .rpc_client = clnt,
7453 .rpc_xprt = xprt,
Andy Adamson02a95de2016-02-05 16:08:37 -05007454 .callback_ops = &nfs4_bind_one_conn_to_session_ops,
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007455 .rpc_message = &msg,
7456 .flags = RPC_TASK_TIMEOUT,
7457 };
7458 struct rpc_task *task;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007459
Trond Myklebust71a097c2015-02-18 09:27:18 -08007460 nfs4_copy_sessionid(&args.sessionid, &clp->cl_session->sess_id);
7461 if (!(clp->cl_session->flags & SESSION4_BACK_CHAN))
7462 args.dir = NFS4_CDFC4_FORE;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007463
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007464 /* Do not set the backchannel flag unless this is clnt->cl_xprt */
7465 if (xprt != rcu_access_pointer(clnt->cl_xprt))
7466 args.dir = NFS4_CDFC4_FORE;
7467
7468 task = rpc_run_task(&task_setup_data);
7469 if (!IS_ERR(task)) {
7470 status = task->tk_status;
7471 rpc_put_task(task);
7472 } else
7473 status = PTR_ERR(task);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007474 trace_nfs4_bind_conn_to_session(clp, status);
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007475 if (status == 0) {
Trond Myklebust71a097c2015-02-18 09:27:18 -08007476 if (memcmp(res.sessionid.data,
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007477 clp->cl_session->sess_id.data, NFS4_MAX_SESSIONID_LEN)) {
7478 dprintk("NFS: %s: Session ID mismatch\n", __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007479 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007480 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007481 if ((res.dir & args.dir) != res.dir || res.dir == 0) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007482 dprintk("NFS: %s: Unexpected direction from server\n",
7483 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007484 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007485 }
Trond Myklebust71a097c2015-02-18 09:27:18 -08007486 if (res.use_conn_in_rdma_mode != args.use_conn_in_rdma_mode) {
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007487 dprintk("NFS: %s: Server returned RDMA mode = true\n",
7488 __func__);
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007489 return -EIO;
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007490 }
7491 }
Anna Schumakerc7ae7632017-04-07 14:15:21 -04007492
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007493 return status;
7494}
7495
Trond Myklebustd9ddbf52016-01-30 22:58:24 -05007496struct rpc_bind_conn_calldata {
7497 struct nfs_client *clp;
7498 struct rpc_cred *cred;
7499};
7500
7501static int
7502nfs4_proc_bind_conn_to_session_callback(struct rpc_clnt *clnt,
7503 struct rpc_xprt *xprt,
7504 void *calldata)
7505{
7506 struct rpc_bind_conn_calldata *p = calldata;
7507
7508 return nfs4_proc_bind_one_conn_to_session(clnt, xprt, p->clp, p->cred);
7509}
7510
7511int nfs4_proc_bind_conn_to_session(struct nfs_client *clp, struct rpc_cred *cred)
7512{
7513 struct rpc_bind_conn_calldata data = {
7514 .clp = clp,
7515 .cred = cred,
7516 };
7517 return rpc_clnt_iterate_for_each_xprt(clp->cl_rpcclient,
7518 nfs4_proc_bind_conn_to_session_callback, &data);
7519}
7520
Weston Andros Adamson7c44f1ae2012-05-24 13:22:50 -04007521/*
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007522 * Minimum set of SP4_MACH_CRED operations from RFC 5661 in the enforce map
7523 * and operations we'd like to see to enable certain features in the allow map
Benny Halevy99fe60d2009-04-01 09:22:29 -04007524 */
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007525static const struct nfs41_state_protection nfs4_sp4_mach_cred_request = {
7526 .how = SP4_MACH_CRED,
7527 .enforce.u.words = {
7528 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7529 1 << (OP_EXCHANGE_ID - 32) |
7530 1 << (OP_CREATE_SESSION - 32) |
7531 1 << (OP_DESTROY_SESSION - 32) |
7532 1 << (OP_DESTROY_CLIENTID - 32)
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007533 },
7534 .allow.u.words = {
7535 [0] = 1 << (OP_CLOSE) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007536 1 << (OP_OPEN_DOWNGRADE) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007537 1 << (OP_LOCKU) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007538 1 << (OP_DELEGRETURN) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007539 1 << (OP_COMMIT),
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007540 [1] = 1 << (OP_SECINFO - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007541 1 << (OP_SECINFO_NO_NAME - 32) |
Andrew Elble99ade3c2015-12-02 09:39:51 -05007542 1 << (OP_LAYOUTRETURN - 32) |
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007543 1 << (OP_TEST_STATEID - 32) |
Weston Andros Adamsona0279622013-09-10 18:44:30 -04007544 1 << (OP_FREE_STATEID - 32) |
7545 1 << (OP_WRITE - 32)
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007546 }
7547};
7548
7549/*
7550 * Select the state protection mode for client `clp' given the server results
7551 * from exchange_id in `sp'.
7552 *
7553 * Returns 0 on success, negative errno otherwise.
7554 */
7555static int nfs4_sp4_select_mode(struct nfs_client *clp,
7556 struct nfs41_state_protection *sp)
7557{
7558 static const u32 supported_enforce[NFS4_OP_MAP_NUM_WORDS] = {
7559 [1] = 1 << (OP_BIND_CONN_TO_SESSION - 32) |
7560 1 << (OP_EXCHANGE_ID - 32) |
7561 1 << (OP_CREATE_SESSION - 32) |
7562 1 << (OP_DESTROY_SESSION - 32) |
7563 1 << (OP_DESTROY_CLIENTID - 32)
7564 };
Trond Myklebust937e3132017-08-01 07:32:50 -04007565 unsigned long flags = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007566 unsigned int i;
Trond Myklebust937e3132017-08-01 07:32:50 -04007567 int ret = 0;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007568
7569 if (sp->how == SP4_MACH_CRED) {
7570 /* Print state protect result */
7571 dfprintk(MOUNT, "Server SP4_MACH_CRED support:\n");
7572 for (i = 0; i <= LAST_NFS4_OP; i++) {
7573 if (test_bit(i, sp->enforce.u.longs))
7574 dfprintk(MOUNT, " enforce op %d\n", i);
7575 if (test_bit(i, sp->allow.u.longs))
7576 dfprintk(MOUNT, " allow op %d\n", i);
7577 }
7578
7579 /* make sure nothing is on enforce list that isn't supported */
7580 for (i = 0; i < NFS4_OP_MAP_NUM_WORDS; i++) {
7581 if (sp->enforce.u.words[i] & ~supported_enforce[i]) {
7582 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007583 ret = -EINVAL;
7584 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007585 }
7586 }
7587
7588 /*
7589 * Minimal mode - state operations are allowed to use machine
7590 * credential. Note this already happens by default, so the
7591 * client doesn't have to do anything more than the negotiation.
7592 *
7593 * NOTE: we don't care if EXCHANGE_ID is in the list -
7594 * we're already using the machine cred for exchange_id
7595 * and will never use a different cred.
7596 */
7597 if (test_bit(OP_BIND_CONN_TO_SESSION, sp->enforce.u.longs) &&
7598 test_bit(OP_CREATE_SESSION, sp->enforce.u.longs) &&
7599 test_bit(OP_DESTROY_SESSION, sp->enforce.u.longs) &&
7600 test_bit(OP_DESTROY_CLIENTID, sp->enforce.u.longs)) {
7601 dfprintk(MOUNT, "sp4_mach_cred:\n");
7602 dfprintk(MOUNT, " minimal mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007603 __set_bit(NFS_SP4_MACH_CRED_MINIMAL, &flags);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007604 } else {
7605 dfprintk(MOUNT, "sp4_mach_cred: disabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007606 ret = -EINVAL;
7607 goto out;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007608 }
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007609
7610 if (test_bit(OP_CLOSE, sp->allow.u.longs) &&
Andrew Elble99ade3c2015-12-02 09:39:51 -05007611 test_bit(OP_OPEN_DOWNGRADE, sp->allow.u.longs) &&
7612 test_bit(OP_DELEGRETURN, sp->allow.u.longs) &&
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007613 test_bit(OP_LOCKU, sp->allow.u.longs)) {
7614 dfprintk(MOUNT, " cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007615 __set_bit(NFS_SP4_MACH_CRED_CLEANUP, &flags);
Weston Andros Adamsonfa940722013-08-13 16:37:34 -04007616 }
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007617
Andrew Elble99ade3c2015-12-02 09:39:51 -05007618 if (test_bit(OP_LAYOUTRETURN, sp->allow.u.longs)) {
7619 dfprintk(MOUNT, " pnfs cleanup mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007620 __set_bit(NFS_SP4_MACH_CRED_PNFS_CLEANUP, &flags);
Andrew Elble99ade3c2015-12-02 09:39:51 -05007621 }
7622
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007623 if (test_bit(OP_SECINFO, sp->allow.u.longs) &&
7624 test_bit(OP_SECINFO_NO_NAME, sp->allow.u.longs)) {
7625 dfprintk(MOUNT, " secinfo mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007626 __set_bit(NFS_SP4_MACH_CRED_SECINFO, &flags);
Weston Andros Adamson8b5bee22013-08-13 16:37:35 -04007627 }
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007628
7629 if (test_bit(OP_TEST_STATEID, sp->allow.u.longs) &&
7630 test_bit(OP_FREE_STATEID, sp->allow.u.longs)) {
7631 dfprintk(MOUNT, " stateid mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007632 __set_bit(NFS_SP4_MACH_CRED_STATEID, &flags);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04007633 }
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007634
7635 if (test_bit(OP_WRITE, sp->allow.u.longs)) {
7636 dfprintk(MOUNT, " write mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007637 __set_bit(NFS_SP4_MACH_CRED_WRITE, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007638 }
7639
7640 if (test_bit(OP_COMMIT, sp->allow.u.longs)) {
7641 dfprintk(MOUNT, " commit mode enabled\n");
Trond Myklebust937e3132017-08-01 07:32:50 -04007642 __set_bit(NFS_SP4_MACH_CRED_COMMIT, &flags);
Weston Andros Adamson8c21c622013-08-13 16:37:37 -04007643 }
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007644 }
Trond Myklebust937e3132017-08-01 07:32:50 -04007645out:
7646 clp->cl_sp4_flags = flags;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007647 return 0;
7648}
7649
Andy Adamson8d89bd72016-09-09 09:22:18 -04007650struct nfs41_exchange_id_data {
7651 struct nfs41_exchange_id_res res;
7652 struct nfs41_exchange_id_args args;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007653};
7654
Andy Adamson8d89bd72016-09-09 09:22:18 -04007655static void nfs4_exchange_id_release(void *data)
7656{
7657 struct nfs41_exchange_id_data *cdata =
7658 (struct nfs41_exchange_id_data *)data;
7659
Olga Kornievskaia63513232017-03-13 10:36:19 -04007660 nfs_put_client(cdata->args.client);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007661 kfree(cdata->res.impl_id);
7662 kfree(cdata->res.server_scope);
7663 kfree(cdata->res.server_owner);
7664 kfree(cdata);
7665}
7666
7667static const struct rpc_call_ops nfs4_exchange_id_call_ops = {
Andy Adamson8d89bd72016-09-09 09:22:18 -04007668 .rpc_release = nfs4_exchange_id_release,
7669};
7670
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007671/*
7672 * _nfs4_proc_exchange_id()
7673 *
7674 * Wrapper for EXCHANGE_ID operation.
7675 */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007676static struct rpc_task *
7677nfs4_run_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
Andy Adamsonad0849a2016-09-09 09:22:28 -04007678 u32 sp4_how, struct rpc_xprt *xprt)
Benny Halevy99fe60d2009-04-01 09:22:29 -04007679{
Benny Halevy99fe60d2009-04-01 09:22:29 -04007680 struct rpc_message msg = {
7681 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_EXCHANGE_ID],
Benny Halevy99fe60d2009-04-01 09:22:29 -04007682 .rpc_cred = cred,
7683 };
Andy Adamson8d89bd72016-09-09 09:22:18 -04007684 struct rpc_task_setup task_setup_data = {
7685 .rpc_client = clp->cl_rpcclient,
7686 .callback_ops = &nfs4_exchange_id_call_ops,
7687 .rpc_message = &msg,
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007688 .flags = RPC_TASK_TIMEOUT,
Andy Adamson8d89bd72016-09-09 09:22:18 -04007689 };
7690 struct nfs41_exchange_id_data *calldata;
Anna Schumakere917f0d2017-04-07 14:15:22 -04007691 int status;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007692
Elena Reshetova212bf412017-10-20 12:53:38 +03007693 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust9c760d12017-07-31 18:38:50 -04007694 return ERR_PTR(-EIO);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007695
Trond Myklebust9c760d12017-07-31 18:38:50 -04007696 status = -ENOMEM;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007697 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007698 if (!calldata)
7699 goto out;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007700
Trond Myklebustfd405592017-08-01 16:02:47 -04007701 nfs4_init_boot_verifier(clp, &calldata->args.verifier);
Jeff Layton873e3852015-06-09 19:44:00 -04007702
7703 status = nfs4_init_uniform_client_string(clp);
7704 if (status)
Andy Adamson8d89bd72016-09-09 09:22:18 -04007705 goto out_calldata;
Jeff Layton3a6bb732015-06-09 19:43:57 -04007706
Andy Adamson8d89bd72016-09-09 09:22:18 -04007707 calldata->res.server_owner = kzalloc(sizeof(struct nfs41_server_owner),
7708 GFP_NOFS);
7709 status = -ENOMEM;
7710 if (unlikely(calldata->res.server_owner == NULL))
7711 goto out_calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007712
Andy Adamson8d89bd72016-09-09 09:22:18 -04007713 calldata->res.server_scope = kzalloc(sizeof(struct nfs41_server_scope),
Trond Myklebustbbafffd2012-05-24 16:31:39 -04007714 GFP_NOFS);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007715 if (unlikely(calldata->res.server_scope == NULL))
Chuck Leveracdeb692012-05-21 22:46:16 -04007716 goto out_server_owner;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007717
Andy Adamson8d89bd72016-09-09 09:22:18 -04007718 calldata->res.impl_id = kzalloc(sizeof(struct nfs41_impl_id), GFP_NOFS);
7719 if (unlikely(calldata->res.impl_id == NULL))
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007720 goto out_server_scope;
Weston Andros Adamson7d2ed9a2012-02-17 15:20:26 -05007721
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007722 switch (sp4_how) {
7723 case SP4_NONE:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007724 calldata->args.state_protect.how = SP4_NONE;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007725 break;
7726
7727 case SP4_MACH_CRED:
Andy Adamson8d89bd72016-09-09 09:22:18 -04007728 calldata->args.state_protect = nfs4_sp4_mach_cred_request;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007729 break;
7730
7731 default:
7732 /* unsupported! */
7733 WARN_ON_ONCE(1);
7734 status = -EINVAL;
Kinglong Mee6b559702015-07-01 11:54:53 +08007735 goto out_impl_id;
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007736 }
Andy Adamsonad0849a2016-09-09 09:22:28 -04007737 if (xprt) {
Andy Adamsonad0849a2016-09-09 09:22:28 -04007738 task_setup_data.rpc_xprt = xprt;
Trond Myklebustd9cb7332017-08-01 16:02:48 -04007739 task_setup_data.flags |= RPC_TASK_SOFTCONN;
Trond Myklebustfd405592017-08-01 16:02:47 -04007740 memcpy(calldata->args.verifier.data, clp->cl_confirm.data,
7741 sizeof(calldata->args.verifier.data));
Andy Adamsonad0849a2016-09-09 09:22:28 -04007742 }
Andy Adamson8d89bd72016-09-09 09:22:18 -04007743 calldata->args.client = clp;
Trond Myklebustbfab2812017-08-01 08:17:34 -04007744 calldata->args.flags = EXCHGID4_FLAG_SUPP_MOVED_REFER |
7745 EXCHGID4_FLAG_BIND_PRINC_STATEID;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007746#ifdef CONFIG_NFS_V4_1_MIGRATION
Trond Myklebustbfab2812017-08-01 08:17:34 -04007747 calldata->args.flags |= EXCHGID4_FLAG_SUPP_MOVED_MIGR;
Andy Adamson8d89bd72016-09-09 09:22:18 -04007748#endif
7749 msg.rpc_argp = &calldata->args;
7750 msg.rpc_resp = &calldata->res;
7751 task_setup_data.callback_data = calldata;
Weston Andros Adamson78fe0f42011-05-31 19:05:47 -04007752
Trond Myklebust9c760d12017-07-31 18:38:50 -04007753 return rpc_run_task(&task_setup_data);
Andy Adamson8d89bd72016-09-09 09:22:18 -04007754
7755out_impl_id:
7756 kfree(calldata->res.impl_id);
7757out_server_scope:
7758 kfree(calldata->res.server_scope);
7759out_server_owner:
7760 kfree(calldata->res.server_owner);
7761out_calldata:
7762 kfree(calldata);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007763out:
Olga Kornievskaia63513232017-03-13 10:36:19 -04007764 nfs_put_client(clp);
Trond Myklebust9c760d12017-07-31 18:38:50 -04007765 return ERR_PTR(status);
7766}
7767
7768/*
7769 * _nfs4_proc_exchange_id()
7770 *
7771 * Wrapper for EXCHANGE_ID operation.
7772 */
7773static int _nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred,
7774 u32 sp4_how)
7775{
7776 struct rpc_task *task;
7777 struct nfs41_exchange_id_args *argp;
7778 struct nfs41_exchange_id_res *resp;
7779 int status;
7780
7781 task = nfs4_run_exchange_id(clp, cred, sp4_how, NULL);
7782 if (IS_ERR(task))
7783 return PTR_ERR(task);
7784
7785 argp = task->tk_msg.rpc_argp;
7786 resp = task->tk_msg.rpc_resp;
7787 status = task->tk_status;
7788 if (status != 0)
7789 goto out;
7790
7791 status = nfs4_check_cl_exchange_flags(resp->flags);
7792 if (status != 0)
7793 goto out;
7794
7795 status = nfs4_sp4_select_mode(clp, &resp->state_protect);
7796 if (status != 0)
7797 goto out;
7798
7799 clp->cl_clientid = resp->clientid;
7800 clp->cl_exchange_flags = resp->flags;
7801 clp->cl_seqid = resp->seqid;
7802 /* Client ID is not confirmed */
7803 if (!(resp->flags & EXCHGID4_FLAG_CONFIRMED_R))
7804 clear_bit(NFS4_SESSION_ESTABLISHED,
7805 &clp->cl_session->session_state);
7806
7807 if (clp->cl_serverscope != NULL &&
7808 !nfs41_same_server_scope(clp->cl_serverscope,
7809 resp->server_scope)) {
7810 dprintk("%s: server_scope mismatch detected\n",
7811 __func__);
7812 set_bit(NFS4CLNT_SERVER_SCOPE_MISMATCH, &clp->cl_state);
7813 }
7814
7815 swap(clp->cl_serverowner, resp->server_owner);
7816 swap(clp->cl_serverscope, resp->server_scope);
7817 swap(clp->cl_implid, resp->impl_id);
7818
7819 /* Save the EXCHANGE_ID verifier session trunk tests */
7820 memcpy(clp->cl_confirm.data, argp->verifier.data,
7821 sizeof(clp->cl_confirm.data));
7822out:
7823 trace_nfs4_exchange_id(clp, status);
7824 rpc_put_task(task);
7825 return status;
Benny Halevy99fe60d2009-04-01 09:22:29 -04007826}
7827
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007828/*
7829 * nfs4_proc_exchange_id()
7830 *
7831 * Returns zero, a negative errno, or a negative NFS4ERR status code.
7832 *
7833 * Since the clientid has expired, all compounds using sessions
7834 * associated with the stale clientid will be returning
7835 * NFS4ERR_BADSESSION in the sequence operation, and will therefore
7836 * be in some phase of session reset.
7837 *
7838 * Will attempt to negotiate SP4_MACH_CRED if krb5i / krb5p auth is used.
7839 */
7840int nfs4_proc_exchange_id(struct nfs_client *clp, struct rpc_cred *cred)
7841{
7842 rpc_authflavor_t authflavor = clp->cl_rpcclient->cl_auth->au_flavor;
7843 int status;
7844
7845 /* try SP4_MACH_CRED if krb5i/p */
7846 if (authflavor == RPC_AUTH_GSS_KRB5I ||
7847 authflavor == RPC_AUTH_GSS_KRB5P) {
Trond Myklebust9c760d12017-07-31 18:38:50 -04007848 status = _nfs4_proc_exchange_id(clp, cred, SP4_MACH_CRED);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007849 if (!status)
7850 return 0;
7851 }
7852
7853 /* try SP4_NONE */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007854 return _nfs4_proc_exchange_id(clp, cred, SP4_NONE);
Weston Andros Adamson2031cd12013-08-13 16:37:32 -04007855}
7856
Andy Adamson04fa2c62016-09-09 09:22:29 -04007857/**
7858 * nfs4_test_session_trunk
7859 *
7860 * This is an add_xprt_test() test function called from
7861 * rpc_clnt_setup_test_and_add_xprt.
7862 *
7863 * The rpc_xprt_switch is referrenced by rpc_clnt_setup_test_and_add_xprt
7864 * and is dereferrenced in nfs4_exchange_id_release
7865 *
7866 * Upon success, add the new transport to the rpc_clnt
7867 *
7868 * @clnt: struct rpc_clnt to get new transport
7869 * @xprt: the rpc_xprt to test
7870 * @data: call data for _nfs4_proc_exchange_id.
7871 */
7872int nfs4_test_session_trunk(struct rpc_clnt *clnt, struct rpc_xprt *xprt,
7873 void *data)
7874{
7875 struct nfs4_add_xprt_data *adata = (struct nfs4_add_xprt_data *)data;
Trond Myklebust9c760d12017-07-31 18:38:50 -04007876 struct rpc_task *task;
7877 int status;
7878
Andy Adamson04fa2c62016-09-09 09:22:29 -04007879 u32 sp4_how;
7880
7881 dprintk("--> %s try %s\n", __func__,
7882 xprt->address_strings[RPC_DISPLAY_ADDR]);
7883
7884 sp4_how = (adata->clp->cl_sp4_flags == 0 ? SP4_NONE : SP4_MACH_CRED);
7885
7886 /* Test connection for session trunking. Async exchange_id call */
Trond Myklebust9c760d12017-07-31 18:38:50 -04007887 task = nfs4_run_exchange_id(adata->clp, adata->cred, sp4_how, xprt);
7888 if (IS_ERR(task))
7889 return PTR_ERR(task);
7890
7891 status = task->tk_status;
7892 if (status == 0)
7893 status = nfs4_detect_session_trunking(adata->clp,
7894 task->tk_msg.rpc_resp, xprt);
7895
7896 rpc_put_task(task);
7897 return status;
Andy Adamson04fa2c62016-09-09 09:22:29 -04007898}
7899EXPORT_SYMBOL_GPL(nfs4_test_session_trunk);
7900
Trond Myklebust66245532012-05-25 17:18:09 -04007901static int _nfs4_proc_destroy_clientid(struct nfs_client *clp,
7902 struct rpc_cred *cred)
7903{
7904 struct rpc_message msg = {
7905 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_CLIENTID],
7906 .rpc_argp = clp,
7907 .rpc_cred = cred,
7908 };
7909 int status;
7910
7911 status = rpc_call_sync(clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04007912 trace_nfs4_destroy_clientid(clp, status);
Trond Myklebust66245532012-05-25 17:18:09 -04007913 if (status)
Trond Myklebust02c67522012-06-07 13:45:53 -04007914 dprintk("NFS: Got error %d from the server %s on "
Trond Myklebust66245532012-05-25 17:18:09 -04007915 "DESTROY_CLIENTID.", status, clp->cl_hostname);
7916 return status;
7917}
7918
7919static int nfs4_proc_destroy_clientid(struct nfs_client *clp,
7920 struct rpc_cred *cred)
7921{
7922 unsigned int loop;
7923 int ret;
7924
7925 for (loop = NFS4_MAX_LOOP_ON_RECOVER; loop != 0; loop--) {
7926 ret = _nfs4_proc_destroy_clientid(clp, cred);
7927 switch (ret) {
7928 case -NFS4ERR_DELAY:
7929 case -NFS4ERR_CLIENTID_BUSY:
7930 ssleep(1);
7931 break;
7932 default:
7933 return ret;
7934 }
7935 }
7936 return 0;
7937}
7938
7939int nfs4_destroy_clientid(struct nfs_client *clp)
7940{
7941 struct rpc_cred *cred;
7942 int ret = 0;
7943
7944 if (clp->cl_mvops->minor_version < 1)
7945 goto out;
7946 if (clp->cl_exchange_flags == 0)
7947 goto out;
Chuck Lever05f4c352012-09-14 17:24:32 -04007948 if (clp->cl_preserve_clid)
7949 goto out;
Chuck Lever73d8bde2013-07-24 12:28:37 -04007950 cred = nfs4_get_clid_cred(clp);
Trond Myklebust66245532012-05-25 17:18:09 -04007951 ret = nfs4_proc_destroy_clientid(clp, cred);
7952 if (cred)
7953 put_rpccred(cred);
7954 switch (ret) {
7955 case 0:
7956 case -NFS4ERR_STALE_CLIENTID:
7957 clp->cl_exchange_flags = 0;
7958 }
7959out:
7960 return ret;
7961}
7962
Andy Adamson2050f0c2009-04-01 09:22:30 -04007963struct nfs4_get_lease_time_data {
7964 struct nfs4_get_lease_time_args *args;
7965 struct nfs4_get_lease_time_res *res;
7966 struct nfs_client *clp;
7967};
7968
7969static void nfs4_get_lease_time_prepare(struct rpc_task *task,
7970 void *calldata)
7971{
Andy Adamson2050f0c2009-04-01 09:22:30 -04007972 struct nfs4_get_lease_time_data *data =
7973 (struct nfs4_get_lease_time_data *)calldata;
7974
7975 dprintk("--> %s\n", __func__);
7976 /* just setup sequence, do not trigger session recovery
7977 since we're invoked within one */
Anna Schumaker7981c8a2017-01-10 11:39:53 -05007978 nfs4_setup_sequence(data->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04007979 &data->args->la_seq_args,
7980 &data->res->lr_seq_res,
7981 task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04007982 dprintk("<-- %s\n", __func__);
7983}
7984
7985/*
7986 * Called from nfs4_state_manager thread for session setup, so don't recover
7987 * from sequence operation or clientid errors.
7988 */
7989static void nfs4_get_lease_time_done(struct rpc_task *task, void *calldata)
7990{
7991 struct nfs4_get_lease_time_data *data =
7992 (struct nfs4_get_lease_time_data *)calldata;
7993
7994 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04007995 if (!nfs41_sequence_done(task, &data->res->lr_seq_res))
7996 return;
Andy Adamson2050f0c2009-04-01 09:22:30 -04007997 switch (task->tk_status) {
7998 case -NFS4ERR_DELAY:
7999 case -NFS4ERR_GRACE:
8000 dprintk("%s Retry: tk_status %d\n", __func__, task->tk_status);
8001 rpc_delay(task, NFS4_POLL_RETRY_MIN);
8002 task->tk_status = 0;
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008003 /* fall through */
8004 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustd00c5d42011-10-19 12:17:29 -07008005 rpc_restart_call_prepare(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008006 return;
8007 }
Andy Adamson2050f0c2009-04-01 09:22:30 -04008008 dprintk("<-- %s\n", __func__);
8009}
8010
Trond Myklebust17280172012-03-11 13:11:00 -04008011static const struct rpc_call_ops nfs4_get_lease_time_ops = {
Andy Adamson2050f0c2009-04-01 09:22:30 -04008012 .rpc_call_prepare = nfs4_get_lease_time_prepare,
8013 .rpc_call_done = nfs4_get_lease_time_done,
8014};
8015
8016int nfs4_proc_get_lease_time(struct nfs_client *clp, struct nfs_fsinfo *fsinfo)
8017{
8018 struct rpc_task *task;
8019 struct nfs4_get_lease_time_args args;
8020 struct nfs4_get_lease_time_res res = {
8021 .lr_fsinfo = fsinfo,
8022 };
8023 struct nfs4_get_lease_time_data data = {
8024 .args = &args,
8025 .res = &res,
8026 .clp = clp,
8027 };
8028 struct rpc_message msg = {
8029 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GET_LEASE_TIME],
8030 .rpc_argp = &args,
8031 .rpc_resp = &res,
8032 };
8033 struct rpc_task_setup task_setup = {
8034 .rpc_client = clp->cl_rpcclient,
8035 .rpc_message = &msg,
8036 .callback_ops = &nfs4_get_lease_time_ops,
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008037 .callback_data = &data,
8038 .flags = RPC_TASK_TIMEOUT,
Andy Adamson2050f0c2009-04-01 09:22:30 -04008039 };
8040 int status;
8041
Chuck Levera9c92d62013-08-09 12:48:18 -04008042 nfs4_init_sequence(&args.la_seq_args, &res.lr_seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008043 nfs4_set_sequence_privileged(&args.la_seq_args);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008044 task = rpc_run_task(&task_setup);
8045
8046 if (IS_ERR(task))
Anna Schumakerf6148712017-04-07 14:15:23 -04008047 return PTR_ERR(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008048
Anna Schumakerf6148712017-04-07 14:15:23 -04008049 status = task->tk_status;
8050 rpc_put_task(task);
Andy Adamson2050f0c2009-04-01 09:22:30 -04008051 return status;
8052}
8053
Andy Adamsonfc931582009-04-01 09:22:31 -04008054/*
8055 * Initialize the values to be used by the client in CREATE_SESSION
8056 * If nfs4_init_session set the fore channel request and response sizes,
8057 * use them.
8058 *
8059 * Set the back channel max_resp_sz_cached to zero to force the client to
8060 * always set csa_cachethis to FALSE because the current implementation
8061 * of the back channel DRC only supports caching the CB_SEQUENCE operation.
8062 */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008063static void nfs4_init_channel_attrs(struct nfs41_create_session_args *args,
8064 struct rpc_clnt *clnt)
Andy Adamsonfc931582009-04-01 09:22:31 -04008065{
Andy Adamson18aad3d2013-06-26 12:21:49 -04008066 unsigned int max_rqst_sz, max_resp_sz;
Chuck Lever6b26cc82016-05-02 14:40:40 -04008067 unsigned int max_bc_payload = rpc_max_bc_payload(clnt);
Andy Adamsonfc931582009-04-01 09:22:31 -04008068
Andy Adamson18aad3d2013-06-26 12:21:49 -04008069 max_rqst_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxwrite_overhead;
8070 max_resp_sz = NFS_MAX_FILE_IO_SIZE + nfs41_maxread_overhead;
8071
Andy Adamsonfc931582009-04-01 09:22:31 -04008072 /* Fore channel attributes */
Andy Adamson18aad3d2013-06-26 12:21:49 -04008073 args->fc_attrs.max_rqst_sz = max_rqst_sz;
8074 args->fc_attrs.max_resp_sz = max_resp_sz;
Andy Adamsonfc931582009-04-01 09:22:31 -04008075 args->fc_attrs.max_ops = NFS4_MAX_OPS;
Trond Myklebustef159e92012-02-06 19:50:40 -05008076 args->fc_attrs.max_reqs = max_session_slots;
Andy Adamsonfc931582009-04-01 09:22:31 -04008077
8078 dprintk("%s: Fore Channel : max_rqst_sz=%u max_resp_sz=%u "
Mike Sager8e0d46e2009-12-17 12:06:26 -05008079 "max_ops=%u max_reqs=%u\n",
Andy Adamsonfc931582009-04-01 09:22:31 -04008080 __func__,
8081 args->fc_attrs.max_rqst_sz, args->fc_attrs.max_resp_sz,
Mike Sager8e0d46e2009-12-17 12:06:26 -05008082 args->fc_attrs.max_ops, args->fc_attrs.max_reqs);
Andy Adamsonfc931582009-04-01 09:22:31 -04008083
8084 /* Back channel attributes */
Chuck Lever6b26cc82016-05-02 14:40:40 -04008085 args->bc_attrs.max_rqst_sz = max_bc_payload;
8086 args->bc_attrs.max_resp_sz = max_bc_payload;
Andy Adamsonfc931582009-04-01 09:22:31 -04008087 args->bc_attrs.max_resp_sz_cached = 0;
8088 args->bc_attrs.max_ops = NFS4_MAX_BACK_CHANNEL_OPS;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008089 args->bc_attrs.max_reqs = min_t(unsigned short, max_session_cb_slots, 1);
Andy Adamsonfc931582009-04-01 09:22:31 -04008090
8091 dprintk("%s: Back Channel : max_rqst_sz=%u max_resp_sz=%u "
8092 "max_resp_sz_cached=%u max_ops=%u max_reqs=%u\n",
8093 __func__,
8094 args->bc_attrs.max_rqst_sz, args->bc_attrs.max_resp_sz,
8095 args->bc_attrs.max_resp_sz_cached, args->bc_attrs.max_ops,
8096 args->bc_attrs.max_reqs);
8097}
8098
Trond Myklebust79969dd2015-02-18 11:30:18 -08008099static int nfs4_verify_fore_channel_attrs(struct nfs41_create_session_args *args,
8100 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008101{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008102 struct nfs4_channel_attrs *sent = &args->fc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008103 struct nfs4_channel_attrs *rcvd = &res->fc_attrs;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008104
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008105 if (rcvd->max_resp_sz > sent->max_resp_sz)
8106 return -EINVAL;
8107 /*
8108 * Our requested max_ops is the minimum we need; we're not
8109 * prepared to break up compounds into smaller pieces than that.
8110 * So, no point even trying to continue if the server won't
8111 * cooperate:
8112 */
8113 if (rcvd->max_ops < sent->max_ops)
8114 return -EINVAL;
8115 if (rcvd->max_reqs == 0)
8116 return -EINVAL;
Vitaliy Gusevb4b9a0c2012-02-15 19:38:25 +04008117 if (rcvd->max_reqs > NFS4_MAX_SLOT_TABLE)
8118 rcvd->max_reqs = NFS4_MAX_SLOT_TABLE;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008119 return 0;
Andy Adamson8d353012009-04-01 09:22:32 -04008120}
8121
Trond Myklebust79969dd2015-02-18 11:30:18 -08008122static int nfs4_verify_back_channel_attrs(struct nfs41_create_session_args *args,
8123 struct nfs41_create_session_res *res)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008124{
8125 struct nfs4_channel_attrs *sent = &args->bc_attrs;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008126 struct nfs4_channel_attrs *rcvd = &res->bc_attrs;
Andy Adamson8d353012009-04-01 09:22:32 -04008127
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008128 if (!(res->flags & SESSION4_BACK_CHAN))
8129 goto out;
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008130 if (rcvd->max_rqst_sz > sent->max_rqst_sz)
8131 return -EINVAL;
8132 if (rcvd->max_resp_sz < sent->max_resp_sz)
8133 return -EINVAL;
8134 if (rcvd->max_resp_sz_cached > sent->max_resp_sz_cached)
8135 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008136 if (rcvd->max_ops > sent->max_ops)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008137 return -EINVAL;
Trond Myklebust5405fc42016-08-29 20:03:52 -04008138 if (rcvd->max_reqs > sent->max_reqs)
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008139 return -EINVAL;
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008140out:
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008141 return 0;
8142}
Andy Adamson8d353012009-04-01 09:22:32 -04008143
Andy Adamson8d353012009-04-01 09:22:32 -04008144static int nfs4_verify_channel_attrs(struct nfs41_create_session_args *args,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008145 struct nfs41_create_session_res *res)
Andy Adamson8d353012009-04-01 09:22:32 -04008146{
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008147 int ret;
Andy Adamson8d353012009-04-01 09:22:32 -04008148
Trond Myklebust79969dd2015-02-18 11:30:18 -08008149 ret = nfs4_verify_fore_channel_attrs(args, res);
J. Bruce Fields43c2e882010-10-02 15:19:01 -04008150 if (ret)
8151 return ret;
Trond Myklebust79969dd2015-02-18 11:30:18 -08008152 return nfs4_verify_back_channel_attrs(args, res);
8153}
8154
8155static void nfs4_update_session(struct nfs4_session *session,
8156 struct nfs41_create_session_res *res)
8157{
8158 nfs4_copy_sessionid(&session->sess_id, &res->sessionid);
Trond Myklebuste11259f2015-03-03 20:35:31 -05008159 /* Mark client id and session as being confirmed */
8160 session->clp->cl_exchange_flags |= EXCHGID4_FLAG_CONFIRMED_R;
8161 set_bit(NFS4_SESSION_ESTABLISHED, &session->session_state);
Trond Myklebust79969dd2015-02-18 11:30:18 -08008162 session->flags = res->flags;
8163 memcpy(&session->fc_attrs, &res->fc_attrs, sizeof(session->fc_attrs));
Trond Myklebustb1c0df52015-02-18 11:34:58 -08008164 if (res->flags & SESSION4_BACK_CHAN)
8165 memcpy(&session->bc_attrs, &res->bc_attrs,
8166 sizeof(session->bc_attrs));
Andy Adamson8d353012009-04-01 09:22:32 -04008167}
8168
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008169static int _nfs4_proc_create_session(struct nfs_client *clp,
8170 struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008171{
8172 struct nfs4_session *session = clp->cl_session;
8173 struct nfs41_create_session_args args = {
8174 .client = clp,
Trond Myklebust79969dd2015-02-18 11:30:18 -08008175 .clientid = clp->cl_clientid,
8176 .seqid = clp->cl_seqid,
Andy Adamsonfc931582009-04-01 09:22:31 -04008177 .cb_program = NFS4_CALLBACK,
8178 };
Trond Myklebust79969dd2015-02-18 11:30:18 -08008179 struct nfs41_create_session_res res;
8180
Andy Adamsonfc931582009-04-01 09:22:31 -04008181 struct rpc_message msg = {
8182 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_CREATE_SESSION],
8183 .rpc_argp = &args,
8184 .rpc_resp = &res,
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008185 .rpc_cred = cred,
Andy Adamsonfc931582009-04-01 09:22:31 -04008186 };
8187 int status;
8188
Chuck Lever6b26cc82016-05-02 14:40:40 -04008189 nfs4_init_channel_attrs(&args, clp->cl_rpcclient);
Andy Adamson0f914212009-04-01 09:23:16 -04008190 args.flags = (SESSION4_PERSIST | SESSION4_BACK_CHAN);
Andy Adamsonfc931582009-04-01 09:22:31 -04008191
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008192 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008193 trace_nfs4_create_session(clp, status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008194
Trond Myklebustb519d402016-09-11 14:50:01 -04008195 switch (status) {
8196 case -NFS4ERR_STALE_CLIENTID:
8197 case -NFS4ERR_DELAY:
8198 case -ETIMEDOUT:
8199 case -EACCES:
8200 case -EAGAIN:
8201 goto out;
8202 };
8203
8204 clp->cl_seqid++;
Trond Myklebust43095d32012-11-20 11:13:12 -05008205 if (!status) {
Andy Adamson8d353012009-04-01 09:22:32 -04008206 /* Verify the session's negotiated channel_attrs values */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008207 status = nfs4_verify_channel_attrs(&args, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008208 /* Increment the clientid slot sequence id */
Trond Myklebust79969dd2015-02-18 11:30:18 -08008209 if (status)
8210 goto out;
8211 nfs4_update_session(session, &res);
Andy Adamsonfc931582009-04-01 09:22:31 -04008212 }
Trond Myklebust79969dd2015-02-18 11:30:18 -08008213out:
Andy Adamsonfc931582009-04-01 09:22:31 -04008214 return status;
8215}
8216
8217/*
8218 * Issues a CREATE_SESSION operation to the server.
8219 * It is the responsibility of the caller to verify the session is
8220 * expired before calling this routine.
8221 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008222int nfs4_proc_create_session(struct nfs_client *clp, struct rpc_cred *cred)
Andy Adamsonfc931582009-04-01 09:22:31 -04008223{
8224 int status;
8225 unsigned *ptr;
Andy Adamsonfc931582009-04-01 09:22:31 -04008226 struct nfs4_session *session = clp->cl_session;
8227
8228 dprintk("--> %s clp=%p session=%p\n", __func__, clp, session);
8229
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008230 status = _nfs4_proc_create_session(clp, cred);
Andy Adamsonfc931582009-04-01 09:22:31 -04008231 if (status)
8232 goto out;
8233
Andy Adamsonaacd5532011-11-09 13:58:21 -05008234 /* Init or reset the session slot tables */
8235 status = nfs4_setup_session_slot_tables(session);
8236 dprintk("slot table setup returned %d\n", status);
Andy Adamsonfc931582009-04-01 09:22:31 -04008237 if (status)
8238 goto out;
8239
8240 ptr = (unsigned *)&session->sess_id.data[0];
8241 dprintk("%s client>seqid %d sessionid %u:%u:%u:%u\n", __func__,
8242 clp->cl_seqid, ptr[0], ptr[1], ptr[2], ptr[3]);
Andy Adamsonfc931582009-04-01 09:22:31 -04008243out:
8244 dprintk("<-- %s\n", __func__);
8245 return status;
8246}
8247
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008248/*
8249 * Issue the over-the-wire RPC DESTROY_SESSION.
8250 * The caller must serialize access to this routine.
8251 */
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008252int nfs4_proc_destroy_session(struct nfs4_session *session,
8253 struct rpc_cred *cred)
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008254{
Trond Myklebust848f5bd2012-05-25 17:51:23 -04008255 struct rpc_message msg = {
8256 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_DESTROY_SESSION],
8257 .rpc_argp = session,
8258 .rpc_cred = cred,
8259 };
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008260 int status = 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008261
8262 dprintk("--> nfs4_proc_destroy_session\n");
8263
8264 /* session is still being setup */
Trond Myklebuste11259f2015-03-03 20:35:31 -05008265 if (!test_and_clear_bit(NFS4_SESSION_ESTABLISHED, &session->session_state))
8266 return 0;
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008267
Trond Myklebust1bd714f2011-04-24 14:29:33 -04008268 status = rpc_call_sync(session->clp->cl_rpcclient, &msg, RPC_TASK_TIMEOUT);
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008269 trace_nfs4_destroy_session(session->clp, status);
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008270
8271 if (status)
Trond Myklebust08106ac2012-06-05 10:08:24 -04008272 dprintk("NFS: Got error %d from the server on DESTROY_SESSION. "
Andy Adamson0f3e66c2009-04-01 09:22:34 -04008273 "Session has been destroyed regardless...\n", status);
8274
8275 dprintk("<-- nfs4_proc_destroy_session\n");
8276 return status;
8277}
8278
Trond Myklebust7b38c362012-05-23 13:23:31 -04008279/*
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008280 * Renew the cl_session lease.
8281 */
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008282struct nfs4_sequence_data {
8283 struct nfs_client *clp;
8284 struct nfs4_sequence_args args;
8285 struct nfs4_sequence_res res;
8286};
8287
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008288static void nfs41_sequence_release(void *data)
8289{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008290 struct nfs4_sequence_data *calldata = data;
8291 struct nfs_client *clp = calldata->clp;
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008292
Elena Reshetova212bf412017-10-20 12:53:38 +03008293 if (refcount_read(&clp->cl_count) > 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008294 nfs4_schedule_state_renewal(clp);
8295 nfs_put_client(clp);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008296 kfree(calldata);
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008297}
8298
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008299static int nfs41_sequence_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8300{
8301 switch(task->tk_status) {
8302 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008303 rpc_delay(task, NFS4_POLL_RETRY_MAX);
8304 return -EAGAIN;
8305 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008306 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008307 }
8308 return 0;
8309}
8310
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008311static void nfs41_sequence_call_done(struct rpc_task *task, void *data)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008312{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008313 struct nfs4_sequence_data *calldata = data;
8314 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008315
Trond Myklebust14516c32010-07-31 14:29:06 -04008316 if (!nfs41_sequence_done(task, task->tk_msg.rpc_resp))
8317 return;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008318
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008319 trace_nfs4_sequence(clp, task->tk_status);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008320 if (task->tk_status < 0) {
8321 dprintk("%s ERROR %d\n", __func__, task->tk_status);
Elena Reshetova212bf412017-10-20 12:53:38 +03008322 if (refcount_read(&clp->cl_count) == 1)
Alexandros Batsakis71358402010-02-05 03:45:05 -08008323 goto out;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008324
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008325 if (nfs41_sequence_handle_errors(task, clp) == -EAGAIN) {
8326 rpc_restart_call_prepare(task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008327 return;
8328 }
8329 }
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008330 dprintk("%s rpc_cred %p\n", __func__, task->tk_msg.rpc_cred);
Alexandros Batsakis71358402010-02-05 03:45:05 -08008331out:
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008332 dprintk("<-- %s\n", __func__);
8333}
8334
8335static void nfs41_sequence_prepare(struct rpc_task *task, void *data)
8336{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008337 struct nfs4_sequence_data *calldata = data;
8338 struct nfs_client *clp = calldata->clp;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008339 struct nfs4_sequence_args *args;
8340 struct nfs4_sequence_res *res;
8341
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008342 args = task->tk_msg.rpc_argp;
8343 res = task->tk_msg.rpc_resp;
8344
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008345 nfs4_setup_sequence(clp, args, res, task);
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008346}
8347
8348static const struct rpc_call_ops nfs41_sequence_ops = {
8349 .rpc_call_done = nfs41_sequence_call_done,
8350 .rpc_call_prepare = nfs41_sequence_prepare,
Alexandros Batsakisdc96aef2010-02-05 03:45:04 -08008351 .rpc_release = nfs41_sequence_release,
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008352};
8353
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008354static struct rpc_task *_nfs41_proc_sequence(struct nfs_client *clp,
8355 struct rpc_cred *cred,
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008356 struct nfs4_slot *slot,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008357 bool is_privileged)
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008358{
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008359 struct nfs4_sequence_data *calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008360 struct rpc_message msg = {
8361 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SEQUENCE],
8362 .rpc_cred = cred,
8363 };
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008364 struct rpc_task_setup task_setup_data = {
8365 .rpc_client = clp->cl_rpcclient,
8366 .rpc_message = &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008367 .callback_ops = &nfs41_sequence_ops,
Trond Myklebustbc7a05c2013-04-08 17:50:28 -04008368 .flags = RPC_TASK_ASYNC | RPC_TASK_TIMEOUT,
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008369 };
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008370 struct rpc_task *ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008371
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008372 ret = ERR_PTR(-EIO);
Elena Reshetova212bf412017-10-20 12:53:38 +03008373 if (!refcount_inc_not_zero(&clp->cl_count))
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008374 goto out_err;
8375
8376 ret = ERR_PTR(-ENOMEM);
Benny Halevydfb4f3092010-09-24 09:17:01 -04008377 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008378 if (calldata == NULL)
8379 goto out_put_clp;
Chuck Levera9c92d62013-08-09 12:48:18 -04008380 nfs4_init_sequence(&calldata->args, &calldata->res, 0);
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008381 nfs4_sequence_attach_slot(&calldata->args, &calldata->res, slot);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008382 if (is_privileged)
8383 nfs4_set_sequence_privileged(&calldata->args);
Trond Myklebustd5f8d3f2010-06-16 09:52:25 -04008384 msg.rpc_argp = &calldata->args;
8385 msg.rpc_resp = &calldata->res;
8386 calldata->clp = clp;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008387 task_setup_data.callback_data = calldata;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008388
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008389 ret = rpc_run_task(&task_setup_data);
8390 if (IS_ERR(ret))
8391 goto out_err;
8392 return ret;
8393out_put_clp:
8394 nfs_put_client(clp);
8395out_err:
8396 nfs41_release_slot(slot);
8397 return ret;
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008398}
8399
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008400static int nfs41_proc_async_sequence(struct nfs_client *clp, struct rpc_cred *cred, unsigned renew_flags)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008401{
8402 struct rpc_task *task;
8403 int ret = 0;
8404
Trond Myklebust2f60ea62011-08-24 15:07:37 -04008405 if ((renew_flags & NFS4_RENEW_TIMEOUT) == 0)
Andy Adamsond1f456b2014-09-29 12:31:57 -04008406 return -EAGAIN;
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008407 task = _nfs41_proc_sequence(clp, cred, NULL, false);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008408 if (IS_ERR(task))
8409 ret = PTR_ERR(task);
8410 else
Trond Myklebustbf294b42011-02-21 11:05:41 -08008411 rpc_put_task_async(task);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008412 dprintk("<-- %s status=%d\n", __func__, ret);
8413 return ret;
8414}
8415
8416static int nfs4_proc_sequence(struct nfs_client *clp, struct rpc_cred *cred)
8417{
8418 struct rpc_task *task;
8419 int ret;
8420
Trond Myklebust3be0f80b2017-10-19 15:46:45 -04008421 task = _nfs41_proc_sequence(clp, cred, NULL, true);
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008422 if (IS_ERR(task)) {
8423 ret = PTR_ERR(task);
8424 goto out;
8425 }
8426 ret = rpc_wait_for_completion_task(task);
Trond Myklebustbe824162015-07-05 14:50:46 -04008427 if (!ret)
Trond Myklebust71ac6da2010-06-16 09:52:26 -04008428 ret = task->tk_status;
8429 rpc_put_task(task);
8430out:
8431 dprintk("<-- %s status=%d\n", __func__, ret);
8432 return ret;
Andy Adamsonfc01cea2009-04-01 09:22:36 -04008433}
8434
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008435struct nfs4_reclaim_complete_data {
8436 struct nfs_client *clp;
8437 struct nfs41_reclaim_complete_args arg;
8438 struct nfs41_reclaim_complete_res res;
8439};
8440
8441static void nfs4_reclaim_complete_prepare(struct rpc_task *task, void *data)
8442{
8443 struct nfs4_reclaim_complete_data *calldata = data;
8444
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008445 nfs4_setup_sequence(calldata->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008446 &calldata->arg.seq_args,
8447 &calldata->res.seq_res,
8448 task);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008449}
8450
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008451static int nfs41_reclaim_complete_handle_errors(struct rpc_task *task, struct nfs_client *clp)
8452{
8453 switch(task->tk_status) {
8454 case 0:
Jeff Layton57174592018-03-18 08:37:03 -04008455 wake_up_all(&clp->cl_lock_waitq);
8456 /* Fallthrough */
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008457 case -NFS4ERR_COMPLETE_ALREADY:
8458 case -NFS4ERR_WRONG_CRED: /* What to do here? */
8459 break;
8460 case -NFS4ERR_DELAY:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008461 rpc_delay(task, NFS4_POLL_RETRY_MAX);
Andy Adamsona8a4ae32011-05-03 13:43:03 -04008462 /* fall through */
8463 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008464 return -EAGAIN;
Trond Myklebust0048fdd2017-05-04 13:44:04 -04008465 case -NFS4ERR_BADSESSION:
8466 case -NFS4ERR_DEADSESSION:
8467 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
8468 nfs4_schedule_session_recovery(clp->cl_session,
8469 task->tk_status);
8470 break;
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008471 default:
Trond Myklebust0400a6b2011-03-09 16:00:53 -05008472 nfs4_schedule_lease_recovery(clp);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008473 }
8474 return 0;
8475}
8476
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008477static void nfs4_reclaim_complete_done(struct rpc_task *task, void *data)
8478{
8479 struct nfs4_reclaim_complete_data *calldata = data;
8480 struct nfs_client *clp = calldata->clp;
8481 struct nfs4_sequence_res *res = &calldata->res.seq_res;
8482
8483 dprintk("--> %s\n", __func__);
Trond Myklebust14516c32010-07-31 14:29:06 -04008484 if (!nfs41_sequence_done(task, res))
8485 return;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008486
Trond Myklebustc6d01c62013-08-09 11:51:26 -04008487 trace_nfs4_reclaim_complete(clp, task->tk_status);
Trond Myklebustaa5190d2010-06-16 09:52:25 -04008488 if (nfs41_reclaim_complete_handle_errors(task, clp) == -EAGAIN) {
8489 rpc_restart_call_prepare(task);
8490 return;
8491 }
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008492 dprintk("<-- %s\n", __func__);
8493}
8494
8495static void nfs4_free_reclaim_complete_data(void *data)
8496{
8497 struct nfs4_reclaim_complete_data *calldata = data;
8498
8499 kfree(calldata);
8500}
8501
8502static const struct rpc_call_ops nfs4_reclaim_complete_call_ops = {
8503 .rpc_call_prepare = nfs4_reclaim_complete_prepare,
8504 .rpc_call_done = nfs4_reclaim_complete_done,
8505 .rpc_release = nfs4_free_reclaim_complete_data,
8506};
8507
8508/*
8509 * Issue a global reclaim complete.
8510 */
Trond Myklebust965e9c22013-05-20 11:05:17 -04008511static int nfs41_proc_reclaim_complete(struct nfs_client *clp,
8512 struct rpc_cred *cred)
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008513{
8514 struct nfs4_reclaim_complete_data *calldata;
8515 struct rpc_task *task;
8516 struct rpc_message msg = {
8517 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_RECLAIM_COMPLETE],
Trond Myklebust965e9c22013-05-20 11:05:17 -04008518 .rpc_cred = cred,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008519 };
8520 struct rpc_task_setup task_setup_data = {
8521 .rpc_client = clp->cl_rpcclient,
8522 .rpc_message = &msg,
8523 .callback_ops = &nfs4_reclaim_complete_call_ops,
8524 .flags = RPC_TASK_ASYNC,
8525 };
8526 int status = -ENOMEM;
8527
8528 dprintk("--> %s\n", __func__);
Trond Myklebust8535b2b2010-05-13 12:51:01 -04008529 calldata = kzalloc(sizeof(*calldata), GFP_NOFS);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008530 if (calldata == NULL)
8531 goto out;
8532 calldata->clp = clp;
8533 calldata->arg.one_fs = 0;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008534
Chuck Levera9c92d62013-08-09 12:48:18 -04008535 nfs4_init_sequence(&calldata->arg.seq_args, &calldata->res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04008536 nfs4_set_sequence_privileged(&calldata->arg.seq_args);
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008537 msg.rpc_argp = &calldata->arg;
8538 msg.rpc_resp = &calldata->res;
8539 task_setup_data.callback_data = calldata;
8540 task = rpc_run_task(&task_setup_data);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008541 if (IS_ERR(task)) {
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008542 status = PTR_ERR(task);
Dan Carpenteracf82b82010-04-22 11:28:39 +02008543 goto out;
8544 }
Anna Schumaker820bf852017-01-11 15:01:43 -05008545 status = rpc_wait_for_completion_task(task);
Andy Adamsonc34c32e2011-03-09 13:13:46 -05008546 if (status == 0)
8547 status = task->tk_status;
Ricardo Labiagafce5c832009-12-05 16:08:41 -05008548 rpc_put_task(task);
8549out:
8550 dprintk("<-- %s status=%d\n", __func__, status);
8551 return status;
8552}
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008553
8554static void
8555nfs4_layoutget_prepare(struct rpc_task *task, void *calldata)
8556{
8557 struct nfs4_layoutget *lgp = calldata;
Fred Isamanc31663d2011-01-06 11:36:24 +00008558 struct nfs_server *server = NFS_SERVER(lgp->args.inode);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008559
8560 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008561 nfs4_setup_sequence(server->nfs_client, &lgp->args.seq_args,
Jeff Layton183d9e72016-05-17 12:28:47 -04008562 &lgp->res.seq_res, task);
8563 dprintk("<-- %s\n", __func__);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008564}
8565
8566static void nfs4_layoutget_done(struct rpc_task *task, void *calldata)
8567{
8568 struct nfs4_layoutget *lgp = calldata;
Jeff Layton183d9e72016-05-17 12:28:47 -04008569
8570 dprintk("--> %s\n", __func__);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008571 nfs41_sequence_process(task, &lgp->res.seq_res);
Jeff Layton183d9e72016-05-17 12:28:47 -04008572 dprintk("<-- %s\n", __func__);
8573}
8574
8575static int
8576nfs4_layoutget_handle_exception(struct rpc_task *task,
8577 struct nfs4_layoutget *lgp, struct nfs4_exception *exception)
8578{
Trond Myklebustee314c22012-10-01 17:25:48 -07008579 struct inode *inode = lgp->args.inode;
8580 struct nfs_server *server = NFS_SERVER(inode);
8581 struct pnfs_layout_hdr *lo;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008582 int nfs4err = task->tk_status;
8583 int err, status = 0;
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008584 LIST_HEAD(head);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008585
Boaz Harroshed7e5422014-01-22 20:34:54 +02008586 dprintk("--> %s tk_status => %d\n", __func__, -task->tk_status);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008587
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008588 switch (nfs4err) {
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008589 case 0:
Trond Myklebustee314c22012-10-01 17:25:48 -07008590 goto out;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008591
8592 /*
8593 * NFS4ERR_LAYOUTUNAVAILABLE means we are not supposed to use pnfs
8594 * on the file. set tk_status to -ENODATA to tell upper layer to
8595 * retry go inband.
8596 */
8597 case -NFS4ERR_LAYOUTUNAVAILABLE:
Jeff Layton183d9e72016-05-17 12:28:47 -04008598 status = -ENODATA;
Peng Tao7c1e6e52015-12-05 17:01:01 +08008599 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008600 /*
Trond Myklebust21b874c2015-08-31 01:19:22 -07008601 * NFS4ERR_BADLAYOUT means the MDS cannot return a layout of
8602 * length lgp->args.minlength != 0 (see RFC5661 section 18.43.3).
8603 */
8604 case -NFS4ERR_BADLAYOUT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008605 status = -EOVERFLOW;
8606 goto out;
Trond Myklebust21b874c2015-08-31 01:19:22 -07008607 /*
Boaz Harroshed7e5422014-01-22 20:34:54 +02008608 * NFS4ERR_LAYOUTTRYLATER is a conflict with another client
Trond Myklebust21b874c2015-08-31 01:19:22 -07008609 * (or clients) writing to the same RAID stripe except when
8610 * the minlength argument is 0 (see RFC5661 section 18.43.3).
Jeff Layton183d9e72016-05-17 12:28:47 -04008611 *
8612 * Treat it like we would RECALLCONFLICT -- we retry for a little
8613 * while, and then eventually give up.
Boaz Harroshed7e5422014-01-22 20:34:54 +02008614 */
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008615 case -NFS4ERR_LAYOUTTRYLATER:
Jeff Layton183d9e72016-05-17 12:28:47 -04008616 if (lgp->args.minlength == 0) {
8617 status = -EOVERFLOW;
8618 goto out;
Boaz Harroshed7e5422014-01-22 20:34:54 +02008619 }
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008620 status = -EBUSY;
8621 break;
Jeff Layton183d9e72016-05-17 12:28:47 -04008622 case -NFS4ERR_RECALLCONFLICT:
Jeff Layton183d9e72016-05-17 12:28:47 -04008623 status = -ERECALLCONFLICT;
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008624 break;
Trond Myklebust26f47442016-09-22 13:39:10 -04008625 case -NFS4ERR_DELEG_REVOKED:
8626 case -NFS4ERR_ADMIN_REVOKED:
Trond Myklebustee314c22012-10-01 17:25:48 -07008627 case -NFS4ERR_EXPIRED:
8628 case -NFS4ERR_BAD_STATEID:
Jeff Layton183d9e72016-05-17 12:28:47 -04008629 exception->timeout = 0;
Trond Myklebustee314c22012-10-01 17:25:48 -07008630 spin_lock(&inode->i_lock);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008631 lo = NFS_I(inode)->layout;
8632 /* If the open stateid was bad, then recover it. */
8633 if (!lo || test_bit(NFS_LAYOUT_INVALID_STID, &lo->plh_flags) ||
Trond Myklebuste8fa33a2017-10-04 13:49:12 -04008634 !nfs4_stateid_match_other(&lgp->args.stateid, &lo->plh_stateid)) {
Trond Myklebustee314c22012-10-01 17:25:48 -07008635 spin_unlock(&inode->i_lock);
Jeff Layton183d9e72016-05-17 12:28:47 -04008636 exception->state = lgp->args.ctx->state;
Trond Myklebust26f47442016-09-22 13:39:10 -04008637 exception->stateid = &lgp->args.stateid;
Trond Myklebust2259f962015-09-20 13:30:30 -04008638 break;
8639 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008640
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008641 /*
8642 * Mark the bad layout state as invalid, then retry
8643 */
Trond Myklebust668f4552016-07-24 17:08:59 -04008644 pnfs_mark_layout_stateid_invalid(lo, &head);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008645 spin_unlock(&inode->i_lock);
Trond Myklebust1f18b822017-04-29 10:10:17 -04008646 nfs_commit_inode(inode, 0);
Trond Myklebustf7db0b22016-07-14 15:14:02 -04008647 pnfs_free_lseg_list(&head);
8648 status = -EAGAIN;
8649 goto out;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008650 }
Jeff Layton183d9e72016-05-17 12:28:47 -04008651
Trond Myklebust8ac09252017-01-23 22:44:12 -05008652 nfs4_sequence_free_slot(&lgp->res.seq_res);
Trond Myklebuste85d7ee2016-07-14 18:46:24 -04008653 err = nfs4_handle_exception(server, nfs4err, exception);
8654 if (!status) {
8655 if (exception->retry)
8656 status = -EAGAIN;
8657 else
8658 status = err;
8659 }
Trond Myklebustee314c22012-10-01 17:25:48 -07008660out:
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008661 dprintk("<-- %s\n", __func__);
Jeff Layton183d9e72016-05-17 12:28:47 -04008662 return status;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008663}
8664
Idan Kedar85541162012-08-02 11:47:10 +03008665static size_t max_response_pages(struct nfs_server *server)
8666{
8667 u32 max_resp_sz = server->nfs_client->cl_session->fc_attrs.max_resp_sz;
8668 return nfs_page_array_len(0, max_resp_sz);
8669}
8670
8671static void nfs4_free_pages(struct page **pages, size_t size)
8672{
8673 int i;
8674
8675 if (!pages)
8676 return;
8677
8678 for (i = 0; i < size; i++) {
8679 if (!pages[i])
8680 break;
8681 __free_page(pages[i]);
8682 }
8683 kfree(pages);
8684}
8685
8686static struct page **nfs4_alloc_pages(size_t size, gfp_t gfp_flags)
8687{
8688 struct page **pages;
8689 int i;
8690
8691 pages = kcalloc(size, sizeof(struct page *), gfp_flags);
8692 if (!pages) {
8693 dprintk("%s: can't alloc array of %zu pages\n", __func__, size);
8694 return NULL;
8695 }
8696
8697 for (i = 0; i < size; i++) {
8698 pages[i] = alloc_page(gfp_flags);
8699 if (!pages[i]) {
8700 dprintk("%s: failed to allocate page\n", __func__);
8701 nfs4_free_pages(pages, size);
8702 return NULL;
8703 }
8704 }
8705
8706 return pages;
8707}
8708
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008709static void nfs4_layoutget_release(void *calldata)
8710{
8711 struct nfs4_layoutget *lgp = calldata;
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008712 struct inode *inode = lgp->args.inode;
8713 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008714 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008715
8716 dprintk("--> %s\n", __func__);
Trond Myklebustbd171932017-06-27 17:33:38 -04008717 nfs4_sequence_free_slot(&lgp->res.seq_res);
Idan Kedar85541162012-08-02 11:47:10 +03008718 nfs4_free_pages(lgp->args.layout.pages, max_pages);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008719 pnfs_put_layout_hdr(NFS_I(inode)->layout);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008720 put_nfs_open_context(lgp->args.ctx);
8721 kfree(calldata);
8722 dprintk("<-- %s\n", __func__);
8723}
8724
8725static const struct rpc_call_ops nfs4_layoutget_call_ops = {
8726 .rpc_call_prepare = nfs4_layoutget_prepare,
8727 .rpc_call_done = nfs4_layoutget_done,
8728 .rpc_release = nfs4_layoutget_release,
8729};
8730
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008731struct pnfs_layout_segment *
Jeff Layton183d9e72016-05-17 12:28:47 -04008732nfs4_proc_layoutget(struct nfs4_layoutget *lgp, long *timeout, gfp_t gfp_flags)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008733{
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008734 struct inode *inode = lgp->args.inode;
8735 struct nfs_server *server = NFS_SERVER(inode);
Idan Kedar85541162012-08-02 11:47:10 +03008736 size_t max_pages = max_response_pages(server);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008737 struct rpc_task *task;
8738 struct rpc_message msg = {
8739 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTGET],
8740 .rpc_argp = &lgp->args,
8741 .rpc_resp = &lgp->res,
Trond Myklebust6ab59342013-05-20 10:49:34 -04008742 .rpc_cred = lgp->cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008743 };
8744 struct rpc_task_setup task_setup_data = {
8745 .rpc_client = server->client,
8746 .rpc_message = &msg,
8747 .callback_ops = &nfs4_layoutget_call_ops,
8748 .callback_data = lgp,
8749 .flags = RPC_TASK_ASYNC,
8750 };
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008751 struct pnfs_layout_segment *lseg = NULL;
Trond Myklebustbc236762016-06-17 16:48:18 -04008752 struct nfs4_exception exception = {
8753 .inode = inode,
8754 .timeout = *timeout,
8755 };
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008756 int status = 0;
8757
8758 dprintk("--> %s\n", __func__);
8759
Peng Tao4bd5a982014-11-17 11:05:17 +08008760 /* nfs4_layoutget_release calls pnfs_put_layout_hdr */
8761 pnfs_get_layout_hdr(NFS_I(inode)->layout);
8762
Idan Kedar85541162012-08-02 11:47:10 +03008763 lgp->args.layout.pages = nfs4_alloc_pages(max_pages, gfp_flags);
8764 if (!lgp->args.layout.pages) {
8765 nfs4_layoutget_release(lgp);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008766 return ERR_PTR(-ENOMEM);
Idan Kedar85541162012-08-02 11:47:10 +03008767 }
8768 lgp->args.layout.pglen = max_pages * PAGE_SIZE;
8769
Weston Andros Adamson35124a02011-03-24 16:48:21 -04008770 lgp->res.layoutp = &lgp->args.layout;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008771 lgp->res.seq_res.sr_slot = NULL;
Chuck Levera9c92d62013-08-09 12:48:18 -04008772 nfs4_init_sequence(&lgp->args.seq_args, &lgp->res.seq_res, 0);
Weston Andros Adamsona47970f2013-02-25 21:27:33 -05008773
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008774 task = rpc_run_task(&task_setup_data);
8775 if (IS_ERR(task))
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008776 return ERR_CAST(task);
Anna Schumaker820bf852017-01-11 15:01:43 -05008777 status = rpc_wait_for_completion_task(task);
Jeff Layton183d9e72016-05-17 12:28:47 -04008778 if (status == 0) {
8779 status = nfs4_layoutget_handle_exception(task, lgp, &exception);
8780 *timeout = exception.timeout;
8781 }
8782
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008783 trace_nfs4_layoutget(lgp->args.ctx,
8784 &lgp->args.range,
8785 &lgp->res.range,
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008786 &lgp->res.stateid,
Trond Myklebust1037e6e2013-08-14 16:36:51 -04008787 status);
Jeff Layton183d9e72016-05-17 12:28:47 -04008788
Weston Andros Adamson085b7a42013-02-15 16:03:46 -05008789 /* if layoutp->len is 0, nfs4_layoutget_prepare called rpc_exit */
8790 if (status == 0 && lgp->res.layoutp->len)
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008791 lseg = pnfs_layout_process(lgp);
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008792 rpc_put_task(task);
8793 dprintk("<-- %s status=%d\n", __func__, status);
Trond Myklebusta0b0a6e2012-09-17 17:12:15 -04008794 if (status)
8795 return ERR_PTR(status);
8796 return lseg;
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008797}
8798
Benny Halevycbe82602011-05-22 19:52:37 +03008799static void
8800nfs4_layoutreturn_prepare(struct rpc_task *task, void *calldata)
8801{
8802 struct nfs4_layoutreturn *lrp = calldata;
8803
8804 dprintk("--> %s\n", __func__);
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008805 nfs4_setup_sequence(lrp->clp,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008806 &lrp->args.seq_args,
8807 &lrp->res.seq_res,
8808 task);
Benny Halevycbe82602011-05-22 19:52:37 +03008809}
8810
8811static void nfs4_layoutreturn_done(struct rpc_task *task, void *calldata)
8812{
8813 struct nfs4_layoutreturn *lrp = calldata;
8814 struct nfs_server *server;
8815
8816 dprintk("--> %s\n", __func__);
8817
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008818 if (!nfs41_sequence_process(task, &lrp->res.seq_res))
Benny Halevycbe82602011-05-22 19:52:37 +03008819 return;
8820
8821 server = NFS_SERVER(lrp->args.inode);
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008822 switch (task->tk_status) {
Trond Myklebustff905142017-11-06 15:28:08 -05008823 case -NFS4ERR_OLD_STATEID:
8824 if (nfs4_refresh_layout_stateid(&lrp->args.stateid,
8825 lrp->args.inode))
8826 goto out_restart;
8827 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008828 default:
8829 task->tk_status = 0;
Trond Myklebustff905142017-11-06 15:28:08 -05008830 /* Fallthrough */
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008831 case 0:
8832 break;
8833 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10008834 if (nfs4_async_handle_error(task, server, NULL, NULL) != -EAGAIN)
Trond Myklebustf22e5ed2013-12-04 12:09:45 -05008835 break;
Trond Myklebustff905142017-11-06 15:28:08 -05008836 goto out_restart;
Benny Halevycbe82602011-05-22 19:52:37 +03008837 }
Benny Halevycbe82602011-05-22 19:52:37 +03008838 dprintk("<-- %s\n", __func__);
Trond Myklebustff905142017-11-06 15:28:08 -05008839 return;
8840out_restart:
8841 task->tk_status = 0;
8842 nfs4_sequence_free_slot(&lrp->res.seq_res);
8843 rpc_restart_call_prepare(task);
Benny Halevycbe82602011-05-22 19:52:37 +03008844}
8845
8846static void nfs4_layoutreturn_release(void *calldata)
8847{
8848 struct nfs4_layoutreturn *lrp = calldata;
Trond Myklebust849b2862012-09-24 14:18:39 -04008849 struct pnfs_layout_hdr *lo = lrp->args.layout;
Benny Halevycbe82602011-05-22 19:52:37 +03008850
8851 dprintk("--> %s\n", __func__);
Trond Myklebust2a974422016-11-20 13:13:54 -05008852 pnfs_layoutreturn_free_lsegs(lo, &lrp->args.stateid, &lrp->args.range,
Trond Myklebust68f74472016-10-12 19:50:54 -04008853 lrp->res.lrs_present ? &lrp->res.stateid : NULL);
Trond Myklebust2e80dbe2016-08-28 11:50:26 -04008854 nfs4_sequence_free_slot(&lrp->res.seq_res);
Trond Myklebust4d796d72016-09-23 11:38:08 -04008855 if (lrp->ld_private.ops && lrp->ld_private.ops->free)
8856 lrp->ld_private.ops->free(&lrp->ld_private);
Trond Myklebust2f065dd2016-12-07 12:29:26 -05008857 pnfs_put_layout_hdr(lrp->args.layout);
8858 nfs_iput_and_deactive(lrp->inode);
Benny Halevycbe82602011-05-22 19:52:37 +03008859 kfree(calldata);
8860 dprintk("<-- %s\n", __func__);
8861}
8862
8863static const struct rpc_call_ops nfs4_layoutreturn_call_ops = {
8864 .rpc_call_prepare = nfs4_layoutreturn_prepare,
8865 .rpc_call_done = nfs4_layoutreturn_done,
8866 .rpc_release = nfs4_layoutreturn_release,
8867};
8868
Peng Tao6c166052014-11-17 09:30:40 +08008869int nfs4_proc_layoutreturn(struct nfs4_layoutreturn *lrp, bool sync)
Benny Halevycbe82602011-05-22 19:52:37 +03008870{
8871 struct rpc_task *task;
8872 struct rpc_message msg = {
8873 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTRETURN],
8874 .rpc_argp = &lrp->args,
8875 .rpc_resp = &lrp->res,
Trond Myklebust95560002013-05-20 10:43:47 -04008876 .rpc_cred = lrp->cred,
Benny Halevycbe82602011-05-22 19:52:37 +03008877 };
8878 struct rpc_task_setup task_setup_data = {
Andy Adamson1771c572013-07-22 12:42:05 -04008879 .rpc_client = NFS_SERVER(lrp->args.inode)->client,
Benny Halevycbe82602011-05-22 19:52:37 +03008880 .rpc_message = &msg,
8881 .callback_ops = &nfs4_layoutreturn_call_ops,
8882 .callback_data = lrp,
8883 };
Peng Tao6c166052014-11-17 09:30:40 +08008884 int status = 0;
Benny Halevycbe82602011-05-22 19:52:37 +03008885
Andrew Elble99ade3c2015-12-02 09:39:51 -05008886 nfs4_state_protect(NFS_SERVER(lrp->args.inode)->nfs_client,
8887 NFS_SP4_MACH_CRED_PNFS_CLEANUP,
8888 &task_setup_data.rpc_client, &msg);
8889
Benny Halevycbe82602011-05-22 19:52:37 +03008890 dprintk("--> %s\n", __func__);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008891 if (!sync) {
8892 lrp->inode = nfs_igrab_and_active(lrp->args.inode);
8893 if (!lrp->inode) {
8894 nfs4_layoutreturn_release(lrp);
8895 return -EAGAIN;
8896 }
8897 task_setup_data.flags |= RPC_TASK_ASYNC;
8898 }
Chuck Levera9c92d62013-08-09 12:48:18 -04008899 nfs4_init_sequence(&lrp->args.seq_args, &lrp->res.seq_res, 1);
Benny Halevycbe82602011-05-22 19:52:37 +03008900 task = rpc_run_task(&task_setup_data);
8901 if (IS_ERR(task))
8902 return PTR_ERR(task);
Trond Myklebust5a0ec8ac2015-02-05 16:35:16 -05008903 if (sync)
8904 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05008905 trace_nfs4_layoutreturn(lrp->args.inode, &lrp->args.stateid, status);
Benny Halevycbe82602011-05-22 19:52:37 +03008906 dprintk("<-- %s status=%d\n", __func__, status);
8907 rpc_put_task(task);
8908 return status;
8909}
8910
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008911static int
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008912_nfs4_proc_getdeviceinfo(struct nfs_server *server,
8913 struct pnfs_device *pdev,
8914 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008915{
8916 struct nfs4_getdeviceinfo_args args = {
8917 .pdev = pdev,
Trond Myklebust4e590802015-03-09 14:01:25 -04008918 .notify_types = NOTIFY_DEVICEID4_CHANGE |
8919 NOTIFY_DEVICEID4_DELETE,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008920 };
8921 struct nfs4_getdeviceinfo_res res = {
8922 .pdev = pdev,
8923 };
8924 struct rpc_message msg = {
8925 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_GETDEVICEINFO],
8926 .rpc_argp = &args,
8927 .rpc_resp = &res,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008928 .rpc_cred = cred,
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008929 };
8930 int status;
8931
8932 dprintk("--> %s\n", __func__);
Bryan Schumaker7c513052011-03-24 17:12:24 +00008933 status = nfs4_call_sync(server->client, server, &msg, &args.seq_args, &res.seq_res, 0);
Trond Myklebust4e590802015-03-09 14:01:25 -04008934 if (res.notification & ~args.notify_types)
8935 dprintk("%s: unsupported notification\n", __func__);
Trond Myklebustdf526992015-03-09 14:48:32 -04008936 if (res.notification != args.notify_types)
8937 pdev->nocache = 1;
Trond Myklebust4e590802015-03-09 14:01:25 -04008938
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008939 dprintk("<-- %s status=%d\n", __func__, status);
8940
8941 return status;
8942}
8943
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008944int nfs4_proc_getdeviceinfo(struct nfs_server *server,
8945 struct pnfs_device *pdev,
8946 struct rpc_cred *cred)
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008947{
8948 struct nfs4_exception exception = { };
8949 int err;
8950
8951 do {
8952 err = nfs4_handle_exception(server,
Trond Myklebustcd5875f2013-05-20 11:42:54 -04008953 _nfs4_proc_getdeviceinfo(server, pdev, cred),
Andy Adamsonb1f69b72010-10-20 00:18:03 -04008954 &exception);
8955 } while (exception.retry);
8956 return err;
8957}
8958EXPORT_SYMBOL_GPL(nfs4_proc_getdeviceinfo);
8959
Andy Adamson863a3c62011-03-23 13:27:54 +00008960static void nfs4_layoutcommit_prepare(struct rpc_task *task, void *calldata)
8961{
8962 struct nfs4_layoutcommit_data *data = calldata;
8963 struct nfs_server *server = NFS_SERVER(data->args.inode);
8964
Anna Schumaker7981c8a2017-01-10 11:39:53 -05008965 nfs4_setup_sequence(server->nfs_client,
Trond Myklebustd9afbd12012-10-22 20:28:44 -04008966 &data->args.seq_args,
8967 &data->res.seq_res,
8968 task);
Andy Adamson863a3c62011-03-23 13:27:54 +00008969}
8970
8971static void
8972nfs4_layoutcommit_done(struct rpc_task *task, void *calldata)
8973{
8974 struct nfs4_layoutcommit_data *data = calldata;
8975 struct nfs_server *server = NFS_SERVER(data->args.inode);
8976
Trond Myklebust6ba7db32012-10-22 20:07:20 -04008977 if (!nfs41_sequence_done(task, &data->res.seq_res))
Andy Adamson863a3c62011-03-23 13:27:54 +00008978 return;
8979
8980 switch (task->tk_status) { /* Just ignore these failures */
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008981 case -NFS4ERR_DELEG_REVOKED: /* layout was recalled */
8982 case -NFS4ERR_BADIOMODE: /* no IOMODE_RW layout for range */
8983 case -NFS4ERR_BADLAYOUT: /* no layout */
8984 case -NFS4ERR_GRACE: /* loca_recalim always false */
Andy Adamson863a3c62011-03-23 13:27:54 +00008985 task->tk_status = 0;
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008986 case 0:
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008987 break;
8988 default:
NeilBrown8478eaa2014-09-18 16:09:27 +10008989 if (nfs4_async_handle_error(task, server, NULL, NULL) == -EAGAIN) {
Trond Myklebuste59d27e2012-03-27 18:22:19 -04008990 rpc_restart_call_prepare(task);
8991 return;
8992 }
8993 }
Andy Adamson863a3c62011-03-23 13:27:54 +00008994}
8995
8996static void nfs4_layoutcommit_release(void *calldata)
8997{
8998 struct nfs4_layoutcommit_data *data = calldata;
8999
Andy Adamsondb29c082011-07-30 20:52:38 -04009000 pnfs_cleanup_layoutcommit(data);
Trond Myklebustd8c951c2014-01-13 12:08:11 -05009001 nfs_post_op_update_inode_force_wcc(data->args.inode,
9002 data->res.fattr);
Andy Adamson863a3c62011-03-23 13:27:54 +00009003 put_rpccred(data->cred);
Trond Myklebust472e2592015-02-05 16:50:30 -05009004 nfs_iput_and_deactive(data->inode);
Andy Adamson863a3c62011-03-23 13:27:54 +00009005 kfree(data);
9006}
9007
9008static const struct rpc_call_ops nfs4_layoutcommit_ops = {
9009 .rpc_call_prepare = nfs4_layoutcommit_prepare,
9010 .rpc_call_done = nfs4_layoutcommit_done,
9011 .rpc_release = nfs4_layoutcommit_release,
9012};
9013
9014int
Andy Adamsonef311532011-03-12 02:58:10 -05009015nfs4_proc_layoutcommit(struct nfs4_layoutcommit_data *data, bool sync)
Andy Adamson863a3c62011-03-23 13:27:54 +00009016{
9017 struct rpc_message msg = {
9018 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_LAYOUTCOMMIT],
9019 .rpc_argp = &data->args,
9020 .rpc_resp = &data->res,
9021 .rpc_cred = data->cred,
9022 };
9023 struct rpc_task_setup task_setup_data = {
9024 .task = &data->task,
9025 .rpc_client = NFS_CLIENT(data->args.inode),
9026 .rpc_message = &msg,
9027 .callback_ops = &nfs4_layoutcommit_ops,
9028 .callback_data = data,
Andy Adamson863a3c62011-03-23 13:27:54 +00009029 };
9030 struct rpc_task *task;
9031 int status = 0;
9032
Kinglong Meeb4839eb2015-07-01 12:00:13 +08009033 dprintk("NFS: initiating layoutcommit call. sync %d "
9034 "lbw: %llu inode %lu\n", sync,
Andy Adamson863a3c62011-03-23 13:27:54 +00009035 data->args.lastbytewritten,
9036 data->args.inode->i_ino);
9037
Trond Myklebust472e2592015-02-05 16:50:30 -05009038 if (!sync) {
9039 data->inode = nfs_igrab_and_active(data->args.inode);
9040 if (data->inode == NULL) {
9041 nfs4_layoutcommit_release(data);
9042 return -EAGAIN;
9043 }
9044 task_setup_data.flags = RPC_TASK_ASYNC;
9045 }
Chuck Levera9c92d62013-08-09 12:48:18 -04009046 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Andy Adamson863a3c62011-03-23 13:27:54 +00009047 task = rpc_run_task(&task_setup_data);
9048 if (IS_ERR(task))
9049 return PTR_ERR(task);
Trond Myklebust472e2592015-02-05 16:50:30 -05009050 if (sync)
9051 status = task->tk_status;
Olga Kornievskaia48c95792015-11-24 13:29:41 -05009052 trace_nfs4_layoutcommit(data->args.inode, &data->args.stateid, status);
Andy Adamson863a3c62011-03-23 13:27:54 +00009053 dprintk("%s: status %d\n", __func__, status);
9054 rpc_put_task(task);
9055 return status;
9056}
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009057
Andy Adamson97431202013-08-08 10:57:56 -04009058/**
9059 * Use the state managment nfs_client cl_rpcclient, which uses krb5i (if
9060 * possible) as per RFC3530bis and RFC5661 Security Considerations sections
9061 */
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009062static int
9063_nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009064 struct nfs_fsinfo *info,
9065 struct nfs4_secinfo_flavors *flavors, bool use_integrity)
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009066{
9067 struct nfs41_secinfo_no_name_args args = {
9068 .style = SECINFO_STYLE_CURRENT_FH,
9069 };
9070 struct nfs4_secinfo_res res = {
9071 .flavors = flavors,
9072 };
9073 struct rpc_message msg = {
9074 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_SECINFO_NO_NAME],
9075 .rpc_argp = &args,
9076 .rpc_resp = &res,
9077 };
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009078 struct rpc_clnt *clnt = server->client;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009079 struct rpc_cred *cred = NULL;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009080 int status;
9081
9082 if (use_integrity) {
9083 clnt = server->nfs_client->cl_rpcclient;
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009084 cred = nfs4_get_clid_cred(server->nfs_client);
9085 msg.rpc_cred = cred;
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009086 }
9087
9088 dprintk("--> %s\n", __func__);
9089 status = nfs4_call_sync(clnt, server, &msg, &args.seq_args,
9090 &res.seq_res, 0);
9091 dprintk("<-- %s status=%d\n", __func__, status);
9092
Weston Andros Adamson7cb852d2013-09-10 18:44:31 -04009093 if (cred)
9094 put_rpccred(cred);
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009095
9096 return status;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009097}
9098
9099static int
9100nfs41_proc_secinfo_no_name(struct nfs_server *server, struct nfs_fh *fhandle,
9101 struct nfs_fsinfo *info, struct nfs4_secinfo_flavors *flavors)
9102{
9103 struct nfs4_exception exception = { };
9104 int err;
9105 do {
Weston Andros Adamsonb1b3e132013-09-04 12:13:19 -04009106 /* first try using integrity protection */
9107 err = -NFS4ERR_WRONGSEC;
9108
9109 /* try to use integrity protection with machine cred */
9110 if (_nfs4_is_integrity_protected(server->nfs_client))
9111 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9112 flavors, true);
9113
9114 /*
9115 * if unable to use integrity protection, or SECINFO with
9116 * integrity protection returns NFS4ERR_WRONGSEC (which is
9117 * disallowed by spec, but exists in deployed servers) use
9118 * the current filesystem's rpc_client and the user cred.
9119 */
9120 if (err == -NFS4ERR_WRONGSEC)
9121 err = _nfs41_proc_secinfo_no_name(server, fhandle, info,
9122 flavors, false);
9123
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009124 switch (err) {
9125 case 0:
9126 case -NFS4ERR_WRONGSEC:
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009127 case -ENOTSUPP:
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009128 goto out;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009129 default:
9130 err = nfs4_handle_exception(server, err, &exception);
9131 }
9132 } while (exception.retry);
Trond Myklebust05e9cfb2012-03-27 18:13:02 -04009133out:
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009134 return err;
9135}
9136
9137static int
9138nfs41_find_root_sec(struct nfs_server *server, struct nfs_fh *fhandle,
9139 struct nfs_fsinfo *info)
9140{
9141 int err;
9142 struct page *page;
Anna Schumaker367156d2013-09-25 17:02:48 -04009143 rpc_authflavor_t flavor = RPC_AUTH_MAXFLAVOR;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009144 struct nfs4_secinfo_flavors *flavors;
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009145 struct nfs4_secinfo4 *secinfo;
9146 int i;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009147
9148 page = alloc_page(GFP_KERNEL);
9149 if (!page) {
9150 err = -ENOMEM;
9151 goto out;
9152 }
9153
9154 flavors = page_address(page);
9155 err = nfs41_proc_secinfo_no_name(server, fhandle, info, flavors);
9156
9157 /*
9158 * Fall back on "guess and check" method if
9159 * the server doesn't support SECINFO_NO_NAME
9160 */
Weston Andros Adamson78b19ba2014-01-13 16:54:45 -05009161 if (err == -NFS4ERR_WRONGSEC || err == -ENOTSUPP) {
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009162 err = nfs4_find_root_sec(server, fhandle, info);
9163 goto out_freepage;
9164 }
9165 if (err)
9166 goto out_freepage;
9167
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009168 for (i = 0; i < flavors->num_flavors; i++) {
9169 secinfo = &flavors->flavors[i];
9170
9171 switch (secinfo->flavor) {
9172 case RPC_AUTH_NULL:
9173 case RPC_AUTH_UNIX:
9174 case RPC_AUTH_GSS:
9175 flavor = rpcauth_get_pseudoflavor(secinfo->flavor,
9176 &secinfo->flavor_info);
9177 break;
9178 default:
9179 flavor = RPC_AUTH_MAXFLAVOR;
9180 break;
9181 }
9182
Weston Andros Adamson4d4b69d2013-10-18 15:15:19 -04009183 if (!nfs_auth_info_match(&server->auth_info, flavor))
9184 flavor = RPC_AUTH_MAXFLAVOR;
9185
Weston Andros Adamson58a8cf12013-09-24 13:58:02 -04009186 if (flavor != RPC_AUTH_MAXFLAVOR) {
9187 err = nfs4_lookup_root_sec(server, fhandle,
9188 info, flavor);
9189 if (!err)
9190 break;
9191 }
9192 }
9193
9194 if (flavor == RPC_AUTH_MAXFLAVOR)
9195 err = -EPERM;
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009196
9197out_freepage:
9198 put_page(page);
9199 if (err == -EACCES)
9200 return -EPERM;
9201out:
9202 return err;
9203}
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009204
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009205static int _nfs41_test_stateid(struct nfs_server *server,
9206 nfs4_stateid *stateid,
9207 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009208{
9209 int status;
9210 struct nfs41_test_stateid_args args = {
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009211 .stateid = stateid,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009212 };
9213 struct nfs41_test_stateid_res res;
9214 struct rpc_message msg = {
9215 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_TEST_STATEID],
9216 .rpc_argp = &args,
9217 .rpc_resp = &res,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009218 .rpc_cred = cred,
Bryan Schumaker7d974792011-06-02 14:59:08 -04009219 };
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009220 struct rpc_clnt *rpc_client = server->client;
9221
9222 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9223 &rpc_client, &msg);
Bryan Schumaker1cab0652012-01-31 10:39:29 -05009224
Chuck Lever38527b12012-07-11 16:30:23 -04009225 dprintk("NFS call test_stateid %p\n", stateid);
Chuck Levera9c92d62013-08-09 12:48:18 -04009226 nfs4_init_sequence(&args.seq_args, &res.seq_res, 0);
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009227 nfs4_set_sequence_privileged(&args.seq_args);
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009228 status = nfs4_call_sync_sequence(rpc_client, server, &msg,
Trond Myklebust8fe72ba2012-10-29 19:02:20 -04009229 &args.seq_args, &res.seq_res);
Chuck Lever38527b12012-07-11 16:30:23 -04009230 if (status != NFS_OK) {
9231 dprintk("NFS reply test_stateid: failed, %d\n", status);
Chuck Lever377e5072012-07-11 16:29:45 -04009232 return status;
Chuck Lever38527b12012-07-11 16:30:23 -04009233 }
9234 dprintk("NFS reply test_stateid: succeeded, %d\n", -res.status);
Chuck Lever377e5072012-07-11 16:29:45 -04009235 return -res.status;
Bryan Schumaker7d974792011-06-02 14:59:08 -04009236}
9237
Trond Myklebust43912bb2016-09-22 13:38:56 -04009238static void nfs4_handle_delay_or_session_error(struct nfs_server *server,
9239 int err, struct nfs4_exception *exception)
9240{
9241 exception->retry = 0;
9242 switch(err) {
9243 case -NFS4ERR_DELAY:
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009244 case -NFS4ERR_RETRY_UNCACHED_REP:
Trond Myklebust43912bb2016-09-22 13:38:56 -04009245 nfs4_handle_exception(server, err, exception);
9246 break;
9247 case -NFS4ERR_BADSESSION:
9248 case -NFS4ERR_BADSLOT:
9249 case -NFS4ERR_BAD_HIGH_SLOT:
9250 case -NFS4ERR_CONN_NOT_BOUND_TO_SESSION:
9251 case -NFS4ERR_DEADSESSION:
9252 nfs4_do_handle_exception(server, err, exception);
9253 }
9254}
9255
Chuck Lever38527b12012-07-11 16:30:23 -04009256/**
9257 * nfs41_test_stateid - perform a TEST_STATEID operation
9258 *
9259 * @server: server / transport on which to perform the operation
9260 * @stateid: state ID to test
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009261 * @cred: credential
Chuck Lever38527b12012-07-11 16:30:23 -04009262 *
9263 * Returns NFS_OK if the server recognizes that "stateid" is valid.
9264 * Otherwise a negative NFS4ERR value is returned if the operation
9265 * failed or the state ID is not currently valid.
9266 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009267static int nfs41_test_stateid(struct nfs_server *server,
9268 nfs4_stateid *stateid,
9269 struct rpc_cred *cred)
Bryan Schumaker7d974792011-06-02 14:59:08 -04009270{
9271 struct nfs4_exception exception = { };
9272 int err;
9273 do {
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009274 err = _nfs41_test_stateid(server, stateid, cred);
Trond Myklebust43912bb2016-09-22 13:38:56 -04009275 nfs4_handle_delay_or_session_error(server, err, &exception);
Bryan Schumaker7d974792011-06-02 14:59:08 -04009276 } while (exception.retry);
9277 return err;
9278}
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009279
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009280struct nfs_free_stateid_data {
9281 struct nfs_server *server;
9282 struct nfs41_free_stateid_args args;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009283 struct nfs41_free_stateid_res res;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009284};
9285
9286static void nfs41_free_stateid_prepare(struct rpc_task *task, void *calldata)
9287{
9288 struct nfs_free_stateid_data *data = calldata;
Anna Schumaker7981c8a2017-01-10 11:39:53 -05009289 nfs4_setup_sequence(data->server->nfs_client,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009290 &data->args.seq_args,
9291 &data->res.seq_res,
9292 task);
9293}
9294
9295static void nfs41_free_stateid_done(struct rpc_task *task, void *calldata)
9296{
9297 struct nfs_free_stateid_data *data = calldata;
9298
9299 nfs41_sequence_done(task, &data->res.seq_res);
9300
9301 switch (task->tk_status) {
9302 case -NFS4ERR_DELAY:
NeilBrown8478eaa2014-09-18 16:09:27 +10009303 if (nfs4_async_handle_error(task, data->server, NULL, NULL) == -EAGAIN)
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009304 rpc_restart_call_prepare(task);
9305 }
9306}
9307
9308static void nfs41_free_stateid_release(void *calldata)
9309{
9310 kfree(calldata);
9311}
9312
Trond Myklebust17f26b12013-08-21 15:48:42 -04009313static const struct rpc_call_ops nfs41_free_stateid_ops = {
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009314 .rpc_call_prepare = nfs41_free_stateid_prepare,
9315 .rpc_call_done = nfs41_free_stateid_done,
9316 .rpc_release = nfs41_free_stateid_release,
9317};
9318
9319static struct rpc_task *_nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009320 const nfs4_stateid *stateid,
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009321 struct rpc_cred *cred,
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009322 bool privileged)
9323{
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009324 struct rpc_message msg = {
9325 .rpc_proc = &nfs4_procedures[NFSPROC4_CLNT_FREE_STATEID],
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009326 .rpc_cred = cred,
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009327 };
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009328 struct rpc_task_setup task_setup = {
9329 .rpc_client = server->client,
9330 .rpc_message = &msg,
9331 .callback_ops = &nfs41_free_stateid_ops,
9332 .flags = RPC_TASK_ASYNC,
9333 };
9334 struct nfs_free_stateid_data *data;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009335
Weston Andros Adamson3787d502013-08-13 16:37:36 -04009336 nfs4_state_protect(server->nfs_client, NFS_SP4_MACH_CRED_STATEID,
9337 &task_setup.rpc_client, &msg);
9338
Chuck Lever38527b12012-07-11 16:30:23 -04009339 dprintk("NFS call free_stateid %p\n", stateid);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009340 data = kmalloc(sizeof(*data), GFP_NOFS);
9341 if (!data)
9342 return ERR_PTR(-ENOMEM);
9343 data->server = server;
9344 nfs4_stateid_copy(&data->args.stateid, stateid);
9345
9346 task_setup.callback_data = data;
9347
9348 msg.rpc_argp = &data->args;
9349 msg.rpc_resp = &data->res;
Trond Myklebust76e8a1b2016-09-22 13:39:19 -04009350 nfs4_init_sequence(&data->args.seq_args, &data->res.seq_res, 1);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009351 if (privileged)
9352 nfs4_set_sequence_privileged(&data->args.seq_args);
9353
9354 return rpc_run_task(&task_setup);
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009355}
9356
Chuck Lever38527b12012-07-11 16:30:23 -04009357/**
9358 * nfs41_free_stateid - perform a FREE_STATEID operation
9359 *
9360 * @server: server / transport on which to perform the operation
9361 * @stateid: state ID to release
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009362 * @cred: credential
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009363 * @is_recovery: set to true if this call needs to be privileged
Chuck Lever38527b12012-07-11 16:30:23 -04009364 *
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009365 * Note: this function is always asynchronous.
Chuck Lever38527b12012-07-11 16:30:23 -04009366 */
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009367static int nfs41_free_stateid(struct nfs_server *server,
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009368 const nfs4_stateid *stateid,
9369 struct rpc_cred *cred,
9370 bool is_recovery)
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009371{
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009372 struct rpc_task *task;
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009373
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009374 task = _nfs41_free_stateid(server, stateid, cred, is_recovery);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009375 if (IS_ERR(task))
9376 return PTR_ERR(task);
Trond Myklebust7c1d5fa2013-05-03 14:40:01 -04009377 rpc_put_task(task);
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009378 return 0;
Bryan Schumaker9aeda352011-06-02 14:59:09 -04009379}
Trond Myklebust36281ca2012-03-04 18:13:56 -05009380
Jeff Laytonf1cdae82014-05-01 06:28:47 -04009381static void
9382nfs41_free_lock_state(struct nfs_server *server, struct nfs4_lock_state *lsp)
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009383{
Trond Myklebustab7cb0d2013-05-20 11:20:27 -04009384 struct rpc_cred *cred = lsp->ls_state->owner->so_cred;
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009385
Trond Myklebustf0b0bf82016-09-22 13:39:04 -04009386 nfs41_free_stateid(server, &lsp->ls_stateid, cred, false);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009387 nfs4_free_lock_state(server, lsp);
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009388}
9389
Trond Myklebust36281ca2012-03-04 18:13:56 -05009390static bool nfs41_match_stateid(const nfs4_stateid *s1,
9391 const nfs4_stateid *s2)
9392{
Trond Myklebust93b717f2016-05-16 17:42:43 -04009393 if (s1->type != s2->type)
9394 return false;
9395
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009396 if (memcmp(s1->other, s2->other, sizeof(s1->other)) != 0)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009397 return false;
9398
Trond Myklebust2d2f24a2012-03-04 18:13:57 -05009399 if (s1->seqid == s2->seqid)
Trond Myklebust36281ca2012-03-04 18:13:56 -05009400 return true;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009401
Anna Schumaker045c5512017-01-11 16:59:48 -05009402 return s1->seqid == 0 || s2->seqid == 0;
Trond Myklebust36281ca2012-03-04 18:13:56 -05009403}
9404
Andy Adamson557134a2009-04-01 09:21:53 -04009405#endif /* CONFIG_NFS_V4_1 */
9406
Trond Myklebust36281ca2012-03-04 18:13:56 -05009407static bool nfs4_match_stateid(const nfs4_stateid *s1,
9408 const nfs4_stateid *s2)
9409{
Trond Myklebustf597c532012-03-04 18:13:56 -05009410 return nfs4_stateid_match(s1, s2);
Trond Myklebust36281ca2012-03-04 18:13:56 -05009411}
9412
9413
Trond Myklebust17280172012-03-11 13:11:00 -04009414static const struct nfs4_state_recovery_ops nfs40_reboot_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009415 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009416 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009417 .recover_open = nfs4_open_reclaim,
9418 .recover_lock = nfs4_lock_reclaim,
Andy Adamson591d71c2009-04-01 09:22:47 -04009419 .establish_clid = nfs4_init_clientid,
Chuck Lever05f4c352012-09-14 17:24:32 -04009420 .detect_trunking = nfs40_discover_server_trunking,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009421};
9422
Andy Adamson591d71c2009-04-01 09:22:47 -04009423#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009424static const struct nfs4_state_recovery_ops nfs41_reboot_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009425 .owner_flag_bit = NFS_OWNER_RECLAIM_REBOOT,
9426 .state_flag_bit = NFS_STATE_RECLAIM_REBOOT,
9427 .recover_open = nfs4_open_reclaim,
9428 .recover_lock = nfs4_lock_reclaim,
Andy Adamson4d643d12009-12-04 15:52:24 -05009429 .establish_clid = nfs41_init_clientid,
Ricardo Labiagafce5c832009-12-05 16:08:41 -05009430 .reclaim_complete = nfs41_proc_reclaim_complete,
Chuck Lever05f4c352012-09-14 17:24:32 -04009431 .detect_trunking = nfs41_discover_server_trunking,
Andy Adamson591d71c2009-04-01 09:22:47 -04009432};
9433#endif /* CONFIG_NFS_V4_1 */
9434
Trond Myklebust17280172012-03-11 13:11:00 -04009435static const struct nfs4_state_recovery_ops nfs40_nograce_recovery_ops = {
Trond Myklebust7eff03a2008-12-23 15:21:43 -05009436 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
Trond Myklebustb79a4a12008-12-23 15:21:41 -05009437 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Trond Myklebust4dfd4f72014-10-17 15:10:25 +03009438 .recover_open = nfs40_open_expired,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009439 .recover_lock = nfs4_lock_expired,
Andy Adamson591d71c2009-04-01 09:22:47 -04009440 .establish_clid = nfs4_init_clientid,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009441};
9442
Andy Adamson591d71c2009-04-01 09:22:47 -04009443#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009444static const struct nfs4_state_recovery_ops nfs41_nograce_recovery_ops = {
Andy Adamson591d71c2009-04-01 09:22:47 -04009445 .owner_flag_bit = NFS_OWNER_RECLAIM_NOGRACE,
9446 .state_flag_bit = NFS_STATE_RECLAIM_NOGRACE,
Bryan Schumakerf062eb62011-06-02 14:59:10 -04009447 .recover_open = nfs41_open_expired,
9448 .recover_lock = nfs41_lock_expired,
Andy Adamson4d643d12009-12-04 15:52:24 -05009449 .establish_clid = nfs41_init_clientid,
Andy Adamson591d71c2009-04-01 09:22:47 -04009450};
9451#endif /* CONFIG_NFS_V4_1 */
9452
Trond Myklebust17280172012-03-11 13:11:00 -04009453static const struct nfs4_state_maintenance_ops nfs40_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009454 .sched_state_renewal = nfs4_proc_async_renew,
Andy Adamsona7b72102009-04-01 09:22:46 -04009455 .get_state_renewal_cred_locked = nfs4_get_renew_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009456 .renew_lease = nfs4_proc_renew,
Benny Halevy29fba382009-04-01 09:22:44 -04009457};
9458
9459#if defined(CONFIG_NFS_V4_1)
Trond Myklebust17280172012-03-11 13:11:00 -04009460static const struct nfs4_state_maintenance_ops nfs41_state_renewal_ops = {
Benny Halevy29fba382009-04-01 09:22:44 -04009461 .sched_state_renewal = nfs41_proc_async_sequence,
Andy Adamsona7b72102009-04-01 09:22:46 -04009462 .get_state_renewal_cred_locked = nfs4_get_machine_cred_locked,
Benny Halevy8e69514f2009-04-01 09:22:45 -04009463 .renew_lease = nfs4_proc_sequence,
Benny Halevy29fba382009-04-01 09:22:44 -04009464};
9465#endif
9466
Chuck Leverec011fe2013-10-17 14:12:39 -04009467static const struct nfs4_mig_recovery_ops nfs40_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009468 .get_locations = _nfs40_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009469 .fsid_present = _nfs40_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009470};
9471
9472#if defined(CONFIG_NFS_V4_1)
9473static const struct nfs4_mig_recovery_ops nfs41_mig_recovery_ops = {
Chuck Leverb03d7352013-10-17 14:12:50 -04009474 .get_locations = _nfs41_proc_get_locations,
Chuck Lever44c99932013-10-17 14:13:30 -04009475 .fsid_present = _nfs41_proc_fsid_present,
Chuck Leverec011fe2013-10-17 14:12:39 -04009476};
9477#endif /* CONFIG_NFS_V4_1 */
9478
Trond Myklebust97dc1352010-06-16 09:52:26 -04009479static const struct nfs4_minor_version_ops nfs_v4_0_minor_ops = {
9480 .minor_version = 0,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009481 .init_caps = NFS_CAP_READDIRPLUS
9482 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009483 | NFS_CAP_POSIX_LOCK,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009484 .init_client = nfs40_init_client,
9485 .shutdown_client = nfs40_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009486 .match_stateid = nfs4_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009487 .find_root_sec = nfs4_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009488 .free_lock_state = nfs4_release_lockowner,
Trond Myklebust45870d62016-09-22 13:38:59 -04009489 .test_and_free_expired = nfs40_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009490 .alloc_seqid = nfs_alloc_seqid,
Chuck Lever9915ea72013-08-09 12:48:27 -04009491 .call_sync_ops = &nfs40_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009492 .reboot_recovery_ops = &nfs40_reboot_recovery_ops,
9493 .nograce_recovery_ops = &nfs40_nograce_recovery_ops,
9494 .state_renewal_ops = &nfs40_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009495 .mig_recovery_ops = &nfs40_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009496};
9497
9498#if defined(CONFIG_NFS_V4_1)
Trond Myklebust63f5f792015-01-23 19:19:25 -05009499static struct nfs_seqid *
9500nfs_alloc_no_seqid(struct nfs_seqid_counter *arg1, gfp_t arg2)
9501{
9502 return NULL;
9503}
9504
Trond Myklebust97dc1352010-06-16 09:52:26 -04009505static const struct nfs4_minor_version_ops nfs_v4_1_minor_ops = {
9506 .minor_version = 1,
Trond Myklebust39c6daa2013-03-15 16:11:57 -04009507 .init_caps = NFS_CAP_READDIRPLUS
9508 | NFS_CAP_ATOMIC_OPEN
Trond Myklebust3b664862013-03-17 15:31:15 -04009509 | NFS_CAP_POSIX_LOCK
Trond Myklebust49f9a0f2013-03-15 16:44:28 -04009510 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009511 | NFS_CAP_ATOMIC_OPEN_V1,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009512 .init_client = nfs41_init_client,
9513 .shutdown_client = nfs41_shutdown_client,
Trond Myklebust36281ca2012-03-04 18:13:56 -05009514 .match_stateid = nfs41_match_stateid,
Bryan Schumakerfca78d62011-06-02 14:59:07 -04009515 .find_root_sec = nfs41_find_root_sec,
Trond Myklebustc8b2d0b2013-05-03 16:22:55 -04009516 .free_lock_state = nfs41_free_lock_state,
Trond Myklebust45870d62016-09-22 13:38:59 -04009517 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009518 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009519 .session_trunk = nfs4_test_session_trunk,
Chuck Lever9915ea72013-08-09 12:48:27 -04009520 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebustc48f4f32010-06-16 09:52:27 -04009521 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9522 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9523 .state_renewal_ops = &nfs41_state_renewal_ops,
Chuck Leverec011fe2013-10-17 14:12:39 -04009524 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Trond Myklebust97dc1352010-06-16 09:52:26 -04009525};
9526#endif
9527
Steve Dickson42c2c422013-05-22 12:50:38 -04009528#if defined(CONFIG_NFS_V4_2)
9529static const struct nfs4_minor_version_ops nfs_v4_2_minor_ops = {
9530 .minor_version = 2,
Bryan Schumaker70173102013-06-19 13:41:43 -04009531 .init_caps = NFS_CAP_READDIRPLUS
9532 | NFS_CAP_ATOMIC_OPEN
Bryan Schumaker70173102013-06-19 13:41:43 -04009533 | NFS_CAP_POSIX_LOCK
9534 | NFS_CAP_STATEID_NFSV41
Anna Schumakere9831202014-10-22 15:53:10 -04009535 | NFS_CAP_ATOMIC_OPEN_V1
Anna Schumakerf4ac1672014-11-25 13:18:15 -05009536 | NFS_CAP_ALLOCATE
Anna Schumaker2e724482013-05-21 16:53:03 -04009537 | NFS_CAP_COPY
Anna Schumaker624bd5b2014-11-25 13:18:16 -05009538 | NFS_CAP_DEALLOCATE
Trond Myklebust6c5a0d82015-06-27 11:45:46 -04009539 | NFS_CAP_SEEK
Peng Taoe5341f32015-09-26 02:24:35 +08009540 | NFS_CAP_LAYOUTSTATS
9541 | NFS_CAP_CLONE,
Chuck Leverabf79bb2013-08-09 12:49:11 -04009542 .init_client = nfs41_init_client,
9543 .shutdown_client = nfs41_shutdown_client,
Steve Dickson42c2c422013-05-22 12:50:38 -04009544 .match_stateid = nfs41_match_stateid,
9545 .find_root_sec = nfs41_find_root_sec,
Bryan Schumaker70173102013-06-19 13:41:43 -04009546 .free_lock_state = nfs41_free_lock_state,
Chuck Lever9915ea72013-08-09 12:48:27 -04009547 .call_sync_ops = &nfs41_call_sync_ops,
Trond Myklebust45870d62016-09-22 13:38:59 -04009548 .test_and_free_expired = nfs41_test_and_free_expired_stateid,
Trond Myklebust63f5f792015-01-23 19:19:25 -05009549 .alloc_seqid = nfs_alloc_no_seqid,
Andy Adamson04fa2c62016-09-09 09:22:29 -04009550 .session_trunk = nfs4_test_session_trunk,
Steve Dickson42c2c422013-05-22 12:50:38 -04009551 .reboot_recovery_ops = &nfs41_reboot_recovery_ops,
9552 .nograce_recovery_ops = &nfs41_nograce_recovery_ops,
9553 .state_renewal_ops = &nfs41_state_renewal_ops,
Kinglong Mee18e3b732015-08-15 21:52:10 +08009554 .mig_recovery_ops = &nfs41_mig_recovery_ops,
Steve Dickson42c2c422013-05-22 12:50:38 -04009555};
9556#endif
9557
Trond Myklebust97dc1352010-06-16 09:52:26 -04009558const struct nfs4_minor_version_ops *nfs_v4_minor_ops[] = {
9559 [0] = &nfs_v4_0_minor_ops,
9560#if defined(CONFIG_NFS_V4_1)
9561 [1] = &nfs_v4_1_minor_ops,
9562#endif
Steve Dickson42c2c422013-05-22 12:50:38 -04009563#if defined(CONFIG_NFS_V4_2)
9564 [2] = &nfs_v4_2_minor_ops,
9565#endif
Trond Myklebust97dc1352010-06-16 09:52:26 -04009566};
9567
Trond Myklebust13997822016-07-24 17:10:52 -04009568static ssize_t nfs4_listxattr(struct dentry *dentry, char *list, size_t size)
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009569{
9570 ssize_t error, error2;
9571
9572 error = generic_listxattr(dentry, list, size);
9573 if (error < 0)
9574 return error;
9575 if (list) {
9576 list += error;
9577 size -= error;
9578 }
9579
9580 error2 = nfs4_listxattr_nfs4_label(d_inode(dentry), list, size);
9581 if (error2 < 0)
9582 return error2;
9583 return error + error2;
9584}
9585
Trond Myklebust17f26b12013-08-21 15:48:42 -04009586static const struct inode_operations nfs4_dir_inode_operations = {
Bryan Schumaker73a79702012-07-16 16:39:12 -04009587 .create = nfs_create,
9588 .lookup = nfs_lookup,
9589 .atomic_open = nfs_atomic_open,
9590 .link = nfs_link,
9591 .unlink = nfs_unlink,
9592 .symlink = nfs_symlink,
9593 .mkdir = nfs_mkdir,
9594 .rmdir = nfs_rmdir,
9595 .mknod = nfs_mknod,
9596 .rename = nfs_rename,
9597 .permission = nfs_permission,
9598 .getattr = nfs_getattr,
9599 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009600 .listxattr = nfs4_listxattr,
Bryan Schumaker73a79702012-07-16 16:39:12 -04009601};
9602
Arjan van de Ven92e1d5b2007-02-12 00:55:39 -08009603static const struct inode_operations nfs4_file_inode_operations = {
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009604 .permission = nfs_permission,
9605 .getattr = nfs_getattr,
9606 .setattr = nfs_setattr,
Andreas Gruenbacherc4803c42015-12-02 14:44:41 +01009607 .listxattr = nfs4_listxattr,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009608};
9609
David Howells509de812006-08-22 20:06:11 -04009610const struct nfs_rpc_ops nfs_v4_clientops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -07009611 .version = 4, /* protocol version */
9612 .dentry_ops = &nfs4_dentry_operations,
9613 .dir_inode_ops = &nfs4_dir_inode_operations,
J. Bruce Fields6b3b5492005-06-22 17:16:22 +00009614 .file_inode_ops = &nfs4_file_inode_operations,
Jeff Layton1788ea62011-11-04 13:31:21 -04009615 .file_ops = &nfs4_file_operations,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009616 .getroot = nfs4_proc_get_root,
Bryan Schumaker281cad42012-04-27 13:27:45 -04009617 .submount = nfs4_submount,
Bryan Schumakerff9099f22012-07-30 16:05:18 -04009618 .try_mount = nfs4_try_mount,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009619 .getattr = nfs4_proc_getattr,
9620 .setattr = nfs4_proc_setattr,
9621 .lookup = nfs4_proc_lookup,
Jeff Layton5b5faaf2017-06-29 06:34:52 -07009622 .lookupp = nfs4_proc_lookupp,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009623 .access = nfs4_proc_access,
9624 .readlink = nfs4_proc_readlink,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009625 .create = nfs4_proc_create,
9626 .remove = nfs4_proc_remove,
9627 .unlink_setup = nfs4_proc_unlink_setup,
Bryan Schumaker34e137c2012-03-19 14:54:41 -04009628 .unlink_rpc_prepare = nfs4_proc_unlink_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009629 .unlink_done = nfs4_proc_unlink_done,
Jeff Laytond3d41522010-09-17 17:31:57 -04009630 .rename_setup = nfs4_proc_rename_setup,
Bryan Schumakerc6bfa1a2012-03-19 14:54:42 -04009631 .rename_rpc_prepare = nfs4_proc_rename_rpc_prepare,
Jeff Laytond3d41522010-09-17 17:31:57 -04009632 .rename_done = nfs4_proc_rename_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009633 .link = nfs4_proc_link,
9634 .symlink = nfs4_proc_symlink,
9635 .mkdir = nfs4_proc_mkdir,
Trond Myklebust912678d2018-03-20 16:43:15 -04009636 .rmdir = nfs4_proc_rmdir,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009637 .readdir = nfs4_proc_readdir,
9638 .mknod = nfs4_proc_mknod,
9639 .statfs = nfs4_proc_statfs,
9640 .fsinfo = nfs4_proc_fsinfo,
9641 .pathconf = nfs4_proc_pathconf,
David Howellse9326dc2006-08-22 20:06:10 -04009642 .set_capabilities = nfs4_server_capabilities,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009643 .decode_dirent = nfs4_decode_dirent,
Anna Schumakera4cdda52014-05-06 09:12:31 -04009644 .pgio_rpc_prepare = nfs4_proc_pgio_rpc_prepare,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009645 .read_setup = nfs4_proc_read_setup,
Trond Myklebustec06c092006-03-20 13:44:27 -05009646 .read_done = nfs4_read_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009647 .write_setup = nfs4_proc_write_setup,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009648 .write_done = nfs4_write_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009649 .commit_setup = nfs4_proc_commit_setup,
Fred Isaman0b7c0152012-04-20 14:47:39 -04009650 .commit_rpc_prepare = nfs4_proc_commit_rpc_prepare,
Trond Myklebust788e7a82006-03-20 13:44:27 -05009651 .commit_done = nfs4_commit_done,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009652 .lock = nfs4_proc_lock,
J. Bruce Fieldse50a1c22005-06-22 17:16:23 +00009653 .clear_acl_cache = nfs4_zap_acl_attr,
Trond Myklebust7fe5c392009-03-19 15:35:50 -04009654 .close_context = nfs4_close_context,
Trond Myklebust2b484292010-09-17 10:56:51 -04009655 .open_context = nfs4_atomic_open,
Bryan Schumaker011e2a72012-06-20 15:53:43 -04009656 .have_delegation = nfs4_have_delegation,
Bryan Schumaker57ec14c2012-06-20 15:53:44 -04009657 .return_delegation = nfs4_inode_return_delegation,
Bryan Schumaker6663ee72012-06-20 15:53:46 -04009658 .alloc_client = nfs4_alloc_client,
Andy Adamson45a52a02011-03-01 01:34:08 +00009659 .init_client = nfs4_init_client,
Bryan Schumakercdb7ece2012-06-20 15:53:45 -04009660 .free_client = nfs4_free_client,
Bryan Schumaker1179acc2012-07-30 16:05:19 -04009661 .create_server = nfs4_create_server,
9662 .clone_server = nfs_clone_server,
Linus Torvalds1da177e2005-04-16 15:20:36 -07009663};
9664
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009665static const struct xattr_handler nfs4_xattr_nfs4_acl_handler = {
Andreas Gruenbacher98e9cb52015-12-02 14:44:36 +01009666 .name = XATTR_NAME_NFSV4_ACL,
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009667 .list = nfs4_xattr_list_nfs4_acl,
9668 .get = nfs4_xattr_get_nfs4_acl,
9669 .set = nfs4_xattr_set_nfs4_acl,
9670};
9671
9672const struct xattr_handler *nfs4_xattr_handlers[] = {
9673 &nfs4_xattr_nfs4_acl_handler,
David Quigleyc9bccef2013-05-22 12:50:45 -04009674#ifdef CONFIG_NFS_V4_SECURITY_LABEL
9675 &nfs4_xattr_nfs4_label_handler,
9676#endif
Aneesh Kumar K.V64c2ce82010-12-09 11:35:25 +00009677 NULL
9678};
9679
Linus Torvalds1da177e2005-04-16 15:20:36 -07009680/*
9681 * Local variables:
9682 * c-basic-offset: 8
9683 * End:
9684 */